Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:40:45+00:00 2026-06-13T11:40:45+00:00

I am trying to adapt this tutorial about shaders to work in my game

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:40:47+00:00Added an answer on June 13, 2026 at 11:40 am

    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];”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to adapt this Dropdown menu on Joomla the styles work great
I am trying to adapt a tutorial about soap web requests. In the tutorial,
I'm trying to adapt THIS question to work with a retina display. Here's how
I'm trying to adapt this DES encrypting example to AES, so I made the
I am trying to adapt some (to me) very complicated code to work with
I'm trying to adapt this python code I found for connecting to the Dropbox
I'm trying to adapt Andy Langton's show/hide/mini-accordion ( http://andylangton.co.uk/jquery-show-hide ) to work within a
I'm trying to do something like in this Tutorial , a very basic gallery.
I'm trying to adapt this solution to my needs: How can I create a
I'm trying to get some code going that lets me display raw trackpad data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.