Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Simple Keyboard Commands


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 YodaTheCoder   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 October 2001 - 06:06 PM

Hello! Could someone give me an example (I''m using GLUT) as to how one might manipulate the position of a simple triangle along the x axis with the left and right keys? The code I have right now just sux... Here it is: #include #include #include int x = 0; int y = 0; float angle=0.0; bool leftkey=0; bool rightkey=0; void renderScene(void); void processKeys(unsigned char key, int x, int y); void changeSize(int w, int h); void changeSize(int w, int h) { if(h == 0) h = 1; float ratio = 1.0* w / h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, w, h); gluPerspective(45,ratio,1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,5.0, 0.0,0.0,-1.0, 0.0f,1.0f,0.0f); } void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); glutInitWindowPosition(100,100); glutInitWindowSize(320,320); glutCreateWindow("Hello World!"); glutDisplayFunc(renderScene); glutKeyboardFunc(processKeys); glutReshapeFunc(changeSize); glutIdleFunc(renderScene); glutMainLoop(); } void processKeys(unsigned char key, int x, int y) { switch(key){ case 27: exit(0); break; case GLUT_KEY_LEFT: leftkey=1;break; case GLUT_KEY_RIGHT: rightkey=1;break; default: break; } } void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0,1.0,0.0); if(leftkey=1){ angle-=0.1; } if(rightkey=1){ angle+=0.1; } glBegin(GL_TRIANGLES); glVertex3f(angle-0.5,-0.5,0.0); glVertex3f(angle+0.5,0.0,0.0); glVertex3f(angle,0.5,0.0); glEnd(); glutSwapBuffers(); } I KNOW! It''s very newbie-ish, and you all are probably laughing your a*ses off at me, but key, I just need some help!! If you see what I''m doing, then good. All it is is I am trying to move the friggin triangle left and right... bah!.... ~Jesse PS I do know enough that if my above code worked as-is, the top point would remain, making a weird shape...

Sponsor:

#2 Gillies   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 October 2001 - 11:49 PM

It looks like you are doing a few things wrong:
- Firstly, you are not changing an angle so I would rename the variable you are using for movement - call it fXPos or something.
- When you process the keys, rather than setting a flag to say you have moved, why not modify your global variable? For example

case GLUT_KEY_LEFT: fXPos--; break;
case GLUT_KEY_RIGHT: fXPos++; break;

- Now in your rendering function you are changing the shape itself which is bad practice - although it could work. The triangle should be drawn the same everytime. Maybe you should put it in a seperate function.

glBegin(GL_TRIANGLES);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.5f, 0.0f,0.0f);
glVertex3f(0.0f, 0.5f,0.0f);
glEnd();

Now before you draw it you want to translate along the XAxis by your fXPos value. So before you call your triangle code

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Move along the x axis
glTranslated(fXPos , 0.0, 0.0);

Hope that helps.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS