Wrong Transformation Rendering a Simple Square on OpenGL ES 2 (iOS)

Incpt.Mobis

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:

enter image description here

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.

Incpt.Mobis

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

OpenGL ES Rendering thread on iOS

From Dev

Drawing A Square in OpenGL ES

From Dev

OpenGl rendering in iOS

From Dev

OpenGL GLFW simple cube not rendering

From Dev

Rendering multiple 2D images in OpenGL-ES 2.0

From Dev

rendering some characters in OpenGL ES 2.x scene

From Dev

Android OpenGL ES 2. 0 VBO Not Rendering

From Dev

Pyramid distorted during rendering under android opengl ES2

From Dev

OpenGL ES Rendering on Older Device

From Dev

Kevin Brothaler OpenGL ES 2 tutorial code is wrong?

From Dev

What is wrong with this font rendering on ios

From Dev

Using SDL2 & Glew, trying to draw a simple white square in OpenGL. Just getting black screen

From Dev

Using SDL2 & Glew, trying to draw a simple white square in OpenGL. Just getting black screen

From Dev

OpenGL is rendering just texture, not lines (probably simple)

From Dev

OpenGL ES 1.0 to OpenGL ES 2.0, I cant draw a square

From Dev

OpenGL ES 2.0 square looks like rectangle

From Dev

OpenGL ES 2.0 square looks like rectangle

From Dev

glGetUniformLocation OpenGL ES 2.0 (wrong return result on ipad 3 iOS 7.0.3)

From Dev

glGetUniformLocation OpenGL ES 2.0 (wrong return result on ipad 3 iOS 7.0.3)

From Dev

iOS SDL2 OpenGL ES Cannot Draw Texture

From Dev

OpenGL ES(WebGL) rendering many small objects

From Dev

OpenGL FreeType2 bitmap not rendering

From Dev

OpenGL ES difference for iOS and Android

From Dev

Change OpenGL ES version on iOS

From Dev

sRGB on iOS OpenGL ES 2.0

From Dev

Opengl ES 3.1 support for ios?

From Dev

OpenGL ES difference for iOS and Android

From Dev

Indexing in opengl-es iOS

From Dev

No transparency with simple OpenGL ES2.0 stencils

Related Related

HotTag

Archive