Jump to content
  • Advertisement
Alio

Why aren't the arrow keys responding

Recommended Posts

Posted (edited)

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;

}

 

Edited by Alio

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!