#define FREEGLUT_STATIC #include <stdlib.h> #include <stdio.h> #include <math.h> #include <GL/freeglut.h>

int width = 800; int height = 600; float cloudX = 0.0; float balloonY = 0.0; bool flag = false;

void define_to_OpenGL(); void when_in_mainloop(); void keyboard_input(unsigned char key, int x, int y);

void when_in_mainloop() // idle callback function { glutPostRedisplay(); // force OpenGL to redraw the current window }

void keyboard_input(unsigned char key, int x, int y) // keyboard interaction { if (key == 'q' || key == 'Q') { exit(0); } else if (key == 'f' || key == 'F') { // press 'f' to toggle polygons drawn in between 'line' and 'fill' modes flag = !flag; } }

void define_to_OpenGL() { glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT);

if (flag)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
else
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

// Draw reference axes
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(-1.0, 0.0);
glVertex2f(1.0, 0.0);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.0, -1.0);
glVertex2f(0.0, 1.0);
glEnd();

// Draw the shapes

// The wall
glColor3f(0.7, 0.7, 0.7); // Light gray color
glBegin(GL_POLYGON);
glVertex2f(-0.5, 0);
glVertex2f(-0.5, 0.5);
glVertex2f(-0.25, 0.5);
glVertex2f(-0.25, 0);
glEnd();

// The roof
glColor3f(1.0, 0.0, 0.0); // Red color
glBegin(GL_POLYGON);
glVertex2f(-0.9, 0);
glVertex2f(0, 0.5);
glVertex2f(0.9, 0);
glEnd();

// The chimney
glColor3f(0.0, 0.0, 1.0); // Blue color
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.9);
glVertex2f(-0.5, 0);
glVertex2f(0.5, 0);
glVertex2f(0.5, -0.9);
glEnd();

// The window
float width = 0.1;
float height = 0.1;
glColor3f(1.0, 1.0, 1.0); // White color
glBegin(GL_POLYGON);
glVertex2f(-width, -height);
glVertex2f(-width, height);
glVertex2f(width, height);
glVertex2f(width, -height);
glEnd();

glFlush();

}

int main(int argc, char **argv) { glutInit(&argc, argv); glutCreateWindow("My House"); glutDisplayFunc(define_to_OpenGL); glutIdleFunc(when_in_mainloop); glutKeyboardFunc(keyboard_input); glutMainLoop();

return 0;

}

OpenGL House Drawing with FreeGLUT: Interactive Line and Fill Modes

原文地址: https://www.cveoy.top/t/topic/FWK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录