Wednesday 26 February 2014

Tagged Under:

How to make an OpenGL/Glut Triangle

Share



Triangle

  • Introductory program; just a static picture of a colored triangle.
  • Shows how to use GLUT.
  • Has minimal structure: only main() and a display callback.
  • Uses only the default viewing parameters (in fact, it never mentions viewing at all). This is an orthographic view volume with bounds of -1..1 in all three dimensions.
  • Draws only using glColor and glVertex withinglBegin and glEnd in the display callback.
  • Uses only the GL_POLYGON drawing mode.
  • Illustrates glClear and glFlush.
OpenGL 3 makes it easy to write complicated stuff, but at the expense that drawing a simple triangle is actually quite difficult.
Source Code
// A simple introductory program; its main window contains a static picture
// of a triangle, whose three vertices are red, green and blue.  The program
// illustrates viewing with default viewing parameters only.

Program: 

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

void display()
{
              glClear(GL_COLOR_BUFFER_BIT);
              glColor3f(1,0,0);
              
              GLfloat i;
              /*
              for(i=-0.8;i<=0.8;)
              {
                              glBegin(GL_LINES);
                              glVertex3f(0+i,-0.8,0);
                              glVertex3f(0+i,0.8,0); 
                              glEnd();
                              i+=0.2;
              }
              for(i=-0.8;i<=0.8;)
              {
                              glBegin(GL_LINES);
                              glVertex3f(-0.8,i,0);
                              glVertex3f(0.8,i,0); 
                              glEnd();
                              i+=0.2;
              }
              */
              
              for(i=-2;i<=2;i+=0.25)
              {
                        glBegin(GL_LINES);
                        glVertex3f(i,0,2);
                        glVertex3f(i,0,-2);
                        glVertex3f(2,0,i);
                        glVertex3f(-2,0,i);
                        glEnd();
              }
                                   
              
              glColor3f(1,0,1);
              glBegin(GL_TRIANGLE_STRIP);
              glVertex3f(0,2,0);
              glVertex3f(-1,0,1);
              glVertex3f(1,0,1);
              glColor3f(0,1,1);
              glVertex3f(0,0,-1);
              glColor3f(0,0,1);
              glVertex3f(0,2,0);
              glColor3f(0,1,0);
              glVertex3f(-1,0,1);
              glEnd();
              
              
              glFlush();
}

void init()
{
           glClearColor(0.1,0.3,0.8,1.0);
           glColor3f(1,1,1);
           glMatrixMode(GL_PROJECTION);
           glLoadIdentity();
           glFrustum(-2,2,-1.5,1.5,1,40);
           glMatrixMode(GL_MODELVIEW);
           glLoadIdentity();
           glTranslatef(0,0,-3);
           glRotatef(50,1,0,0);
           glRotatef(70,0,1,0);
}

int main()
{
          glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
          glutInitWindowSize(500,500);
          glutInitWindowPosition(100,100);
          glutCreateWindow("Grid");
          
          glutDisplayFunc(display);
          init();
          glutMainLoop();
}  

Output

0 comments:

Post a Comment

Contact Us

Want to ask anything? Be our guest, give us a message.

Name Email * Message *