2

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);
David John
  • 496
  • 1
  • 5
  • 17

2 Answers2

2

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...

  1. Remove all GLKBaseEffect from the default OGL project (one cube is drawn with GLKBaseEffect but the other is with shaders)
  2. Change projection Matrix to be GLKMatrix4MakeOrtho.
  3. Change vertices from Cube to 2D. Get it working.
  4. Add textures.
  5. Get it all working.
  6. Move all OGL code into an OpenGLHelper Class.
  7. 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!

David John
  • 496
  • 1
  • 5
  • 17
  • hello David, I'd like to follow the path you've taken, but it seems to me that the XCode's OpenGL default project uses `GLKBaseEffect` as well. Am I missing something? Are you willing to share your sources? – Jonas Sourlier Aug 29 '12 at 12:59
  • sorry, just saw your comment that one of the cubes in the OGL default project is rendered without `GLKBaseEffect`. Thank you! – Jonas Sourlier Aug 29 '12 at 13:27
  • Hope that worked out for you. I'd be happy to share more of my code if it helps though I know it isn't perfect! – David John Sep 22 '12 at 18:27
  • @DavidJohn: Could you upload your code to GitHub? That'd be really helpful. – Ricardo Sanchez-Saez Jan 12 '15 at 14:36
1

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

Community
  • 1
  • 1
lazi74
  • 78
  • 7
  • Ok. In my case, Instruments was showing the leak there. No worries since you got it working with shaders and GL 2, then all the better. – lazi74 Jul 25 '12 at 09:58