Shishu

house,star,pyramid and chase board in visual baosc

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