Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c++ - glm rotate usage in Opengl

I am rendering a cone, and I would like to rotate it, 90 degrees anti-clockwise, so that the pointy end faces west! I am using OpenGL 3+.

Here is my code in my Cone.cpp so far:

//PROJECTION
    glm::mat4 Projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);

    //VIEW
    glm::mat4 View = glm::mat4(1.);
    View = glm::translate(View, glm::vec3(2.0f,4.0f, -25.0f));

    //MODEL
    glm::mat4 Model = glm::mat4(1.0);
    //Scale by factor 0.5
    Model = glm::scale(glm::mat4(1.0f),glm::vec3(0.5f));

    glm::mat4 MVP = Projection * View * Model;
    glUniformMatrix4fv(glGetUniformLocation(shaderprogram_spaceship, "MVP_matrix"), 1, GL_FALSE, glm::value_ptr(MVP));

    glClearColor(0.0, 0.0, 0.0, 1.0);

    glDrawArrays(GL_LINE_STRIP, start_cone, end_cone );

Not all of the code is shown.

Can somebody guide me through the rotation? I do have to multiply the View matrix right ? with "glm rotate" function ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You need to multiply your Model matrix. Because that is where model position, scaling and rotation should be (that's why it's called the model matrix).

All you need to do is (see here)

Model = glm::rotate(Model, angle_in_radians, glm::vec3(x, y, z)); // where x, y, z is axis of rotation (e.g. 0 1 0)

Note that to convert from degrees to radians, use glm::radians(degrees)

That takes the Model matrix and applies rotation on top of all the operations that are already in there. The other functions translate and scale do the same. That way it's possible to combine many transformations in a single matrix.

note: earlier versions accepted angles in degrees. This is deprecated since 0.9.6

Model = glm::rotate(Model, angle_in_degrees, glm::vec3(x, y, z)); // where x, y, z is axis of rotation (e.g. 0 1 0)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...