Advertisement
ruhul0

lab

Sep 6th, 2020
1,400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.90 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include <windows.h>
  5. #include <glut.h>
  6. #define pi (2*acos(0.0))
  7.  
  8. int x;
  9. double angle;
  10. double incxA=0, incy,incxB=0,incxC=0;
  11. int state;
  12. double points;
  13. int check1=0,check2=0,check3=0;
  14. struct point
  15. {
  16.     double x,y,z;
  17. };
  18. void draw_tri()
  19. {
  20.     glBegin(GL_POLYGON);
  21.         glVertex2f(10, 10);
  22.         glVertex2f(10, 0);
  23.         glVertex2f(-10, 0);
  24.     glEnd();
  25. }
  26. void push_pop(void)
  27. {
  28.     glPushMatrix();
  29.         //glRotatef(45, 0, 0, 1);
  30.         glPushMatrix(); // Furthest Triangle, Draw first
  31.             //glRotatef(45, 0, 0, 1);
  32.             glTranslatef(-20, 0, 0);
  33.             glScaled(0.5, 2, 0);
  34.             glColor3f(0.0, 0.0, 1.0);
  35.             draw_tri();
  36.  
  37.         glPopMatrix();
  38.         glPushMatrix(); // Middle Triangle, Draw 2nd
  39.             glColor3f(0.0, 1.0, 0.0);
  40.             draw_tri();
  41.         glPopMatrix();
  42.  
  43.         glPushMatrix(); // Nearest Triangle, Draw last
  44.             glTranslatef(20, 0, 0);
  45.             glColor3f(1.0, 0.0, 0.0);
  46.             draw_tri();
  47.         glPopMatrix();
  48.  
  49.     glPopMatrix();
  50.  
  51. }
  52.  
  53.  
  54. void drawAxes()
  55. {
  56.  
  57.     glColor3f(1.0, 1.0, 1.0);
  58.     glBegin(GL_LINES);{
  59.         glVertex3f( 100,0,0);
  60.         glVertex3f(-100,0,0);
  61.  
  62.         glVertex3f(0,-100,0);
  63.         glVertex3f(0, 100,0);
  64.  
  65.         glVertex3f(0,0, 100);
  66.         glVertex3f(0,0,-100);
  67.     }glEnd();
  68.  
  69. }
  70.  
  71.  
  72. void drawGrid()
  73. {
  74.     int i;
  75.  
  76.     glColor3f(0.6, 0.6, 0.6);   //grey
  77.     glBegin(GL_LINES);{
  78.         for(i=-8;i<=8;i++){
  79.  
  80.             if(i==0)
  81.                 continue;   //SKIP the MAIN axes
  82.  
  83.             //lines parallel to Y-axis
  84.             glVertex3f(i*10, -90, 0);
  85.             glVertex3f(i*10,  90, 0);
  86.  
  87.             //lines parallel to X-axis
  88.             glVertex3f(-90, i*10, 0);
  89.             glVertex3f( 90, i*10, 0);
  90.         }
  91.     }glEnd();
  92.  
  93. }
  94.  
  95. void drawSquare(double a)
  96. {
  97.     //glColor3f(1.0,0.0,0.0);
  98.     glBegin(GL_QUADS);{
  99.         glVertex3f( a, a,0);
  100.         glVertex3f( a,-a,0);
  101.         glVertex3f(-a,-a,0);
  102.         glVertex3f(-a, a,0);
  103.     }glEnd();
  104. }
  105.  
  106. void rec_animation()
  107. {
  108.     glColor3f(0,1,0);
  109.     //glRotatef(5*angle,0,0,1);
  110.     glTranslatef(incxA,incy,0);
  111.     glTranslatef(incxB,incy,0);
  112.     glTranslatef(incxC,incy,0);
  113.     glRotatef(5*angle,0,0,1);
  114.     drawSquare(5);
  115.  
  116. }
  117. void draw_rec()
  118. {
  119.  
  120.     drawSquare(1);
  121.  
  122. }
  123.  
  124. void simple_trans()
  125. {
  126.     glPushMatrix();
  127.         glPushMatrix();
  128.             glRotatef(10*angle, 0, 0, 1);
  129.             //glTranslatef(20, 0, 0);
  130.             glColor3f(1.0, 1.0, 1.0);
  131.             draw_rec();
  132.         glPopMatrix();
  133.  
  134.  
  135.  
  136.         glRotatef(angle, 0, 0, 1);
  137.         glTranslatef(20, 0, 0);
  138.         glRotatef(angle, 0, 0, 1);
  139.  
  140.         glColor3f(0.0, 0.0, 1.0);
  141.  
  142.         draw_rec();
  143.     glPopMatrix();
  144. }
  145.  
  146. /*
  147. You will implement the following function to produce the simulation in the video.
  148.  
  149. */
  150. void body()
  151. {
  152.     //rectangle
  153.  
  154.     glBegin(GL_QUADS);{
  155.         glVertex3f( 30, 10,0);
  156.         glVertex3f( 30,-10,0);
  157.         glVertex3f(-30,-10,0);
  158.         glVertex3f(-30, 10,0);
  159.     }glEnd();
  160. }
  161. void cycle()
  162. {
  163.     //tire
  164.     glBegin(GL_QUADS);{
  165.         glVertex3f( 5, 10,0);
  166.         glVertex3f( 5,-10,0);
  167.         glVertex3f(-5,-10,0);
  168.         glVertex3f(-5, 10,0);
  169.     }glEnd();/*
  170.     glBegin(GL_QUADS);{
  171.         glVertex3f( 2, 10,0);
  172.         glVertex3f( 2,-10,0);
  173.         glVertex3f(-2,-10,0);
  174.         glVertex3f(-2, 10,0);
  175.     }glEnd();*/
  176. }
  177. void yellow()
  178. {
  179.     glPushMatrix();
  180.  
  181.     glRotatef(angle,0,0,1);
  182.     glTranslatef(incxA,incy,0);
  183.  
  184.     glPushMatrix();
  185.     glTranslatef(-25,0,0);
  186.     //glRotatef(angle,0,0,1);
  187.     glColor3f(1,1,0);
  188.     cycle();
  189.     glPopMatrix();
  190.  
  191.  
  192. }
  193.  
  194. void violet(){
  195.     glPushMatrix();
  196.  
  197.     glRotatef(angle,0,0,1);
  198.     glTranslatef(incxB,incy,0);
  199.  
  200.     glPushMatrix();
  201.     glTranslatef(25,0,0);
  202.     glColor3f(1,0,1);
  203.     cycle();
  204.     glPopMatrix();
  205. }
  206.  
  207. void green(){
  208.     glPushMatrix();
  209.     //glTranslatef(incxC,incy,0);
  210.     //glTranslatef(15,10,0);
  211.  
  212.     glRotatef(angle,0,0,1);
  213.     //glRotatef(angle,0,0,1);//<--
  214.     glColor3f(0,1,0);
  215.  
  216.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  217.     //glRectf( -0.8, -0.96, -0.7, -0.86 );
  218.     //glRectf( -0.79, -0.95, -0.71, -0.87 );
  219.     glTranslatef(0,0,0);
  220.     body();
  221.     glPopMatrix();
  222. }
  223.  
  224. void display(){
  225.  
  226.     //clear the display
  227.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  228.     glClearColor(0,0,0,0);  //color black
  229.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  230.  
  231.  
  232.  
  233.     glMatrixMode(GL_MODELVIEW);
  234.  
  235.  
  236.     glLoadIdentity();
  237.  
  238.  
  239.     gluLookAt(0,0,70,   0,0,0,  0,1,0);
  240.  
  241.  
  242.  
  243.     glMatrixMode(GL_MODELVIEW);
  244.  
  245.     green();
  246.     yellow();
  247.     violet();
  248.  
  249.     drawAxes();
  250.     drawGrid();
  251.     //BusMove_Assignment();
  252.  
  253.  
  254.  
  255.  
  256.  
  257.     //ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
  258.     glutSwapBuffers();
  259. }
  260.  
  261.  
  262. void animate(){
  263.     //rotation
  264.  
  265.     angle-=0.05; //speed of rotation
  266.  
  267.     if(incxB>50){
  268.         incxA-=0.01;
  269.     } else {
  270.         incxA+=0.01;
  271.     }
  272.     if(incxA>50){
  273.         incxB+=0.01;
  274.     } else {
  275.         incxB-=0.01;
  276.     }
  277.  
  278.     incxC=0;
  279.     points+=0.005;
  280.  
  281.     glutPostRedisplay();
  282. }
  283.  
  284. void init(){
  285.     //codes for initialization
  286.     angle=0;
  287.  
  288.     //clear the screen
  289.     glClearColor(0,0,0,0);
  290.  
  291.  
  292.     //load the PROJECTION matrix
  293.     glMatrixMode(GL_PROJECTION);
  294.  
  295.     //initialize the matrix
  296.     glLoadIdentity();
  297.  
  298.     //give PERSPECTIVE parameters
  299.     gluPerspective(80,  1,  1,  1000.0);
  300.  
  301. }
  302.  
  303. int main(int argc, char **argv){
  304.     glutInit(&argc,argv);
  305.     glutInitWindowSize(500, 500);
  306.     glutInitWindowPosition(0, 0);
  307.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);   //Depth, Double buffer, RGB color
  308.  
  309.     glutCreateWindow("My OpenGL Program");
  310.  
  311.     init();
  312.  
  313.     glEnable(GL_DEPTH_TEST);    //enable Depth Testing
  314.  
  315.     glutDisplayFunc(display);   //display callback function
  316.     glutIdleFunc(animate);      //what you want to do in the idle time (when no drawing is occuring)
  317.  
  318.     //glutKeyboardFunc(keyboardListener);
  319.     //glutSpecialFunc(specialKeyListener);
  320.     //glutMouseFunc(mouseListener);
  321.  
  322.     glutMainLoop();     //The main loop of OpenGL
  323.  
  324.     return 0;
  325. }
  326.  
  327.  
  328.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement