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();
}
「我不知道它出錯的地方「不是很有幫助,請準確地描述一下發生了什麼,以及一些示例輸入會發生什麼。 – BoBTFish 2013-04-24 14:55:55
這是很多代碼(和葡萄牙語),它很難說出它的作用你更熟悉你所調用的所有其他方法。你能花一些時間只挑選最相關的代碼片段,並提供一些關於問題的更多細節嗎? – Reyan 2013-04-24 15:03:06
當用戶按下某個鍵時,它看起來像通過glColor3f更改顏色,但在繪製(Desenha)函數中顏色再次發生變化。 – mnajera 2013-04-24 15:07:32