views:

4312

answers:

6

I am having some trouble getting a perspective set up in OpenGL ES. Anyone have any tips? My ortho project looked like this: glOrthof(-8.0f, self.frame.size.width, -12.0f, self.frame.size.height, -8.0f, 20.0f);

How can I get a similar set up with glFrustumf?

A: 

Here is a post with a perspective function using glFrustumf and how to convert a glOrthof call to it, perhaps it helps.

Note that you perhaps might have to call glTranslatef afterwards to center the camera.

schnaader
How do I know where to translate to?
That's a good question :) In the post, -eyex/-eyey/-eyez are used where eyeX/Y/Z is the point where the camera is.
schnaader
Still nothing. I'll post my code below, maybe you can take a look..
Did you see the triangles when using glOrthof? Perhaps there's some other problem why you can't see anything. You could also try to rotate around an axis to see where the triangles are which could give you a hint what else you have to do.
schnaader
I did see the triangles in glOrthof. Rotating still gives me just a black screen.
That's weird. You have GL_CULL_FACE enabled, so could it be you view at the triangles from behind? Try to disable it and also try to rotate around the other two axes, too, if you haven't done so already.
schnaader
+1  A: 
    - (void)drawView {

    // Replace the implementation of this method to do your own custom drawing

    const GLfloat squareVertices[] = {
        -5.0f, -5.0f, 0.0f,
        5.0f,  -5.0f, 0.0f,
        -5.0f,  5.0f, 0.0f,
        5.0f,   5.0f, 0.0f,
    };

    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_VERTEX_ARRAY);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, 0.0f);
    //glOrthof(-8.0f, self.frame.size.width, -12.0f, self.frame.size.height, -8.0f, 20.0f);
    glEnable(GL_DEPTH_TEST);
    perspective(85, 480/320, 0.1,20);
    gluLookAt(0.0f, 0.0f, -5.0f,

        1.0f,  1.0f,  1.0f,

        1.0f,  1.0f,  1.0f); 
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glDepthMask(GL_TRUE);
    glEnable(GL_CULL_FACE);
    glClearDepthf(1.0f);
    glColor4f(0.0f, 0.0f, 1.0f, .90f);

    //glTranslatef(2.5f, 2.5f, 0.0f);
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
perhaps this could be an edit on the question? ;)
f3r3nc
A: 

Not sure what was breaking in the projected that I created from Apple's XCode template for an OpenGL ES application, but here is a nifty template that actually works for depth nad perspective:

http://iphonedevelopment.blogspot.com/2008/12/updated-opengl-es-xcode-project.html

Thank you schnaader for all your assistance.

+1  A: 

There is no such thing in OpenGL ES like gluLookAt, assuming this is about iPhone and OpenGL ES. How is that implemented?

Keep in mind that the camera position is (0,0,0) and your square is also on that z plane. If you call glLoadIdentity(), glTranslatef(0.0f, 0.0f, 0.0f) won't do any changes.

Maybe you could consider moving most of the setup part in a setup method. ie: GlEnable calls and camera setup. Make sure the glMatrixMode is used in order to operate on the Projection or the Modelview matrix. Setup could be:

- (void) setupView {
  glViewport(0, 0, backingWidth, backingHeight);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  // think about the box where all the vertices are.
  glFrustum(-8.0f, 8.0f, -12.0f, 12.0f, -8.0f, 20.0f); 
  // .. all sort of glEnable
}

In drawView make sure that your vertices are in front of the camera, by applying some translation on them. Also have colors for you vertices.

- (void) drawView {
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0, 0, 5.0f); // move in front

  glVertexPointer(3, GL_FLOAT, 0, squareVertices);

  // maybe you want to attach colors to your vertices
  const GLubyte squareColors[] = {
            255, 255,   0, 255,
            0,   255, 255, 255,
            0,     0,   0,   0,
            255,   0, 255, 255,
  };
  glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
  glEnableClientState(GL_COLOR_ARRAY);

  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  // .. some other loadidentity and translate and vertex pointer setup and draw
}

The OpenGL ES reference might be also useful: http://www.khronos.org/opengles/sdk/1.1/docs/man/

f3r3nc
A: 

Although gluLookAt is not part of openGlEs, most folks just grab the source and drop it into their project. its not a lot of code. at the very least it'll give a good example of whats going on with glFrustum.

A: 

I've just been messing with projection problems in opengl es with iphone. The parameters for glfrustum in the example code above don't work (-8.0f,20.f), they give invalid parameter errors. zNear seems to 'need' to be positive.

Just wanted to share the knowledge, since I'd been banging my head trying to work out why changing those parameters had no effect whatsoever on the render!