Advertisement
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 incxA=0, incy,incxB=0,incxC=0;
- int state;
- double points;
- int check1=0,check2=0,check3=0;
- 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(incxA,incy,0);
- glTranslatef(incxB,incy,0);
- glTranslatef(incxC,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( 5, 10,0);
- glVertex3f( 5,-10,0);
- glVertex3f(-5,-10,0);
- glVertex3f(-5, 10,0);
- }glEnd();/*
- glBegin(GL_QUADS);{
- glVertex3f( 2, 10,0);
- glVertex3f( 2,-10,0);
- glVertex3f(-2,-10,0);
- glVertex3f(-2, 10,0);
- }glEnd();*/
- }
- void yellow()
- {
- glPushMatrix();
- glRotatef(angle,0,0,1);
- glTranslatef(incxA,incy,0);
- glPushMatrix();
- glTranslatef(-25,0,0);
- //glRotatef(angle,0,0,1);
- glColor3f(1,1,0);
- cycle();
- glPopMatrix();
- }
- void violet(){
- glPushMatrix();
- glRotatef(angle,0,0,1);
- glTranslatef(incxB,incy,0);
- glPushMatrix();
- glTranslatef(25,0,0);
- glColor3f(1,0,1);
- cycle();
- glPopMatrix();
- }
- void green(){
- glPushMatrix();
- //glTranslatef(incxC,incy,0);
- //glTranslatef(15,10,0);
- glRotatef(angle,0,0,1);
- //glRotatef(angle,0,0,1);//<--
- glColor3f(0,1,0);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- //glRectf( -0.8, -0.96, -0.7, -0.86 );
- //glRectf( -0.79, -0.95, -0.71, -0.87 );
- glTranslatef(0,0,0);
- body();
- 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);
- green();
- yellow();
- violet();
- 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.05; //speed of rotation
- if(incxB>50){
- incxA-=0.01;
- } else {
- incxA+=0.01;
- }
- if(incxA>50){
- incxB+=0.01;
- } else {
- incxB-=0.01;
- }
- incxC=0;
- 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
Advertisement