OpenGL ES 3.0 renders not centered?

Hi there,


I'm just starting with OpenGL ES (it's some years ago since my last OpenGL project too) and I'm wondering why the quad is not drawn centered? In my understanding it should be drawn in the middle of the simulator screen. Here is my code snippet. (Drawing a quad is not exactly what I want but after I noticed this offset I could break it down to this simple example showing the same behavior, so when I could solve this I will also know what's going on with my code...) Any help is really appreciated, because I'm now running out of ideas. I should also mention that i'm currently using Xcode 8 beta 2/iOS 10 simulator. By the way - a second question: does anybody know how long we need to wrap any GL type with GLenum? It does not feel really "swifty", Perhaps this a subject to change?


self.effect = GLKBaseEffect()

...


var vertices: [Float] = [

2, -2, -10,

2, 2, -10,

-2, 2, -10,

-2, -2, -10

]

var colors: [Float] = [

1, 0, 0, 1,

1, 1, 0, 1,

0, 0, 1, 1,

0, 0, 0, 1

]


var indices: [GLubyte] = [ 0, 1, 2, 2, 3, 0 ]


glClearColor(0.0, 0.0, 0.10, 1.0)

glClear(GLenum(GL_COLOR_BUFFER_BIT))


glViewport(0, 0, GLsizei(size.width), GLsizei(size.height))


let aspect = size.width/size.height

let projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(90.0), Float(aspect), 0.1, 20.0)


let modelViewMatrix = GLKMatrix4.identity

self.effect.transform.modelviewMatrix = modelViewMatrix

self.effect.transform.projectionMatrix = projectionMatrix;


self.effect.prepareToDraw()


glEnableVertexAttribArray(GLuint(GLKVertexAttrib.position.rawValue))

glVertexAttribPointer(GLuint(GLKVertexAttrib.position.rawValue),

3, GLenum(GL_FLOAT), GLboolean(GL_FALSE),

GLsizei(vertices.count),

&vertices)


glEnableVertexAttribArray(GLuint(GLKVertexAttrib.color.rawValue))

glVertexAttribPointer(GLuint(GLKVertexAttrib.color.rawValue),

4, GLenum(GL_FLOAT), GLboolean(GL_FALSE),

GLsizei(colors.count),

&colors)


glDrawElements(GLenum(GL_TRIANGLES),

6, GLenum(GL_UNSIGNED_BYTE),

&indices)


As mentioned before: it draws a colored quad, but its not centered on the simulator screen and all my trials to make it centered failed, but it should be centered, it's the identity matrix and the coordinates are at around the center. And size is the frame.size property of the GLKView. I would really appreciate any help. Thanks in advance...

Answered by JuergenT in 156806022

Okay,


I finally solved the problem. The GL code is correct, the problem was just the size assigned for the viewport was taken from the views frame, but is given in logical points, not in pixels. Multiplying it with the scale factor solved the problem. It took me many hours to find this, hopefully it will help you if you are running into the same issue.

Accepted Answer

Okay,


I finally solved the problem. The GL code is correct, the problem was just the size assigned for the viewport was taken from the views frame, but is given in logical points, not in pixels. Multiplying it with the scale factor solved the problem. It took me many hours to find this, hopefully it will help you if you are running into the same issue.

OpenGL ES 3.0 renders not centered?
 
 
Q