Can't draw shape when I press a key in OpenGL - c++

I'm having a problem when I draw some shapes. When I execute the program I should only see the coordinates, and then after pressing a key to draw the shape, but by default it is already drawn the cube and after this the pyramid. How can I do to draw the pyramid when I press 'p' key and cube when I press 'c'? I tried something but it seems that it's not working :(
#include <stdlib.h>
#include <math.h>
#include "dependente\freeglut\freeglut.h"
#include "dependente\glfw\glfw3.h"
#include <stdio.h>
#define RADDEG 57.29577951f
float XUP[3] = { 1,0,0 }, XUN[3] = { -1, 0, 0 },
YUP[3] = { 0,1,0 }, YUN[3] = { 0,-1, 0 },
ZUP[3] = { 0,0,1 }, ZUN[3] = { 0, 0,-1 },
ORG[3] = { 0,0,0 };
GLfloat viewangle = 0, tippangle = 0, traj[120][3];
GLfloat d[3] = { 0.1, 0.1, 0.1 };
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
// Use arrow keys to rotate entire scene !!!
void Special_Keys(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT: viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP: tippangle -= 5; break;
case GLUT_KEY_DOWN: tippangle += 5; break;
default: printf("Special key %c == %d", key, key);
}
glutPostRedisplay();
}
void Pyramid(void) //draw the pyramid shape
{
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); //V0(red)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); //V2(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); //V3(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); //V4(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glEnd();
}
void Draw_Box(void)
{
glBegin(GL_QUADS);
glColor3f(0.0, 0.7, 0.1); // Front - green
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.9, 1.0, 0.0); // Back - yellow
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.2, 0.2, 1.0); // Top - blue
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(0.7, 0.0, 0.1); // Bottom - red
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
}
void Keyboard(unsigned char key, int x, int y) //press a key to perform actions
{
switch (key) {
case 'd': d[0] += 0.1; break;
case 'a': d[0] -= 0.1; break;
case 'w': d[1] += 0.1; break;
case 's': d[1] -= 0.1; break;
case 'm': d[2] += 0.1; break;
case 'n': d[2] -= 0.1; break;
case 'p': Pyramid(); break;
case 'c': Draw_Box(); break;
case 'x': xAngle += 5; break;
case 'y': yAngle += 5; break;
case 'z': zAngle += 5; break;
default: printf(" Keyboard %c == %d", key, key);
}
glutPostRedisplay();
}
void Triad(void)
{
glColor3f(1.0, 1.0, 1.0); //set the dark grey color
glBegin(GL_LINES);
glVertex3fv(ORG); glVertex3fv(XUP);
glVertex3fv(ORG); glVertex3fv(YUP);
glVertex3fv(ORG); glVertex3fv(ZUP);
glEnd();
glRasterPos3f(1.1, 0.0, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'X'); //draw the x axis
glRasterPos3f(0.0, 1.1, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Y'); //draw the y axis
glRasterPos3f(0.0, 0.0, 1.1);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Z'); //draw the z axis
}
void redraw(void)
{
int v;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
glTranslatef(0, 0, -3);
glRotatef(tippangle, 1, 0, 0); // Up and down arrow keys 'tip' view.
glRotatef(viewangle, 0, 1, 0); // Right/left arrow keys 'turn' view.
glDisable(GL_LIGHTING);
Triad();
glPushMatrix();
glTranslatef(d[0], d[1], d[2]); // Move box down X axis.
glScalef(0.2, 0.2, 0.2);
glRotatef(zAngle, 0, 0, 1);
glRotatef(yAngle, 0, 1, 0);
glRotatef(xAngle, 1, 0, 0);
//Draw_Box(); // if i delete comment the cube it's drawn
//Pyramid(); // if i delete this the pyramid it's drawn inside the cube
glPopMatrix();
glutSwapBuffers();
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(900, 600);
glutInitWindowPosition(300, 300);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("Big HW1");
glutDisplayFunc(redraw);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(Special_Keys);
glClearColor(0.1, 0.0, 0.1, 1.0);
glMatrixMode(GL_PROJECTION);
gluPerspective(60, 1.5, 1, 10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 1;
}

There are several ways to solve that. One is to create a global variable that will tell you if you want to draw the shapes or not:
bool draw_pyramid = false;
bool draw_box = false;
Then, in you keyboard handle function, do this:
case 'p': draw_pyramid = true; break;
case 'c': draw_box = true; break;
and finally, at your redraw function, do a conditional draw:
if ( draw_pyramid )
Pyramid();
if ( draw_box )
Draw_Box();

Related

Can't draw hexagonal prism when I press a key in OpenGL

I tried to draw a hexagonal prism when I press a key but I have a big problem: it is drawn a random shape by default on the screen..this is my code. When I press p it is showned a pyramid, when I press c I can see a cube but by default it is drawn a random shape and I don't understand why... this is my code and the sample photo:
#include <stdlib.h>
#include <math.h>
#include "dependente\freeglut\freeglut.h"
#include "dependente\glfw\glfw3.h"
#include <stdio.h> //incluziuni librarii
#define RADDEG 57.29577951f //constanta
float XUP[3] = { 1,0,0 }, XUN[3] = { -1, 0, 0 }, //vector coordonate
YUP[3] = { 0,1,0 }, YUN[3] = { 0,-1, 0 },
ZUP[3] = { 0,0,1 }, ZUN[3] = { 0, 0,-1 },
ORG[3] = { 0,0,0 };
GLfloat viewangle = 0, tippangle = 0, traj[120][3]; //variabila pentru unghi camera
GLfloat d[3] = { 0.1, 0.1, 0.1 }; //vector directie
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
bool draw_pyramid = false; //variabila desenat figuri
bool draw_box = false;
bool draw_prism = false;
// Use arrow keys to rotate entire scene !!!
void Special_Keys(int key, int x, int y) //functie ptr taste sus jos stanga dreapta
{
switch (key) {
case GLUT_KEY_LEFT: viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP: tippangle -= 5; break;
case GLUT_KEY_DOWN: tippangle += 5; break;
default: printf("Special key %c == %d", key, key);
}
glutPostRedisplay();
}
void Pyramid(void) //draw the pyramid shape
{
glBegin(GL_TRIANGLE_FAN);//triangles have a common vertex, which is the central vertex
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); //V0(red)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); //V2(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); //V3(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); //V4(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glEnd();
}
void Draw_Box(void) //functie desenat cub
{
glBegin(GL_QUADS);//// Draw A Quad
glColor3f(0.0, 0.7, 0.1); // Front - green
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.9, 1.0, 0.0); // Back - yellow
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.2, 0.2, 1.0); // Top - blue
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(0.7, 0.0, 0.1); // Bottom - red
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
}
void hexagonalPrism()
{
glBegin(GL_QUADS);
glVertex3f(0.5, 0, 0.5);
glVertex3f(0.5, 0, -0.5);
glVertex3f(-0.5, 0, -0.5);
glVertex3f(-0.5, 0, 0.5);
glVertex3f(0.5, 0, -0.5);
glVertex3f(0.5, 1, -0.5);
glVertex3f(-0.5, 1, -0.5);
glVertex3f(-0.5, 0, -0.5);
glVertex3f(0.5, 1, -0.5);
glVertex3f(-0.5, 1, -0.5);
glVertex3f(-0.5, 0, 0.5);
glVertex3f(0.5, 0, 0.5);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(0.5, 0, 0.5);
glVertex3f(0.5, 1, -0.5);
glVertex3f(0.5, 0, -0.5);
glVertex3f(-0.5, 0, 0.5);
glVertex3f(-0.5, 1, -0.5);
glVertex3f(-0.5, 0, -0.5);
glEnd();
}
void Keyboard(unsigned char key, int x, int y) //press a key to perform actions
{
switch (key) {
case 'd': d[0] += 0.1; break; //camera right
case 'a': d[0] -= 0.1; break; //camera left
case 'w': d[1] += 0.1; break; //camera up
case 's': d[1] -= 0.1; break; //camera down
case 'm': d[2] += 0.1; break; //magnify
case 'n': d[2] -= 0.1; break; //minify
case 'p': draw_pyramid = true; draw_box = false; break; //draw pyramid when key is pressed
case 'c': draw_box = true; draw_pyramid = false; break; //draw cube when key is pressed
case 't': draw_box = false; draw_pyramid = false; draw_prism = true; break; //draw prism when key is pressed
case 'x': xAngle += 5; break; //modify x axis angle
case 'y': yAngle += 5; break; //modify y axis angle
case 'z': zAngle += 5; break; //modify z axis angle
default: printf(" Keyboard %c == %d", key, key); //see what key it's pressed
}
glutPostRedisplay();
}
void Triad(void)
{
glColor3f(1.0, 1.0, 1.0); //set the dark grey color
glBegin(GL_LINES);
glVertex3fv(ORG); glVertex3fv(XUP);
glVertex3fv(ORG); glVertex3fv(YUP);
glVertex3fv(ORG); glVertex3fv(ZUP);
glEnd();
glRasterPos3f(1.1, 0.0, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'X'); //draw the x axis
glRasterPos3f(0.0, 1.1, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Y'); //draw the y axis
glRasterPos3f(0.0, 0.0, 1.1);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Z'); //draw the z axis
}
void redraw(void)
{
int v;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
glTranslatef(0, 0, -3);
glRotatef(tippangle, 1, 0, 0); // Up and down arrow keys 'tip' view.
glRotatef(viewangle, 0, 1, 0); // Right/left arrow keys 'turn' view.
glDisable(GL_LIGHTING);
Triad();
glPushMatrix();
glTranslatef(d[0], d[1], d[2]); // Move box down X axis.
glScalef(0.2, 0.2, 0.2);
glRotatef(zAngle, 0, 0, 1);
glRotatef(yAngle, 0, 1, 0);
glRotatef(xAngle, 1, 0, 0);
if ( draw_pyramid )
Pyramid();
if (hexagonalPrism)
hexagonalPrism();
if ( draw_box )
Draw_Box();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(900, 600);
glutInitWindowPosition(300, 300);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("Big HW1");
glutDisplayFunc(redraw);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(Special_Keys);
glClearColor(0.1, 0.0, 0.1, 1.0);
glMatrixMode(GL_PROJECTION);//specify which matrix is the current matrix, matrix that represents your camera's lens (aperture, far-field, near-field, etc).
gluPerspective(60, 1.5, 1, 10); //set up a perspective projection matrix
glMatrixMode(GL_MODELVIEW); //specify which matrix is the current matrix,matrix that represents your camera (position, pointing, and up vector).
glutMainLoop();
return 1;
}
[...] now it looks like it's a triangular prism
Of course, a hexagon has 6 sides, but in the function hexagonalPrism you only draw 3 quads for the sides and 2 triangles for the top and the bottom.
Define the 6 points for the corner points of the Hexagon:
x: 0.866, 0.0, -0.866, -0.866, 0.0, 0.866
y: 0.5, 1.0, 0.5, -0.5, -1.0, -0.5
Use the point to draw the 6 quads for the sides and the a the polygon for the top and the bottom. e.g.:
void hexagonalPrism()
{
float x[] = { 0.866f, 0.0f, -0.866f, -0.866f, 0.0f, 0.866f };
float y[] = { 0.5f, 1.0f, 0.5f, -0.5f, -1.0f, -0.5f };
glBegin(GL_QUADS);
for (int i1 = 0; i1 < 6; ++i1)
{
glColor4f(
i1 < 2 || i1 > 4 ? 1.0f : 0.0f,
i1 > 0 && i1 < 5 ? 1.0f : 0.0f,
i1 > 2 ? 1.0f : 0.0f,
1.0f
);
int i2 = (i1 + 1) % 6;
glVertex3f(x[i1], 0.0f, y[i1]);
glVertex3f(x[i2], 0.0f, y[i2]);
glVertex3f(x[i2], 1.0f, y[i2]);
glVertex3f(x[i1], 1.0f, y[i1]);
}
glEnd();
glColor4f( 1, 1, 1, 1 );
glBegin(GL_POLYGON);
for (int i = 0; i < 6; ++i)
glVertex3f(x[i], 0.0f, y[i]);
glEnd();
glBegin(GL_POLYGON);
for (int i = 0; i < 6; ++i)
glVertex3f(x[i], 1.0f, y[i]);
glEnd();
}
Maybe, the problem is here:
if (hexagonalPrism) // should be if(draw_prism)
hexagonalPrism();

Black screen on OpenGL

I have an openGL program that drawn 3 houses and another things but, when I compile with gcc, just appears a black screen - and this is happening with all my openGL programs
What can I do?
PS: If I resize my screen, the drawn appears.
OS: macOS Mojave
PC: MacBook Air 2017
Development in Visual Studio Code
gcc version: 10.0.0
#include <stdlib.h>
#include <stdio.h>
#include <glut/glut.h>
#include <math.h>
#define PI 3.14159265358979324
//teto
bool teto = true;
bool base = true;
bool janela = true;
bool porta = true;
void Teto(void)
{
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(20.0, 10.0, 0.0);
glVertex3f(40.0, 0.0, 0.0);
glEnd();
}
void Base(void)
{
glBegin(GL_QUADS);
glColor3f(0, 1, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 30.0, 0.0);
glVertex3f(35.0, 30.0, 0.0);
glVertex3f(35.0, 0.0, 0.0);
glEnd();
}
void Porta(void)
{
glBegin(GL_QUADS);
glColor3f(0, 0, 1);
glVertex3f(0, 0.0, 0.0);
glVertex3f(0, 10.0, 0.0);
glVertex3f(5.0, 10.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
void Janela()
{
glBegin(GL_QUADS);
glColor3f(0, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 5.0, 0.0);
glVertex3f(5.0, 5.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
void Sun()
{
static float R = 10.0; // Radius of circle.
static float X = 20.0; // X-coordinate of center of circle.
static float Y = 70.0; // Y-coordinate of center of circle.
static int numVertices = 30; // Number of vertices on circle.
float t = 0; // Angle parameter.
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
// Draw a line loop with vertices at equal angles apart on a circle
// with center at (X, Y) and radius R, The vertices are colored randomly.
glBegin(GL_LINE_LOOP);
for(i = 0; i < numVertices; ++i)
{
glColor3f(1.0, 1.0, 0.0);
glVertex3f(X + R * cos(t), Y + R * sin(t), 0.0);
t += 2 * PI / numVertices;
}
glEnd();
}
// Drawing routine.
void drawHouse(void)
{
glPopMatrix();
glPushMatrix();
//glPopMatrix();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Sun();
glTranslatef(30.0f,30.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glPopMatrix();
glScalef(0.75f, 0.75f, 0.0f);
glTranslatef(3.0f,30.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glPopMatrix();
glScalef(1.75f, 1.75f, 0.0f);
glScalef(1.25f, 1.25f, 0.0f);
//glScalef(0.75f, 0.75f, 0.0f);
glTranslatef(25.0f,20.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glFlush();
}
// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 130.0, 0.0, 100.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'c':
base = !base;
break;
case 't':
teto = !teto;
break;
case 'j':
janela = !janela;
break;
case 'p':
porta = !porta;
break;
case 's':
Sun();
//glRotatef(10.0, 0.0f, 0.0f, 1.0f);
break;
default:
break;
}
glutPostRedisplay();
}
// Main routine.
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(900, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow("House.cpp");
glutDisplayFunc(drawHouse);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
setup();
glutMainLoop();
}

Cube Rotation - OpenGL

i heed help with openGL and rotations. This is My code:
#include <math.h>
#include <GL/glut.h>
float transZ=50;
float rotateA=0;
float rotateAspeed=0.0;
void cube (float dimX, float dimY, float dimZ)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0,dimY/2,0);
glScalef(dimX/2, dimY/2, dimZ/2);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0); // Color Green - TOP
glVertex3f(1.0, 1.0, 1.0); // TOP-RIGHT-NEAR
glVertex3f(-1.0, 1.0, 1.0); // TOP-LEFT-NEAR
glVertex3f(-1.0, 1.0, -1.0); //TOP-LEFT-FAR
glVertex3f(1.0, 1.0, -1.0); // TOP-RIGHT-FAR
glColor3f(1.0, 0.0, 0.0); // Color RED - Bottom
glVertex3f(1.0, -1.0, 1.0); //BOTTOM-RIGHT-NEAR
glVertex3f(-1.0, -1.0, 1.0); //BOTTOM-LEFT-NEAR
glVertex3f(-1.0, -1.0, -1.0); //BOTTOM-LEFT-FAR
glVertex3f(1.0, -1.0, -1.0); //BOTTOM-RIGHT-FAR
glColor3f(1.0, 1.0, 0.0); // Color Yellow - Back
glVertex3f(1.0, 1.0, -1.0); //TOP-RIGHT-FAR
glVertex3f(-1.0, 1.0, -1.0); //TOP-LEFT-FAR
glVertex3f(-1.0, -1.0, -1.0); //BOTTOM-LEFT-FAR
glVertex3f(1.0, -1.0, -1.0); //BOTTOM-RIGHT-FAR
glColor3f(0.0, 0.0, 1.0); //Color Blue - RIGHT
glVertex3f(1.0, 1.0, 1.0); //TOP-FRONT-NEAR
glVertex3f(1.0, 1.0, -1.0); //TOP-BACK-FAR
glVertex3f(1.0, -1.0, -1.0); //BOTTOM-BACK-FAR
glVertex3f(1.0, -1.0, 1.0); //BOTTOM-FRONT-NEAR
glColor3f(1.0, 0.5, 0.0); //Color Orange - Left
glVertex3f(-1.0, 1.0, 1.0); //TOP-FRONT-NEAR
glVertex3f(-1.0, 1.0, -1.0); //TOP-BACK-FAR
glVertex3f(-1.0, -1.0, -1.0);//BOTTOM-BACK-FAR
glVertex3f(-1.0, -1.0, 1.0); //BOTTOM-FRONT-NEAR
glEnd();
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(transZ*cos(rotateA),10,transZ*sin(rotateA), 0,10,0, 0,1,0);
cube(30,30,30);
glFlush();
glutSwapBuffers();
}
void init (void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1, 1000);
glEnable(GL_DEPTH_TEST);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case 'S':
transZ+=1.0f;
glutPostRedisplay();
break;
case 'W':
transZ-=1.0f;
if (transZ<0) transZ=0;
glutPostRedisplay();
break;
case 's':
transZ+=0.5f;
glutPostRedisplay();
break;
case 'w':
transZ-=0.5f;
if (transZ<0) transZ=0;
glutPostRedisplay();
break;
case 'A':
rotateAspeed+=0.001f;
glutPostRedisplay();
break;
case 'a':
rotateAspeed+=0.001f;
glutPostRedisplay();
break;
case 'D':
rotateAspeed-=0.001f;
glutPostRedisplay();
break;
case 'd':
rotateAspeed-=0.001f;
glutPostRedisplay();
break;
}
}
void idle(void)
{
rotateA=rotateA + rotateAspeed;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Cube");
init ();
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
When i press A or D the cube is rotated horizontally but i need to make, when i press, for example J the cube to start rotating vertically. Any help on how to do that?
You need to look at glRotate and how transformations are done in openGL. Then parameterize the rotation by three angles, called Euler angles, or use quaternions, or some other suitable rotation representation. From this the transformations will rotate the cube properly, it might look like this:
glPushMatrix()
glRotatef(alpha, 1, 0, 0); //rotate alpha around the x axis
glRotatef(beta, 0, 1, 0); //rotate beta around the y axis
glRotatef(gamma, 0, 0, 1); //rotate gamma around the z axis
//dram my cube
glPopMatrix();

`xxxx' : looks like a function definition, but there is no parameter list; skipping apparent body

I have the code below which is a bowling game. This code was working good in Eclipse but I Then moved this to microsoft visual studio express 2012 for windows desktop, it gives the following error.
Error 1 error C2470: 'cone1' : looks like a function definition, but there is no parameter list; skipping apparent body c:\users\rocckky\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 26 1 ConsoleApplication1
Note: - I am not pasting all errors, it gives the same error where type cones are defined.
#include "stdafx.h"
#include "cstdlib"
#include "GL/glut.h"
#include "GL/glu.h"
#include "stdlib.h"
#include "vector"
using namespace std;
int refreshMillis = 30; // Refresh period in milliseconds
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner y
bool fullScreenMode = false; // Full-screen or windowed mode?
GLfloat ballSpeed = 0.150f; // Ball's speed in y directions
GLfloat speedLine;
GLfloat ballMaxSpeed = 0.550f, ballMinSpeed = 0.150f;
bool moveBallUp = false, moveBallDown = false, isCollision = false, resetCall =
false, moveRight = false, moveLeft = false, ballInRight = false,
ballInMiddle = true, ballInLeft = false;
GLfloat vp_x = 2.0f, vp_y = 10.0f, vp_z = 25.0f, vt_x = 2.0f, vt_y = 20.0f,
vt_z = 0.0f, vu_x = 0.0f, vu_y = 0.0f, vu_z = 1.0f;
//eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz
/* the cones in the center */
vector<GLfloat> cone1 { 0.0f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0f, 0.0f };
vector<GLfloat> cone2 { 1.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone3 { -1.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
/* the cones in the left */
vector<GLfloat> cone4 { -4.9f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone5 { -6.7f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone6 { -8.3f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
/* the right cones */
vector<GLfloat> cone7 { 4.9f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone8 { 6.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone9 { 8.3f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> ball {/* X */0.0f, /* Y */0.0f, /* Z */-3.0f, /*sphere*/0.85f,
50.0, 50.0 };
//
void resetGame() {
resetCall = true;
cone1 = {0.0f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0f, 0.0f};;
cone2= {1.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone3= {-1.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
/* the cones in the left */
cone4 = {-4.9f,0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone5 = {-6.7f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone6 = {-8.3f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
/* the right cones */
cone7= {4.9f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone8 = {6.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone9= {8.3f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
ball = {/* X */0.0f, /* Y */0.0f, /* Z */-3.0f, /*sphere*/0.85f, 50.0,
50.0};
}
float ar;
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Called back when the timer expired */
void Timer(int value) {
if (moveBallUp) {
// ball[1] += ballSpeed;
ball[2] -= ballSpeed;
if (ballInRight)
ball[0] += 0.08;
if (ballInLeft)
ball[0] -= 0.08;
}
if (moveRight) {
if (ball[0] >= 4.0) {
moveRight = false;
ballInMiddle = false;
ballInLeft = false;
ballInRight = true;
}
ball[0] += 0.156;
if (ball[0] >= -0.02 && ball[0] <= 0.02) {
moveRight = false;
ballInLeft = false;
ballInRight = false;
ballInMiddle = true;
}
}
if (moveLeft) {
if (ball[0] <= -4.0) {
moveLeft = false;
ballInRight = false;
ballInMiddle = false;
ballInLeft = true;
}
ball[0] -= 0.156;
if (ball[0] >= -0.02 && ball[0] <= 0.02) {
moveLeft = false;
ballInLeft = true;
}
}
/* If ball reaches to Z coordinates of the all cones */
if (ball[2] <= cone1[2]) {
/* Now check ball's x axis that which set of cones has it hit */
/* check for the middle set of cones */
if (ball[0] >= cone3[0] && ball[0] <= cone2[0]) {
if (!isCollision /* isCollision ! = true */) {
cone1[0] -= 0.5;
cone1[4] -= 10.0;
cone1[5] += 10.0;
cone1[2] += -0.3;
cone2[0] += 0.5;
cone2[4] -= 10.0;
cone2[5] -= 10.0;
cone2[2] += -0.4;
cone3[0] += 0.5;
cone3[4] -= 10.0;
cone3[5] -= 10.0;
cone3[2] += -0.4;
}
isCollision = true;
moveBallUp = false; // stop moving the ball
}
/* check if ball is in the range of x axis of right set of cones */
if (ball[0] >= cone6[0] && ball[0] <= cone4[0]) {
if (!isCollision /* isCollision ! = true */) {
cone4[0] -= 0.5;
cone4[4] -= 10.0;
cone4[5] += 10.0;
cone4[2] += -0.3;
cone5[0] += 0.5;
cone5[4] -= 10.0;
cone5[5] -= 10.0;
cone5[2] += -0.4;
cone6[0] += 0.5;
cone6[4] -= 10.0;
cone6[5] -= 10.0;
cone6[2] += -0.4;
}
isCollision = true;
moveBallUp = false; // stop moving the ball
}
if (ball[0] >= cone7[0] && ball[0] <= cone9[0]) {
if (!isCollision /* isCollision ! = true */) {
cone7[0] -= 0.5;
cone7[4] -= 10.0;
cone7[5] += 10.0;
cone7[2] += -0.3;
cone8[0] += 0.5;
cone8[4] -= 10.0;
cone8[5] -= 10.0;
cone8[2] += -0.4;
cone9[0] += 0.5;
cone9[4] -= 10.0;
cone9[5] -= 10.0;
cone9[2] += -0.4;
}
isCollision = true;
moveBallUp = false; // stop moving the ball
}
}
if (resetCall) {
if (ball[2] >= -3.0f) {
resetCall = false;
isCollision = false;
}
else {
if (ballInRight)
ball[0] -= 0.08;
if (ballInLeft)
ball[0] += 0.08;
ball[2] -= ballSpeed;
}
}
glutPostRedisplay(); // Post a paint request to activate display()
glutTimerFunc(refreshMillis, Timer, 0); // subsequent timer call at milliseconds
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27: // ESC key
exit(0);
break;
case 'r':
resetGame();
break;
case 'i':
vp_x += 0.5;
break;
case 'I':
vp_x -= 0.5;
break;
/*GLfloat vp_x = 3.0f, vp_y = 10.0f, vp_z = 10.0f, vt_x = 0.0f, vt_y = 0.0f,
vt_z = 0.0f, vu_x = 0.0f, vu_y = 0.0f, vu_z = -1.0f;*/
case 'o':
vp_y += 0.5;
break;
case 'O':
vp_y -= 0.5;
break;
case 'p':
vp_z += 0.5;
break;
case 'P':
vp_z -= 0.5;
break;
case 'j':
vt_x += 0.5;
break;
case 'J':
vt_x -= 0.5;
break;
case 'k':
vt_y += 0.5;
break;
case 'K':
vt_y -= 0.5;
break;
case 'l':
vt_z += 0.5;
break;
case 'L':
vt_z -= 0.5;
break;
case 'b':
vu_x += 0.5;
break;
case 'B':
vu_x -= 0.5;
break;
case 'n':
vu_y += 0.5;
break;
case 'N':
vu_y -= 0.5;
break;
case 'm':
vt_z += 0.5;
break;
case 'M':
vu_z -= 0.5;
break;
}
glutPostRedisplay();
}
void specialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_F1: // F1: Toggle between full-screen and windowed mode
fullScreenMode = !fullScreenMode; // Toggle state
if (fullScreenMode) { // Full-screen mode
windowPosX = glutGet(GLUT_WINDOW_X ); // Save parameters for restoring later
windowPosY = glutGet(GLUT_WINDOW_Y );
windowWidth = glutGet(GLUT_WINDOW_WIDTH );
windowHeight = glutGet(GLUT_WINDOW_HEIGHT );
glutFullScreen(); // Switch into full screen
} else { // Windowed mode
glutReshapeWindow(windowWidth, windowHeight); // Switch into windowed mode
glutPositionWindow(windowPosX, windowPosX); // Position top-left corner
}
break;
case GLUT_KEY_UP:
if (!isCollision)
moveBallUp = true;
break;
case GLUT_KEY_PAGE_UP:
if (ballSpeed >= ballMaxSpeed)
break;
ballSpeed *= 1.1f;
break;
case GLUT_KEY_PAGE_DOWN:
if (ballSpeed <= ballMinSpeed)
break;
ballSpeed *= 0.95f;
break;
case GLUT_KEY_RIGHT:
if (ball[0] >= 4.0)
break;
moveRight = true;
break;
case GLUT_KEY_LEFT:
if (ball[0] <= -4.0)
break;
moveLeft = true;
break;
}
}
static void display(void) {
int const width = glutGet(GLUT_WINDOW_WIDTH );
int const height = glutGet(GLUT_WINDOW_HEIGHT );
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -ar, ar, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(vp_x, vp_y, vp_z, vt_x, vt_y, vt_z, vu_x, vu_y, vu_z);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
/* Center */
glColor3d(1, 1, 0);
glPushMatrix();
glTranslated(cone1[0], cone1[1], cone1[2]);
glRotated(cone1[3], cone1[4], cone1[5], cone1[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 0, 1);
glPushMatrix();
glTranslated(cone2[0], cone2[1], cone2[2]);
glRotated(cone2[3], cone2[4], cone2[5], cone2[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(0, 0, 1);
glPushMatrix();
glTranslated(cone3[0], cone3[1], cone3[2]);
glRotated(cone3[3], cone3[4], cone3[5], cone3[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
/************************************************************/
/* Left cones */
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone4[0], cone4[1], cone4[2]);
glRotated(cone4[3], cone4[4], cone4[5], cone4[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone5[0], cone5[1], cone5[2]);
glRotated(cone5[3], cone5[4], cone5[5], cone5[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone6[0], cone6[1], cone6[2]);
glRotated(cone6[3], cone6[4], cone6[5], cone6[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
/*******************************************************/
/* Right cones */
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone7[0], cone7[1], cone7[2]);
glRotated(cone7[3], cone7[4], cone7[5], cone7[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone8[0], cone8[1], cone8[2]);
glRotated(cone8[3], cone8[4], cone8[5], cone8[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone9[0], cone9[1], cone9[2]);
glRotated(cone9[3], cone9[4], cone9[5], cone9[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
/****************************/
/* THE BALLING BALL */
glColor3d(1, 0, 0);
glPushMatrix();
glTranslated(ball[0], ball[1], ball[2]);
glutSolidSphere(ball[3], ball[4], ball[5]);
glPopMatrix();
/******* Floor ********/
//glPushMatrix();
glColor3d(0.4, 1, 0.20);
glBegin(GL_QUADS);
glVertex3f(21.0, -1.0, -25.0);
glVertex3f(-21.0, -1.0, -25.0);
glVertex3f(-20.0, -1.0, -4.0);
glVertex3f(20.0, -1.0, -4.0);
glEnd();
/** wall at the back **/
glColor3d(0, 1, 1);
glBegin(GL_QUADS);
glVertex3f(21.0, 10.0, -25.0);
glVertex3f(-21.0, 10.0, -25.0);
glVertex3f(-21.0, -1.0, -25.0);
glVertex3f(21.0, -1.0, -25.0);
glEnd();
/* // speed line
glColor3d(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glVertex3f(7.0, 7.0, -24.9999);
glVertex3f(7.5, 7.0, -24.9999);
glVertex3f(6.0, -4.0, -4.9999);
glVertex3f(6.0, -4.0, -4.9999);
glEnd();
Lines between three
glColor3d(0.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glVertex3f(-7.5, 7.0, -24.9999);
glVertex3f(-7.0, 7.0, -24.9999);
glVertex3f(-2.0, -4.0, -4.9999);
glVertex3f(-1.5, -4.0, -4.9999);
glEnd();
Speed line
glLineWidth(8.0f);
glColor3d(1.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
%age from the range of speed by the current speed
speedLine = (100 / (ballMaxSpeed - ballMinSpeed)) * ballSpeed;
speedLine = (3.0 / 100) * speedLine;
glVertex3f(6.0, speedLine, -5.0);
glVertex3f(6.0, 0.0, -4.999);
glEnd();
*/
//glPopMatrix();
glutSwapBuffers();
}
/* Program entry point */
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(windowWidth, windowHeight); // Initial window width and height
glutInitWindowPosition(windowPosX, windowPosY); // Initial window top-left corner (x, y)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Balling Game 3d");
glutDisplayFunc(display);
glutTimerFunc(0, Timer, 0); // First timer call immediately
glutSpecialFunc(specialKeys); // Register callback handler for special-key event
glutKeyboardFunc(keyboard); // Register callback handler for special-key event
glutMainLoop();
return 1;
}
Visual studio 2012 does not have list-initialization yet for the standard containers.
Take a look here and here to see the features already supported by VS. (or here for VS2013).

OpenGL door animation

I'm trying to animate a door so that if I press a button the door will open. At the moment it compiles, and when I go to press the button it moves the camera and then I can't control the camera anymore. What is wrong with the code?
glPushMatrix;
glTranslatef (door_Xpos,0.0, 0.0);
glRotatef (door_Angle, 1,0,0);
glBegin(GL_QUADS);
//Door left
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(0.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(0.0, 0.0, -25.0);
//door right
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(0.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(0.0, 0.0, -25.0);
glPopMatrix;
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
case 'A':
glTranslatef(5.0, 0.0, 0.0);
break;
case 'd':
case 'D':
glTranslatef(-5.0, 0.0, 0.0);
break;
case 'w':
case 'W':
glTranslatef(0.0, 0.0, 5.0);
break;
case 's':
case 'S':
glTranslatef(0.0, 0.0, -5.0);
break;
case 't':
case 'T':
if (is_depth)
{
is_depth = 0;
glDisable(GL_DEPTH_TEST);
}
else
{
is_depth = 1;
glEnable(GL_DEPTH_TEST);
}
#Fall-through...
case 'o':
case 'O':
door_Xpos += 90.0;
break;
}
display();
}
OpenGL is not a scene graph, glTranslate, glRotate and similar don't move objects around, they change the transformation applied to triangles drawn to the screen. Thus it makes no sense to call matrix manipulations functions in an event handler. Actually most OpenGL functions, that includes matrix manipulation, are to be called from the display function. The only exception to this rule are data uploads like textures or buffer object data.
Try animating your door from within the display function based on variables set by the event handlers.