Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #include <windows.h>
- #include <glut.h>
- #define pi (2*acos(0.0))
- int x;
- double angle;
- double incx=0, incy;
- int state;
- double points;
- struct point
- {
- double x,y,z;
- };
- void draw_tri()
- {
- glBegin(GL_POLYGON);
- glVertex2f(10, 10);
- glVertex2f(10, 0);
- glVertex2f(-10, 0);
- glEnd();
- }
- void push_pop(void)
- {
- glPushMatrix();
- //glRotatef(45, 0, 0, 1);
- glPushMatrix(); // Furthest Triangle, Draw first
- //glRotatef(45, 0, 0, 1);
- glTranslatef(-20, 0, 0);
- glScaled(0.5, 2, 0);
- glColor3f(0.0, 0.0, 1.0);
- draw_tri();
- glPopMatrix();
- glPushMatrix(); // Middle Triangle, Draw 2nd
- glColor3f(0.0, 1.0, 0.0);
- draw_tri();
- glPopMatrix();
- glPushMatrix(); // Nearest Triangle, Draw last
- glTranslatef(20, 0, 0);
- glColor3f(1.0, 0.0, 0.0);
- draw_tri();
- glPopMatrix();
- glPopMatrix();
- }
- void drawAxes()
- {
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_LINES);{
- glVertex3f( 100,0,0);
- glVertex3f(-100,0,0);
- glVertex3f(0,-100,0);
- glVertex3f(0, 100,0);
- glVertex3f(0,0, 100);
- glVertex3f(0,0,-100);
- }glEnd();
- }
- void drawGrid()
- {
- int i;
- glColor3f(0.6, 0.6, 0.6); //grey
- glBegin(GL_LINES);{
- for(i=-8;i<=8;i++){
- if(i==0)
- continue; //SKIP the MAIN axes
- //lines parallel to Y-axis
- glVertex3f(i*10, -90, 0);
- glVertex3f(i*10, 90, 0);
- //lines parallel to X-axis
- glVertex3f(-90, i*10, 0);
- glVertex3f( 90, i*10, 0);
- }
- }glEnd();
- }
- void drawSquare(double a)
- {
- //glColor3f(1.0,0.0,0.0);
- glBegin(GL_QUADS);{
- glVertex3f( a, a,0);
- glVertex3f( a,-a,0);
- glVertex3f(-a,-a,0);
- glVertex3f(-a, a,0);
- }glEnd();
- }
- void rec_animation()
- {
- glColor3f(0,1,0);
- //glRotatef(5*angle,0,0,1);
- glTranslatef(incx,incy,0);
- glRotatef(5*angle,0,0,1);
- drawSquare(5);
- }
- void draw_rec()
- {
- drawSquare(1);
- }
- void simple_trans()
- {
- glPushMatrix();
- glPushMatrix();
- glRotatef(10*angle, 0, 0, 1);
- //glTranslatef(20, 0, 0);
- glColor3f(1.0, 1.0, 1.0);
- draw_rec();
- glPopMatrix();
- glRotatef(angle, 0, 0, 1);
- glTranslatef(20, 0, 0);
- glRotatef(angle, 0, 0, 1);
- glColor3f(0.0, 0.0, 1.0);
- draw_rec();
- glPopMatrix();
- }
- /*
- You will implement the following function to produce the simulation in the video.
- */
- void body()
- {
- //rectangle
- glBegin(GL_QUADS);{
- glVertex3f( 30, 10,0);
- glVertex3f( 30,-10,0);
- glVertex3f(-30,-10,0);
- glVertex3f(-30, 10,0);
- }glEnd();
- }
- void cycle()
- {
- //tire
- glBegin(GL_QUADS);{
- glVertex3f( 10, 2,0);
- glVertex3f( 10,-2,0);
- glVertex3f(-10,-2,0);
- glVertex3f(-10, 2,0);
- }glEnd();
- glBegin(GL_QUADS);{
- glVertex3f( 2, 10,0);
- glVertex3f( 2,-10,0);
- glVertex3f(-2,-10,0);
- glVertex3f(-2, 10,0);
- }glEnd();
- }
- void BusMove_Assignment()
- {
- glPushMatrix();
- glTranslatef(incx,incy,0);
- glPushMatrix();
- glRotatef(angle,0,0,1);
- glColor3f(0,0,1);
- cycle();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(30,0,0);
- glRotatef(angle,0,0,1);
- glColor3f(0,1,0);
- cycle();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(15,10,0); //<--
- glColor3f(1,0,0);
- body();
- glPopMatrix();
- glPopMatrix();
- }
- void display(){
- //clear the display
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glClearColor(0,0,0,0); //color black
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0,0,70, 0,0,0, 0,1,0);
- glMatrixMode(GL_MODELVIEW);
- BusMove_Assignment();
- drawAxes();
- drawGrid();
- //BusMove_Assignment();
- //ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
- glutSwapBuffers();
- }
- void animate(){
- //rotation
- angle-=0.1; //speed of rotation
- incx+=0.01;
- points+=0.005;
- glutPostRedisplay();
- }
- void init(){
- //codes for initialization
- angle=0;
- //clear the screen
- glClearColor(0,0,0,0);
- //load the PROJECTION matrix
- glMatrixMode(GL_PROJECTION);
- //initialize the matrix
- glLoadIdentity();
- //give PERSPECTIVE parameters
- gluPerspective(80, 1, 1, 1000.0);
- }
- int main(int argc, char **argv){
- glutInit(&argc,argv);
- glutInitWindowSize(500, 500);
- glutInitWindowPosition(0, 0);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); //Depth, Double buffer, RGB color
- glutCreateWindow("My OpenGL Program");
- init();
- glEnable(GL_DEPTH_TEST); //enable Depth Testing
- glutDisplayFunc(display); //display callback function
- glutIdleFunc(animate); //what you want to do in the idle time (when no drawing is occuring)
- //glutKeyboardFunc(keyboardListener);
- //glutSpecialFunc(specialKeyListener);
- //glutMouseFunc(mouseListener);
- glutMainLoop(); //The main loop of OpenGL
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment