C++ 如何使用GLUT在按键上绘制形状?

C++ 如何使用GLUT在按键上绘制形状?,c++,opengl,glut,glew,C++,Opengl,Glut,Glew,我想在按下空格键时画一个圆柱体 这是我现在掌握的代码摘录: void draw_cylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B) { GLfloat x = 0.0; GLfloat y = 0.0; GLfloat angle = 0.0; GLfloat angle_stepsize = 0.1; /** Draw the

我想在按下空格键时画一个圆柱体

这是我现在掌握的代码摘录:

void draw_cylinder(GLfloat radius,
    GLfloat height,
    GLubyte R,
    GLubyte G,
    GLubyte B)
{
    GLfloat x = 0.0;
    GLfloat y = 0.0;
    GLfloat angle = 0.0;
    GLfloat angle_stepsize = 0.1;

    /** Draw the tube */
    glColor3ub(R - 40, G - 40, B - 40);
    glBegin(GL_QUAD_STRIP);
    angle = 0.0;
    while (angle < 2 * 3.141) {
        x = radius * cos(angle);
        y = radius * sin(angle);
        glVertex3f(x, y, height);
        glVertex3f(x, y, 0.0);
        angle = angle + angle_stepsize;
    }
    glVertex3f(radius, 0.0, height);
    glVertex3f(radius, 0.0, 0.0);
    glEnd();

    /** Draw the circle on top of cylinder */
    glColor3ub(R, G, B);
    glBegin(GL_POLYGON);
    angle = 0.0;
    while (angle < 2 * 3.141) {
        x = radius * cos(angle);
        y = radius * sin(angle);
        glVertex3f(x, y, height);
        angle = angle + angle_stepsize;
    }
    glVertex3f(radius, 0.0, height);
    glEnd();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW); 
}

void reshape(GLsizei width, GLsizei height) {  
    if (height == 0) height = 1;
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();            
    gluPerspective(35.0f, aspect, 0.1f, 100.0f);
}

void processKeys(unsigned char key, int x, int y) {

    if (key == 32) { // space key
        glLoadIdentity();
        glTranslatef(0.0, -0.4, -5.0);
        glRotatef(-90, 0.4, 0.0, 0.0);
        draw_cylinder(0.3, 1.0, 255, 160, 100);
    }
    glutPostRedisplay();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);            // Initialize GLUT
    glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
    glutInitWindowSize(640, 480);   // Set the window's initial width & height
    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner

    glutCreateWindow(title);          // Create window with the given title
    glutKeyboardFunc(processKeys);
    glutDisplayFunc(display);       // Register callback handler for window re-paint event
    glutReshapeFunc(reshape);       // Register callback handler for window re-size event
    initGL();                       // Our own OpenGL initialization
    glutMainLoop();                 // Enter the infinite event-processing loop

    return 0;
}
void draw\u圆柱体(GLfloat半径,
GLfloat高度,
GLubyte R,
G字节,
(字节B)
{
glx=0.0;
gly=0.0;
GLfloat角度=0.0;
GLfloat角度_步长=0.1;
/**拔管*/
GL3UB(R-40,G-40,B-40);
glBegin(GLU四条带);
角度=0.0;
而(角度<2*3.141){
x=半径*cos(角度);
y=半径*sin(角度);
glVertex3f(x,y,高度);
glVertex3f(x,y,0.0);
角度=角度+角度+步长;
}
glVertex3f(半径,0.0,高度);
glVertex3f(半径,0.0,0.0);
格伦德();
/**在圆柱体顶部画一个圆*/
gl3ub(R,G,B);
glBegin(GL_多边形);
角度=0.0;
而(角度<2*3.141){
x=半径*cos(角度);
y=半径*sin(角度);
glVertex3f(x,y,高度);
角度=角度+角度+步长;
}
glVertex3f(半径,0.0,高度);
格伦德();
}
无效显示(){
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
glMatrixMode(GLU模型视图);
}
空洞重塑(GLsizei宽度、GLsizei高度){
如果(高度==0)高度=1;
GLfloat纵横比=(GLfloat)宽度/(GLfloat)高度;
glViewport(0,0,宽度,高度);
glMatrixMode(GL_投影);
glLoadIdentity();
透视图(35.0f,纵横比,0.1f,100.0f);
}
无效进程键(无符号字符键,整数x,整数y){
如果(键==32){//空格键
glLoadIdentity();
glTranslatef(0.0,-0.4,-5.0);
glRotatef(-90,0.4,0.0,0.0);
拉拔气缸(0.3,1.0,255,160,100);
}
再发现();
}
int main(int argc,字符**argv){
glutInit(&argc,argv);//初始化GLUT
glutInitDisplayMode(GLUT_DOUBLE);//启用双缓冲模式
GLUTINITWindowsSize(640480);//设置窗口的初始宽度和高度
glutInitWindowPosition(50,50);//定位窗口的初始左上角
glutCreateWindow(标题);//使用给定标题创建窗口
glutKeyboardFunc(进程键);
glutDisplayFunc(display);//为窗口重新绘制事件注册回调处理程序
GLUTREGRAPEFUNC(REGRAPE);//为窗口重新调整大小事件注册回调处理程序
initGL();//我们自己的OpenGL初始化
glutMainLoop();//进入无限事件处理循环
返回0;
}
但是,当我按下空格键时,不会绘制圆柱体。我不知道该怎么办。当我复制我在processKeys方法中的代码并将其粘贴到display方法中时,它工作正常。
我该怎么做呢?

正如函数名所说,您可以使用
display
函数绘制对象。所以,为了实现您想要的,创建一个布尔变量(例如,全局范围),并使用它来控制是否要绘制圆柱体。例如:

bool cylinder_draw = false;

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW); 
    if ( cylinder_draw ) {
        glLoadIdentity();
        glTranslatef(0.0, -0.4, -5.0);
        glRotatef(-90, 0.4, 0.0, 0.0);
        draw_cylinder(0.3, 1.0, 255, 160, 100);
    }
}

void processKeys(unsigned char key, int x, int y) {

    if (key == 32) { // space key
        cylinder_draw = true;
    }
    glutPostRedisplay();
}

我想除了显示功能外,你不能在任何地方画画。谢谢,但我用这种方法只能画一个圆柱体。我怎样才能用每一次按键来创建一个全新的向量呢?你可以使用一个变换向量,所以每次按键后,你都会给这个向量添加一个新的变换,在draw函数中,你只需在它上面插入