2013-04-24 37 views
0

我寫了一個代碼,但我不知道它出錯的地方。有人可以幫助我嗎?如何在用戶按下某些按鍵時改變顏色的正方形?

我在我的代碼中寫了一些評論。如果有人能幫助我,我會很高興。 因爲我自己找不到這個錯誤。 。:(

#include <windows.h> 
#include <gl/glut.h> 

// Function callback that is called to draw 
void Desenha(void) 
{ 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    // Clean the window 
    glClear(GL_COLOR_BUFFER_BIT); 

    // Shows that the color is red 
    //   R  G  B 
    glColor3f(1.0f, 0.0f, 0.0f); 

    // Draw a square 
    glBegin(GL_QUADS); 
    glVertex2i(30,226); 
    glVertex2i(226,30); 
    // Shows that the color is blue 
    glColor3f(0.0f, 0.0f, 1.0f); 
    glVertex2i(30,226); 
    glVertex2i(226,30); 
    glEnd(); 

    // Executes the OpenGL's commands 
    glFlush(); 
} 

// rendering 
void Inicializa (void) 
{ 
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
} 

void AlteraTamanhoJanela(GLsizei w, GLsizei h) 
{ 
    // Evita a divisao por zero 
    if(h == 0) h = 1; 

    // Especifica as dimensões da Viewport 
    glViewport(0, 0, w, h); 

    // Inicializa o sistema de coordenadas 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    // (left, right, bottom, top) 
    if (w <= h) 
     gluOrtho2D (0.0f, 250.0f, 0.0f, 250.0f*h/w); 
    else 
     gluOrtho2D (0.0f, 250.0f*w/h, 0.0f, 250.0f); 
} 

// Function callback that is called to manage the keyboard tasks 
void GerenciaTeclado(unsigned char key, int x, int y) 
{ 
    switch (key) { 
    case 97: 
    case 'a':// change the actual color to red 
     glColor3f(1.0f, 0.0f, 0.0f); 
     break; 
    case 118: 
    case 'v':// change de color do blue 
     glColor3f(0.0f, 0.0f, 1.0f); 
     break; 
    case 27: 
    case 'esc':// close the screen 
     exit(0); 
     break; 
    } 
    glutPostRedisplay(); 
} 

// Main Program 
int main(void) 
{ 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(256,256); 
    glutInitWindowPosition(10,10); 
    glutCreateWindow("Quadrado"); 
    glutDisplayFunc(Desenha); 
    glutReshapeFunc(AlteraTamanhoJanela); 
    Inicializa(); 
    glutMainLoop(); 
} 
+3

「我不知道它出錯的地方「不是很有幫助,請準確地描述一下發生了什麼,以及一些示例輸入會發生什麼。 – BoBTFish 2013-04-24 14:55:55

+1

這是很多代碼(和葡萄牙語),它很難說出它的作用你更熟悉你所調用的所有其他方法。你能花一些時間只挑選最相關的代碼片段,並提供一些關於問題的更多細節嗎? – Reyan 2013-04-24 15:03:06

+2

當用戶按下某個鍵時,它看起來像通過glColor3f更改顏色,但在繪製(Desenha)函數中顏色再次發生變化。 – mnajera 2013-04-24 15:07:32

回答

1

別的不說你switch()沒有編譯,你錯過了glutInit()打電話來,你從來沒有通過glutKeyboardFunc()迷上了GerenciaTeclado()

試試這個:

#include <GL/glut.h> 

// Function callback that is called to manage the keyboard taks 
float r = 0.0f; 
float g = 0.0f; 
float b = 0.0f; 
void GerenciaTeclado(unsigned char key, int x, int y) 
{ 
    switch (key) { 
    case 'a':// change the actual color to red 
     r = 1.0f; 
     g = 0.0f; 
     b = 0.0f; 
     break; 
    case 'v':// change de color do blue 
     r = 0.0f; 
     g = 0.0f; 
     b = 1.0f; 
     break; 
    case 27:// close the screen 
     exit(0); 
     break; 
    } 
    glutPostRedisplay(); 
} 

// Function callback that is called to draw 
void Desenha(void) 
{ 
    // Clean the window 
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT); 

    // Inicializa o sistema de coordenadas 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    double w = glutGet(GLUT_WINDOW_WIDTH); 
    double h = glutGet(GLUT_WINDOW_HEIGHT); 
    double ar = w/h; 
    glOrtho(-2 * ar, 2 * ar, -2, 2, -1, 1); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    // Draw a square 
    glBegin(GL_QUADS); 
    // Shows that the color is red 
    //  R G B 
    glColor3f(r, g, b); 
    glVertex2f(-1, -1); 
    glVertex2f(1, -1); 
    // Shows that the color is blue 
    glColor3f(0.0f, 0.0f, 1.0f); 
    glVertex2f(1, 1); 
    glVertex2f(-1, 1); 
    glEnd(); 

    glutSwapBuffers(); 
} 

// Main Program 
int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowSize(256,256); 
    glutInitWindowPosition(10,10); 
    glutCreateWindow("Quadrado"); 
    glutDisplayFunc(Desenha); 
    glutKeyboardFunc(GerenciaTeclado); 
    glutMainLoop(); 
} 
相關問題