[SOLVED]OpenGL: drawing update problem

Hello guys.
I'm trying to make a drawing in a window change his size every time i hit 'b' in my keyboard, but is not working properly.
Every time i hit 'b', "glVertex3f(x,y,z)" in "renderPrimitive()"function recieves new values for x and y and may drawing gets bigger. This is working fine, but my drawing gets bigger only if i click outside of my window and click back in the window. What i have to do to solve this problem?
1
2
3
4
5
6
7
8
9
10
void renderPrimitive(void) 
{
	glBegin(GL_QUADS);
	//spaceship is a global TAD who update his variables everytime i hit 'b', i do this in a callback.
    	glVertex3f(spaceship->blcx, spaceship->blcy, 0.0); // The bottom left corner  
    	glVertex3f(spaceship->tlcx, spaceship->tlcy, 0.0); // The top left corner  
    	glVertex3f(spaceship->trcx, spaceship->trcy, 0.0); // The top right corner  
    	glVertex3f(spaceship->brcx, spaceship->brcy, 0.0); // The bottom right corner  
	glEnd();
} 

1
2
3
4
5
6
7
8
void reshape(int width, int height)
{
  	glViewport(0, 0, (GLsizei)width, (GLsizei)height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
	glMatrixMode(GL_MODELVIEW);
}

1
2
3
4
5
6
7
8
9
void display(void)
{
	glClearColor(1.f, 0.f, 0.f, 1.f);
	glClear (GL_COLOR_BUFFER_BIT);
	glLoadIdentity();	
	glTranslatef(0.0f, 0.0f, -5.0f);	
	renderPrimitive();
	glFlush();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(int argc, char **argv)
{
	spaceship = create_spaceship(spaceship);
	glutInit(&argc, argv);

	glutInitDisplayMode (GLUT_SINGLE); 
	glutInitWindowSize (800, 600);
	glutInitWindowPosition (100, 50);
	glutCreateWindow ("My first OpenGL Window");

	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	
	glutKeyboardFunc(key_pressed);

	glutMainLoop();
}
Last edited on
try doing:

 
glutIdleFunc(display);
fafner, i did what you said. It worked. I put "glutIdleFunc(display);" between "glutDisplayFunc(display);" and "glutReshapeFunc(reshape);" in main function, but my window almost freezes, it gets heavy very very heavy!
I tried to put after both functions too, but same thing happened.
Last edited on
Please, guys, i really need your help!
Idle Function isn't so great for this, as it will basically try to render your scene as many times as it can.

You can use the timer function to get a specific FPS
http://hohohuhu.blogspot.com/2011/03/opengl-timer-function.html

If you only want the scene updated when you press a button, use glutPostRedisplay().
1
2
3
4
5
void key_pressed(/*  */)
{
  // Do things with the key
  glutPostRedisplay();
}
Last edited on
LowestOne.
Thank you, i did what you said and now everything is working fine.
Topic archived. No new replies allowed.