Jump to content
  • Advertisement
Sign in to follow this  
crazykid48x

Opengl translate problem

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trieng to draw a triangle and have it move around the screen, according to keys you press. As you will be able to tell im using GLUT. this is what I have so far: #include <GL/glut.h> #include <stdlib.h> float xaxis=0.0; float yaxis=-1.0; void tri() { glBegin(GL_TRIANGLES); glVertex2f( 0.0, 0.0); glVertex2f( 0.1, 0.2); glVertex2f( 0.2, 0.0); glEnd(); } void draw() { glClearColor( 0.0, 0.0, 0.0, 0.0); glClear ( GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslatef(xaxis, yaxis, 0.0); tri(); glPopMatrix(); glutSwapBuffers(); glFlush(); } void keyboard(unsigned char key, int x, int y) { switch(key) { case 27: exit(0); break; case 'a': xaxis-=.5; break; case 'd': xaxis+=.5; break; case 'w': yaxis+=.5; break; case 'x': yaxis-=.5; break; } } int main() { glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(500,500); glutCreateWindow("space shooter"); glutDisplayFunc(draw); glutKeyboardFunc(keyboard); glutMainLoop(); } So far the triangle apears but doesnt move. I really need some help making this work. thanks.

Share this post


Link to post
Share on other sites
Advertisement
Haven't used GLUT in a while, but it looks like your draw function is only being called once, at the start of the program, when the window is created.

GLUT won't call your draw function continuously. In fact, draw will only be called in two cases: if you call the glutPostRedisplay function, or if the window needs to be redrawn due to some outside activity like another window being dragged over it. The code you've actually written is fine, but your drawing code isn't being called to display the changes.

Add an idle function to your code, and register it with GLUT using glutIdleFunc. Inside the idle function, place a call to glutPostRedisplay. That should work. Alternatively, you could call glutPostRedisplay inside your keyboard function, after the switch statement. Another alternative is to call glutPostRedisplay at the very end of your draw function.

One other thing: if you need to post code in future, you can enclose it in <source> ... </source> tags to put it in a fancy source box...

Share this post


Link to post
Share on other sites
You need to redraw. I'm at work and do't have code to give you more of an explanation. The previous poster did though.

The books I've read start with GLUT and move to SDL after the first chapter or two. I'm just starting out with openGL right now, so I'm only concerned with the graphics aspect. I'm going to go to SDL when it all makes more sense. Anyone else?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!