17

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 ?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Test Test
  • 2,831
  • 8
  • 44
  • 64
  • 16
    What is the problem? You need to tell us what the problem is. It's not fair to not only want us to tell you *the solution*, but to want us to figure out *the problem* as well. – R. Martinho Fernandes Jan 13 '12 at 01:16

3 Answers3

48

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)
kvantour
  • 25,269
  • 4
  • 47
  • 72
the swine
  • 10,713
  • 7
  • 58
  • 100
10

GLM has good example of rotation : 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));
SystemParadox
  • 8,203
  • 5
  • 49
  • 57
Anonymous
  • 1,823
  • 2
  • 35
  • 74
  • GLM code example link: [https://glm.g-truc.net/0.9.1/api/a00006.html](https://glm.g-truc.net/0.9.1/api/a00006.html) – befzz Oct 27 '17 at 12:41
7

I noticed that you can also get errors if you don't specify the angles correctly, even when using glm::rotate(Model, angle_in_degrees, glm::vec3(x, y, z)) you still might run into problems. The fix I found for this was specifying 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 just write the second argument, the angle in radians (previously in degrees), as a float with no cast needed such as 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 keep using degrees. And add the includes:

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
LastBlow
  • 617
  • 9
  • 16
abe732
  • 125
  • 2
  • 12
  • 3
    According to the [documentation](http://glm.g-truc.net/0.9.4/api/a00206.html#ga3eed13fc5bcaff2f2204b12013bb83bf) the angle it takes is in degrees. So passing `3.14f` rotates by a small amount (3 degrees rather than 180 degrees). Also, the function is a template so passing integer as the angle makes argument type ambiguous (which yields a compile-time error). Specifying `180.0f` would likely be what you meant. – the swine Jul 18 '17 at 08:20
  • BTW: "GLM 0.9.5 deprecates degrees so that in GLM 0.9.6 those will be removed. Starting with GLM 0.9.5.1, warnings will be prompt for each instance of usage degrees. This is designed to help transitioning user code. Defining GLM_FORCE_RADIANS will quiet these messages." From https://www.g-truc.net/post-0623.html – Vegard Dec 29 '21 at 21:09