glOrtho not working?

Started by
6 comments, last by blueshogun96 11 years, 2 months ago

I started writing a totally new OpenGL game for iOS a few days ago, and I cannot get 2D working to save my life. 3D works fine (using a customized implementation of gluPerspective), but 2D just won't. I've done a 2D game for iOS before, and I didn't have any problems. Now, no matter what I do, nothing renders.

My code:

Initialization:


// 2. Create a Context
    EAGLContext *context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1] autorelease];
    [EAGLContext setCurrentContext:context];
    
    // 3. Create a View
    GLView *glView = [[[GLView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
    [self.view addSubview:glView];
    
    // 4. Create a Renderbuffer
    GLuint renderbuffer;
    glGenRenderbuffers(1, &renderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)glView.layer];
    
    // 5. Create a Framebuffer
    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, 320, 480 );
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
    
    GLuint depthBuffer;
    glGenRenderbuffers( 1, &depthBuffer );
    glBindRenderbuffer( GL_RENDERBUFFER, depthBuffer );
    glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 320, 480 );
    glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer );
    
    glBindRenderbuffer( GL_RENDERBUFFER, renderbuffer );
    
    GLenum status = glCheckFramebufferStatus( GL_FRAMEBUFFER );
    if( status != GL_FRAMEBUFFER_COMPLETE )
    {
        NSLog( @"Error completing FBO! %x", status );
    }
    
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glClearDepthf( 1.0f );
    
    glDisable( GL_CULL_FACE );
    glDisable( GL_LIGHT0 );

Rendering:


-(void) drawView:(id)sender
{
    glViewport( 0, 0, 320, 480 );
    glMatrixMode( GL_PROJECTION );
    glOrthof( 0, 320, 0, 480, -1, 1);
    
    glClearColor( 0.0f, 0.5f, 0.0f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
  
    draw_quad( 0, 50, 50, 300, 300 );
    
    [[EAGLContext currentContext] presentRenderbuffer:GL_RENDERBUFFER];
}

void draw_quad( GLuint texture, float x, float y, float w, float h )
{
	float vertices[] = { x, y, x+w, y, x+w, y+h, x, y+h };
	float tex[] = { 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f };

	glBindTexture( GL_TEXTURE_2D, texture );

	glTexCoordPointer( 2, GL_FLOAT, 0, tex );
	glVertexPointer( 2, GL_FLOAT, sizeof(float)*2, vertices );
	glDrawArrays( GL_TRIANGLE_FAN, 0, 4 );

	spf++;
}

Majority of this code I'm reusing, but I don't see what the problem is. Arrgh, iOS is a complete NIGHTMARE to work with (problems at every corner for me). I hate to say it, but PS2 was more straight forward than iOS. This really sucks. Any ideas? Thanks.

Advertisement

OpenGL/OpenGL ES are the pain, not iOS. Bind-to-modify is a design flaw from the early 90’s that they have refused to fix for compatibility reasons.

Your own headaches are compounded by the fact that you are using OpenGL ES 1.0 instead of OpenGL ES 2.0. Learning how to handle OpenGL’s quirks, such as modifying your lighting system to deal with the fact that they modify the lights you set by the current model-view matrix, and only once (sigh), is a lot harder than learning shaders. No joke.

Anyway, firstly you need to disable lighting. You didn’t call ::glDisable( GL_LIGHTING ).

Secondly, you should be glad you are on iOS because there is a large toolset to help you debug this.

Firstly, only code using a real device. This is a simple fact of life.

Secondly, once you are on a real device, you can take a screenshot and step through to see what is happening.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

OpenGL/OpenGL ES are the pain, not iOS. Bind-to-modify is a design flaw from the early 90’s that they have refused to fix for compatibility reasons.

Your own headaches are compounded by the fact that you are using OpenGL ES 1.0 instead of OpenGL ES 2.0. Learning how to handle OpenGL’s quirks, such as modifying your lighting system to deal with the fact that they modify the lights you set by the current model-view matrix, and only once (sigh), is a lot harder than learning shaders. No joke.

Anyway, firstly you need to disable lighting. You didn’t call ::glDisable( GL_LIGHTING ).

Secondly, you should be glad you are on iOS because there is a large toolset to help you debug this.

Firstly, only code using a real device. This is a simple fact of life.

Secondly, once you are on a real device, you can take a screenshot and step through to see what is happening.

L. Spiro

I beg to differ. I've never had any serious problems out of OpenGL until I started on iOS. I'm using OpenGL ES 1.1 simply because I don't have an OpenGL ES 2.0 compatible iOS device (can't afford one atm). I tried disabling lighting with glDisable( GL_LIGHTING ); still doesn't work. Even when I got 3D working, it didn't make a difference. And whether I use a real device or the simulator, I get the same results. I still don't see what I'm doing wrong here.

The same code on any other device will yield the same results. The problem is your code, not iOS.

And I told you to use a real device for debugging purposes.

I told you to Capture an OpenGL ES Frame.

As in, run the project on a real device, go to Xcode, select Product->Debug->Capture OpenGL ES Frame.

Now you can single-step through every OpenGL ES call, or just the render calls, and see everything about the context and look for what could be wrong.

Look:

frame_capture.png

You should count your blessings that you are on iOS. There is no such feature on Android™.

If you can’t figure it out on your own after doing this, go to the draw call that you think should be drawing a quad and post a screenshot.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Oh, wow, I didn't know you could do that! Thanks, I'll try that right away.

Here's your problem:


    glMatrixMode( GL_PROJECTION );
    glOrthof( 0, 320, 0, 480, -1, 1);

glOrtho doesn't load the generated ortho matrix onto the stack; what it does is take the current matrix and multiplies it by the generated ortho matrix (http://www.khronos.org/opengles/sdk/1.1/docs/man/). So instead you do:


    glMatrixMode( GL_PROJECTION );
    glLoadIdentity ()
    glOrthof( 0, 320, 0, 480, -1, 1);

And it works!

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This functionality isn't supported on my iPod 1st Generation... =(

mhagain, tried it. Still isn't working. =(

This topic is closed to new replies.

Advertisement