glOrtho 不工作?

发布于 2024-12-08 18:59:12 字数 1097 浏览 0 评论 0原文

 /* OpenGL animation code goes here */
        glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
        glClear (GL_COLOR_BUFFER_BIT);
        glDisable(GL_DEPTH_TEST);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0f,512.0f,0.0f,512.0f,0.0f,1.0f);

        glMatrixMode(GL_MODELVIEW_MATRIX);
        glPushMatrix ();
        glLoadIdentity();
        glTranslatef(x,y,0.0f);
        //glRotatef (theta, 0.0f, 0.0f, 1.0f);

        glBegin (GL_TRIANGLES);
        glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
        glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.8f, -0.5f);
        glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.8f, -0.5f);
        glEnd ();
        glPopMatrix ();

        SwapBuffers (hDC);
        //theta += 0.1f;
        Sleep(1);
        }

这是主要的简单 opengl 循环。

最近,我回来学习Opengl(离开它两年了)。问题是,我尝试使用 glOrtho 创建 2D 环境,但它不起作用。我的意思是,该程序似乎忽略了 glOrtho 调用(没有错误,没有警告,没有投影,什么都没有)。这会导致 GL_TRIANGLE 的顶点 (0,1) 碰到窗口顶部,并且任何超过 1.0f 的东西都会飞离视口(我认为它仍然是单位矩阵?)。我尝试更改参数,但视口中没有发生任何变化。我不知道我是否遗漏了某些东西(可能缺少一些初始化步骤或一些我不知道的弃用)。

 /* OpenGL animation code goes here */
        glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
        glClear (GL_COLOR_BUFFER_BIT);
        glDisable(GL_DEPTH_TEST);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0f,512.0f,0.0f,512.0f,0.0f,1.0f);

        glMatrixMode(GL_MODELVIEW_MATRIX);
        glPushMatrix ();
        glLoadIdentity();
        glTranslatef(x,y,0.0f);
        //glRotatef (theta, 0.0f, 0.0f, 1.0f);

        glBegin (GL_TRIANGLES);
        glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
        glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.8f, -0.5f);
        glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.8f, -0.5f);
        glEnd ();
        glPopMatrix ();

        SwapBuffers (hDC);
        //theta += 0.1f;
        Sleep(1);
        }

This is the main simple opengl loop.

Recently, I come back to study Opengl (after leaving it for like 2 yrs). The problem is, I tried to use glOrtho to create 2D environment, but It is not working. I mean, the program seems to ignore glOrtho call (no errors, no warnings, no projection, nothing). This causes the vertex (0,1) of GL_TRIANGLE to hit the top of windows, and anything more than 1.0f will fly off the viewport (it is still an identity matrix I think?). I tried changing the parameters but no changes occur in the vieewport. I don't know if I am missing something (maybe missing some initialization step or some deprecation that I am not aware of).

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

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

发布评论

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

评论(1

初心 2024-12-15 18:59:12
glOrtho(0.0f,512.0f,0.0f,512.0f,0.0f,1.0f);

该投影有效地选择宽度为 512、高度为 512、深度为 1 单位的框作为可见世界空间。然而,您指定的顶点将全部位于框的一个非常小的子集内,即在一个单位内。

glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.8f, -0.5f);
glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.8f, -0.5f);
glEnd();

我认为您只是混淆了 glOrtho 和 glViewport 的使用。

glOrtho(0.0f,512.0f,0.0f,512.0f,0.0f,1.0f);

This projection effectively selects the box of width 512, height 512 and depth 1 units to be the visible world space. However the vertices you specify will all be within a very small subset of the box, i.e. within one unit.

glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.8f, -0.5f);
glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.8f, -0.5f);
glEnd();

I think you just confused the use of glOrtho with glViewport.

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