Advertisement

Why aren't the arrow keys responding

Started by March 19, 2019 02:37 PM
4 comments, last by CrazyCdn 5 years, 8 months ago

compiles fine with

g++ colourfulrotatingcube.cpp -lglut -lGL -lGLU -o colourfulrotatingcube

but the arrow keys are not moving the cube. Why?

 

#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
#include<math.h>

void keyboard(int key, int x, int y);
void drawCube();

// Rotate X
double rX=0;
// Rotate Y
double rY=0;

// The coordinates for the vertices of the cube
double x = 0.6;
double y = 0.6;
double z = 0.6;

void drawCube()
{
// Set background Colour
glClearColor(0.4,0.4,0.4,1.0);
// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Reset transformations
glLoadIdentity();

// Rotate when user changes rX and rY
glRotatef(rX,1.0,0.0,0.0);
glRotatef(rY,0.0,1.0,0.0);

// BACK

glBegin(GL_TRIANGLES);
glColor3f(0.4,0.3,0.5);
glVertex3f(x,y,z);
glVertex3f(x,-y,z);
glVertex3f(-x,y,z);
glEnd();

// FRONT
// Using 4 triangles!
glBegin(GL_TRIANGLES);
glColor3f(0.1,0.5,0.3);
glVertex3f(-x,y,z);
glVertex3f(0,0,-z);
glVertex3f(-x,-y,-z);
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(0.0,0.5,0.0);
glVertex3f(-x,-y,-z);
glVertex3f(-0,0,-z);
glVertex3f(-x,-y,-z);
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(0.1,0.3,0.3);
glVertex3f(-x,y,-z);
glVertex3f(x,y,-z);
glVertex3f(0,0,-z);
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(0.2,0.2,0.2);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(-x,y,-z);
glEnd();

// LEFT
glBegin(GL_TRIANGLES);
glColor3f(0.3,0.5,0.6);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(-x,y,-z);
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(0.5,0.5,0.5);
glVertex3f(-x,y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,y,-z);
glEnd();

//RIGHT
glBegin(GL_TRIANGLES);
glColor3f(0.2,0.2,0.2);
glVertex3f(x,y,z);
glVertex3f(x,y,-z);
glVertex3f(x,-y,z);
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(0.0,0.0,0.0);
glVertex3f(x,y,z);
glVertex3f(x,y,-z);
glVertex3f(-x,y,-z);
glEnd();

//TOP
glBegin(GL_TRIANGLES);
glColor3f(0.6,0.0,0.0);
glVertex3f(x,y,z);
glVertex3f(x,y,-z);
glVertex3f(-x,y,-z);
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(0.6,0.1,0.2);
glVertex3f(-x,y,z);
glVertex3f(x,y,z);
glVertex3f(-x,y,-z);
glEnd();

//BOTTOM
glBegin(GL_TRIANGLES);
glColor3f(0.4,0.0,0.4);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(x,-y,z);
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(0.3,0.0,0.3);
glVertex3f(x,-y,-z);
glVertex3f(-x,-y,-z);
glVertex3f(x,-y,z);
glEnd();

glFlush();
glutSwapBuffers();
}

void keyboard(int key, int x, int y)
{

if(key == GLUT_KEY_RIGHT)
{
rY +=15;
}
else if(key == GLUT_KEY_LEFT)
{
rY -=15;
}
else if(key ==GLUT_KEY_DOWN)
{
rX -=15;
}
else if(key ==GLUT_KEY_UP)
{
rX +=15;
}

}

int main(int argc, char **argv)
{
// Initializr GLUT and process parameters
glutInit(&argc, argv);

// Request double buffered true colour window with Z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(700,700);
glutInitWindowPosition(100,100);

// Create window
glutCreateWindow("Linux Journal Open GL Cube");

//Enable Z-buffer depth test
glEnable(GL_DEPTH_TEST);

//Callback functions
glutDisplayFunc(drawCube);
glutSpecialFunc(keyboard);

// Pass control to GLUT for events
glutMainLoop();

return 0;

}

 

I think the problem here is that you only draw the cube once. When you press the arrow, the angle changes, but drawCube() does not get called again. 

Look into glutTimerFunc and glutPostRedisplay to make your window draw itself again.

Advertisement

Thanks. I was missing glutPostRedisplay

1 hour ago, Alio said:

Thanks. I was missing glutPostRedisplay

I am genuinely interested in why you are using antiquated OpenGL. Can you please tell why?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

GLUT was last updated in 2001.  Why are you using OpenGL/GLUT from the late 90s?  None of the knowledge you learn will transfer to anything remotely modern.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

This topic is closed to new replies.

Advertisement