Using rotm rotm in Opengl

I am making a cone, and I would like to rotate it 90 degrees counterclockwise, so that the pointed end faces west! I am using OpenGL 3+.

Here is my code in my Cone.cpp:

//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 ); 

All code is not displayed.

Can someone guide me through the rotation? Do I need to multiply the view matrix directly? with rot rot rot function?

+8
c ++ opengl glm-math
source share
3 answers

You need to multiply the model matrix. Because this is where the model’s position, scaling and rotation should be (therefore it was called the model matrix).

All you have to do is:

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

This takes the model matrix and applies rotation on top of all operations that already exist. Other functions translate and scale the same. Thus, it is possible to combine many transformations into one matrix.

+30
source share

GLM has a good rotation example: http://glm.g-truc.net/code.html

 glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f); glm::mat4 ViewTranslate = glm::translate( glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Translate) ); glm::mat4 ViewRotateX = glm::rotate( ViewTranslate, Rotate.y, glm::vec3(-1.0f, 0.0f, 0.0f) ); glm::mat4 View = glm::rotate( ViewRotateX, Rotate.x, glm::vec3(0.0f, 1.0f, 0.0f) ); glm::mat4 Model = glm::scale( glm::mat4(1.0f), glm::vec3(0.5f) ); glm::mat4 MVP = Projection * View * Model; glUniformMatrix4fv(LocationMVP, 1, GL_FALSE, glm::value_ptr(MVP)); 
+8
source share

I noticed that you can also get errors if you did not specify the correct angles, even if you use glm::rotate(Model, angle_in_degrees, glm::vec3(x, y, z)) , you can still encounter problems . The fix I found for this indicated the type as glm::rotate(Model, (glm::mediump_float)90, glm::vec3(x, y, z)) instead of just saying glm::rotate(Model, 90, glm::vec3(x, y, z))

Or simply write the second argument, the angle in radians (previously in degrees), like a float without any throw, for example, in:

 glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), 3.14f, glm::vec3(1.0)); 

You can add glm :: radians () if you want to continue using degrees. And add include:

 #include "glm/glm.hpp" #include "glm/gtc/matrix_transform.hpp" 
+5
source share

All Articles