2017-06-29 84 views
0

我想按空格鍵時畫一個圓柱體。如何使用GLUT在按鍵上繪製形狀?

這是代碼的摘錄我現在所擁有的:

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; 
} 

然而,當我按下空格鍵,氣缸不繪製。我不知道該怎麼做。當我複製我在processKeys方法中的代碼並將其粘貼到顯示器方法中時,它工作正常。 我該怎麼做?

+0

我不認爲你被允許從任何地方繪製,但顯示功能。 – HolyBlackCat

回答

1

如函數名稱所示,使用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(); 
} 
+0

謝謝,但我只能使用這種方法繪製一個柱面。我怎樣才能創建一個全新的每一個按鍵? – OldMcDonald

+1

你可以使用一個變換矢量,所以在每次按下之後,向這個矢量和繪圖函數添加一個新的變換,你只需要對它進行修改 – Amadeus