gpt4 book ai didi

iphone - OpenGl ES 2.0 和 GLKit : From GLKBaseEffect shaders to OpenGl

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:21 25 4
gpt4 key购买 nike

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

我最近发现 GLKBaseEffect(提供简单的着色器管理)泄漏,有时会使我的应用程序崩溃。我现在正在使用我自己的着色器文件(基于 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

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

这里是 pastebin 上的文件:

P3Shape.m on pastebin

以及创建 ogl 程序的经理

P3GameManager.m on pastebin

有什么想法吗?

编辑:我的着色器可能有问题?

顶点:

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
}

最佳答案

我建议使用 GLKViewController 和 GLKView 来简化您的设置。同样,用 GLKTextureLoader 替换您的纹理加载。

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

关于iphone - OpenGl ES 2.0 和 GLKit : From GLKBaseEffect shaders to OpenGl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12117772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com