下面是我的代码。我可以通过绘制身体,窗户,门和屋顶来画房子。现在,我想扩展我的代码;这样,如果按'f',身体就会被移除。
如何修改代码才能执行此操作?
// ============================================= ======
#include <stdlib.h>
#include "GL\glut.h" // Header file for GLUT and OpenGL
void house();
void door();
void window();
void roof();
//--------------------------------------------------------------
// DRAWING CALLBACK FUNCTION
//--------------------------------------------------------------
void draw(){
// background colour: yellow (Now GREEN)
glClearColor( 100, 100, 0, 0 );
glClear ( GL_COLOR_BUFFER_BIT );
// Sets up the DOMAIN (xmin,xmax,ymin,ymax) in R2.
// Let (xmin,xmax,ymin,ymax)=(0.0,20.0,0.0,20.0)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,22.0,0.0,22.0);
// Let us now define the line segment in red
// Endpoints: (5.0,1.5) and (9.3,7.2)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//====================================
//=============House==================
glTranslatef(5,5,0);
house ();
//=============Door===================
glTranslatef(3.5,0,0);
door();
//==============Window=================
glTranslatef(3.5,6,0);
window();
glTranslatef(-6,0,0);
window();
//=============Roof====================
glTranslatef(-3,4,0);
roof();
//=====================================
// display line segment
glutSwapBuffers();
}
//---------------------------------------------------------------
// KEYBOARD CALLBACK FUNCTION
//---------------------------------------------------------------
void house (){
glColor3f( 1, 0, 0 );
glBegin(GL_QUADS);
glVertex2f(0.0,0.0);
glVertex2f(10.0,0.0);
glVertex2f(10.0,10.0);
glVertex2f(0.0,10.0);
glEnd();
}
//=====================================
void door (){
glColor3f( 0, 1, 0 );
glBegin(GL_QUADS);
glVertex2f(0.0,0.0);
glVertex2f(3.0,0.0);
glVertex2f(3.0,5.0);
glVertex2f(0.0,5.0);
glEnd();
}
//=====================================
void window (){
glColor3f( 0, 0, 1 );
glBegin(GL_QUADS);
glVertex2f(0.0,0.0);
glVertex2f(2.0,0.0);
glVertex2f(2.0,2.0);
glVertex2f(0.0,2.0);
glEnd();
}
//=====================================
void roof (){
glColor3f( 0, 0, 1 );
glBegin(GL_TRIANGLES);
glVertex2f(0.0,0.0);
glVertex2f(14.0,0.0);
glVertex2f(7.0,5.0);
glEnd();
}
//=====================================
void keyboard(unsigned char key,int x,int y)
{
// press ESC key to quit
if(key==27) exit(0);
}
//---------------------------------------------------------------
// MAIN FUNCTION
//---------------------------------------------------------------
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
// Double Buffered RGB display
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
// Set window size
glutInitWindowSize( 600,600 );
glutCreateWindow("Line Segment");
// Declare callback functions
glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
// Start the main loop of events
glutMainLoop();
return 0;
}
// ============================================= ============
答案 0 :(得分:0)
在keyboard()
中添加一个支票,用于切换bool
可以看到的house()
:
#include <GL/glut.h>
bool showBody = true;
void keyboard(unsigned char key,int x,int y)
{
// press ESC key to quit
if(key==27) exit(0);
if(key=='f') showBody = !showBody;
glutPostRedisplay();
}
void house ()
{
if( !showBody ) return;
glColor3f( 1, 0, 0 );
glBegin(GL_QUADS);
glVertex2f(0.0,0.0);
glVertex2f(10.0,0.0);
glVertex2f(10.0,10.0);
glVertex2f(0.0,10.0);
glEnd();
}
void door ()
{
glColor3f( 0, 1, 0 );
glBegin(GL_QUADS);
glVertex2f(0.0,0.0);
glVertex2f(3.0,0.0);
glVertex2f(3.0,5.0);
glVertex2f(0.0,5.0);
glEnd();
}
void window ()
{
glColor3f( 0, 0, 1 );
glBegin(GL_QUADS);
glVertex2f(0.0,0.0);
glVertex2f(2.0,0.0);
glVertex2f(2.0,2.0);
glVertex2f(0.0,2.0);
glEnd();
}
void roof ()
{
glColor3f( 0, 0, 1 );
glBegin(GL_TRIANGLES);
glVertex2f(0.0,0.0);
glVertex2f(14.0,0.0);
glVertex2f(7.0,5.0);
glEnd();
}
void draw()
{
// background colour: yellow (Now GREEN)
glClearColor( 100, 100, 0, 0 );
glClear ( GL_COLOR_BUFFER_BIT );
// Sets up the DOMAIN (xmin,xmax,ymin,ymax) in R2.
// Let (xmin,xmax,ymin,ymax)=(0.0,20.0,0.0,20.0)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,22.0,0.0,22.0);
// Let us now define the line segment in red
// Endpoints: (5.0,1.5) and (9.3,7.2)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//=============House==================
glTranslatef(5,5,0);
house ();
//=============Door===================
glTranslatef(3.5,0,0);
door();
//==============Window=================
glTranslatef(3.5,6,0);
window();
glTranslatef(-6,0,0);
window();
//=============Roof====================
glTranslatef(-3,4,0);
roof();
glutSwapBuffers();
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
// Double Buffered RGB display
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
// Set window size
glutInitWindowSize( 600,600 );
glutCreateWindow("Line Segment");
// Declare callback functions
glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
// Start the main loop of events
glutMainLoop();
return 0;
}