views:

725

answers:

1

I'm new to OpenGL, so I'm sure this is a dummy mistake, but I've read every post, and reviewed sample code, and I can't find a difference, explaining why glFrustum wont display as I'd like it to.

I initialize OpenGL like:

- (void) initOpenGL{
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    //glOrthof(0.0f, self.bounds.size.width, self.bounds.size.height, 0.0f, -10.0f, 10.0f);

    const GLfloat zNear = -0.1, zFar = 1000.0, fieldOfView = 60.0;
    GLfloat size;
    size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);

    // This give us the size of the iPhone display
    CGRect rect = self.bounds;
    glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size / (rect.size.width / rect.size.height), zNear, zFar);
    glViewport(0, 0, rect.size.width, rect.size.height);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

#if 0
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
#endif

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);

#if TARGET_IPHONE_SIMULATOR
    glColor4f(0.0, 0.0, 0.0, 0.0f);
#else
    glColor4f(0.0, 0.0, 0.0, 0.0f);
#endif

    [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"GreenLineTex.png"] filter:GL_LINEAR];

    glInitialised = YES;

And my drawing is done like:

- (void)drawView {
    if(!glInitialised) {
     [self initOpenGL];
    }

    [EAGLContext setCurrentContext:context];

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);     
    glEnable(GL_TEXTURE_2D);
    static const GLfloat texCoords[] = {
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0
    };

    // draw the edges
    glEnableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);

    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glBindTexture(GL_TEXTURE_2D, 1);  
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);

    for (int i = 0; i < connectionNumber; i++){

     Vertex2DSet(&vertices[0], connectionLines[i].lineVertexBeginPoint.x, connectionLines[i].lineVertexBeginPoint.y);
     Vertex2DSet(&vertices[1], connectionLines[i].lineVertexBeginPoint.x+connectionLines[i].normalVector.x, connectionLines[i].lineVertexBeginPoint.y+connectionLines[i].normalVector.y);
     Vertex2DSet(&vertices[2], connectionLines[i].lineVertexEndPoint.x, connectionLines[i].lineVertexEndPoint.y);
     Vertex2DSet(&vertices[3], connectionLines[i].lineVertexEndPoint.x+connectionLines[i].normalVector.x, connectionLines[i].lineVertexEndPoint.y+connectionLines[i].normalVector.y);

     glVertexPointer(2, GL_FLOAT, 0, vertices);
     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    }

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

Where the block in the for loop is a set of vertices that make up some triangle strips.

If I uncomment the glOrthof() line, then I can see my display, however it's orthographic, and I'd like to move the camera in and out, to change the scaling of the whole scene.

What have I done incorrectly that causes glFrustumf() to display only the clear color?

+2  A: 

Short answer: you are looking in the wrong direction.

Long answer: Your frustum is symmetric while your orthographic matrix isn't. So if your model is set up to be visible in the glOrtho case, it may not be visible with your glFrustum. Also you shouldn't use glOrtho AND glFrustum together, because the matrices are multiplied and will surely yield a funny projection matrix.

You can use Nate Robins' GL tutors at http://www.xmission.com/~nate/tutors.html to experiment with glFrustum and glOrtho (in the "projection" application).

nschmidt