我绘制一些形状时遇到问题。当我执行程序时,我应该只看到坐标,然后按一个键绘制形状,但是默认情况下它已经绘制了立方体,然后是金字塔。当我按下“ p”键时如何绘制金字塔,而当我按下“ c”时如何绘制立方体?我尝试了一些东西,但似乎不起作用:(
#include <stdlib.h>
#include <math.h>
#include "dependente\freeglut\freeglut.h"
#include "dependente\glfw\glfw3.h"
#include <stdio.h>
#define RADDEG 57.29577951f
float XUP[3] = { 1,0,0 }, XUN[3] = { -1, 0, 0 },
YUP[3] = { 0,1,0 }, YUN[3] = { 0,-1, 0 },
ZUP[3] = { 0,0,1 }, ZUN[3] = { 0, 0,-1 },
ORG[3] = { 0,0,0 };
GLfloat viewangle = 0, tippangle = 0, traj[120][3];
GLfloat d[3] = { 0.1, 0.1, 0.1 };
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
// Use arrow keys to rotate entire scene !!!
void Special_Keys(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT: viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP: tippangle -= 5; break;
case GLUT_KEY_DOWN: tippangle += 5; break;
default: printf("Special key %c == %d", key, key);
}
glutPostRedisplay();
}
void Pyramid(void) //draw the pyramid shape
{
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); //V0(red)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); //V2(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); //V3(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); //V4(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glEnd();
}
void Draw_Box(void)
{
glBegin(GL_QUADS);
glColor3f(0.0, 0.7, 0.1); // Front - green
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.9, 1.0, 0.0); // Back - yellow
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.2, 0.2, 1.0); // Top - blue
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(0.7, 0.0, 0.1); // Bottom - red
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
}
void Keyboard(unsigned char key, int x, int y) //press a key to perform actions
{
switch (key) {
case 'd': d[0] += 0.1; break;
case 'a': d[0] -= 0.1; break;
case 'w': d[1] += 0.1; break;
case 's': d[1] -= 0.1; break;
case 'm': d[2] += 0.1; break;
case 'n': d[2] -= 0.1; break;
case 'p': Pyramid(); break;
case 'c': Draw_Box(); break;
case 'x': xAngle += 5; break;
case 'y': yAngle += 5; break;
case 'z': zAngle += 5; break;
default: printf(" Keyboard %c == %d", key, key);
}
glutPostRedisplay();
}
void Triad(void)
{
glColor3f(1.0, 1.0, 1.0); //set the dark grey color
glBegin(GL_LINES);
glVertex3fv(ORG); glVertex3fv(XUP);
glVertex3fv(ORG); glVertex3fv(YUP);
glVertex3fv(ORG); glVertex3fv(ZUP);
glEnd();
glRasterPos3f(1.1, 0.0, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'X'); //draw the x axis
glRasterPos3f(0.0, 1.1, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Y'); //draw the y axis
glRasterPos3f(0.0, 0.0, 1.1);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Z'); //draw the z axis
}
void redraw(void)
{
int v;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
glTranslatef(0, 0, -3);
glRotatef(tippangle, 1, 0, 0); // Up and down arrow keys 'tip' view.
glRotatef(viewangle, 0, 1, 0); // Right/left arrow keys 'turn' view.
glDisable(GL_LIGHTING);
Triad();
glPushMatrix();
glTranslatef(d[0], d[1], d[2]); // Move box down X axis.
glScalef(0.2, 0.2, 0.2);
glRotatef(zAngle, 0, 0, 1);
glRotatef(yAngle, 0, 1, 0);
glRotatef(xAngle, 1, 0, 0);
//Draw_Box(); // if i delete comment the cube it's drawn
//Pyramid(); // if i delete this the pyramid it's drawn inside the cube
glPopMatrix();
glutSwapBuffers();
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(900, 600);
glutInitWindowPosition(300, 300);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("Big HW1");
glutDisplayFunc(redraw);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(Special_Keys);
glClearColor(0.1, 0.0, 0.1, 1.0);
glMatrixMode(GL_PROJECTION);
gluPerspective(60, 1.5, 1, 10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 1;
}
答案 0 :(得分:1)
有几种解决方法。一种是创建一个全局变量,该变量将告诉您是否要绘制形状:
bool draw_pyramid = false;
bool draw_box = false;
然后,在您的键盘手柄功能中,执行以下操作:
case 'p': draw_pyramid = true; break;
case 'c': draw_box = true; break;
最后,在您的重绘功能处,执行条件绘制:
if ( draw_pyramid )
Pyramid();
if ( draw_box )
Draw_Box();