PDA

View Full Version : Problem with keyboard interface - freeglut



thor36
12-02-2014, 10:56 AM
Greetings all,
I have a problem using freeglut for keyboard interface and would very much appreciate some help.

I am using these functions to handle keys being pressed:



glutKeyboardFunc(handleKeypress);


void handleKeypress(unsigned char key, int x, int y) {


if (key=='q')
{
rot1 += 1.0;
if (rot1 >360) rot1 -= 360;
}

if (key=='w')
{
rot2 += 1.0;
if (rot2 >360) rot2 -= 360;
}

if (key=='e')
{
rot3 += 1.0;
if (rot3 >360) rot3 -= 360;
}

if (key=='r')
{
rot4 += 1.0;
if (rot4 >360) rot4 -= 360;
}

if (key=='t')
{
rot5 += 1.0;
if (rot4 >360) rot4 -= 360;
}

if (key=='y')
{
rot6 += 1.0;
if (rot4 >360) rot4 -= 360;
}

}


But it doesn't function as I would expect. The only way for the scene to rotate is to hold pressed the key and then with each mouse click ( either left or right ) the scene rotates for the specified angle. I don't understand why does mouse play a role in this function at all, I never specified any action for mouse click. I would like the scene to simply rotate for the specified angle with each button press.

Thank you and kind regards,
T

ParagonArcade
12-03-2014, 02:33 PM
I believe the reason for the "mouse click" thing may be because of your window handling. What does your window resize function look like? Perhaps you are only updating your scene when you "resize" not in your "display" func. Please post code for both.

thor36
12-06-2014, 07:17 PM
Hi, thank you for answer. Window resize function looks like this :




void handleResize(int w, int h) {

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)w / (float)h, 1.0, 100.0);

}


and for drawing




void drawScene() {

glClearColor (1.0,1.0,1.0,1.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0f, 0.0f, -1000.0f);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


glRotatef(rot1,0.0f,-1.0f,0.0f);
glRotatef(rot2,0.0f,1.0f,0.0f);
glRotatef(rot3,-1.0f,0.0f,0.0f);
glRotatef(rot4,1.0f,0.0f,0.0f);
glRotatef(rot5,0.0f,0.0f,-1.0f);
glRotatef(rot6,0.0f,0.0f,1.0f);


glBegin();
//....
//....stuff
//....
glEnd();



glutSwapBuffers();

}

ParagonArcade
12-06-2014, 07:41 PM
glRotatef(rot1,0.0f,-1.0f,0.0f);
glRotatef(rot2,0.0f,1.0f,0.0f);
glRotatef(rot3,-1.0f,0.0f,0.0f);
glRotatef(rot4,1.0f,0.0f,0.0f);
glRotatef(rot5,0.0f,0.0f,-1.0f);
glRotatef(rot6,0.0f,0.0f,1.0f);



I think this may be your problem. You are passing in two variables for a single rotation around an axis for each axis. And rotation "decrease" or "increase" on seperate variables. You need to at least set rot1 <--> rot2, rot3 <--> rot4, rot5 <--> rot6.... equal to one another each frame. Or you are just deflecting each value from its initial value. That makes sense?

Try using one variable for each axis of rotation, like this:


float rotX = 0.0f;
float rotY = 0.0f;
float rotZ = 0.0f;

glRotatef(rotX,1.0f,0.0f,0.0f);
glRotatef(rotY,0.0f,1.0f,0.0f);
glRotatef(rotZ,0.0f,0.0f,1.0f);

...

then update your variables with your keys:

switch(key)
{
case GLUT_UP_ARROW:
rotX += 1.0f
if(rotX > 360.0f) rotX -= 360.0f;
break;

case GLUT_DOWN_ARROW:
rotX -= 1.0f
if(rotX < 0.0f) rotX += 360.0f;
break;

default:
//Do Nothing
break;
}