问题
Seems that everyone agrees that this is broken and and you need to get rid of GLKBaseEffect to stop it leaking. But no one mentions what you'd replace it with.
Can someone can point me in the right direction? Some sample code or a tutorial would be amazingly useful!
I'm doing very basic stuff, just drawing 2D sprites. Works great apart from all the leaks :p
I just need to know what prepareToDraw is doing and replace it with some code that works. All the tutorials I've found seem to focus on 3D rendering...
Could I use OpenGL ES1 instead of 2 perhaps?
//---Sprite drawing code ----------------------
effect.transform.modelviewMatrix = viewMatrix;
effect.texture2d0.name = textureInfo.name;
effect.texture2d0.envMode = GLKTextureEnvModeReplace;
// LEAK Here
[effect prepareToDraw];
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, textureVerts);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
glDisable(GL_BLEND);
回答1:
So... in the end I did what other people had to do and wrote my own version of GLKBaseEffect.
It's fiddly but it's not THAT hard, took about a day. There are tutorials out there but none offered exactly what I needed. In the end I used the OGL default project as my starting point as that gave me the framework - compiling and running shaders and all that, even though it wasn't doing what I wanted.
Here is the gist of what I did. It took a lot of iterations to get working...
- Remove all GLKBaseEffect from the default OGL project (one cube is drawn with GLKBaseEffect but the other is with shaders)
- Change projection Matrix to be GLKMatrix4MakeOrtho.
- Change vertices from Cube to 2D. Get it working.
- Add textures.
- Get it all working.
- Move all OGL code into an OpenGLHelper Class.
- Move code into my project and replace all GLKEffects with my new class
The end result may not be the best code, but it works, and there are no leaks any more.
Here are my shaders
// FRAGMENT SHADER //////////////
precision mediump float;
varying lowp vec2 texCoordOut;
uniform sampler2D textureUniform;
uniform float alphaUniform;
uniform lowp vec4 colorUniform;
void main()
{
vec4 color = colorUniform * texture2D(textureUniform, texCoordOut); ;
gl_FragColor = color;
}
// VERTEX SHADER ////////////////////
attribute vec4 position;
uniform mat4 projection;
uniform mat4 modelView;
attribute vec2 texCoordIn;
varying vec2 texCoordOut;
void main()
{
gl_Position = projection * modelView * position;
texCoordOut = texCoordIn;
}
I'd happily share the rest of the code but there's a bit much of it to put in here!
回答2:
Not directly related to your question, but I got some similar issue. The tough part was to figure out what was leaking:
You seem to be using a texture. You must call glDeleteTextures
otherwise each update of your textureInfo
will leak.
See also this related post : Release textures (GLKTextureInfo objects) allocated by GLKTextureLoader
来源:https://stackoverflow.com/questions/11112517/glkbaseeffect-preparetodraw-is-leaking