PDA

View Full Version : Rotating one of a set of double doors 90 degrees



PyroMessiah86
03-31-2011, 06:32 PM
Hi there,

I'm currently making a gallery and at the moment I'm having trouble with Rotatef() and rotating one of my doors (set of double doors) 90 degrees so its open.

Basically I've created 1 door in the door function in my program then I have a builddoor function which can calls the door function to translate and rotate the other door.

Ive tried using:
glRotatef(90.0f,0.0f,1.0f,0.0f);

But it sends the door completely out of the gallery in random directions.

Can anyone help?

My code
----



void door()
{

//front of door texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, DoorID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Door Front Quad
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(20.0f, -7.0f, -49.8f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(20.0f, 10.0f, -49.8f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(30.0f, 10.0f, -49.8f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(30.0f, -7.0f, -49.8f);
glEnd();
glDisable(GL_TEXTURE_2D);

// Door Back Texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, DoorID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Door Back Quad
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(20.0f, -7.0f, -50.2f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(20.0f, 10.0f, -50.2f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(30.0f, 10.0f, -50.2f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(30.0f, -7.0f, -50.2f);
glEnd();
glDisable(GL_TEXTURE_2D);


// Door End Texture

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Door2ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Door End Quad
glBegin(GL_QUADS);
glNormal3f(-1.0, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 30.0f, -7.0f, -49.8f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 30.0f, 10.0f, -49.8f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 30.0f, 10.0f, -50.2f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 30.0f, -7.0f, -50.2f);
glEnd();
glDisable(GL_TEXTURE_2D);


// Door End Texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Door2ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Door End Quad
glBegin(GL_QUADS);
glNormal3f(-1.0, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 20.0f, -7.0f, -49.8f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 20.0f, 10.0f, -49.8f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 20.0f, 10.0f, -50.2f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 20.0f, -7.0f, -50.2f);
glEnd();
glDisable(GL_TEXTURE_2D);
}

void builddoor()

{

glPushMatrix();
door(); // first door
glPopMatrix();


glPushMatrix();
glTranslatef(10.0, 0.0, 00.0);// places 2nd door by the other
//so the doors appear closed
glRotatef(90.0f,0.0f,1.0f,0.0f); // ????????
door();
glPopMatrix();

}


Any help is greatly appreciated! :) Thanks

Alfonse Reinheart
03-31-2011, 07:06 PM
What is the origin point of your doors in model space? That's the coordinate system that the Y-axis rotation will be rotating around. So you need to make sure that the door's model space puts the pivot point at zero in the Y direction.

Or, if you're unwilling to change your model, you can translate the doors to a coordinate system where the pivot point is at zero.

Oh, and texture objects store their texture parameters. So unless some other code actually changes the texture parameters, you only need to set them once.

PyroMessiah86
04-02-2011, 01:37 PM
Hi there, thanks for the reply.

Just trying to understand what you mean by origin point? In my first function void door() the 4 quads created there that make up the door are not translated so they contain the origin point on the first door. (theres no top side or underside to the door as its not visible)

In builddoor() I use translatef and call door() to position the second door of the double doors + 10.0 on the x axis. (effectively creating the doors 'shut')

---
glPushMatrix();
glTranslatef(10.0, 0.0, 00.0);// places 2nd door by the other
//so the doors appear closed
glRotatef(90.0f,0.0f,1.0f,0.0f); // ????????
door();
glPopMatrix();
---


The pivot point on the second door 'opening' door would be (40.0(x ,-49.8 (z))

Thanks for the tip on texture parameters, yeah I need to remove certain bits form my code like this still. ( i realized this the other day)

In reading you message do I understand correctly that I need to translate the door where Y would be 0....rotate the door and use translatef again back to the position required?

Alfonse Reinheart
04-02-2011, 02:18 PM
Just trying to understand what you mean by origin point?

The origin is the (0, 0, 0) point of a coordinate system. All rotations are defined to happen around this point. You provide an axis of rotation, but the coordinate system's origin is the point that is rotated around.

Therefore, if you want to rotate around a different point, you must first use a translation to move that object to a coordinate system where that point is the origin, do your rotation, and then move it back.

For example:



glTranslatef(pivotX, pivotY, pivotZ); //return to original coordinate system.
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glTranslatef(-pivotX, -pivotyY, -pivotZ); //Transform to coordinate system where pivot is at the origin.

PyroMessiah86
04-02-2011, 02:21 PM
WOO problem solved based on your original answer. Thanks. I repositioned the door so it was drawn from 0.0,0.0,0.0. Then called it twice...like so


glPushMatrix();
glTranslatef(20.0, -7.0, -49.8);
door();
glPopMatrix();


glPushMatrix();
glTranslatef(39.9, -7.0, -49.8);
glRotatef(90.0f,0.0f,1.0f,0.0f);
door();
glPopMatrix();

:) Cheers Alfonse