I am trying to adapt this tutorial about shaders to work in my game and later I am going to mess with glsl to get the desired effect I want.
I created a Cocos2d 2.x project with Box2D. The Box2D template gives me a PhysicsSprite class which is why I don’t use CCSprite. Here is my init method:
-(id) init
{
if( (self=[super init])) {
s = [[CCDirector sharedDirector] winSize];
// enable events
self.isTouchEnabled = YES;
self.isAccelerometerEnabled = YES;
// init physics
[self initPhysics];
//Init shader effects.
[self initShaderEffects];
ball = [PhysicsSprite spriteWithFile:@"Ball.png"];
ball.position = ccp(s.width/2, s.height/2);
b_body = [self createCirBody:10 andSpr:ball];
[ball setPhysicsBody:b_body];
[self addChild:ball];
b_body->SetLinearVelocity(b2Vec2(1.5f,0));
[self scheduleUpdate];
}
return self;
}
If you exclude the [self initShaderEffects]; line I have tested the code, it works so far, I get a moving ball. Here is my initShaderEffects (which is essentially the same as the tutorials except I use ball instead of sprite and I changed the fragmentSource initialization to use a non-deprecated method):
-(void)initShaderEffects {
const GLchar *fragmentSource = (GLchar*)[[NSString stringWithContentsOfFile:@"MyCustomShader.fsh" encoding:NSUTF8StringEncoding error:nil] UTF8String];
ball.shaderProgram = [[CCGLProgram alloc] initWithVertexShaderByteArray:ccPositionTextureA8Color_vert fragmentShaderByteArray:fragmentSource];
[ball.shaderProgram addAttribute:kCCAttributeNamePosition index:kCCVertexAttrib_Position];
[ball.shaderProgram addAttribute:kCCAttributeNameTexCoord index:kCCVertexAttrib_TexCoords];
[ball.shaderProgram link];
[ball.shaderProgram updateUniforms];
colorRampUniformLocation = glGetUniformLocation(ball.shaderProgram->program_, "u_colorRampTexture"); //EXC_BAD_ACCESS
glUniform1i(colorRampUniformLocation, 1);
colorRampTexture = [[CCTextureCache sharedTextureCache] addImage:@"x2.png"];
[colorRampTexture setAliasTexParameters];
[ball.shaderProgram use];
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, [colorRampTexture name]);
glActiveTexture(GL_TEXTURE0);
}
And lastly here is my shader “MyCustomShader.fsh” copied straight from the tutorial:
#ifdef GL_ES
precision mediump float;
#endif
// 1
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform sampler2D u_colorRampTexture;
void main()
{ // 2
vec3 normalColor = texture2D(u_texture, v_texCoord).rgb;
// 3
float rampedR = texture2D(u_colorRampTexture, vec2(normalColor.r, 0)).r;
float rampedG = texture2D(u_colorRampTexture, vec2(normalColor.g, 0)).g;
float rampedB = texture2D(u_colorRampTexture, vec2(normalColor.b, 0)).b;
// 4
gl_FragColor = vec4(rampedR, rampedG, rampedB, 1);
}
This code gives me EXC_BAD_ACCESS on the line I have comment EXC_BAD_ACCESS on in my initShaderEffects method. I find shaders difficult and it would be much appreciated if someone could tell me where am I going wrong.
Please ensure that “MyCustomShader.fsh” is added to “Copy Boundle Resources” section in “Project target” => “Build phases”. If it doesn’t help check values of fragmentSource and ball.shaderProgram in debugger.
I found another issue in your code: initShaderEffects executed before “ball” creation. You should move line with “[self initShaderEffects];” after line “[self addChild:ball];”.