Advertisement
HugoBallee

triangles.cpp

Feb 28th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. // File triangles.cpp
  2. // Hugo Gallée
  3. // Sat Feb 25 23:06:03 CET 2012
  4.  
  5. #include <GL/glut.h>
  6. #include <math.h>
  7.  
  8. typedef struct point {
  9.   float x;
  10.   float y;
  11. } Point;
  12.  
  13.  
  14. void drawTriangle (Point a, Point b, Point c) {
  15.   float z = 0.0;
  16.   glBegin(GL_TRIANGLES);
  17.     glColor3f (1.0, 0.0, 0.0);
  18.     glVertex3f(a.x, a.y, z);
  19.     glVertex3f(b.x, b.y, z);
  20.     glVertex3f(c.x, c.y, z);
  21.   glEnd();
  22. }
  23.  
  24. void init (void) {
  25.     // select clearing color
  26.     glClearColor (1.0, 1.0, 1.0, 0.0);
  27.  
  28.     // initialize viewing values
  29.     glMatrixMode(GL_PROJECTION);
  30.     glLoadIdentity();
  31.     glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
  32. }
  33.  
  34. void display(void) {
  35.     // clear all pixels
  36.     glClear (GL_COLOR_BUFFER_BIT);
  37.  
  38.     Point a, b, c;
  39.     float margin = 0.1;
  40.     float size = 0.4;
  41.     a.x = margin;
  42.     a.y = margin;
  43.     b.x = margin + size;
  44.     b.y = margin;
  45.     c.x = margin;
  46.     c.y = margin + size;
  47.  
  48.     for (int i = 0; i < 6; i++) {
  49.       drawTriangle(a, b, c);
  50.       margin /= 2;
  51.       a.x += margin + size;
  52.       size /= 2;
  53.       b.x = a.x + size;;
  54.       c.x = a.x;
  55.       c.y = a.y + size;
  56.     }
  57.  
  58.     // Swap the buffers to show the oneon which we writed
  59.     glutSwapBuffers();
  60. }
  61.  
  62. /*
  63.  * Declare initial window size, position, and display mode
  64.  * (double buffer and RGB).  Open window with "hello"
  65.  * in its title bar.  Call initialization routines.
  66.  * Register callback function to display graphics.
  67.  * Enter main loop and process events.
  68.  */
  69. int main(int argc, char** argv) {
  70.     glutInit(&argc, argv);
  71.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  72.     glutInitWindowSize (500, 500);
  73.     glutInitWindowPosition (100, 100);
  74.     glutCreateWindow ("triangles");
  75.     init ();
  76.     glutDisplayFunc(display);
  77.     glutMainLoop();
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement