hi! i try to use my custom shaders in my cocos2d-x helloworld project following this tutorial: http://www.raywenderlich.com/10862/how-to-create-cool-effects-with-custom-shaders-in-opengl-es-2-0-and-cocos2d-2-x
i added Light.h and Light.cpp files to my project.
this is my header file:
//
// Light.h
// NewLightTest
//
// Created by roko on 20.08.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#ifndef NewLightTest_Light_h
#define NewLightTest_Light_h
#include "cocos2d.h"
using namespace cocos2d;
class Light : public CCLayer
{
CCSprite *sprite; //1
int colorRampUniformLocation; //2
CCTexture2D *colorRampTexture; //3
void changeImage();
void goMenu();
public:
virtual ~Light();
Light();
bool init();
LAYER_CREATE_FUNC(Light);
};
#endif
and this is source file:
//
// Light.cpp
// NewLightTest
//
// Created by roko on 20.08.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include "Light.h"
Light::~Light()
{
}
Light::Light()
{
}
bool Light::init()
{
if (CCLayer::init()) {
sprite = CCSprite::create("Default.png");
sprite->setAnchorPoint(CCPointZero);
sprite->setRotation(90);
sprite->setPosition(ccp(0, 320));
this->addChild(sprite);
CCGLProgram *shaderProgram_ = new CCGLProgram();
this->setShaderProgram(shaderProgram_);
shaderProgram_->initWithVertexShaderFilename("PositionColor.vsh", "PositionColor.fsh");
shaderProgram_->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
shaderProgram_->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
shaderProgram_->link();
shaderProgram_->updateUniforms();
// 3
colorRampUniformLocation = glGetUniformLocation(sprite->getShaderProgram()->getProgram(), "u_colorRampTexture");
glUniform1i(colorRampUniformLocation, 1);
// 4
colorRampTexture = CCTextureCache::sharedTextureCache()->addImage("colorRamp.png");
colorRampTexture->setAliasTexParameters();
// 5
sprite->getShaderProgram()->use();
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, colorRampTexture->getName());
glActiveTexture(GL_TEXTURE0);
return true;
}
return false;
}
then i added shaders into resources and in build phases.
vertex shader PositionColor.vsh
//1
attribute vec4 a_position;
attribute vec2 a_texCoord;
//2
uniform mat4 u_MVPMatrix;
//3
#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif
//4
void main()
{
//5
gl_Position = u_MVPMatrix * a_position;
//6
v_texCoord = a_texCoord;
}
and fragment shader PositionColor.fsh
#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);
}
and in HelloWorldScene.cpp file i have added this line
this->addChild(Light::create(), 1);
but it only show me “Default.png”. It seems like my custom shaders has no any affect.
what should i do to make my shaders work?