School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,578 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,206 people online right now. Registration is fast and FREE... Join Now!




opengl animation problem part 1

 

opengl animation problem part 1

deli10

29 Apr, 2009 - 12:25 AM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2009
Posts: 2


hi, i have a fairly easy problem i cant resolve, i want my bike to fly after the track is finished. i am wondering how can i do that in a loop to check if the x coordinate is empty then fly.
the A key is working for acceleration already, with the deceleration (a).

2- my 2nd problem is the first person camera. i will add some code later because the i removed the first person camera. i am unable to work the glulookat right to position it on the chair of the bike looking straight at the track.

any help would be appreciated . thanks







[code]

#include <gl\glut.h>
#include <math.h>

GLfloat red[] = { 0.8f, 0.0, 0.0, 1.0 };
GLfloat blue[] = { 0.0, 0.2f, 1.0, 1.0 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat polished[] = { 100.0 };
GLfloat dull[] = { 0.0 };

GLfloat m_diff[] = { 0.8, 0.0, 1.0, 1.0 }; // Diffuse
GLfloat m_amb[] = { 0.5f, 0.5f, 0.5f, 1.0 }; // Ambient
GLfloat m_spec[] = { 0.0, 0.0, 0.0, 1.0 }; // Specular
GLfloat m_emit[] = { 0.0, 0.0, 0.0, 1.0 }; // Emission
GLfloat m_shine[] = { 0.4 }; // Shininess

void myDisplay();
void resize(int w, int h);
void myKeyboard(unsigned char key, int x , int y);
void Skimmer(bool fly);
void axis();
void DrawWheels();
void DrawRims();
void sky();
void land();
void Init();

void DrawDeck();
//Update scene for animaiton
void updateScene();
GLvoid window_idle();

//ADD
void inputKey(int key, int x, int y);
float angleX;
float angleY;
float radius;

//Add
/*
draw objects as "wire" or "solid"
*/
bool wire;

//Add
/*
set projecttion as "perspective" or "ortho"
*/
bool perspective;


float eyeZ;
float eyeX;
float eyeY;


GLint angle1;
GLint angle2;
GLint angle3;

//Add
GLfloat zoom; // Camera zoom setting

//Add to control resize();
GLint win_width = 400; // Window dimensions
GLint win_height = 300;

GLdouble left,right,bottom,top,near,far;
//This variable is used to control the wing of skimmer
//True means the skimmer is flying
bool fly;

//This variable is used to control wheel rotation
bool rotate;
//Wheel rotate angle
double wheelRotateAngle;

//This variable is used to control the animation of the Skimmer
bool animation;

//This variable is used to control the acceleration/deceleration of Skmmer
int accelerate;

int speed;


// Position the light at the origin.
const GLfloat light_pos[] = { 0, 10, 0, 1.0 };

// Attenuation factors for light.
GLfloat const_att = 0.1;

// Translation values for light.
GLfloat move_y =-5.0;
GLfloat move_z =-5.0;
GLfloat move_x =0.0;

bool light;


int main(int argc, char ** argv)
{
// initialize the display window
glutInit(&argc, argv);


//Add
/*
Initial all values
*/
Init();



// Request hidden surface elimination and register callbacks.
glEnable(GL_DEPTH_TEST);

//Glfloat global_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};


glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(1200, 1200);
glutInitWindowPosition(100, 100);
glutCreateWindow("Skimmer ");

// Callback functions

//Add
glutSpecialFunc(inputKey);

glutDisplayFunc(myDisplay);

glutIdleFunc(&window_idle);

glutReshapeFunc(resize);
glutKeyboardFunc(myKeyboard);


// opengl loop
glutMainLoop();

return 0;
}

void myDisplay()
{
glClearColor(0.12, 0.45, 0.11, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();



eyeZ = radius * cos(angleX)*cos(angleY);
eyeX = radius * sin(angleX)*cos(angleY);
eyeY = radius * sin(angleY);
gluLookAt(eyeX, eyeY, -1*eyeZ, 0, 0, 0, 0, 1, 0);

if (light)
{
glPushMatrix();
glTranslatef(10,0,25);
// Initialize the light.

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, const_att);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, m_amb);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);

glPopMatrix();
}

else
{
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);

}
// enable color tracking
glEnable(GL_COLOR_MATERIAL);
// set material properties which will be assigned by glColor
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

glPushMatrix();

sky();

land();

//glPushMatrix();

//These 3 statements place the skimmer on the path
glTranslatef(5,-5,10);
glScalef(0.1,0.1,0.1);
glRotatef(-90,0,1,0);

//This statement control moving forward/backward along the path

while
glTranslatef(-speed,0,0);



}
Skimmer(fly);
glPopMatrix();

glutSwapBuffers();
}

GLvoid window_idle()
{
updateScene();

glutPostRedisplay(); // redraw the window at the next possible moment
}

void updateScene()
{

wheelRotateAngle+=1.0f;


if(accelerate>0)
speed+=accelerate/20;
else
accelerate=0;

}
void DrawShaft() // bike cylinder shafts that connects the wheel to the steering.

{
glColor3f(0.75, 0.75, 0.75);
GLUquadricObj * qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj, 0.15, 0.20, 3.0, 80,80);

}

void Track() // bike track start and end

{
//glColor3f(0.2, 0.2, 1.0);
glColor3f(0.0f,0.0f,1.0f);
GLUquadricObj * qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluPartialDisk(qobj, 0.0, 10, 50, 4,0, 180);

}
void DrawRims()
{
glColor3f(0.75, 0, 0.2);
//glutSolidTorus(0.2, 0.5, 100 ,100);
glScalef(0.3,2.5,0.1);
glutSolidCube(0.8);
glRotatef(45,0,0,1);
glutSolidCube(1);
}

void DrawWheels()
{
glColor3f(0, 0, 0);


if (rotate)
{
glRotatef(wheelRotateAngle,0,0,1);
}
glutSolidTorus(0.9, 1.0, 100 ,100);
DrawRims();

}



void DrawGasTank()
{
glColor3f(0.0, 0.0, 0.41);
glutSolidSphere(0.4, 20.0, 100.0);
}

void TankLock()
{
glColor3f(0.55, 0.55, 0.55);

glutSolidTorus(0.1, 0.15, 100 ,100);
}

void DrawAxe()

{
glColor3f(0.00, 0.00, 0.00); // cylinder which is connected to the wheel and shaft to tighten the shaft to the wheel.
GLUquadricObj * qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj, 0.4, 0.4, 1.5, 80,80);

}




void DrawSteering() // bike cylinder shafts that connects the wheel to the steering.

{
glColor3f(0.65, 0.65, 0.65);
GLUquadricObj * qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj, 0.15, 0.15, 4.0, 80,80);

}

void DrawWing()
{
glColor3f(0.6,0.0, 0.0);
glScalef(1.5, 1.5, 0.2);
glutSolidCube(1.0);

}


void DrawEngine()

{
glColor3f(0.25, 0.50, 0.50); // cylinder for the jet engine
GLUquadricObj * qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj, 0.3, 0.2, 1.5, 80,80);

}

void Skimmer(bool fly)
{

//Add
//draw a temp main body
glPushMatrix();
glColor3f(0, 0, 1);
glScalef(1,0.2,0.3);
glutSolidCube(6);
glPopMatrix();
// draw chair

glPushMatrix();
glColor3f(0.5, 0.25, 0.25);
glTranslatef (-1.4, 0.8, 0.0);
glRotatef (0.0, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);

glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutSolidCube (1.0);
glPopMatrix();

glTranslatef (1.0, 0.0, 0.0);
glRotatef (65.0, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);

glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutSolidCube (1.0);
glPopMatrix();

glPopMatrix();


// draw front wheel & front Rims with the axe.
glPushMatrix();
glTranslatef(-4, 0, 0);
DrawWheels();
glPopMatrix();

glPushMatrix();
// back wheel & rims with the axe.
glTranslatef(4, 0, 0);
DrawWheels();
glPopMatrix();


glPushMatrix();
glTranslatef(-4, 0.0, -0.5);
glRotatef (90.0, 0.0, 1.0, 0.0);
glRotatef (-60.0, 1.0, 0.0, 0.0);
DrawShaft(); // outer front shaft to connect the steering to the wheel
glPopMatrix();


glPushMatrix();
glTranslatef(-4, 0.0, 0.5);
glRotatef (90.0, 0.0, 1.0, 0.0);
glRotatef (-60.0, 1.0, 0.0, 0.0);
DrawShaft(); // inner front shaft to connect the steering to the wheel
glPopMatrix();


glPushMatrix();
glTranslatef(-2.0, 0.9, 0.0);
glScalef(1.4, 1.0,1.0);
DrawGasTank();
glPopMatrix();

glPushMatrix();
glTranslatef(-2.0, 1.1, 0.0);
glScalef(0.4, 0.4,0.4);
TankLock();
glPopMatrix();

glPushMatrix();
glTranslatef(-2.9,1.8,-0.15);
glScalef(0.2, 0.8,0.9); // modeling windscreen
glutWireCube(1.3);
glPushMatrix();
glTranslatef(0.0, 0.6, -1.9);
DrawSteering(); /// drawing steering
glPopMatrix();
glPopMatrix();

glPushMatrix();
//Add

glTranslatef(-2, 1, -1.0);
if (fly)
{

glScalef(0.5, 6, 0.5);
glRotatef(75, -1.0, 0.0, 0.0);
}


DrawWing(); // draw right wing
glPopMatrix();

glPushMatrix();
//Add
glTranslatef(-2.0, 1, 1.0);
if (fly)
{

glScalef(0.5, 6, 0.5);
glRotatef(-75, -1.0, 0.0, 0.0);
}
else
glTranslatef(-2.0, 0, 1.0);

DrawWing(); // draw left wing
glPopMatrix();


glPushMatrix();
glTranslatef(2.0, 0.0, -1.0);
glRotatef(90.0, 0.0, 1.0, 0.0);
DrawEngine(); // draw right Jet Engine
glPopMatrix();

glPushMatrix();
glTranslatef(2.0, 0.0, 1.0);
glRotatef(90.0, 0.0, 1.0, 0.0);
DrawEngine(); // draw left Jet Engine
glPopMatrix();

glPopMatrix();
}
//axis();




void sky()
{
glBegin(GL_POLYGON);
// x axis
glColor3f(0.3, 0.4, 0.9);
glVertex3f(60, 0, 60);
glVertex3f(60, 0, -60);
glVertex3f(-60, 0, -60);
glVertex3f(-60, 0, 60);
glEnd();
}

void land()
{ glPushMatrix();

glTranslatef(30,-10,20);
glRotatef(-90.0,1,0,0);
glRotatef(90,0,0,-1);
Track();
glPopMatrix();

glPushMatrix();
glTranslatef(30,-10,0);



//Track();
for (int i=-10;i<10;i++)
for(int j=-20;j<20;j++)
{
glBegin(GL_TRIANGLES);
glColor3f(0.6f,0.8f,0.7f);
glVertex3f(0.0f+i,0.0f,1.0f+j);
glVertex3f(0.0f+i,0.0f,0.0f+j);
glVertex3f(1.0f+i,0.0f,0.0f+j);

glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f+i,0.0f,1.0f+j);
glVertex3f( 1.0f+i,0.0f,0.0f+j);
glVertex3f( 1.0f+i,0.0f,1.0f+j);
glEnd();

}

glPopMatrix();

glPushMatrix();
glTranslatef(30,-10,-20);
glRotatef(-90.0,1,0,0);
glRotatef(90,0,0,1);
Track();
glPopMatrix();
glPushMatrix();
glTranslatef(30,-10,0);
//glRotatef(-90.0,1,0,0);
glRotatef(90,0,1,0);
glColor3fv(black);
glRotatef(90,0,0,1);
glRectf(-0,0,2,24);
glTranslatef(0,-4,0);
glRectf(-0,0,2,-20);
glPopMatrix();

glPopMatrix();
}

void axis()
{
glBegin(GL_LINES);
// x axis
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(5, 0, 0);

// y axis
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 5, 0);

// z axis
glColor3f(0, 0, 1);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);

glEnd();
}


//Add
/*
Put every initial value to this function
*/
void Init(){

angleX =-1.6;
angleY = -0.15;
radius = 10;

zoom=90.0;

wire=true;
perspective=true;

eyeZ = 0;
eyeX = 0;
eyeY = 0;

angle1 = 0;
angle2 = 0;
angle3 = 0;

//firstperson = false;
float firstpersonX = -3.0;
float firstpersonY = -3.0;
float firstpersonZ = 0.0;

float firstpersonViewX = -3.0;
float firstpersonViewY = -3.0;
float firstpersonViewZ = 0.0;


//parameter for ortho
left=-10;
right=10;
bottom=-10;
top=10;
near=1;
far=500;


//Control Skimmer wings
fly=true;
rotate=false;
accelerate=0;
animation=false;

//acceleration
speed=0;

//Lighting
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);


}

//Add
void set_ortho(){

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(left,right,bottom,top,near,far);
}

//Add
void set_projection () {
// Reset the projection when zoom setting or window shape changes.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(zoom, GLfloat(win_width) / GLfloat(win_height),
1.0, 500.0);
}
//Add resize windows
void resize (GLint new_width, GLint new_height) {
// Window reshaping function.
win_width = new_width;
win_height = new_height;
glViewport(0, 0, win_width, win_height);

//Switch between perspective or othor
if(perspective)
set_projection();
else
set_ortho();
}



void myKeyboard(unsigned char key, int x, int y)
{ //keyboard input to exit program
if (key == 27)
exit(0);

switch (key)
{

case 'q' :
axis();
break;
//Add
case 'o':
case'O': ////keyboard input to activate ortho view
perspective=false;
set_ortho();
break;
//Add
case 'p': //keyboard input to activate prespective view
case 'P':
perspective=true;
set_projection();
break;

//Add for Zoom in // //keyboard input to zoom in and out
case 'z':
if(perspective==true)
{
zoom += 1.0f;
set_projection();
}
else
{
left-=1.00;
right+=1.00;
bottom-=1.00;
top+=1.00;
//near+=0.2;
//far+=0.2f;
set_ortho();
}
break;
//Add for zoom out
case 'Z':
if(perspective==true)
{
zoom -= 1.00f;
set_projection();
}
else
{
left+=1.00;
right-=1.00;
bottom+=1.00;
top-=1.00;
//near-=0.2f;
//far-=0.2f;
set_ortho();
}
break;


case 'c':
case 'C': ////keyboard input to reset view
Init();
set_projection();
break;


//reduce the distance between camera and object by changing the "radius" of camera
case 'f':
case 'F':

radius-=1.00;
break;

///increase the distance between camera and object by changing the "radius" of camera
case 'b':
case 'B':

radius+=1.00;
break;


//start skimmer animation
case 's':{
animation=true;
break;
}
//Stop skimmer animation
case 'S':{
animation=false;
break;
}
//Accelerate
case 'a':{
accelerate+=2;
rotate=true;
break;
}
//Decelerate
case 'A':{
accelerate-=2;
rotate=true;
break;
}
//Stop
case't':
case'T':{
accelerate=0;
rotate=false;
break;
}
case '1':{
// firstperson=true;
break;
}
case '3': {
// firstperson=false;
break;
}


case'l':
case'L':{
light=!light;
break;

}
}
//glutPostRedisplay();
myDisplay();

}


//ADD special Keys
void inputKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT :
angleX -= 0.05f;
break;
case GLUT_KEY_RIGHT :
angleX +=0.05f;
break;
case GLUT_KEY_UP :
angleY +=0.05f;

break;
case GLUT_KEY_DOWN :
angleY -=0.05f;

break;

//case GLUT_KEY_F1 :
//case GLUT_KEY_F2 :
}
glutPostRedisplay();


}


[code]


Attached File(s)
Attached File  Assign3.pdf ( 154.27k ) Number of downloads: 15

User is offlineProfile CardPM
+Quote Post


firebolt

RE: Opengl Animation Problem Part 1

1 May, 2009 - 05:16 PM
Post #2

D.I.C Lover
Group Icon

Joined: 20 Feb, 2009
Posts: 5,464



Thanked: 75 times
Dream Kudos: 1675
My Contributions
End the code with [/code ]
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 08:19AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month