Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2012, 08:31 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 6
classyDev is on a distinguished road
Default OpenGL ES glOrthof 2D completely covers Frustrum 3D

I am beginning to learn OpenGL ES 1.1 for iPhone and would like to draw a 2D image in ortho projection behind a few 3D objects. Using Jeff Lamarche's tutorials and the book Pro OpenGL ES for iPhone I've come up with the following couple methods to attempt to do this. If I disable the call to 'drawSunInRect' the 3d objects are rendered just fine and I can move them with touch controls.. etc. If I uncomment that call and try to draw the sun image, the image appears in the CGRect I supply, but I cannot see any of my other 3D objects - the rest of the screen is black. I've tried to disable/enable depth testing in various places, pass different parameters to glOrthof, and move around the rect but I keep getting the sun image only when the drawSunInRect method is called. I'm assuming it is covering my 3D objects. I am clearly not understanding something here and would greatly appreciate any help in figuring out what is wrong. Thanks in advance!

Code:
// Draw Sun in Rect and with Depth
- (void)drawSunInRect:(CGRect)rect withDepth:(float)depth {
    
    // Get screen bounds
    CGRect frame = [[UIScreen mainScreen] bounds];
    
    // Calculate vertices from passed CGRect and depth
    GLfloat	 vertices[] = 
    {
        rect.origin.x, rect.origin.y, depth,
        
        rect.origin.x + rect.size.width , rect.origin.y, depth,
        
        rect.origin.x, rect.size.height+rect.origin.y , depth,
        
        rect.origin.x + rect.size.width , rect.size.height+rect.origin.y ,depth
    };
    
    // Map the texture coords - no repeating
    static  GLfloat textureCoords[] = 
    {				
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0
    };
    
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
    
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D); 
    
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();		
    glLoadIdentity();	   
    
    glDisable(GL_LIGHTING);                                                                                          
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glDisableClientState(GL_COLOR_ARRAY);	
    
    glOrthof(0,frame.size.width,frame.size.height,0,-10,1000);
    
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    
    glColor4f(1,1,1,1);
    
    glBindTexture(GL_TEXTURE_2D,sunInt);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT,0,textureCoords);
    
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glMatrixMode(GL_MODELVIEW); 
    glPopMatrix();
    
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    
    glEnable(GL_LIGHTING);	
    glDisable(GL_TEXTURE_2D);	
    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    
}
Code:
#pragma mark - GLKView and GLKViewController delegate methods
// Override the draw in rect function
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    
    // Initialize and init the rotation stuff for the object
    // Identity Matrix
    glLoadIdentity();
    static GLfloat rot = 0.0;
    
    // Clear any remnants in the drawing buffers
    // and fill the background with black
	glClearColor(0.0f,0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    // Render the BattleCruiser - 3D Battlecruiser (Translated -15 Z units back)
    [self renderTheBattleCruiser];
    
    // Render the Stars Scene - 3D Stars sphere (Translated -15 Z units back)
    [self renderTheStarsScene];

    // Render the Sun  
    [self drawSunInRect:CGRectMake(100, 100, 50, 50) withDepth:-35]; // 2D Sun Image
    //[self drawSunInRect:CGRectMake(150, 150, 150, 150) withDepth:-500.0]; // 2D Sun Image
    
    // Calculate a time interval to use to rotate the cruiser lives
    static NSTimeInterval lastDrawTime;
    if (lastDrawTime)
    {
        NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
        rot += 75 * timeSinceLastDraw;                
    }
    lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
    
}
classyDev is offline   Reply With Quote
Old 02-04-2012, 10:15 AM   #2 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 6
classyDev is on a distinguished road
Default

Any ideas at all ?? When I first launch the app on the Simulator the 3d objects flash briefly and then are covered with a black screen and the small Sun image. Presumably because the ortho is called after the frustrum. I'd really appreciate any input.
classyDev is offline   Reply With Quote
Old 02-04-2012, 10:46 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2011
Age: 25
Posts: 169
mer10 is on a distinguished road
Default

Quote:
Originally Posted by classyDev View Post
Any ideas at all ?? When I first launch the app on the Simulator the 3d objects flash briefly and then are covered with a black screen and the small Sun image. Presumably because the ortho is called after the frustrum. I'd really appreciate any input.
What happens when you try to call drawSunInRect before the 3D drawing?
mer10 is offline   Reply With Quote
Old 02-04-2012, 10:53 AM   #4 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 6
classyDev is on a distinguished road
Default

When I move the drawSun method before the render Battlecruiser calls then there is no quick flicker before the black screen and sun image. The screen is immediately solid black with the sun image.
classyDev is offline   Reply With Quote
Old 02-04-2012, 11:22 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2011
Age: 25
Posts: 169
mer10 is on a distinguished road
Default

Dunno. It's been a while since I've used OpenGl ES 1.1. My guess would be to check the stateful calls and maybe check glError for any clues. You might want to run the analysis tool cause it can tell you some errors like trying to pop the stack too many times or likewise.
mer10 is offline   Reply With Quote
Old 02-04-2012, 12:03 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by classyDev View Post
Any ideas at all ?? When I first launch the app on the Simulator the 3d objects flash briefly and then are covered with a black screen and the small Sun image. Presumably because the ortho is called after the frustrum. I'd really appreciate any input.
Where is the code that sets up your frustrum for your perspective drawing? I see the code that sets up for orthographic drawing, but not the setup for perspective drawing.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-04-2012, 02:56 PM   #7 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 6
classyDev is on a distinguished road
Default

Thanks for the input, this solved my problem. I realized I was calling setClipping only once in my viewController viewDidLoad. I moved it to my glkView method and now I get my Sun image and my 3d object.

Code:
// Set the fulstrum and our field of view for the window
-(void)setClipping
{
    // Near and far are the front and back walls
    // FOV is in degrees
	float aspectRatio;
	const float zNear = .1;					
	const float zFar = 2000;					
	GLfloat	size;
	float scale;
    
    // Get the main screen and define the aspect ratio
	CGRect frame = [[UIScreen mainScreen] bounds];		
	aspectRatio=(float)frame.size.width/(float)frame.size.height;					
	scale=[[UIScreen mainScreen]scale];
    
    // Use the 2D projection matrix to project our 3D into 2D
	glMatrixMode(GL_PROJECTION);				
	glLoadIdentity();
    if (m_FieldOfView > 75.0) {
        m_FieldOfView = 75.0;
    }
    size = zNear * tanf(GLKMathDegreesToRadians (m_FieldOfView) / 2.0);	
    
    // Define the pyramid of Giza (4 sided pyramid with top lopped off on its side)
    // ... this is how were viewing things
	glFrustumf(-size, size, -size/aspectRatio, size/aspectRatio, zNear, zFar);	
	glViewport(0, 0, frame.size.width*scale, frame.size.height*scale);		
	
    // To be safe go back to tranformational matrix
	glMatrixMode(GL_MODELVIEW);				
}
classyDev is offline   Reply With Quote
Reply

Bookmarks

Tags
cocoa, iphone, objective-c, opengl es 1.1

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 395
18 members and 377 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, kukat, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0