OpenGL creating a command to open a door

Hi,

I am new to these forums and I have a few questions so any help would be amazing :slight_smile:

The first issue is:

I have a door:
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-25.0f, 50.0f, -100.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(25.0f, 50.0f, -100.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(25.0f, 0.0f, -100.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-25.0f, 0.0f, -100.0f);
glEnd();

However I want to be able for the user to press a key, and when pressed that door will either rotate open , or move upwards +50.0

Any advice or code examples on how to do this would be great.

Thanks

You using GLUT? If so it has a keyboard handler. Google for it and i’m sure there’s plenty of examples.

that door will either rotate open , or move upwards +50.0

Which is it then? Rotate or move?
Either way, before you draw the door, you can issue matrix altering commands which affect where OpenGL draws objects w.r.t the camera.
So something along the lines of:


glPushMatrix;
glTranslatef (0.0,door_Ypos, 0.0); //position the door 'up' according to variable 'door_Ypos' 
glRotatef (door_Angle, 0,1,0); //rotate the door x degrees on the Y axis
drawdoor; //your door drawing code
glPopMatrix;

Thanks for the quick response :slight_smile:

yes I am using Glut

I dont mind if the door rotated or went upwards, whatever was easier :stuck_out_tongue:

I have a question the ‘door Ypos’ where would I be writing that in my code and how?

Door_Ypos was just a variable name I made up to store the current YPosition of your door.
When you press a key on the keyboard, you need to add/subtract a value from it.
Same with the rotate variable I created too.

ok so how would i write it? like this?
The door:
glPushMatrix;
glTranslatef (0.0,door_Ypos, 0.0);
glRotatef (door_Angle, 0,1,0);
drawdoor;
//door
glBegin(GL_QUADS);
glNormal3f(0.0,0.0,1.0);
glTexCoord2f(0.0,0.0);
glVertex3f(-25.0, 50.0, 100.0);
glTexCoord2f(0.0,1.0);
glVertex3f(25.0, 50.0, 100.0);
glTexCoord2f(1.0,1.0);
glVertex3f(25.0, 0.0, 100.0);
glTexCoord2f(1.0,0.0);
glVertex3f(-25.0, 0.0, 100.0);
glEnd();

    glPopMatrix;

and then the keyboard press…

case 'a':
case 'A':
	door(0.0, 50.0, 0.0);
	break;

something like that?
:s

Almost… :wink:

drawdoor; was a function name I made to to represent:
//door
glBegin(GL_QUADS);
glNormal3f(0.0,0.0,1.0);
glTexCoord2f(0.0,0.0);
…so take it out if you are going to inline the door code.

and then the keyboard press…

case ‘a’:
case ‘A’:
door_Ypos += 50.0;
break;

Ok, I have placed this into my code, but i get 2 errors saying:
error C2065: ‘door_Ypos’ : undeclared identifier

Do i need to identify door_Ypos somewhere else other than before i draw the door?

Ok i’m an idiot i declared GLfloat door_Ypos at the top now and it compiles.

When i pree my keyboard press something happens, i move up instead of the door.

I have been using glTranslatef for my camera too. Is this going to be a problem?

Or do i just do something like : glMatrixMode(GL_MODELVIEW);

I have been using glTranslatef for my camera too. Is this going to be a problem?

No, that’s why I had added glPushMatrix and glPopMatrix to preserve the current modelview matrix.
I assumed that the current matrix was the ModelView, which is what it would need to be 99.9% of the time.

How are you controlling the camera position? I hope the same key does not ‘move’ the door and control the camera as well?!

no, my camera movements are: w,a,s,d (for,bck,lft,rght)

c = open door.

but when im at the door and i press c,
the camera moves downwards and i cant control it after that point
:s

i tried doing the same but instead of the y position in the x position but it moves the camera in the x position, so i have the same issue as ctrlvee

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);
	}
	case 'o':
    case 'O':
    door_Xpos += 90.0;
    break;
}
display();

}