Also Like

📁 last Posts

Drawing a Simple House in OpenGL (Step by Step)

Drawing a Simple House in OpenGL (Step by Step)




#include <GL/glut.h>
#include <math.h>

void SetupRC() {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glLineWidth(4.0f);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-500.0, 500.0, -500.0, 500.0);
}

void RenderScene() {
    glClear(GL_COLOR_BUFFER_BIT);


    // 1 - Draw the main house body (square)

    glBegin(GL_POLYGON);
    glColor3f(1, 1, 0);
    glVertex2f(-400, -500);
    glVertex2f(400, -500);
    glVertex2f(400, 100);
    glVertex2f(-400, 100);
    glEnd();

    // 2 - Draw the roof (triangle)
    glBegin(GL_TRIANGLES);
    glColor3f(1, 0, 0);
    glVertex2f(400, 100);
    glVertex2f(-400, 100);
    glVertex2f(0, 500);
    glEnd();

    // 3 - Left window
    glBegin(GL_POLYGON);
    glColor3f(0, 0, 1);
    glVertex2f(300, 90);
    glVertex2f(100, 90);
    glVertex2f(100, -100);
    glVertex2f(300, -100);
    glEnd();

    // 4 - Right window
    glBegin(GL_POLYGON);
    glVertex2f(-100, 90);
    glVertex2f(-300, 90);
    glVertex2f(-300, -100);
    glVertex2f(-100, -100);
    glEnd();
    // 5 - Door

    glBegin(GL_POLYGON);
    glVertex2f(100, -200);
    glVertex2f(-100, -200);
    glVertex2f(-100, -500);
    glVertex2f(100, -500);
    glEnd();
    double pi = 3.1415962;
    int r = 100;
    int steps = 100;

    glBegin(GL_POLYGON);
    for (double i = 0; i <= 2*pi; i+= pi/steps)
    {
        double x, y;
        x = r * cos(i);
        y = -200+ r * sin(i);
        glVertex2f(x, y);


    }
    glEnd();







    glFlush();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("OpenGL Blue Line");

    SetupRC();
    glutDisplayFunc(RenderScene);
    glutMainLoop();
    return 0;
}
Comments