Hi guys,
When I use to code in regular old opengl it had glLoadIdentity() which would help me translate my object , load Identity matrix and then rotate it around its own axis.
when stacking translation and rotation together an object will not rotate around it`s own origin, but rather (around the initial point it was translated from).(which according to matrix math it is supposed to)
How do I translate an object and then rotatate it about it`s own origin using glm library or even better manually since I want to understand how it can be done. I presume loadIdentity resets the model matrix to identity matrix but then you will loose your previous translation .. I cannot seem to figure out how you could do it manually here is my code:
RenderScene
void renderScene()
{
glm::mat4 ModelViewProject;
glm::mat4 View;
glm::mat4 Model;
glm::mat4 Projection = glm::perspective(45.0f, 1024.0f / 768.0f, 0.1f, 10.0f);
glm::mat4 rotation = glm::rotate(Model, angle, glm::vec3(1, 0, 0));
Model *= rotation;
rotation = loadIdentity();
glm::mat4 transRotation = glm::translate(rotation, glm::vec3(xCh, yCh, zCh));
Model *= transRotation;
View = glm::lookAt(glm::vec3(0, 0, 2), glm::vec3(0, 0, 0), glm::vec3(0.0, 1.0, 0.0));
ModelViewProject = Projection * View * Model;
GLint transform = glGetUniformLocation(programId, "ModelViewProject");
glUniformMatrix4fv(transform, 1, false, glm::value_ptr(ModelViewProject));;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glutSwapBuffers();
}
Vertex Initial position
void init()
{
int z = 1.0f;
GLfloat vertices[] =
{
0.999999f, 1.000000f, 1.0f,
0.0f, 0.0f, 1.0f,
-1.000000f, 1.000000f, 1.0f,
1.0f, 1.0f, 0.0f,
-1.000000f, -1.000000f, 1.0f,
0.0f, 1.0f, 1.0f, // colors
1.000000f, -1.000000f, 1.0f,
1.0f, 1.0f, 0.4f,
0.999999f, 1.000000f, 1.0f,
1.0f, 0.8f, 0.4f,
-1.000000f, -1.000000f, 1.0f,
0.7f, 0.8f, 0.4f,
};
GLint FragmentShaderColorId;
GLint VertexId;
VertexId = glGetAttribLocation(programId, "position");
FragmentShaderColorId = glGetAttribLocation(programId, "ColorIn");
glEnable(GL_DEPTH_TEST);
GLuint myBuffId = 0;
glGenBuffers(1, &myBuffId);
glEnableVertexAttribArray(VertexId);
glBindBuffer(GL_ARRAY_BUFFER, myBuffId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(VertexId, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);
glEnableVertexAttribArray(FragmentShaderColorId);
glVertexAttribPointer(FragmentShaderColorId, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (char *)(sizeof(float) * 3)); // stride of 5 floats describing my colors starting at position 2