# Thread: Object Rotation

1. ## Object Rotation

Hi everyone. I'm a newbie for GLSL.
I wrote my first simple opengl project using GLSL. My program draw a cube, now I want to make the cube rotate on itself.
I try to do so putting the glRotate inside the rendering function of my object (the cube) in the same way I did in project that don't use GLSL, but the cube doesn't rotate.

Ho can I fix my problem? I should put the rotation inside the shader?
Here are the rotation code:

Code :
```void Cube::render()
{
glRotatef(angle, 0.0f, 1.0f, 0.0f);

glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glVertexAttribPointer((GLint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
glVertexAttribPointer((GLint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);

glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0);
}```

angle is updated by the animate() function of my app class:

Code :
```void MyApp::animate()
{
// We increment the rotation angle for the triangle, and if it went over 360 we wrap it back to 0
time = glutGet(GLUT_ELAPSED_TIME);
timediff = time - timeprec;
timeprec = time;
const float xSpeed = 1.0f;
const float ySpeed = 1.0f;

m_xRotation += xSpeed * timediff/4;
m_yRotation += ySpeed * timediff/4;

if (m_xRotation > 360.0f) {
m_xRotation -= 360.0f;
}

if (m_yRotation > 360.0f) {
m_yRotation -= 360.0f;
}

m_Cube.angle = m_xRotation;
// Normally openGL doesn't continuously draw frames. It puts one in place and waits for you to tell him what to do next.
// Calling glutPostRedisplay() forces a redraw with the new angle
glutPostRedisplay();
}```

thank you

2. ## Re: Object Rotation

without seeing your shader it's not possible to tell what's going on.
What ModelMatrix do you use to render with in your shader? Do you rely upon the built-in gl_ModelView matrix?
Do you use glPushMatrix and glPopMatrix before and after the call to render the cube? If not, how are you perserving the camera between draw calls?

3. ## Re: Object Rotation

vertex shader:
Code :
```#version 120

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;

attribute vec3 a_Vertex;
attribute vec3 a_Color;
varying vec4 color;

void main(void)
{
vec4 pos = modelview_matrix * vec4(a_Vertex, 1.0);
gl_Position = projection_matrix * pos;
color = vec4(a_Color, 1.0);
}```

fragment shader:
Code :
```#version 120

varying vec4 color;

void main(void)
{
gl_FragColor = color;
}```

No, I don't use glPushM and glPoPM before and after the call to render the cube. I use only glRotate before the call to render the cube

4. ## Re: Object Rotation

glrotatef(angle,x,y,z) function rotate object by gives angle with respect to the given axis but it does not rotate object continuously.TO rotate object continuously you have to change the angle by small value like this angle+=1.0; for this we have to use glutTimerFunc so you can visit link below for the sample code and can take idea from this example. http://codeincodeblock.blogspot.com/...object-in.html

This example actually for changing color of object using keyboard function but I have written code for rotating object here.

5. ## Re: Object Rotation

Yes, I use glutTimerFunc in the animate() function of my Application class. I pass the function in the idleFunc in main.
angle is set to 0.0f and updated by animate() func using glutTimerFunc.

Now my program works. I solved the problem moving the glRotate function before I get the ModelView Matrix and pass it to shader (stupid error).
Here is the modified code:

Code :
```void MyApp::render()
{
float modelviewMatrix[16];
float projectionMatrix[16];

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
gluLookAt(0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -5.0f);

glPushMatrix();
glRotatef(angle, 0.0f, 1.0f, 0.0f);

glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
glGetFloatv(GL_PROJECTION_MATRIX, projectionMatrix);

m_GLSLProgram->sendUniform("modelview_matrix", modelviewMatrix);
m_GLSLProgram->sendUniform("projection_matrix", projectionMatrix);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

m_Cube.render();

glPopMatrix();

printf("%f", angle);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•