0

i am trying to learn transformation in OpenGL & I am using glm for math calculation. I have a vector& I want to apply some rotation to that vector.but i get output as vec3(0,0,0)

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_float4x4.hpp> // mat4x4
#include <glm/ext/matrix_transform.hpp> // translate, rotate, scale, identity
#include <glm/gtx/string_cast.hpp>
#include<iostream>
int main(){
        //define  a 4x4 model matrix
        glm::mat4 model;
        //define a vector
        glm::vec4 Position = glm::vec4(glm::vec3(0.4,0.2,0.2), 1.0);
        //create a model matrix with rotation of 45 degrees around z aixs
        model = glm::rotate(model,  45.0f, glm::vec3(0.0f, 0.0f, 1.0f));        
        //print the final vector position after rotation is applied
        std::cout<< glm::to_string(model*Position) <<std::endl;
        return 0;
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
anekix
  • 2,393
  • 2
  • 30
  • 57
  • Possible duplicate of [Why is the sprite not rendering in OpenGL?](https://stackoverflow.com/questions/49651388/why-is-the-sprite-not-rendering-in-opengl) – Rabbid76 Nov 19 '18 at 18:24

1 Answers1

2

You did not initialize your model matrix. Should be:

glm::mat4 model = glm::mat4(1.0f);

to initialize it to identity matrix.

Rhu Mage
  • 667
  • 1
  • 8
  • 21
  • but why initialize to an identity matrix? – anekix Nov 19 '18 at 18:29
  • You can initialize it to whatever you want. But if you want the model matrix to only contain the rotation you have to use identity, rotation multiplied by identity is rotation. – Rhu Mage Nov 19 '18 at 18:52