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:

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

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?

vertex shader:

#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:

#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

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:

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);
}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.