0

我在这里使用半立方体这个词,它不是真正的立方体,它只有 3 个面。我执行以下操作:

1.用蓝色画一个立方体的三个面,第一个面是蓝色的,另外两个是红色的; 2.将半立方体旋转 45 度,使我应该看到一半的红脸。

但是然后我只显示立方体,只有蓝色的脸在那里,我应该看到一半蓝色和一半红色。
也许我无法启用深度(我使用 glEnable()),我的印象是深度尺寸在我的绘图中被忽略了。

#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>

int width=500, height=500, depth=500;

void init()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    glViewport(0, 0, width, height);
    glOrtho(0, width, height, 0, 0, 1);
}

void display()
{
    glClearColor(0.9, 0.9, 0.9, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor4f(0, 0, 1, 0);
    glBegin(GL_QUADS);


    // First face
    glVertex3i(100, 100,0);
    glVertex3i(300, 100,0);
    glVertex3i(300, 300,0);
    glVertex3i(100, 300,0);

    glColor4f(1, 0, 0, 0);
    // Second face
    glVertex3i(300,100,0);
    glVertex3i(300,300,0);
    glVertex3i(300,100,300);
    glVertex3i(300,100,300);

    // Third face
    glVertex3i(100, 100,300);
    glVertex3i(300, 100,300);
    glVertex3i(300, 300,300);
    glVertex3i(100, 300,300);

    glEnd();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(45, 1, 0, 0);

    glFlush();
}

int main(int argc, char * argv[])
{
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow("Test");
    glutDisplayFunc(display);
    init();
    glutMainLoop();
    return 0;
}

这是我得到的图像:

Image

编辑:我有点解决改变视口:

glOrtho(0, width, height, 0, -depth, depth);

但是我仍然缺少基础知识,现在我会继续。

4

2 回答 2

2

Rotation only effects objects that are drawn after the rotation. When you call glBegin, whatever you draw is immediately drawn using the current modelview matrix on the stack.

Modifying the matrix after drawing has no effect. You should move the rotation before the draw call.

于 2012-11-12T22:38:40.833 回答
0

Rotation updates the current matrix, which will affect all object drawn after the rotation.

In order to see the rotation just move it above your draw lines.

于 2012-11-12T22:44:15.347 回答