Advertisement
Shishu

house,star,pyramid and chase board in visual baosc

Mar 1st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. Computer Graphics
  4.               .2019
  5.  
  6. Name: Arifur Rahman
  7. ID:     163015004
  8. Task_01:
  9. Draw a house using OpenGL.
  10. Requirements:
  11. ● Visual Studio IDE with
  12. ● C++ Development Environment With OpenGL.
  13. Code Details:
  14. #include <GL/glut.h>
  15.  
  16. void shapes(void) {
  17.     //Drawing House
  18.     glBegin(GL_TRIANGLES);
  19.     glColor3f(0.0, 1.0, 0.0);
  20.     glVertex3f(-0.5, 0.5, 0);
  21.     glVertex3f(0, 1.0, 0);
  22.     glVertex3f(0.5, 0.5, 0);
  23.     glEnd();
  24.  
  25.     glBegin(GL_QUADS);
  26.     glColor3f(0.8, 0.8, 0.8);
  27.     glVertex3f(-0.5, -0.5, 0.0);
  28.     glVertex3f(-0.5, 0.5, 0.0);
  29.     glVertex3f(0.5, 0.5, 0.0);
  30.     glVertex3f(0.5, -0.5, 0.0);
  31.     glEnd();
  32.  
  33.     glBegin(GL_QUADS);
  34.     glColor3f(0.0, 0.0, 1.0);
  35.     glVertex3f(-0.15, -0.5, 0.0);
  36.     glVertex3f(-0.15, 0.2, 0.0);
  37.     glVertex3f(0.15, 0.2, 0.0);
  38.     glVertex3f(0.15, -0.5, 0.0);
  39.     glEnd();
  40.  
  41.     glBegin(GL_QUADS);
  42.     glColor3f(1.0, 0.0, 0.0);
  43.     glVertex3f(-0.45, -0.05, 0.0);
  44.     glVertex3f(-0.45, 0.15, 0.0);
  45.     glVertex3f(-0.25, 0.15, 0.0);
  46.     glVertex3f(-0.25, -0.05, 0.0);
  47.     glEnd();
  48.  
  49.     glBegin(GL_QUADS);
  50.     glColor3f(1.0, 0.0, 0.0);
  51.     glVertex3f(0.45, -0.05, 0.0);
  52.     glVertex3f(0.45, 0.15, 0.0);
  53.     glVertex3f(0.25, 0.15, 0.0);
  54.     glVertex3f(0.25, -0.05, 0.0);
  55.     glEnd();
  56. }
  57.  
  58. void display(void) {
  59.     // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
  60.     glClearColor(0.0, 0.0, 0.0, 1.0);
  61.     glClear(GL_COLOR_BUFFER_BIT);
  62.     glLoadIdentity();
  63.  
  64.     //viewing transformation
  65.     //glulookat() positions the camera towards the object
  66.     //camera position, camera target, up vector
  67.     gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  68.     shapes();
  69.     glFlush();
  70. }
  71.  
  72. void reshape(int w, int h) {
  73.     // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
  74.     // the next coordinates are width and height of the viewport
  75.     //Set the viewport to be the entire window
  76.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  77.  
  78.     // setting the camera
  79.     glMatrixMode(GL_PROJECTION);
  80.     glLoadIdentity();
  81.  
  82.     //perspective transform
  83.     //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
  84.     gluPerspective(30, 1, 1.0, 100.0);
  85.     glMatrixMode(GL_MODELVIEW); //switch back the the model editing mode.
  86.  
  87. }
  88.  
  89. int main(int argc, char **argv) {
  90.     glutInit(&argc, argv);
  91.     // single buffering.. (double buffering for animation)
  92.     glutInitDisplayMode(GLUT_SINGLE);
  93.     // full screen is 1000,1000
  94.     // this 0,0 or 1000,1000 are world coordinates
  95.     glutInitWindowSize(700, 700);
  96.     glutInitWindowPosition(100, 100);
  97.     glutCreateWindow("A basic OpenGL Window");
  98.     // registering callback functions
  99.     glutDisplayFunc(display);
  100.     glutReshapeFunc(reshape);
  101.  
  102.     glutMainLoop();
  103.     return 0;
  104. }
  105.  
  106. Output:
  107.  
  108.  
  109. Conclusion:
  110. We learn about OpenGL triangles, quads commands. 
  111.  
  112. Task_02:
  113. Draw a star using OpenGL.
  114. Requirements:
  115. ● Visual Studio IDE with
  116. ● C++ Development Environment with OpenGL.
  117. Code Details:
  118. #include <GL/glut.h>
  119.  
  120. void shapes (void) {
  121.  
  122.     /*glColor3f(1.0, 1.0, 1.0); //this will set a color of the square.
  123.     glBegin(GL_LINES); // write shape u want to create
  124.     glVertex3f(-0.5, -0.5, 0.0);
  125.     glVertex3f(-0.5, 0.5, 0.0);
  126.     glVertex3f(0.5, 0.5, 0.0);
  127.     glVertex3f(0.5, -0.5, 0.0);*/
  128.  
  129.     /*glBegin(GL_POINTS);
  130.     glVertex3f(.5,.5,0);
  131.     glVertex3f(-.5,-.5,0);
  132.     glVertex3f(0,.5,0);
  133.     glVertex3f(.5,0,0);
  134.     glVertex3f(0,0,0);*/
  135.  
  136.     glBegin(GL_LINE_LOOP);
  137.       glVertex3f(-0.5,0.5,0);
  138.       glVertex3f(0.5,0.5,0);
  139.       glVertex3f(-0.3,-0.2,0);
  140.      glVertex3f(0.0,1,0.0);
  141.       glVertex3f(0.3,-0.2,0);
  142.        
  143.      
  144.  
  145.     /*glBegin(GL_POLYGON);
  146.     glColor3f(1, 0, 0);
  147.     glVertex3f(-0.6, -0.75, 0);
  148.     glColor3f(0, 1, 0);
  149.     glVertex3f(0.6, -0.75, 0);
  150.     glColor3f(0, 0, 1);
  151.     glVertex3f(0, 0.75, 0);*/
  152.  
  153.     /*glBegin(GL_TRIANGLES);
  154.     glColor3f(0.5, 0.5, 0.5);
  155.     glVertex3f(0, 0, 0);
  156.     glVertex3f(1, .2, 1);
  157.     glVertex3f(-.5, .5, 2);*/
  158.  
  159.  
  160.     /*glBegin(GL_QUADS);
  161.     glVertex3f(-0.5, -0.5, 0.0);
  162.     glVertex3f(-0.5, 0.5, 0.0);
  163.     glVertex3f(0.5, 0.5, 0.0);
  164.     glVertex3f(0.5, -0.5, 0.0);*/
  165.  
  166.     /*glBegin(GL_TRIANGLE_FAN);
  167.          glColor3f(1,0,0);
  168.          glVertex2f(0,0.5);
  169.          glVertex2f(-0.4,0);
  170.          glVertex2f(0.4,0);
  171.          glVertex2f(0,-0.5);
  172.     */
  173.  
  174.     glEnd();
  175. }
  176.  
  177. void display (void) {
  178.  
  179.     // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
  180.     glClearColor (0.0,0.0,0.0,1.0);
  181.     glClear (GL_COLOR_BUFFER_BIT);
  182.     glLoadIdentity();  
  183.          //viewing transformation
  184.     //glulookat() positions the camera towards the object
  185.     //camera position, camera target, upvector
  186.     gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  187.     shapes();
  188.     glFlush();
  189. }
  190.  
  191. void reshape (int w, int h) {
  192.  
  193.     // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
  194.     // the next coordinates are width and hight of the viewport
  195. //Set the viewport to be the entire window
  196.     glViewport (0, 0, (GLsizei)w, (GLsizei)h);
  197.  
  198.     // setting the camera
  199.     glMatrixMode (GL_PROJECTION);
  200.     glLoadIdentity ();  
  201.  
  202.     //perspective transform
  203.     //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
  204.     gluPerspective (30, 1, 1.0, 100.0);
  205.     glMatrixMode (GL_MODELVIEW); //switch back the the model editing mode.
  206.  
  207. }
  208.  
  209. int main (int argc, char **argv) {
  210.     glutInit (&argc, argv);
  211.     glutInitDisplayMode (GLUT_SINGLE); // single buffering.. (double buffering for animation)
  212.     // full screen is 1000,1000
  213.     // this 0,0 or 1000,1000 are world co ordinates
  214.     glutInitWindowSize (700, 700);
  215.     glutInitWindowPosition (100, 100);
  216.     glutCreateWindow ("A basic OpenGL Window");
  217.     // registering callback functions
  218.     glutDisplayFunc (display);  
  219.     glutReshapeFunc (reshape);
  220.    
  221.     glutMainLoop ();
  222.     return 0;
  223. }
  224.  
  225. Output:
  226.  
  227.  
  228. Conclusion:
  229. We learn about OpenGL line strip commands. 
  230. Task_03:
  231. Draw a Pyramid using OpenGL.
  232. Requirements:
  233. ● Visual Studio IDE with
  234. ● C++ Development Environment With OpenGL.
  235. Code Details:
  236. #include <GL/glut.h>
  237.  
  238. void shapes(void) {
  239.     float x1 = 0, y1 = .5;
  240.     float x2 = -0.5, y2 = -0.2;
  241.     float x3 = 0.5, y3 = -0.2;
  242.  
  243.     glBegin(GL_TRIANGLES);
  244.     glColor3f(1, 0, 0);
  245.     glVertex3f(x1, y1, 0);
  246.     glVertex3f(x2, y2, 0);
  247.     glVertex3f(x3, y3, 0);
  248.     glEnd();
  249.  
  250.     float p1 = (x1 + x2) / 2, q1 = (y1 + y2) / 2;
  251.  
  252.     float p2 = (x1 + x3) / 2, q2 = (y1 + y3) / 2;
  253.  
  254.     float p3 = (x2 + x3) / 2, q3 = (y2 + y3) / 2;
  255.  
  256.     glBegin(GL_TRIANGLES);
  257.     glColor3f(0, 1, 0);
  258.     glVertex3f(p1, q1, 0);
  259.     glVertex3f(p2, q2, 0);
  260.     glVertex3f(p3, q3, 0);
  261.     glEnd();
  262.  
  263.     float m1 = (p1 + x2) / 2, n1 = (q1 + y2) / 2;
  264.  
  265.     float m2 = (p1 + p3) / 2, n2 = (q1 + q3) / 2;
  266.  
  267.     float m3 = (x2 + p3) / 2, n3 = (y2 + q3) / 2;
  268.  
  269.     glBegin(GL_TRIANGLES);
  270.     glColor3f(0, 1, 1);
  271.     glVertex3f(m1, n1, 0);
  272.     glVertex3f(m2, n2, 0);
  273.     glVertex3f(m3, n3, 0);
  274.     glEnd();
  275.  
  276.     float c1 = (x1 + p1) / 2, d1 = (y1 + q1) / 2;
  277.     float c2 = (x1 + p2) / 2, d2 = (y1 + q2) / 2;
  278.     float c3 = (p1 + p2) / 2, d3 = (q1 + q2) / 2;
  279.  
  280.  
  281.     glBegin(GL_TRIANGLES);
  282.     glColor3f(0, 0, 1);
  283.     glVertex3f(c1, d1, 0);
  284.     glVertex3f(c2, d2, 0);
  285.     glVertex3f(c3, d3, 0);
  286.     glEnd();
  287.  
  288.     float s1 = (p2 + p3) / 2, t1 = (q2 + q3) / 2;
  289.  
  290.     float s2 = (p2 + x3) / 2, t2 = (q2 + y3) / 2;
  291.     float s3 = (x3 + p3) / 2, t3 = (y3 + q3) / 2;
  292.  
  293.     glBegin(GL_TRIANGLES);
  294.     glColor3f(1, 0, 1);
  295.     glVertex3f(s1, t1, 0);
  296.     glVertex3f(s2, t2, 0);
  297.     glVertex3f(s3, t3, 0);
  298.     glEnd();
  299.  
  300.     float a1 = (x1 + c1) / 2, b1 = (y1 + d1) / 2;
  301.  
  302.     float a2 = (x1 + c2) / 2, b2 = (y1 + d2) / 2;
  303.     float a3 = (c1 + c2) / 2, b3 = (d1 + d2) / 2;
  304.  
  305.     glBegin(GL_TRIANGLES);
  306.     glColor3f(1, 1, .5);
  307.     glVertex3f(a1, b1, 0);
  308.     glVertex3f(a2, b2, 0);
  309.     glVertex3f(a3, b3, 0);
  310.     glEnd();
  311.  
  312.     float e1 = (p1 + c1) / 2, f1 = (q1 + d1) / 2;
  313.     float e2 = (c1 + c3) / 2, f2 = (d1 + d3) / 2;
  314.     float e3 = (p1 + c3) / 2, f3 = (q1 + d3) / 2;
  315.     glBegin(GL_TRIANGLES);
  316.     glColor3f(1, 1, .5);
  317.     glVertex3f(e1, f1, 0);
  318.     glVertex3f(e2, f2, 0);
  319.     glVertex3f(e3, f3, 0);
  320.     glEnd();
  321.  
  322.     float g1 = (c2 + c3) / 2, h1 = (d2 + d3) / 2;
  323.     float g2 = (c2 + p2) / 2, h2 = (d2 + q2) / 2;
  324.     float g3 = (c3 + p2) / 2, h3 = (d3 + q2) / 2;
  325.     glBegin(GL_TRIANGLES);
  326.     glColor3f(1, 1, .5);
  327.     glVertex3f(g1, h1, 0);
  328.     glVertex3f(g2, h2, 0);
  329.     glVertex3f(g3, h3, 0);
  330.     glEnd();
  331.  
  332.     float i1 = (p1 + m1) / 2, j1 = (q1 + n1) / 2;
  333.     float i2 = (p1 + m2) / 2, j2 = (q1 + n2) / 2;
  334.     float i3 = (m1 + m2) / 2, j3 = (n1 + n2) / 2;
  335.     glBegin(GL_TRIANGLES);
  336.     glColor3f(1, 1, .5);
  337.     glVertex3f(i1, j1, 0);
  338.     glVertex3f(i2, j2, 0);
  339.     glVertex3f(i3, j3, 0);
  340.     glEnd();
  341.  
  342.     float k1 = (s1 + p2) / 2, l1 = (t1 + q2) / 2;
  343.  
  344.     float k2 = (p2 + s2) / 2, l2 = (q2 + t2) / 2;
  345.     float k3 = (s1 + s2) / 2, l3 = (t1 + t2) / 2;
  346.     glBegin(GL_TRIANGLES);
  347.     glColor3f(1, 1, .5);
  348.     glVertex3f(k1, l1, 0);
  349.     glVertex3f(k2, l2, 0);
  350.     glVertex3f(k3, l3, 0);
  351.     glEnd();
  352.  
  353.     float ax1 = (m1 + x2) / 2, ay1 = (n1 + y2) / 2;
  354.     float ax2 = (m1 + m3) / 2, ay2 = (n1 + n3) / 2;
  355.     float ax3 = (x2 + m3) / 2, ay3 = (y2 + n3) / 2;
  356.     glBegin(GL_TRIANGLES);
  357.     glColor3f(1, 1, .5);
  358.     glVertex3f(ax1, ay1, 0);
  359.     glVertex3f(ax2, ay2, 0);
  360.     glVertex3f(ax3, ay3, 0);
  361.     glEnd();
  362.  
  363.     float bx1 = (m2 + m3) / 2, by1 = (n2 + n3) / 2;
  364.     float bx2 = (m2 + p3) / 2, by2 = (n2 + q3) / 2;
  365.     float bx3 = (m3 + p3) / 2, by3 = (n3 + q3) / 2;
  366.     glBegin(GL_TRIANGLES);
  367.     glColor3f(1, 1, .5);
  368.     glVertex3f(bx1, by1, 0);
  369.     glVertex3f(bx2, by2, 0);
  370.     glVertex3f(bx3, by3, 0);
  371.     glEnd();
  372.  
  373.     float cx1 = (p3 + s1) / 2, cy1 = (q3 + t1) / 2;
  374.     float cx2 = (s1 + s3) / 2, cy2 = (t1 + t3) / 2;
  375.     float cx3 = (p3 + s3) / 2, cy3 = (q3 + t3) / 2;
  376.     glBegin(GL_TRIANGLES);
  377.     glColor3f(1, 1, .5);
  378.     glVertex3f(cx1, cy1, 0);
  379.     glVertex3f(cx2, cy2, 0);
  380.     glVertex3f(cx3, cy3, 0);
  381.     glEnd();
  382.  
  383.     float dx1 = (s2 + s3) / 2, dy1 = (t2 + t3) / 2;
  384.     float dx2 = (s2 + x3) / 2, dy2 = (t2 + y3) / 2;
  385.     float dx3 = (s3 + x3) / 2, dy3 = (t3 + y3) / 2;
  386.     glBegin(GL_TRIANGLES);
  387.     glColor3f(1, 1, .5);
  388.     glVertex3f(dx1, dy1, 0);
  389.     glVertex3f(dx2, dy2, 0);
  390.     glVertex3f(dx3, dy3, 0);
  391.     glEnd();
  392. }
  393.  
  394. void display(void) {
  395.     // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
  396.     glClearColor(0.0, 0.0, 0.0, 1.0);
  397.     glClear(GL_COLOR_BUFFER_BIT);
  398.     glLoadIdentity();
  399.  
  400.     //viewing transformation
  401.     //glulookat() positions the camera towards the object
  402.     //camera position, camera target, up vector
  403.     gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  404.     shapes();
  405.     glFlush();
  406. }
  407.  
  408. void reshape(int w, int h) {
  409.     // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
  410.     // the next coordinates are width and height of the viewport
  411.     //Set the viewport to be the entire window
  412.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  413.  
  414.     // setting the camera
  415.     glMatrixMode(GL_PROJECTION);
  416.     glLoadIdentity();
  417.  
  418.     //perspective transform
  419.     //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
  420.     gluPerspective(30, 1, 1.0, 100.0);
  421.     glMatrixMode(GL_MODELVIEW); //switch back the the model editing mode.
  422.  
  423. }
  424.  
  425. int main(int argc, char **argv) {
  426.     glutInit(&argc, argv);
  427.     // single buffering.. (double buffering for animation)
  428.     glutInitDisplayMode(GLUT_SINGLE);
  429.     // full screen is 1000,1000
  430.     // this 0,0 or 1000,1000 are world coordinates
  431.     glutInitWindowSize(700, 700);
  432.     glutInitWindowPosition(100, 100);
  433.     glutCreateWindow("A basic OpenGL Window");
  434.     // registering callback functions
  435.     glutDisplayFunc(display);
  436.     glutReshapeFunc(reshape);
  437.  
  438.     glutMainLoop();
  439.     return 0;
  440. }
  441.  
  442. Output:
  443.  
  444.  
  445.  
  446. Conclusion:
  447. We learn about OpenGL commands. 
  448. Task_04:   
  449. Draw a Chess Board using OpenGL.
  450. Requirements:
  451. ● Visual Studio IDE with
  452. ● C++ Development Environment with OpenGL.
  453. Code Details:
  454. #include <GL/glut.h>
  455.  
  456. void shapes(void) {
  457.     float y1 = 0.0;
  458.     float y2 = 0.0;
  459.     float y3 = 0.1;
  460.     float y4 = 0.1;
  461.     for (int j = 0; j < 8; j++) {
  462.         float x1 = 0.0;
  463.         float x2 = 0.1;
  464.         float x3 = 0.1;
  465.         float x4 = 0.0;
  466.         for (int i = 0; i < 8; i++) {
  467.             glBegin(GL_QUADS);
  468.             if ((j % 2) != 0) {
  469.                 if ((i % 2) != 0) {
  470.                     glColor3f(1.0, 1.0, 1.0);
  471.                 }
  472.                 else {
  473.                     glColor3f(0.2, 0.2, 0.2);
  474.                 }
  475.             }
  476.             if ((j % 2) == 0) {
  477.                 if ((i % 2) != 0) {
  478.                     glColor3f(0.2, 0.2, 0.2);
  479.                 }
  480.                 else {
  481.                     glColor3f(1.0, 1.0, 1.0);
  482.                 }
  483.             }
  484.             glVertex3f(x1, y1, 0.0);
  485.             glVertex3f(x2, y2, 0.0);
  486.             glVertex3f(x3, y3, 0.0);
  487.             glVertex3f(x4, y4, 0.0);
  488.             glEnd();
  489.             x1 += 0.1;
  490.             x2 += 0.1;
  491.             x3 += 0.1;
  492.             x4 += 0.1;
  493.         }
  494.         y1 += 0.1;
  495.         y2 += 0.1;
  496.         y3 += 0.1;
  497.         y4 += 0.1;
  498.     }
  499. }
  500.  
  501. void display(void) {
  502.     // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
  503.     glClearColor(0.0, 0.0, 0.0, 1.0);
  504.     glClear(GL_COLOR_BUFFER_BIT);
  505.     glLoadIdentity();
  506.  
  507.     //viewing transformation
  508.     //glulookat() positions the camera towards the object
  509.     //camera position, camera target, up vector
  510.     gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  511.     shapes();
  512.     glFlush();
  513. }
  514.  
  515. void reshape(int w, int h) {
  516.     // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
  517.     // the next coordinates are width and height of the viewport
  518.     //Set the viewport to be the entire window
  519.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  520.  
  521.     // setting the camera
  522.     glMatrixMode(GL_PROJECTION);
  523.     glLoadIdentity();
  524.  
  525.     //perspective transform
  526.     //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
  527.     gluPerspective(30, 1, 1.0, 100.0);
  528.     glMatrixMode(GL_MODELVIEW); //switch back the the model editing mode.
  529.  
  530. }
  531.  
  532. int main(int argc, char **argv) {
  533.     glutInit(&argc, argv);
  534.     // single buffering.. (double buffering for animation)
  535.     glutInitDisplayMode(GLUT_SINGLE);
  536.     // full screen is 1000,1000
  537.     // this 0,0 or 1000,1000 are world coordinates
  538.     glutInitWindowSize(700, 700);
  539.     glutInitWindowPosition(100, 100);
  540.     glutCreateWindow("A basic OpenGL Window");
  541.     // registering callback functions
  542.     glutDisplayFunc(display);
  543.     glutReshapeFunc(reshape);
  544.  
  545.     glutMainLoop();
  546.     return 0;
  547. }
  548. Output:
  549.  
  550.  
  551.  
  552. Conclusion:
  553. We learn about OpenGL line strip commands.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement