Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File triangles.cpp
- // Hugo Gallée
- // Sat Feb 25 23:06:03 CET 2012
- #include <GL/glut.h>
- #include <math.h>
- typedef struct point {
- float x;
- float y;
- } Point;
- void drawTriangle (Point a, Point b, Point c) {
- float z = 0.0;
- glBegin(GL_TRIANGLES);
- glColor3f (1.0, 0.0, 0.0);
- glVertex3f(a.x, a.y, z);
- glVertex3f(b.x, b.y, z);
- glVertex3f(c.x, c.y, z);
- glEnd();
- }
- void init (void) {
- // select clearing color
- glClearColor (1.0, 1.0, 1.0, 0.0);
- // initialize viewing values
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
- }
- void display(void) {
- // clear all pixels
- glClear (GL_COLOR_BUFFER_BIT);
- Point a, b, c;
- float margin = 0.1;
- float size = 0.4;
- a.x = margin;
- a.y = margin;
- b.x = margin + size;
- b.y = margin;
- c.x = margin;
- c.y = margin + size;
- for (int i = 0; i < 6; i++) {
- drawTriangle(a, b, c);
- margin /= 2;
- a.x += margin + size;
- size /= 2;
- b.x = a.x + size;;
- c.x = a.x;
- c.y = a.y + size;
- }
- // Swap the buffers to show the oneon which we writed
- glutSwapBuffers();
- }
- /*
- * Declare initial window size, position, and display mode
- * (double buffer and RGB). Open window with "hello"
- * in its title bar. Call initialization routines.
- * Register callback function to display graphics.
- * Enter main loop and process events.
- */
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize (500, 500);
- glutInitWindowPosition (100, 100);
- glutCreateWindow ("triangles");
- init ();
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement