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?