OpenGL ES如何正确组合Orthof和Frustum

时间:2012-02-03 18:39:29

标签: iphone objective-c cocoa opengl-es opengl-es-1.1

我开始学习用于iPhone的OpenGL ES 1.1,并希望在一些3D对象后面的正投影中绘制2D图像。使用Jeff Lamarche的教程和适用于iPhone的Pro OpenGL ES一书,我想出了以下几种方法来尝试这样做。如果我禁用对drawSunInRect的调用,则3D对象渲染得很好,我可以使用触摸控件等移动它们。如果我取消注释该调用并尝试绘制太阳图像,则图像将显示在{{1}中我提供,但我看不到任何其他3D对象 - 屏幕的其余部分是黑色的。我尝试在不同的地方禁用/启用深度测试,将不同的参数传递给CGRect,然后在矩形周围移动,但只有在调用glOrthof()方法时才会获得太阳图像。我假设它覆盖了我的3D物体。

drawSunInRect

更新

我将绘制太阳方法修改为// 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 }; // Disable DEPTH test and setup Ortho glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrthof(0,frame.size.width,frame.size.height,0,0.1f,1000.0); // Enable blending and configure glEnable(GL_BLEND); glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA); // Enable VERTEX and TEXTURE client states glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable Textures glEnable(GL_TEXTURE_2D); // Projection Matrix Mode for ortho and reset glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); // No lighting or Depth Mask for 2D - no Culling glDisable(GL_LIGHTING); glDepthMask(GL_FALSE); glDisable(GL_CULL_FACE); glDisableClientState(GL_COLOR_ARRAY); // Define ortho glOrthof(0,frame.size.width,frame.size.height,0,0.1f,1000); // From Jeff Lamarche Tutorials // glOrthof(-1.0, // Left // 1.0, // Right // -1.0 / (rect.size.width / rect.size.height), // Bottom // 1.0 / (rect.size.width / rect.size.height), // Top // 0.01, // Near // 10000.0); // Far // Setup Model View Matrix glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); // Bind and draw the Texture 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(); // Re-enable lighting glEnable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); glDisable(GL_DEPTH_TEST); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDepthMask(GL_TRUE); } // 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 Sun - 2D Sun Image in CGRect //[self drawSunInRect:CGRectMake(100, 100, 50, 50) withDepth:-30.0]; // 2D Sun Image // Render the BattleCruiser - 3D Battlecruiser [self renderTheBattleCruiser]; // 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]; } 只有1次推送和1次弹出。我仍然有同样的问题。出现太阳图像,但我看不到我的3D对象。我试图重新安排对渲染方法的调用,以便首先渲染3D对象,但我得到相同的结果。我会很感激如何看到我的正交纹理和3D对象的其他想法。

GL_PROJECTION

1 个答案:

答案 0 :(得分:0)

我意识到我在viewController viewDidLoad中只调用了一次setClipping。我将它移动到我的glkView方法,现在我得到了我的太阳图像和我的3D对象。