开发者

Projection Matrix in Apple's iPhone OpenGLES 2.0 Template Project

Background:

I am trying to learn OpenGLES 2.0 for the iPhone and have been stumped by the template project that Apple provides in xCode.

The default project shows a 2d square, but I want to experiment with adding vertexes with varying depths on the z axis.

In Apple's default OpenGL project, they draw a 2d square using only x and y coordinates passed to the vertex shader.

In order to experiment with placing vertices in 3d space, I added a z value to the vertex data in the sample project. Then I changed the size parameter in the glVertexAttribPointer method from 2 to 3. I changed the z values on the vertices in the hope that it would cause the square to appear tilted backwards when I ran the program.

The problem is that it still shows up as the same 2d square. From my reading, I believe this is caused by the projection matrix being set to Orthographic instead of Perspective.

Question:

How do I change the projection matrix from Orthographic to Perspective using OpenGLES 2.0 in Apple's template OpenGL project?

I have tried:

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustumf(-0.5, 0.5, -0.5, 0.5, 1, 10);

But it didn't change the result.

I also tried adding something like this to the vertex shader program:


mat4 projectionMatrix = { 2.0/320.0, 0.0, 0.0, -1.0, 0.0, 2.0/480.0, 0.0, 开发者_如何学Python-1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0};

gl_Position *= projectionMatrix;


But that didn't work either.


Is the method for setting the projection matrix different for OpenGLES 2.0 that it was for 1.0. I am thinking maybe the stuff I am trying is not compatible with 2.0.

Thanks in advance.

Mike

Update

Thanks for your help John. I really appreciate it. I tried adding a depth buffer based on your suggestion by adding the following code to the createFramebuffer method in my EAGL view, but still no change.

// Create depth render buffer and allocate backing store.
        glGenRenderbuffers(1, &depthRenderbuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);

        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, framebufferWidth, framebufferHeight);

        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);

And if I add the following code to my render method:

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glFrustumf(-0.5, 0.5, -0.5, 0.5, 1, 10);

Xcode throws an exception. Any thoughts?


I finally got the projection to work.

So, it wasn't a depth buffer issue, since apparently that isn't needed unless you are structuring multiple objects at multiple depth, from what I hear.

It seems that in Opengles 2.0 you need to create your own projection matrix and apply it to the position attribute in order for things to render with a 3d perspective. The problem is that I couldn't find any built in methods in 2.0 for generating the matrix.

From looking at Jeff LeMarche's code posted as part of his chapters on Opengles 2.0 I found that he used file with a bunch of convenience methods for generating the perspective matrix (and lots of other good stuff). You can find that sample project here: http://iphonedevelopment.blogspot.com/2010/07/opengl-es-20-book-teaser.html The file is called GLCommon. Thank you Jeff!

In order to create and apply the projection matrix, I used a method called Matrix3DSetPerspectiveProjectionWithFieldOfView from the GLCommon file.

That sets up a matrix that can be passed into the vertex shader as a uniform and then multiplied by the position attribute and then passed into gl_Position.

It took some adjustments to the near and far values in Matrix3DSetPerspectiveProjectionWithFieldOfView in order to get stuff to show up in the field of view.

As a tip for other people trying to learn OpenGLES 2.0, there is a ton of information on the web about how to do things in OpenGLES that is meant for 1.0, but doesn't say so, since it was probably posted before 2.0 came out. Check the 2.0 headers whenever trying something to make sure the code will work with 2.0. If you are using Apple's template project, it includes the frameworks for both versions, so your 1.0 code won't be recognized as problematic until you build and run.


It sounds like you are using the example that doesn't default to using a depth buffer. Look for references to GL_DEPTH_COMPONENT16 in the sample project and implement the use of the depth buffer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜