Wednesday, December 18, 2013

To draw a red triangle using a display list

/*
 * 4. Give the opengl code segment that generates a display list defining
 *  a red triangle with vertices at (50,50),(150,50) and (100,0)?
 * disp.c
 *
 *  Created on: Dec 18, 2013
 *      Author: kamath
 */
#include<GL/glut.h>
GLuint triangle; //The OpenGL id of the display list
void draw_triangle()
{
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_TRIANGLES);
    glVertex2f(50,50);
    glVertex2f(150,50);
    glVertex2f(100,0);
    glEnd();
}
void init()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,500,0.0,500);
    glMatrixMode(GL_MODELVIEW);
    glClearColor(1.0,1.0,1.0,1.0);
    triangle = glGenLists(1);
    glNewList(triangle, GL_COMPILE); //Begin the display list
    draw_triangle(); //Add commands for drawing a triangle
                        //to the display list
    glEndList(); //End the display list
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glCallList(triangle);
    glFlush();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutCreateWindow("Triangle Display List");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
Snapshot:


No comments:

Post a Comment