1

OpenGLを使用して深度バッファでコンテキストを作成しようとしていますCore OpenGl。次に、OpenGL コンテンツをCAOpenGLLayer. 私が読んだことから、次の方法で目的のコンテキストを作成できるはずです。

インターフェイスで次のインスタンス変数を宣言します

@interface TorusCAOpenGLLayer : CAOpenGLLayer
{
    //omitted code
    CGLPixelFormatObj pix;
    GLint pixn;
    CGLContextObj ctx;
} 

次に、実装でオーバーライドしますcopyCGLContextForPixelFormat。これにより、必要なコンテキストが作成されるはずです

- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
{
    CGLPixelFormatAttribute attrs[] = 
    {
        kCGLPFAColorSize,     (CGLPixelFormatAttribute)24,
        kCGLPFAAlphaSize,     (CGLPixelFormatAttribute)8,
        kCGLPFADepthSize,     (CGLPixelFormatAttribute)24,
        (CGLPixelFormatAttribute)0
    };

    NSLog(@"Pixel format error:%d", CGLChoosePixelFormat(attrs, &pix, &pixn)); //returns 0

    NSLog(@"Context error: %d", CGLCreateContext(pix, NULL, &ctx)); //returns 0

    NSLog(@"The context:%p", ctx); // returns same memory address as similar NSLog call in function below

   return ctx;
}

最後にdrawInCGLContext、コンテンツを表示するようにオーバーライドします。

-(void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:    (CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
{
    // Set the current context to the one given to us.
    CGLSetCurrentContext(glContext);

    int depth;

    NSLog(@"The context again:%p", glContext); //returns the same memory address as the NSLog in the previous function

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glOrtho(0.5, 0.5, 1.0, 1.0, -1.0, 1.0);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnable(GL_DEPTH_TEST);

    glGetIntegerv(GL_DEPTH_BITS, &depth);
    NSLog(@"%i bits depth", depth); // returns 0

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //drawing code here

    // Call super to finalize the drawing. By default all it does is call glFlush().
    [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:timeInterval displayTime:timeStamp];
}

プログラムは正常にコンパイルされ、コンテンツが表示されますが、深度テストは行われません。これを機能させるために何か追加しなければならないことはありますか? それとも私のアプローチ全体が間違っていますか?

4

1 に答える 1

1

間違ったメソッドをオーバーライドしていたようです。必要な深度バッファを取得するには、次のcopyCGLPixelFormatForDisplayMaskようにオーバーライドする必要があります。

- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
    CGLPixelFormatAttribute attributes[] =
    {
        kCGLPFADepthSize, 24,
        0
    };
    CGLPixelFormatObj pixelFormatObj = NULL;
    GLint numPixelFormats = 0;
    CGLChoosePixelFormat(attributes, &pixelFormatObj, &numPixelFormats);
    if(pixelFormatObj == NULL)
        NSLog(@"Error: Could not choose pixel format!");
return pixelFormatObj;
}

ここのコードに基づいています。

于 2012-11-09T04:55:22.293 に答える