2

我正在使用 OpenGL ES 2.0 和 GLKit 编写 2D 游戏。我的架构基于 Ian Terrel 的 Games 教程。

我最近发现 GLKBaseEffect(提供简单的着色器 mgmt)泄漏并且有时使我的应用程序崩溃。我现在正在使用我自己的着色器文件(基于 Ray Wenderlich 教程),但此时,我刚刚成功显示了 openGl 背景颜色。我的形状颜色和纹理不再显示。

重要提示:我错误地设置了当前上下文,现在 openGL 说无效的 Drawable。

这是我的 shape.m 代码的一部分:

@implementation P3Shape

const GLushort indices[] = {
    0,1,2,3
};

typedef struct {
    float Position[2];
    float Color[4];
    float TexCoord[2];
} Vertex;

//testing
const Vertex Vertices[] = {
    // Front
    {{10, -15}, {1, 0, 0, 1}, {1, 0}},
    {{10, 15}, {0, 1, 0, 1}, {1, 0}},
    {{-10, 10}, {0, 0, 1, 1}, {0, 0}},
    {{-10, -1}, {0, 0, 0, 1}, {0, 1}},
};

- (id)initWithLayer:(CAEAGLLayer *)layer {
    self = [super init];
    if (self) {
        [...]
        _currentContext = [P3SessionState currentContext];
        _eaglLayer = layer;
        _eaglLayer.opaque = YES;
        [self setupRenderBuffer];        
        [self setupFrameBuffer]; 
        [self addToProgram];
        [self createBuffers];
    }
    return self;
}


- (int)numVertices {
    return 0;
}

- (void) addToProgram {
    // setted before in the gamemanager
    GLuint programHandle = [P3SessionState programHandle];

    _positionSlot = glGetAttribLocation(programHandle, "Position");
    _colorSlot = glGetAttribLocation(programHandle, "SourceColor");

    _projectionUniform = glGetUniformLocation(programHandle, "Projection");
    _modelViewUniform = glGetUniformLocation(programHandle, "Modelview");

    _texCoordSlot = glGetAttribLocation(programHandle, "TexCoordIn");
    _textureUniform = glGetUniformLocation(programHandle, "Texture");
}

- (void)setupRenderBuffer {
    glGenRenderbuffers(1, &_colorRenderBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer);        
    [_currentContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];    
}

- (void)setupFrameBuffer {    
    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);   
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
}

- (void)createBuffers {
    // Static index data
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}

// initialize the vertex data

- (GLKVector2 *)vertices {
    if (vertexData == nil) {
        vertexData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.numVertices];
    }
    return [vertexData mutableBytes];
}

// set a textureImage, allocate a glname and enable the GL_TEXTURE_2D and all
- (void)setTextureImage:(UIImage *)image {
    CGImageRef spriteImage = image.CGImage;
    if (!spriteImage) {
        NSLog(@"Failed to load image %@", image);
        exit(1);
    }

    size_t width = CGImageGetWidth(spriteImage);
    size_t height = CGImageGetHeight(spriteImage);

    GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));

    CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);    

    CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);

    CGContextRelease(spriteContext);

    GLuint texName;
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

    free(spriteData);        
    _imageTexture = texName;
}    

- (GLKVector2 *)textureCoordinates {
    if (textureCoordinateData == nil)
        textureCoordinateData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.numVertices];
    return [textureCoordinateData mutableBytes];
}


- (void)render {

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUniformMatrix4fv(_modelViewUniform, 1, 0, modelview.m);
    glUniformMatrix4fv(_projectionUniform, 1, 0, projectionMatrix.m);

    glEnableVertexAttribArray(_positionSlot);

    glEnableVertexAttribArray(_colorSlot);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

    glVertexAttribPointer(_positionSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
    if(texture != nil) {
        glEnableVertexAttribArray(_texCoordSlot);
        glActiveTexture(GL_TEXTURE0); // unneccc in practice
        glBindTexture(GL_TEXTURE_2D, _imageTexture);
        glUniform1i(_textureUniform, 0); // unnecc in practice
        glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));
    }

        glDrawElements(GL_TRIANGLE_FAN, sizeof(indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, (void*)0);
    }
    //is cleanup code that tells the shader that we’re done using vertex position data.
    glDisableVertexAttribArray(GLKVertexAttribPosition);

    if (texture != nil)
        glDisableVertexAttribArray(GLKVertexAttribTexCoord0);

    [_currentContext presentRenderbuffer:GL_RENDERBUFFER];

    glDisable(GL_BLEND);
}

// draw the shape in the selected scene
- (void)renderInScene:(P3Scene *)theScene {

    projectionMatrix = [theScene projectionMatrix];

    modelview = GLKMatrix4Multiply(GLKMatrix4MakeTranslation(0, 0, 0), GLKMatrix4MakeRotation(0, 0, 0, 1));
    modelview = GLKMatrix4Multiply(modelview, GLKMatrix4MakeScale(scale.x, scale.y, 1));

    [self render];
}

-(void)update:(NSTimeInterval)dt {
    if(self.toDraw)
        [spriteAnimation update:dt];
}

@end

如您所见,我正在使用 Vertices 数组和基本颜色进行测试,但没有区别。OpenGL 似乎设置正确,因为glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);我的视图是绿色的 在渲染函数中。

这里是 pastebin 上的文件:

粘贴箱上的 P3Shape.m

以及创建 ogl 程序的经理

pastebin 上的 P3GameManager.m

有任何想法吗 ?

编辑:也许我的着色器有问题?

顶点:

attribute vec4 Position; 
attribute vec4 SourceColor; 

varying vec4 DestinationColor; 

uniform mat4 Projection;
uniform mat4 Modelview;

attribute vec2 TexCoordIn; // New
varying vec2 TexCoordOut; // New

void main(void) { 
    DestinationColor = SourceColor; 
    gl_Position = Projection * Modelview * Position;
    TexCoordOut = TexCoordIn; // New
}

和片段:

varying lowp vec4 DestinationColor;

varying lowp vec2 TexCoordOut; // New
uniform sampler2D Texture; // New

void main(void) {
    gl_FragColor = DestinationColor * texture2D(Texture, TexCoordOut); // New
}
4

1 回答 1

1

我推荐使用 GLKViewController 和 GLKView 来简化你的设置。同样,将纹理加载替换为 GLKTextureLoader。

我没有看到 GLKBaseEffect 泄漏内存并导致崩溃的任何问题。我建议发布一个关于这个特定问题的新问题,如果您不需要自定义着色器(看起来您不需要),则返回使用 GLKBaseEffect。将使您的代码更易于管理和调试。

于 2012-09-11T23:04:21.597 回答