4 Program to Draw Home in Computer Graphics using OpenGL

Program:

 
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#define pi 3.142857


void DrawCircle(float cx, float cy, float r, int num_segments)
{
    glBegin(GL_LINE_LOOP);
    for(int ii = 0; ii < num_segments; ii++)
    {
        float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle

        float x = r * cosf(theta);//calculate the x component
        float y = r * sinf(theta);//calculate the y component

        glVertex2f(x + cx, y + cy);//output vertex

    }
    glEnd();
}

void draw()
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    glClearColor(0,0,0,1);
    glColor3f(1,0,0);

    glOrtho(0,500,500,0,0,1);

    glPointSize(4);

    
    glBegin(GL_LINES);
        glVertex2f(70,100);
        glVertex2f(300,100);

        glVertex2f(300,100);
        glVertex2f(300,400);


        glVertex2f(70,100);
        glVertex2f(30,250);

        glVertex2f(70,100);
        glVertex2f(120,250);
        

        glVertex2f(120,250);
        glVertex2f(120,400);

    glEnd();

    glBegin(GL_LINE_LOOP);
        glVertex2f(30,250);
        glVertex2f(300,250);
        glVertex2f(300,400);
        glVertex2f(30,400);
    glEnd();    

    DrawCircle(70,180,15,20);
    
    glFlush();
}

int main(int argc, char **argv)
{
    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(300,300);
    glutInitWindowSize(500,500);

    glutCreateWindow("Basic Structure");
    glutDisplayFunc(draw);

    glutMainLoop();

    return 0;
}

Output:



Previous
Next Post »