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:
angle is updated by the animate() function of my app class:
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();
}
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?
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/2011/11/how-to-change-color-of-object-in.html
This example actually for changing color of object using keyboard function but I have written code for rotating object here.
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: