OpenGL投影问题:为什么我在屏幕上看不到任何东西?

发布于 2024-11-08 19:41:15 字数 1192 浏览 0 评论 0原文

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

    //glMatrixMode(GL_MODELVIEW);
    //gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);

    glMatrixMode(GL_PROJECTION);
    gluPerspective(45, 2, -1, 1);
    //glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
    //gluPerspective(45.0, 45, -1, 1);
}

void displayFcn (void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 1.0, 0.0);
    glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_LINE);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.1, 0.0, 0.0);
        glVertex3f(0.50, 0.866025, 0.0);    
    glEnd();

    glFlush();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0,0,newWidth, newHeight);

    winWidth = newWidth;
    winHeight = newHeight;

}
void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Test");

    init();
    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}

有人可以解释一下并给出如何使该三角形可见的建议吗?

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

    //glMatrixMode(GL_MODELVIEW);
    //gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);

    glMatrixMode(GL_PROJECTION);
    gluPerspective(45, 2, -1, 1);
    //glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
    //gluPerspective(45.0, 45, -1, 1);
}

void displayFcn (void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 1.0, 0.0);
    glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_LINE);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.1, 0.0, 0.0);
        glVertex3f(0.50, 0.866025, 0.0);    
    glEnd();

    glFlush();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0,0,newWidth, newHeight);

    winWidth = newWidth;
    winHeight = newHeight;

}
void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Test");

    init();
    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}

Could someone explain a little bit and give suggestions how to make that triangle visible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哎呦我呸! 2024-11-15 19:41:16

我不认为你可以将“zNear”设置为负值。这会对深度缓冲区产生奇怪的影响,并且意味着你会看到眼睛后面的东西。 文档说它总是积极的。如果将纵横比固定为 2,无论窗口的纵横比如何,也可能会得到一些奇怪的结果。

您还需要将当前矩阵设置回 MODEL_VIEW,如 datenwolf 所示。您不需要使用 gluLookAt,但您需要采取一些措施将三角形移离眼睛所在的原点。您可以通过将 z 分量设置为某个负值,或者在顶点之前应用平移来做到这一点:

glPushMatrix();
glTranslated(0,0,-5);
glBegin(GL_TRIANGLES);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.1, 0.0, 0.0);
    glVertex3f(0.50, 0.866025, 0.0);    
glEnd();
glPopMatrix();

I don't think that you're allowed to set 'zNear' negative. That will do weird things to the depth buffer and mean that you see things behind the eye. Docs say it's always positive. You're also liable to get somewhat strange results if you fix the aspect ratio to 2, regardless of the aspect ratio of the window.

You also need to set the current matrix back to MODEL_VIEW as datenwolf has shown. You don't need to use gluLookAt, but you need to do something to move the triangle away from the origin where the eye is located. You could do that by setting the z component to some negative value, or by applying a translation before the vertices:

glPushMatrix();
glTranslated(0,0,-5);
glBegin(GL_TRIANGLES);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.1, 0.0, 0.0);
    glVertex3f(0.50, 0.866025, 0.0);    
glEnd();
glPopMatrix();
倒带 2024-11-15 19:41:16

gluPrespectivegluLookAt 在所选堆栈上当前矩阵的顶部相乘。您需要先加载身份才能有意义。此外,您还需要在渲染之前设置视口。最佳实践是在显示函数中设置所有矩阵和视口,而不是在其他地方。遵守这条规则会让你的生活变得更加轻松。同样在 OpenGL 中通常没有专门的初始化阶段。资源按需加载。

void displayFcn (void)
{
    glViewport(0,0,winWidth, winHeight);

    glClearColor (1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 2, 1, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);

    glColor3f(0.0, 1.0, 0.0);
    glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_LINE);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.1, 0.0, 0.0);
        glVertex3f(0.50, 0.866025, 0.0);    
    glEnd();

    glFinish();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{
    winWidth = newWidth;
    winHeight = newHeight;
    glutPostRedisplay();
}

void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Test");

    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}

gluPrespective and gluLookAt multiply on top of the current matrix on the selected stack. You need to load an identity first to make sense. Also you need to set a viewport before rendering. Best practice is to set all matrices and the viewport in the display function, and nowhere else. Sticking to that rule will make your life a lot easier. Also in OpenGL one usually doesn't have a dedicated initializtion phase. Resources are loaded on demand.

void displayFcn (void)
{
    glViewport(0,0,winWidth, winHeight);

    glClearColor (1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 2, 1, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);

    glColor3f(0.0, 1.0, 0.0);
    glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_LINE);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.1, 0.0, 0.0);
        glVertex3f(0.50, 0.866025, 0.0);    
    glEnd();

    glFinish();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{
    winWidth = newWidth;
    winHeight = newHeight;
    glutPostRedisplay();
}

void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Test");

    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文