ios opengl es 2.0,应用正交时我的矩形不是正方形
我正在学习OpenGLES2.0。 我正在尝试在OpenGLES2.0中应用正交投影 我画了一个正方形,但实际上屏幕上没有正方形。 我不知道我遗漏了哪一部分。 谢谢你的帮助! setupRenderingEnv中有一些我没有发布的方法。但这些方法都是用来设置框架的,效果很好。 m_程序创建得很好。 再次感谢你的帮助ios opengl es 2.0,应用正交时我的矩形不是正方形,ios,opengl-es-2.0,orthographic,Ios,Opengl Es 2.0,Orthographic,我正在学习OpenGLES2.0。 我正在尝试在OpenGLES2.0中应用正交投影 我画了一个正方形,但实际上屏幕上没有正方形。 我不知道我遗漏了哪一部分。 谢谢你的帮助! setupRenderingEnv中有一些我没有发布的方法。但这些方法都是用来设置框架的,效果很好。 m_程序创建得很好。 再次感谢你的帮助 // my vertex shader attribute vec4 Position; attribute vec4 SourceColor; uniform mat4 Proj
// my vertex shader
attribute vec4 Position;
attribute vec4 SourceColor;
uniform mat4 Projection;
varying vec4 DestinationColor;
void main(void)
{
DestinationColor = SourceColor;
gl_Position = Projection * Position;
}
// my drawing file
typedef struct
{
float Position[3];
float Color[4];
}Vertex;
const Vertex Vertices[] =
{
{{100, -100, 0}, {1, 0, 0, 1}},
{{100, 100, 0}, {0, 1, 0, 1}},
{{-100, 100, 0}, {0, 0, 1, 1}},
{{-100, -100, 0}, {0, 0, 0, 1}}
};
const GLubyte Indices[] =
{
0, 1, 2,
2, 3, 0
};
- (void)setupRenderingEnv
{
[super setupRenderingEnv];
[self setupVertexBufferObjects];
[self setupRunLoop];
[self applyOrthoWithX:self.frame.size.width andY:self.frame.size.height];
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
}
//-- used for applying ortho in opengl es 2.0
- (void)applyOrthoWithX:(float)maxX andY:(float)maxY
{
float a = 1.0f / maxX;
float b = 1.0f / maxY;
float ortho[16] =
{
a, 0, 0, 0,
0, b, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1
};
GLint projectionUniform = glGetUniformLocation(super.m_programHandle, "Projection");
glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]);
}
//-- overriding drawCandle. it render image, draw candle
- (void)drawCandle
{
glClearColor(0, 104.0/255, 55.0/255, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
GLuint positionSlot = glGetAttribLocation(super.m_programHandle, "Position");
GLuint colorSlot = glGetAttribLocation(super.m_programHandle, "SourceColor");
glEnableVertexAttribArray(positionSlot);
glEnableVertexAttribArray(colorSlot);
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), 0);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid *)(sizeof(float) * 3));
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
glDisableVertexAttribArray(positionSlot);
glDisableVertexAttribArray(colorSlot);
[super drawCandle];
}
你的视口是什么形状?如果不是正方形,那就是问题所在。您正在创建的矩阵-按逆宽度和逆高度缩放-将使宽度始终为1单位宽,高度始终为1单位高。如果宽度和高度的像素数不同,则正方形将不会绘制正方形。您需要考虑视口的纵横比。在我的脑海里,我想它会更像这样:
float ortho [ 16 ] = {
a / b, 0, 0, 0,
0, b, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1
};
(我可能把a/b倒过来了-记不得了)你的视口是什么形状?如果不是正方形,那就是问题所在。您正在创建的矩阵-按逆宽度和逆高度缩放-将使宽度始终为1单位宽,高度始终为1单位高。如果宽度和高度的像素数不同,则正方形将不会绘制正方形。您需要考虑视口的纵横比。在我的脑海里,我想它会更像这样:
float ortho [ 16 ] = {
a / b, 0, 0, 0,
0, b, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1
};
(我可能a/b颠倒了-不记得了)我在320 x 480的iphone simulator 5.1上运行了这个程序。。但这真的是问题吗?是的,这很可能是问题所在。由于320 x 480不是1:1的纵横比,因此您需要在正交投影矩阵或用于绘制到屏幕的坐标中说明这一点。我强烈建议您设置正交投影矩阵以补偿它,使您的像素为正方形,因为处理非正方形像素是一件非常痛苦的事情。我在iphone simulator 5.1(320 x 480)上运行了此操作。。但这真的是问题吗?是的,这很可能是问题所在。由于320 x 480不是1:1的纵横比,因此您需要在正交投影矩阵或用于绘制到屏幕的坐标中说明这一点。我强烈建议您设置正交投影矩阵以补偿它,使您的像素为正方形,因为处理非正方形像素是一件非常痛苦的事情。