OpenGL坐标问题

发布于 2024-07-15 06:16:36 字数 1473 浏览 8 评论 0原文

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

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

发布评论

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

评论(3

赴月观长安 2024-07-22 06:16:37

这取决于您正在使用的系统,但通常大多数 Windows 坐标系从左下角开始,一直到顶部和右侧。 在这种情况下,您的 gluOrth02D 调用将是错误的。

您有:

gluOrtho2D(0, appWidth, appHeight, 0);

窗口的顶部映射到底部,反之亦然。

大多数时候:

gluOrtho2D(0, appWidth, 0, appHeight);

正如我所说,这取决于您使用的系统和平台。
我只能谈论大多数 Linux 实现。

只是需要检查一下其他内容,以防它影响您的错误。

It depends on what system you are working on, but usually most windows coordinate systems start at the bottom left corner and count up to the top and to the right. In this case your gluOrth02D call would be wrong.

You Have:

gluOrtho2D(0, appWidth, appHeight, 0);

Which has the top of the window mapping to the bottom, vice-verse.

Most of the time it's:

gluOrtho2D(0, appWidth, 0, appHeight);

As I said it depends on the system, platform your working with.
I can only speak for most linux implementations.

Just something else to check out just in case it affecting your bug.

星星的軌跡 2024-07-22 06:16:36

您需要在 reshape 函数(resize())中设置投影矩阵,这也自动解决了用户调整窗口大小的问题:

void resize(int w, int h)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, w, h, 0);
}

然后在您的绘制函数中,确保矩阵模式是模型视图:

void draw()
{
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  ...
}

代码的其他问题:

  • 您可能不应该在 draw() 末尾调用 glutPostRedisplay()。 这将使您的 CPU 100% 运行。 您可以改为使用 glutTimerFunc() 来仍然每隔一定毫秒数进行更新。
  • processMouse() 中,您在 char 数组上使用 wsprintf()wsprintf()接受宽字符数组 (wchar_t),因此您应该将局部变量 s 设为 wchar_t[] 类型,或使用 sprintf()MessageBoxA() 而不是 wsprintf()MessageBoxW() (其中 MessageBox() 在编译 Unicode 应用程序时扩展为宏,我假设您正在这样做)。 您还容易受到缓冲区溢出的影响 - 您应该使用至少 12 个字符的缓冲区,即使实际上您永远不会传递非常大的 x 值。 最后,您还应该使用 snprintf()/wsnprintf() 而不是 sprintf()/wsprintf() 来防止缓冲区溢出。

You need to set the projection matrix inside the reshape function (resize()), which also automatically solves the problem of the user resizing the window:

void resize(int w, int h)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, w, h, 0);
}

And then in your draw function, make sure that the matrix mode is model-view:

void draw()
{
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  ...
}

Other problems with your code:

  • You probably shouldn't be calling glutPostRedisplay() at the end of draw(). This is going to make your CPU run at 100%. You can instead use glutTimerFunc() to still have updates every some number of milliseconds.
  • In processMouse(), you're using wsprintf() on an array of chars: wsprintf() takes an array of wide characters (wchar_t), so you should make the local variable s of type wchar_t[], or use sprintf() and MessageBoxA() instead of wsprintf() and MessageBoxW() (to which MessageBox() expands as a macro when compiling a Unicode application, which I'm assuming you're doing). You're also vulnerable to a buffer overflow -- you should use a buffer of at least 12 characters, even though realistically you'll never be passed a very large x value. Finally, you should also use snprintf()/wsnprintf() instead of sprintf()/wsprintf() to protect against the buffer overflow.
人海汹涌 2024-07-22 06:16:36

您似乎在 ModelView 矩阵上调用 glOrtho2D 。 我怀疑这就是问题所在(因为我猜在这种情况下,您的投影应该是身份),但您仍然应该在投影矩阵上调用它。

您还应该在调整大小调用中打印出 w 和 h ,只是为了确保您的窗口大小实际上是您认为的大小(我不知道 glut 是如何工作的,但 glutInitWindowSize() 可能包括边框,这会弄乱事情向上)。

You seem to be calling your glOrtho2D on your ModelView matrix. I doubt that that's the problem (since I guess in this case, your Projection should be the identity), but you should still call it on your Projection matrix instead.

You should also print out w and h in your resize call, just to make sure that your window size is actually what you think it is (I don't know how glut works, but glutInitWindowSize() may include borders, which would mess things up).

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