I am learning OpenGL ES2 from the ground (My Reference). I want to render a simple square with the size of 2 at center of the screen.
This is my Square (2 Triangles):
typedef struct {
float Position[3];
float Color[4];
} Vertex;
const Vertex Vertices[] = {
{{1, -1, 0}, {1, 0, 0, 1}},
{{1, 1, 0}, {0, 1, 0, 1}},
{{-1, 1, 0}, {0, 0, 1, 1}},
{{-1, -1, 0}, {0, 0, 0, 1}}
};
const GLubyte Indices[] = {
0, 1, 2,
2, 3, 0
};
I setup Layer, Context, Render as some popular Examples on Google.
This is my Vertex shader:
attribute vec4 Position; // 1
attribute vec4 SourceColor; // 2
varying vec4 DestinationColor; // 3
uniform mat4 Projection;
void main(void) { // 4
DestinationColor = SourceColor; // 5
gl_Position = Projection * Position;
}
This is my Rendering function:
-(void)render:(CADisplayLink*)displayLink
{
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// Setup Projection Matrix
GLfloat aspect = self.frame.size.height / self.frame.size.width;
//GLKMatrix4 mat = GLKMatrix4MakeFrustum(-2, 2, -2*aspect, 2*aspect, 2, 100);
GLKMatrix4 mat = GLKMatrix4MakeOrtho(-2.0, 2.0, -2.0*aspect, 2.0*aspect, 0, 100);
glUniformMatrix4fv(_projectionUniform, 1, 0, mat.m);
// Setup color & vertices
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
// Draw...
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]),
GL_UNSIGNED_BYTE, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
I setup Othor Project Matrix with width of 4 and height of 1.77*4 (16:9 of iPhone). I expect an Square in the center of the screen but I get an rectangle instead:
The size and location are not what I expect. What did I do wrong ? I also change to GLKMatrix4MakeFrustum but also receive same result.
You can also download my project here: https://www.mediafire.com/?w0tb74z5tk0xig0
Thank you for reading my long question.
After a half day of checking line by line of code.
I found out the AutoLay-Out is the reason of the problem. Turn off Auto layout and everything works perfect.
JUST DONT USE AUTOLAY OUT TO RESIZE THE OPENGL VIEW. Maybe it effects CA layer that is rendered by OpenGL.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments