1

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

// 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];

}

更新

我将 draw sun 方法修改为只有 1 次推送和 1 次弹出GL_PROJECTION。我仍然有同样的问题。太阳图像出现,但我看不到我的 3D 对象。我试图重新安排对渲染方法的调用,以便首先渲染 3D 对象,但我得到了相同的结果。我将不胜感激有关如何同时查看我的正交纹理和 3D 对象的其他想法。

// 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,0,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);
4

1 回答 1

0

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

于 2012-02-04T20:57:52.277 回答