Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
opengl program moving train
#1

opengl program moving train

Design and Implementation

For developing the train - it's bogies and engine we have defined a simple function. In this simple opengl projects example we are coding this function so we can make any no trains, by just calling it. The following is sample code for drawing train -

void TRAINS(int x1,int y1,int a,int b)
{
int i=0; glBegin(GL_QUADS);
glColor3f(0,0.0,1.0); //ENGINE
glVertex2f(x1,y1); //lengh of engine=60;height of engine=30;
glColor3f(0,0.0,1.0);
glVertex2f(x1+60,y1);
glColor3f(1.0,0.0,0.0);
glVertex2f(x1+60,y1-30);
glColor3f(0,0.0,0.0);
glVertex2f(x1,y1-30);
glEnd();
while(i<3) {
glBegin(GL_QUADS); //BOGIES
glColor3f(1.0,0.0,0.0); //For right train a=795,b=510
glVertex2f(a,b);
glColor3f(1.0,0.0,0.0);
glVertex2f(a+60,b);
glColor3f(1.0,0.0,0.0);
glVertex2f(a+60,b-20);
glColor3f(1.0,0.0,0.0);
glVertex2f(a,b-20);
glEnd();
a+=65; i++;
}
}

This function can be called any where hence easy to create the sample trains. In similar fashion we have different functions for Sky, Environments and Tracks. You can download the free cg source code below. All the things made in the opengl projects example have used primitive opengl objects.

Next level of difficulty is to move the whole Train - bogies and engine. For the motion of train we have logically used the call-list, push-pop matrix and the most important Clock() function. The clock() is defined in <time.h> header so you need to include it as well in the project.

clock_t goal = mseconds + clock(); while (goal > clock());

Our new function will help the train runs according a time level else we may go so fast or so slow. We have fixed a goal and add certain time for it to move the objects. The above code is called in a function and we give the parameter mseconds according to our requirement of speed we want.
Reply

#2

how to write the code for moving train using c programming..????
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.