PDA

View Full Version : HELP:rotating airplane left and right

jakes
03-30-2009, 04:52 AM
Hi i need some help i need to rotate my airplane left and right with GLUT_KEY_LEFT & GLUT_KEY_RIGHT keys can anyone help
as i am new to open gl i am pulling my hair out the code below is what i have so far it moves how i want it i just need to get the rotation sorted..

Any help will be great......

jakes
///////////////////////////////////////

#include <math.h>
#include <GL/glut.h>

#include <stdlib.h>

static float angle = 0.0, rotateAngle = 0.0, ratio;
static float x = 0.0f, y = 3.0f, z = 2.0f;
static float lx = 0.0f, ly = 0.0f, lz = -1.0f;

int deltaMove = 0;
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
static GLfloat yPos = 0.0f;
static GLfloat zRot = 0.0f;
static GLfloat zPos = 0.0f;
static GLfloat xPos = 0.0f;

void changeSize(int w, int h)
{

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

ratio = 1.0f * w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the clipping volume
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);

}

void drawPlane()
{
glPushMatrix();

//moves in Z direction
//glTranslatef(0.0f, 0.0f, zPos);
//Banks left & right
glRotatef(xRot,0.0f,0.0f,1.0f);

//Translate to centre of camera
glTranslatef(0.0f, 0.0f, -13.0f);
//Rotate to move with camera
glRotatef(180.0, 0.0,250.0, 0.0);
//Scale to size
glScalef(0.0800f, 0.1f, 0.1f);

glBegin(GL_TRIANGLES);
//white
glColor3ub(255, 255, 255);
glVertex3f(0.0f, 0.0f, 60.0f);
glVertex3f(-15.0f, 0.0f, 30.0f);
glVertex3f(15.0f,0.0f,30.0f);

// Black
glColor3ub(255, 255, 255);
// glColor3ub(0,0,0);
glVertex3f(15.0f,0.0f,30.0f);
glVertex3f(0.0f, 15.0f, 30.0f);
glVertex3f(0.0f, 0.0f, 60.0f);

// some code taking out this would be the airplane

glEnd(); //
glPopMatrix();
}

void initScene() {

glEnable(GL_DEPTH_TEST);

}

void orientMe(float ang) {

lx = sin(ang);
lz = -cos(ang);
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}

void moveMeFlat(int i) {
x = x + i*(lx)*0.01;
z = z + i*(lz)*0.01;
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}

void renderScene(void) {
glClearColor(0.0f, 0.5f, 1.0f, 0.0f );

if (deltaMove)

moveMeFlat(deltaMove);

if (rotateAngle) {
angle += rotateAngle;
//xrot code below rotates the plane when i press the left key
//and when i press the right key does the same this is not what i want .
xRot += 0.01f;
orientMe(angle);

}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

drawPlane();
// Draw ground

glColor3f(0.0f, 0.0f, 0.3f);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();

glutSwapBuffers();
}

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

switch (key) {
case GLUT_KEY_LEFT : rotateAngle = -0.0003f; break;
case GLUT_KEY_RIGHT : rotateAngle =0.0003f;break;
case GLUT_KEY_UP : deltaMove = 1;break;
case GLUT_KEY_DOWN : deltaMove = -1;break;
}
}

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

switch (key) {
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT : rotateAngle = 0.0f; break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = 0;break;
}
}

void processNormalKeys(unsigned char key, int x, int y) {

if (key == 27)
exit(0);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1000, 800);
glutCreateWindow("plane");

initScene();

//glutIgnoreKeyRepeat(1);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);

glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);

glutReshapeFunc(changeSize);

glutMainLoop();

return(0);
}