OpenGL 2.0 ES 坐标
我正在使用 OpenGL ES 2.0 在我的 iPod touch 上尝试一些东西。我想知道如何解决这个坐标问题。
为了更好地解释,我试图绘制一个四边形并使用顶点着色器旋转/平移它(也因为从我读到的内容来看,这似乎是唯一的方法)。
由于我使用的是 ipod,因此我的比例为 1.5 : 1
,视口设置为
glViewport(0, 0, backingWidth, backingHeight);
因此 0,0
为中心,剪辑边界应位于 >-1.0、-1.0
、-1.0、1.0
等(对吗?)
绘制一个正方形,由于纵横比,我必须使用不同的 x 和 y 坐标值:
static const GLfloat lineV[] = {
-0.5f, 0.33f, 0.5f, 0.33f,
0.5f, 0.33f, 0.5f,-0.33f,
0.5f,-0.33f, -0.5f,-0.33f,
-0.5f,-0.33f, -0.5f, 0.33f,
-0.5f, 0.33f, 0.5f,-0.33f,
0.5f, 0.33f, -0.5f,-0.33f,
};
为了 具有两个对角线的正方形(我知道使用索引会更有效,但这不是重点)..
然后我尝试编写一个顶点着色器来在移动对象时旋转对象:
void main()
{
m = mat4( cos(rotation), sin(rotation), 0.0, 0.0,
-sin(rotation), cos(rotation), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
m2 = mat4(1.0);
m2[1][3] = sin(rotation)*0.8;
gl_Position = position*(m*m2);
}
它可以工作,但由于坐标不相同,所以四边形是旋转时会变形。我应该如何预防呢?我想是否可以将视锥体更改为具有不同的边界(两个轴上不是 -1.0 到 1.0,以便在 y 轴上放大可以解决问题)。
另外还有更好的方法来使用矩阵吗?我的意思是,我习惯于使用glRotatef,而无需指定整个矩阵。是否存在方便的函数/构造函数来完成此任务?
I'm doing my first steps with OpenGL ES 2.0 trying things on my ipod touch. I was wondering how to solve this coordinates issue..
To explain better, I was trying to draw a quad and rotate/translate it using a vertex shader (also because from what I've read it seems the only way to do it).
Since I'm working with a ipod I have a 1.5 : 1
ratio and a viewport set by
glViewport(0, 0, backingWidth, backingHeight);
So 0,0
is the center and bounds for clipping should be at -1.0, -1.0
, -1.0, 1.0
, etc (right?)
To draw a square I had to use different values for x and y coordinates because of the aspect ratio:
static const GLfloat lineV[] = {
-0.5f, 0.33f, 0.5f, 0.33f,
0.5f, 0.33f, 0.5f,-0.33f,
0.5f,-0.33f, -0.5f,-0.33f,
-0.5f,-0.33f, -0.5f, 0.33f,
-0.5f, 0.33f, 0.5f,-0.33f,
0.5f, 0.33f, -0.5f,-0.33f,
};
It's a square with both diagonals (I know that using indexes would be more efficient but that's not the point)..
Then I tried writing a vertex shader to rotate the object while moving it:
void main()
{
m = mat4( cos(rotation), sin(rotation), 0.0, 0.0,
-sin(rotation), cos(rotation), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
m2 = mat4(1.0);
m2[1][3] = sin(rotation)*0.8;
gl_Position = position*(m*m2);
}
It works but since coordinates are not the same the quad is distorted while it rotates. How should I prevent that? I thought if it was possible to change the view frustum to have different bounds (not -1.0 to 1.0 on both axis so that enlarging on y-axis would fix the problem).
In addition is there a better way to use matrixes? I mean, I was used to use glRotatef
without having to specify the whole matrix.. does convenience functions/constructors exist to accomplish this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
glViewport()
的第一个参数不是中心,而是左下角的坐标。您可能应该设置一个考虑您的方面的投影,通常使用 gluPerspective()(如果 GLU 在 ES 中可用)。
The first arguments to
glViewport()
is not the center, it's the bottom left corner's coordinates.You should probably set up a projection that takes your aspect into account, typically using gluPerspective() (if GLU is available in ES).
据我所知,没有提供过剩或支持功能。基本上,我通过在构建顶点时使用相等坐标并使用顶点着色器按正确的纵横比在 y 轴上缩放来解决它。
No glut or support functions are provided from what I've seen. Basically I solved it by using equal coordinates when building vertices and using a vertex shader to scale on y axis by the right aspect ratio.