我正在使用OpenGL ES 2.0和GLKit编写2D游戏。我的架构基于Ian Terrel的游戏教程。
我最近发现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上的文件:
创建ogl程序的经理
有什么想法吗?
编辑:我的着色器可能有问题吗?顶点:
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
}
答案 0 :(得分:1)
我建议使用GLKViewController和GLKView来简化您的设置。同样,用GLKTextureLoader替换纹理加载。
我没有看到GLKBaseEffect泄漏内存并导致崩溃的任何问题。我建议发布一个关于这个特定问题的新问题,如果你不需要自定义着色器(看起来你没有),则返回使用GLKBaseEffect。将使您的代码更易于管理和调试。