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();
}
4

1 回答 1

1

除其他外,您switch()没有编译,您错过了一个glutInit()电话,并且您从未GerenciaTeclado()通过glutKeyboardFunc().

尝试这个:

#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();
}
于 2013-04-24T16:03:19.607 回答