开发者

openGL ES how to get perspective view?

开发者 https://www.devze.com 2023-02-22 07:49 出处:网络
I\'m trying to draw a simple cube, rotated 45 deg about y-axis. My drawing code looks like this: - (void) Draw:

I'm trying to draw a simple cube, rotated 45 deg about y-axis. My drawing code looks like this:

- (void) Draw:
{
    // Set the viewport
    //glFrustumf( -1.0, 1.0, -1.0/(backingWidth/backingHeight), 1.0/(backingWidth/backingHeight), 0.01, 10.0 );
    glViewport ( 0, 0, backingWidth, backingHeight );

    // Clear the color buffer
    glClear ( GL_COLOR_BUFFER_BIT );

    if( [context API] == kEAGLRenderingAPIOpenGLES2 )
        [self DrawES2];
    else 
        [self DrawES1];

    glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indices);
}

- (void) DrawES2
{   
    // Use the program object
    glUseProgram ( programObject );

    // Load the vertex data
    glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
    glEnableVertexAttribArray ( 0 );

    // Load transformation matrix
    GLint mvpLoc = glGetUniformLocation( programObject, "u_mvpMatrix" );
    glUniformMatrix4fv( mvpLoc, 1, GL_FALSE, yRotation );
}

- (void) DrawES1
{   
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glRotatef( 45, 0, 1, 开发者_开发百科0 );

    glVertexPointer(3, GL_FLOAT, 0, vVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColor4f(1.0, 0.0, 0.0, 1.0);
}

What I get is something like this: enter link description here

What I want is something like this: enter link description here

When I uncomment the glFrustum line I get no change in ES1 version and app crashes in ES2 version. I'm new to OpenGL so I guess I could be doing something really wrong, but how do I get a perspective view so that the cube looks right?


Just copy the logic of gluPerspective().

0

精彩评论

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