Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

c++ - glm returning nan on simple translate

I'm tinkering with OpenGL using glfw, glad, and glm. In one of the tutorials I'm using, they demonstrate some simple usage of glm as so:

glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);
glm::mat4 trans;
trans = glm::translate(trans, glm::vec3(1.0f, 1.0f, 0.0f));
vec = trans * vec;
std::cout << vec.x << vec.y << vec.z << std::endl;

When I compile and run this code, I get trash values (usually NAN). The tutorial specifically noted that instantiating

glm::mat4 trans;

would by default create an identity matrix for the varialbe "trans". I'm thinking that perhaps this is the issue, though I have verified that glm does do this by default.

In case it would be helpful, you can find the entire source file here on line 308. I greatly appreciate your time!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to initialize the matrix variable glm::mat4 trans.

The glm API documentation refers to The OpenGL Shading Language specification 4.20.

5.4.2 Vector and Matrix Constructors

If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.

This means, that an identity matrix can be initialized by the single parameter 1.0: glm::mat4(1.0f).

Change your code somehow like this:

glm::mat4 trans(1.0f);


See also OpenGL Mathematics (GLM); 2. Vector and Matrix Constructors


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...