将坐标系设置为 OpenGL for Cocoa 中的像素
我最近开始学习 OpenGL,到目前为止看起来很直观,但我一直坚持这一点。如何更改设置以便 OpenGL 使用基于像素的坐标系而不是现在使用的坐标系。例如,这段代码绘制了一个大约 10x20 像素的矩形:
glBegin(GL_QUADS); {
glVertex3f(player.x, player.y, 0);
glVertex3f(player.x+0.05, player.y, 0);
glVertex3f(player.x+0.05, player.y+0.1, 0);
glVertex3f(player.x, player.y+0.1, 0);
}
我怎样才能使这段代码:
glBegin(GL_QUADS); {
glVertex3f(player.x, player.y, 0);
glVertex3f(player.x+10.0, player.y, 0);
glVertex3f(player.x+10.0, player.y+20.0, 0);
glVertex3f(player.x, player.y+20.0, 0);
}
产生基本相同的东西?谢谢。
编辑:我看到有人建议在某处使用 glOrtho() ,但他们没有提供任何有关如何执行此操作的信息。这可行吗?如果可行,我该怎么做?
I recently satrted learning OpenGL, and it seems pretty intuitive so far, but this I am stuck on. How can I change the settings so that OpenGL uses a pixel based coordinate system instead of what it uses now. For instance this code draws a rectangle about 10x20 pixels:
glBegin(GL_QUADS); {
glVertex3f(player.x, player.y, 0);
glVertex3f(player.x+0.05, player.y, 0);
glVertex3f(player.x+0.05, player.y+0.1, 0);
glVertex3f(player.x, player.y+0.1, 0);
}
How can I make it so that this code:
glBegin(GL_QUADS); {
glVertex3f(player.x, player.y, 0);
glVertex3f(player.x+10.0, player.y, 0);
glVertex3f(player.x+10.0, player.y+20.0, 0);
glVertex3f(player.x, player.y+20.0, 0);
}
would produce basically the same thing? Thanks.
EDIT: I saw someone suggest using glOrtho() somewhere, but they didn't give any info on how to do that. Would this work, and if so, how would I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,如果我听起来很简短;事实证明,iPad 上的 Safari 在执行其中一项操作(可能是内存警告引起的重新加载)时会丢弃您的输入内容。
假设:您已将问题标记为 iPhone,因此您正在谈论 Cocoa Touch(原点位于左上角,y 轴正值向下)。同样,您可能需要点,而不是像素 - 因此无论是否在视网膜显示屏上,屏幕上的像素都是 320x480。
glOrtho 正是您想要的 - 请参阅 http://www 的手册页.khronos.org/opengles/documentation/opengles1_0/html/glOrtho.html。因此,您可能想对投影堆栈执行以下操作:
glOrthof(0, 320, 480, 0, 1, 1000);
然后在模型视图上执行一次:
glTranslatef(0, 0, -500)
并提交所有未来没有 az 组件的几何图形。
它的作用是设置一个没有透视的视口(即 z 位置不影响对象大小),屏幕的左边缘位于 x=0,右边缘位于 x=320,顶部位于 y=0, y=480 处的底部。它任意地将近剪裁平面放置在距离 1 处,将远剪裁平面放置在距离 1000 处,因为它们必须位于某个地方。但随后它会转换为两者之间的中间位置(好吧,不完全准确),因此如果您不再提交任何 z 坐标,那么您的所有几何图形都将可见,并且您有一些围绕 y 或 x 旋转的余地,如果您想要他们。
但请注意:glVertex 调用在 iOS 上不可用,因为它们不是 OpenGL ES 的一部分。它们在现代 OpenGL 中也已被弃用。简而言之,它们确实效率低下,而且驾驶员很难快速处理。 glVertexPointer 是要使用的东西,传入一个 C 数字数组。
Sorry if I sound brief; it turns out that Safari on the iPad throws away your typing when it does one of it's probably memory warning induced reloads.
Assumptions: you've tagged the question iPhone so you're talking about Cocoa Touch (with the origin in the top left, positive on the y axis being down). Similarly, you probably want points, not pixels - so 320x480 of them on the screen whether on a retina display or not.
glOrtho is exactly what you want - see the man page at http://www.khronos.org/opengles/documentation/opengles1_0/html/glOrtho.html. So, you probably want to do this to your projection stack:
glOrthof(0, 320, 480, 0, 1, 1000);
And then do this once on modelview:
glTranslatef(0, 0, -500)
And submit all future geometry without a z component.
What that does is set up a viewport with no perspective (ie, z position doesn't affect object size), with the left edge of the screen at x=0, the right at x=320, the top at y=0 and the bottom at y=480. It arbitrarily puts the near clip plane at a distance of 1 and the far at a distance of 1000, since they have to be somewhere. But then it translates to halfway (okay, not quite exactly) between the two and so if you don't submit any more z coordinates then all your geometry will be visible and you've got some leeway for rotations around y or x if you want them.
Be warned though: glVertex calls aren't available on iOS as they aren't part of OpenGL ES. They're also deprecated in modern OpenGL. The short version is that they're really inefficient and hard for a driver to deal with quickly. glVertexPointer is the thing to use, passing in a C array of numbers.