OpenGL,GL_MODELVIEW GL_PROJECTION的问题

时间:2010-05-09 21:25:01

标签: opengl matrix

伙计们,我正在努力完成我的作业,但我在openGL上的这些模型上遇到了一些问题......任何想法为什么我的画不会发生?有一件事奇怪的是,如果我改为gluPerspective它就有效..

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

static int shoulder = 0;
static int elbow = 0;

void init(void) {
    glClearColor(1.0, 1.0, 1.0, 0.0);
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

        /* BASE */
        glRotatef((GLfloat) shoulder, 0.0, 0.0, 1.0);
        glTranslatef(1.0, 0.0, 0.0);

        glPushMatrix();
            //glScalef(2.0, 0.4, 1.0);
            glBegin(GL_QUADS);
                glColor3f(0, 0, 0);
                    glVertex2f(0.0, 0.0);
                    glVertex2f(0.0, 10.0);
                    glVertex2f(10.0, 10.0);
                    glVertex2f(10.0, 0.0);
            glEnd();
        glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho((GLfloat)-w/2, (GLfloat)w/2, (GLfloat)-h/2, (GLfloat)h/2, -1.0, 1.0); // modo de projecao ortogonal
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -5.0);
}

void keyboard(unsigned char key, int x, int y) {
    switch (key) {
    case 's':
        shoulder = (shoulder + 5) % 360;
        glutPostRedisplay();
        break;
    case 'S':
        shoulder = (shoulder - 5) % 360;
        glutPostRedisplay();
        break;
    case 'e':
        elbow = (elbow + 5) % 360;
        glutPostRedisplay();
        break;
    case 'E':
        elbow = (elbow - 5) % 360;
        glutPostRedisplay();
        break;
    case 27:
        exit(0);
        break;
    default:
        break;
    }
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 400);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

您绘制的多边形位于glOrtho()设置的体积之外。您只能看到Z坐标在-1和1之间的点。

这部分代码会导致绘图超出该范围。

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);

将glOrtho()的最后2个参数替换为-10,+ 10或删除glTranslatef()