Vincent22
11-11-2011, 03:27 AM
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:
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:
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
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:
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:
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