Need help with basic shader

I tried to do the basic shader by reading this tutorial (http://www.raywenderlich.com/10862/how-to-create-cool-effects-with-custom-shaders-in-opengl-es-2-0-and-cocos2d-2-x)
Yet i’m unable to run the project.

I created a CSEColorRamp.fsh file like this

#ifdef GL_ES
precision mediump float;
#endif

varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform sampler2D u_colorRampTexture;

void main()
{ vec3 normalColor = texture2D(u_texture, v_texCoord).rgb;

  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;

  gl_FragColor = vec4(rampedR, rampedG, rampedB, 1);
}

I added CSEColorRamp.fsh file into resources.
and then i created sprite and loaded CSEColorRamp shader like this

CCSprite *sprite = CCSprite::spriteWithFile("HelloWorld.png");


CCFileUtils *fileUtils = CCFileUtils::sharedFileUtils();
const char* fragmentSource = fileUtils->fullPathFromRelativePath("CSEColorRamp.fsh");

CCGLProgram *glProgram = new CCGLProgram();
glProgram->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, fragmentSource);

sprite->setShaderProgram(glProgram);

sprite->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sprite->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
sprite->getShaderProgram()->link();
sprite->getShaderProgram()->updateUniforms();

int colorRampUniformLocation = glGetUniformLocation(sprite->getShaderProgram()->getProgram(),"u_colorRampTexture");
glUniform1i(colorRampUniformLocation, 1);

CCTexture2D *colorRampTexture = CCTextureCache::sharedTextureCache()->addImage("colorRamp.png");
colorRampTexture->setAliasTexParameters();

sprite->getShaderProgram()->use();

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, colorRampTexture->getName());
glActiveTexture(GL_TEXTURE0);

this->addChild(sprite, 100);

but it is crashing at call to “initWithVertexShaderByteArray”.
I checked weather “fragmentSource” holds correct location for the .fsh file and it did have correct location.

in output window this is comming

First-chance exception at 0x62ae8f20 in TankMap.win32.exe: 0xC0000005: Access violation.
Unhandled exception at 0x62ae8f20 in TankMap.win32.exe: 0xC0000005: Access violation.

and the call stack is

>   libcocos2d.dll!cocos2d::CCGLProgram::logForOpenGLObject(unsigned int object, void (unsigned int, unsigned int, int *)* infoFunc, void (unsigned int, int, int *, char *)* logFunc)  Line 215 + 0x12 bytes   C++
    libcocos2d.dll!cocos2d::CCGLProgram::fragmentShaderLog()  Line 236  C++
    libcocos2d.dll!cocos2d::CCGLProgram::compileShader(unsigned int * shader, unsigned int type, const char * source)  Line 150 + 0x8 bytes C++
    libcocos2d.dll!cocos2d::CCGLProgram::initWithVertexShaderByteArray(const char * vShaderByteArray, const char * fShaderByteArray)  Line 97 + 0x18 bytes  C++
    TankMap.win32.exe!GameLayer::init()  Line 26 + 0x18 bytes   C++
    TankMap.win32.exe!GameLayer::node()  Line 39 + 0xa7 bytes   C++
    TankMap.win32.exe!HelloWorld::init()  Line 47 + 0x5 bytes   C++
    TankMap.win32.exe!HelloWorld::create()  Line 22 + 0xb1 bytes    C++
    TankMap.win32.exe!HelloWorld::scene()  Line 19 + 0x5 bytes  C++
    TankMap.win32.exe!AppDelegate::applicationDidFinishLaunching()  Line 42 + 0x5 bytes C++
    libcocos2d.dll!cocos2d::CCApplication::run()  Line 46 + 0xf bytes   C++
    TankMap.win32.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow)  Line 36 + 0x19 bytes   C++
    TankMap.win32.exe!__tmainCRTStartup()  Line 547 + 0x2c bytes    C
    TankMap.win32.exe!wWinMainCRTStartup()  Line 371    C

I googled all day and every link i come across is for iphone and in cocos2d not cocos2d-x. They say that shader has to added in build path or something in xcode. But how to do that in Visual studios?
Is there any tutorial on how to compile shaders in visual studios using cocos2d-x?

:frowning: I met the same problem……
it seems fail to compile fragment Shader,
but before the error log is output, it crashed….

who can give any advice?
Thanks a lot~

yes same for me … i crashes while compiling fragment shader.
i kept a break point at the line

infoFunc(object, GL_INFO_LOG_LENGTH, &logLength);

the infoFunc has a value assigned to it. but when i press continue infoFunc became 0xcccccc and that is where it is crashing.

Any one please help.

Have you solved the problem @Plato

Ok Solved the problem…
Made vertex shader file also custom and loaded from Resources

glProgram->initWithVertexShaderFilename(“CSEColorRamp.vsh”, “CSEColorRamp.fsh”);

That solved the loading issue…
Cheers

great! I solved the problem in xcode according your way

@ Viraj Dasondi
can you show me your vertex shader file?

@Plato Manchi

This is what my working vertex shader contains

attribute vec4 a_position;
attribute vec2 a_texCoord;

#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif

void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_texCoord = a_texCoord;
}

Make sure for cocos2d-x 2.1 you replace u_MVPMatrix the use of with CC_MVPMatrix in you Vertex shader.

http://www.cocos2d-x.org/boards/6/topics/19548?r=19598#message-19598

I have attached that file…

i tried both shaders files and still i couldn’t get it working

i created a new project and just added this code into the helloworld.cpp

CCGLProgram *glProgram = new CCGLProgram();
glProgram->initWithVertexShaderFilename("CSEColorRamp.vsh", "CSEColorRamp.fsh");

pSprite->setShaderProgram(glProgram);
pSprite->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
pSprite->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);

pSprite->getShaderProgram()->link();
pSprite->getShaderProgram()->updateUniforms();
int colorRampUniformLocation = glGetUniformLocation(pSprite->getShaderProgram()->getProgram(),"u_colorRampTexture");
glUniform1i(colorRampUniformLocation, 1);

CCTexture2D *colorRampTexture = CCTextureCache::sharedTextureCache()->addImage("colorRamp.png");
colorRampTexture->setAliasTexParameters();
pSprite->getShaderProgram()->use();
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, colorRampTexture->getName());
glActiveTexture(GL_TEXTURE0);
// Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSprite, 0);

this is the CSEColorRamp.vsh file

attribute vec4 a_position;
attribute vec2 a_texCoord;

#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif

void main() {
gl_Position = u_MVPMatrix * a_position;
v_texCoord = a_texCoord;
}

and this is CSEColorRamp.fsh

#ifdef GL_ES
precision mediump float;
#endif

varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform sampler2D u_colorRampTexture;

void main()
{ vec3 normalColor = texture2D(u_texture, v_texCoord).rgb;

  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;

  gl_FragColor = vec4(rampedR, rampedG, rampedB, 1);
}

and i used the same colorRamp.png that the link gave (http://cdn5.raywenderlich.com/wp-content/uploads/2012/03/colorRamp.png)

this it the error i’m getting

and i’m using cocos2dx 2.0.1 version

sorry i got it :slight_smile:
because of notepad*+ the .fsh file is saved in “UCS-2 Big Endian” encoding so cocos2d couldn’t read the .fsh file : CCString::createWithContentsOfFile~~>fullPathFromRelativePath)~~>getCString;
</pre>
and the fragmentSource have some stupid value stored inside and then i checked the encoding in notepad*+
changed the encoding to ANSI
finally got it :slight_smile:

thanks :slight_smile: