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!
#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];
}
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.
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?
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.
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.
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.
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.
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);
}