Wednesday 13 March 2013

Using OpenGL

Write a program in C/C++ using OpenGL to draw a circle of red color
inside of a rectangle of blue color on a background of green colors.


#include <windows.h>
#include <gl/glut.h>
#include <math.h>
const float PI=3.14;

void drawCircle(){
    glBegin(GL_LINE_LOOP);
    glColor3f(1.0,0.0,0.0);
    for(int i =0; i <= 300; i++){
        double angle = 2 * PI * i / 300;
        double x = 5*cos(angle);
        double y = 5*sin(angle);
        glVertex2d(x,y);
    }
    glEnd();
}
void drawRect(){
    glColor3f(0.0,0.0,1.0);
    glRectf(-5.0,5.0,5.0,-5.0);
}
void init(void){   
    glClearColor(0.0,1.0,0.0,0.0);   
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10.0,10.0,-10.0,10.0,-10.0,10.0);
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    drawRect();
    drawCircle();
    glutSwapBuffers();
}

int main(int argc, char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(320,320);
    glutInitWindowPosition(50,50);
    glutCreateWindow("2D Shapes");
    init();
    glutDisplayFunc(display);   
    glutMainLoop();
    return 0;
}

No comments:

Post a Comment