当我在OpenGL中按下一个键时无法绘制形状
Posted
技术标签:
【中文标题】当我在OpenGL中按下一个键时无法绘制形状【英文标题】:Can't draw shape when I press a key in OpenGL 【发布时间】:2019-10-07 11:02:56 【问题描述】:我在绘制一些形状时遇到了问题。当我执行程序时,我应该只看到坐标,然后按下一个键来绘制形状,但默认情况下它已经绘制了立方体,然后是金字塔。当我按“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;
【问题讨论】:
您的redraw
函数会清除屏幕,因此不要在键盘处理程序中绘制立方体和金字塔,而是设置一个类似 drawPyramid
或 drawCube
的标志并在您的 redraw
中检查它功能。
【参考方案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();
【讨论】:
谢谢,它工作了 通过取消设置其他标志,或使用单个变量来确定要绘制的内容。 你可以试试,例如:case 'p': draw_pyramid = true; draw_box = false; break;
是的,成功了,非常感谢,今天的灵感对我来说非常低,这似乎是一件容易的事情......以上是关于当我在OpenGL中按下一个键时无法绘制形状的主要内容,如果未能解决你的问题,请参考以下文章
在 OpenGL 中使用箭头键移动一些形状 - 它缩小而不是移动