Advertisement
Tusohian

Triangle Pyramid (VS10)

Mar 1st, 2019
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. #include <GL/glut.h>
  2.  
  3. void shapes (void) {
  4.  
  5.     /*glColor3f(1.0, 1.0, 1.0); //this will set a color of the square.
  6.     glBegin(GL_LINES); // write shape u want to create
  7.     glVertex3f(-0.5, -0.5, 0.0);
  8.     glVertex3f(-0.5, 0.5, 0.0);
  9.     glVertex3f(0.5, 0.5, 0.0);
  10.     glVertex3f(0.5, -0.5, 0.0);*/
  11.  
  12.     /*glBegin(GL_POINTS);
  13.     glVertex3f(.5,.5,0);
  14.     glVertex3f(-.5,-.5,0);
  15.     glVertex3f(0,.5,0);
  16.     glVertex3f(.5,0,0);
  17.     glVertex3f(0,0,0);*/
  18.  
  19.     /*glBegin(GL_LINE_STRIP);
  20.       glVertex3f(0,0,0);
  21.       glVertex3f(-.5,0,0);  
  22.       glVertex3f(.5,.5,0);
  23.       glVertex3f(-.5,.5,0);
  24.       glVertex3f(.5,0,0);*/
  25.  
  26.     /*glBegin(GL_POLYGON);
  27.     glColor3f(1, 0, 0);
  28.     glVertex3f(-0.6, -0.75, 0);
  29.     glColor3f(0, 1, 0);
  30.     glVertex3f(0.6, -0.75, 0);
  31.     glColor3f(0, 0, 1);
  32.     glVertex3f(0, 0.75, 0);*/
  33.  
  34.  
  35.     // Triangle 01
  36.     float x1=-0.8;   float x2=0;     float x3=0.8;
  37.     float y1=-0.5;   float y2=1;     float y3=-0.5;
  38.  
  39.     glBegin(GL_TRIANGLES);
  40.     glColor3f(1, 0, 0);
  41.     glVertex3f(x1, y1, 0);
  42.     glVertex3f(x2, y2, 0);
  43.     glVertex3f(x3, y3, 0);
  44.  
  45.     // Triangle 02
  46.     float x4=(x1+x2)/2;    float x5=(x1+x3)/2;     float x6=(x3+x2)/2;
  47.     float y4=(y1+y2)/2;    float y5=-y2/2;         float y6=(y3+y2)/2;
  48.  
  49.     glBegin(GL_TRIANGLES);
  50.     glColor3f(0, 1, 0);
  51.     glVertex3f(x4, y4, 0);
  52.     glVertex3f(x5, y5, 0);
  53.     glVertex3f(x6, y6, 0);
  54.  
  55.  
  56.     // Triangle 03
  57.     float x7=(x4+x2)/2;            float x8=(x1+x3)/2;     float x9=(x2+(x2+x3)/2)/2;
  58.     float y7=(((y1+y2)/2)+y2)/2;   float y8=(y2/2)/2;      float y9=(y2+(y2+y3)/2)/2;
  59.  
  60.     glBegin(GL_TRIANGLES);
  61.     glColor3f(0, 0, 1);
  62.     glVertex3f(x7, y7, 0);
  63.     glVertex3f(x8, y8, 0);
  64.     glVertex3f(x9, y9, 0);
  65.  
  66.     // Triangle 04
  67.     float x10= (x4+x1)/2 ;      float x11= (x1+x5)/2;     float x12= (x4+x8)/2;
  68.     float y10= (y4+y1)/2 ;      float y11= (y1+y5)/2;     float y12= (y1+y8)/2;
  69.  
  70.     glBegin(GL_TRIANGLES);
  71.     glColor3f(1, 1, 0);
  72.     glVertex3f(x10, y10, 0);
  73.     glVertex3f(x11, y11, 0);
  74.     glVertex3f(x12, y12, 0);
  75.  
  76.     // Triangle 05
  77.     float x13= (x6+x3)/2 ;      float x14= (x3+x5)/2;     float x15= (x6+x8)/2;
  78.     float y13= (y6+y3)/2 ;      float y14= (y3+y5)/2;     float y15= (y3+y8)/2;
  79.  
  80.     glBegin(GL_TRIANGLES);
  81.     glColor3f(0, 1, 1);
  82.     glVertex3f(x13, y13, 0);
  83.     glVertex3f(x14, y14, 0);
  84.     glVertex3f(x15, y15, 0);
  85.  
  86.  
  87.     // Triangle 06
  88.     float x16= (x10+x1)/2 ;      float x17= (x1+x11)/2 ;     float x18= (x10+x11)/2 ;
  89.     float y16= (y10+y1)/2 ;      float y17= (y1+y11)/2 ;     float y18= (y10+y11)/2 ;
  90.  
  91.     glBegin(GL_TRIANGLES);
  92.     glColor3f(1, 1, 1);
  93.     glVertex3f(x16, y16, 0);
  94.     glVertex3f(x17, y17, 0);
  95.     glVertex3f(x18, y18, 0);
  96.  
  97.  
  98.     // Triangle 07
  99.     float x19=  ;      float x20=  ;     float x21=  ;
  100.     float y19=  ;      float y20=  ;     float y21=  ;
  101.  
  102.     glBegin(GL_TRIANGLES);
  103.     glColor3f(1, 1, 1);
  104.     glVertex3f(x19, y19, 0);
  105.     glVertex3f(x20, y20, 0);
  106.     glVertex3f(x21, y21, 0);
  107.  
  108.  
  109.  
  110.     /*glBegin(GL_QUADS);
  111.     glVertex3f(-0.5, -0.5, 0.0);
  112.     glVertex3f(-0.5, 0.5, 0.0);
  113.     glVertex3f(0.5, 0.5, 0.0);
  114.     glVertex3f(0.5, -0.5, 0.0);*/
  115.  
  116.     /*glBegin(GL_TRIANGLE_FAN);
  117.          glColor3f(1,0,0);
  118.          glVertex2f(0,0.5);
  119.          glVertex2f(-0.4,0);
  120.          glVertex2f(0.4,0);
  121.          glVertex2f(0,-0.5);
  122.     */
  123.  
  124.     glEnd();
  125. }
  126.  
  127. void display (void) {
  128.  
  129.     // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
  130.     glClearColor (0.0,0.0,0.0,1.0);
  131.     glClear (GL_COLOR_BUFFER_BIT);
  132.     glLoadIdentity();  
  133.          //viewing transformation
  134.     //glulookat() positions the camera towards the object
  135.     //camera position, camera target, upvector
  136.     gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  137.     shapes();
  138.     glFlush();
  139. }
  140.  
  141. void reshape (int w, int h) {
  142.  
  143.     // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
  144.     // the next coordinates are width and hight of the viewport
  145. //Set the viewport to be the entire window
  146.     glViewport (0, 0, (GLsizei)w, (GLsizei)h);
  147.  
  148.     // setting the camera
  149.     glMatrixMode (GL_PROJECTION);
  150.     glLoadIdentity ();  
  151.  
  152.     //perspective transform
  153.     //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
  154.     gluPerspective (30, 1, 1.0, 100.0);
  155.     glMatrixMode (GL_MODELVIEW); //switch back the the model editing mode.
  156.  
  157. }
  158.  
  159. int main (int argc, char **argv) {
  160.     glutInit (&argc, argv);
  161.     glutInitDisplayMode (GLUT_SINGLE); // single buffering.. (double buffering for animation)
  162.     // full screen is 1000,1000
  163.     // this 0,0 or 1000,1000 are world co ordinates
  164.     glutInitWindowSize (700, 700);
  165.     glutInitWindowPosition (100, 100);
  166.     glutCreateWindow ("A basic OpenGL Window");
  167.     // registering callback functions
  168.     glutDisplayFunc (display);  
  169.     glutReshapeFunc (reshape);
  170.    
  171.     glutMainLoop ();
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement