This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
这取决于您正在使用的系统,但通常大多数 Windows 坐标系从左下角开始,一直到顶部和右侧。 在这种情况下,您的 gluOrth02D 调用将是错误的。
您有:
窗口的顶部映射到底部,反之亦然。
大多数时候:
正如我所说,这取决于您使用的系统和平台。
我只能谈论大多数 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:
Which has the top of the window mapping to the bottom, vice-verse.
Most of the time it's:
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.
您需要在 reshape 函数(
resize()
)中设置投影矩阵,这也自动解决了用户调整窗口大小的问题:然后在您的绘制函数中,确保矩阵模式是模型视图:
代码的其他问题:
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:And then in your draw function, make sure that the matrix mode is model-view:
Other problems with your code:
glutPostRedisplay()
at the end ofdraw()
. This is going to make your CPU run at 100%. You can instead useglutTimerFunc()
to still have updates every some number of milliseconds.processMouse()
, you're usingwsprintf()
on an array ofchar
s:wsprintf()
takes an array of wide characters (wchar_t
), so you should make the local variables
of typewchar_t[]
, or usesprintf()
andMessageBoxA()
instead ofwsprintf()
andMessageBoxW()
(to whichMessageBox()
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 largex
value. Finally, you should also usesnprintf()/wsnprintf()
instead ofsprintf()/wsprintf()
to protect against the buffer overflow.您似乎在 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).