2012-03-15 10:42:22 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright 2012 cocos2d-x.org
|
|
|
|
Copyright 2011 Jeff Lamarche
|
|
|
|
Copyright 2012 Goffredo Marocchi
|
|
|
|
Copyright 2012 Ricardo Quesada
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
#ifndef __CCGLPROGRAM_H__
|
|
|
|
#define __CCGLPROGRAM_H__
|
|
|
|
|
|
|
|
#include "ccMacros.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocoa/CCObject.h"
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
#include "CCGL.h"
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup shaders
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
enum {
|
2012-04-19 14:35:52 +08:00
|
|
|
kCCVertexAttrib_Position,
|
|
|
|
kCCVertexAttrib_Color,
|
|
|
|
kCCVertexAttrib_TexCoords,
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kCCVertexAttrib_MAX,
|
2012-03-12 15:22:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2012-11-09 12:08:18 +08:00
|
|
|
kCCUniformPMatrix,
|
|
|
|
kCCUniformMVMatrix,
|
|
|
|
kCCUniformMVPMatrix,
|
|
|
|
kCCUniformTime,
|
|
|
|
kCCUniformSinTime,
|
|
|
|
kCCUniformCosTime,
|
|
|
|
kCCUniformRandom01,
|
|
|
|
kCCUniformSampler,
|
|
|
|
|
|
|
|
kCCUniform_MAX,
|
2012-03-12 15:22:03 +08:00
|
|
|
};
|
|
|
|
|
2012-11-09 12:08:18 +08:00
|
|
|
#define kCCShader_PositionTextureColor "ShaderPositionTextureColor"
|
|
|
|
#define kCCShader_PositionTextureColorAlphaTest "ShaderPositionTextureColorAlphaTest"
|
|
|
|
#define kCCShader_PositionColor "ShaderPositionColor"
|
|
|
|
#define kCCShader_PositionTexture "ShaderPositionTexture"
|
|
|
|
#define kCCShader_PositionTexture_uColor "ShaderPositionTexture_uColor"
|
|
|
|
#define kCCShader_PositionTextureA8Color "ShaderPositionTextureA8Color"
|
|
|
|
#define kCCShader_Position_uColor "ShaderPosition_uColor"
|
|
|
|
#define kCCShader_PositionLengthTexureColor "ShaderPositionLengthTextureColor"
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
// uniform names
|
2012-11-09 12:08:18 +08:00
|
|
|
#define kCCUniformPMatrix_s "CC_PMatrix"
|
|
|
|
#define kCCUniformMVMatrix_s "CC_MVMatrix"
|
|
|
|
#define kCCUniformMVPMatrix_s "CC_MVPMatrix"
|
|
|
|
#define kCCUniformTime_s "CC_Time"
|
|
|
|
#define kCCUniformSinTime_s "CC_SinTime"
|
|
|
|
#define kCCUniformCosTime_s "CC_CosTime"
|
|
|
|
#define kCCUniformRandom01_s "CC_Random01"
|
|
|
|
#define kCCUniformSampler_s "CC_Texture0"
|
|
|
|
#define kCCUniformAlphaTestValue "CC_alpha_value"
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
// Attribute names
|
2012-11-13 11:06:32 +08:00
|
|
|
#define kCCAttributeNameColor "a_color"
|
2012-04-19 14:35:52 +08:00
|
|
|
#define kCCAttributeNamePosition "a_position"
|
|
|
|
#define kCCAttributeNameTexCoord "a_texCoord"
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-03-15 10:42:22 +08:00
|
|
|
struct _hashUniformEntry;
|
|
|
|
|
2012-03-15 10:54:07 +08:00
|
|
|
typedef void (*GLInfoFunction)(GLuint program, GLenum pname, GLint* params);
|
|
|
|
typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length, GLchar* infolog);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
/** CCGLProgram
|
|
|
|
Class that implements a glProgram
|
|
|
|
|
|
|
|
|
|
|
|
@since v2.0.0
|
2012-03-12 15:22:03 +08:00
|
|
|
*/
|
2012-03-14 14:55:17 +08:00
|
|
|
class CC_DLL CCGLProgram : public CCObject
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
CCGLProgram();
|
|
|
|
virtual ~CCGLProgram();
|
|
|
|
/** Initializes the CCGLProgram with a vertex and fragment with bytes array */
|
|
|
|
bool initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
|
|
|
|
/** Initializes the CCGLProgram with a vertex and fragment with contents of filenames */
|
|
|
|
bool initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename);
|
|
|
|
/** It will add a new attribute to the shader */
|
|
|
|
void addAttribute(const char* attributeName, GLuint index);
|
|
|
|
/** links the glProgram */
|
|
|
|
bool link();
|
|
|
|
/** it will call glUseProgram() */
|
|
|
|
void use();
|
2012-11-09 12:08:18 +08:00
|
|
|
/** It will create 4 uniforms:
|
2012-04-19 14:35:52 +08:00
|
|
|
- kCCUniformPMatrix
|
|
|
|
- kCCUniformMVMatrix
|
2012-11-09 12:08:18 +08:00
|
|
|
- kCCUniformMVPMatrix
|
2012-04-19 14:35:52 +08:00
|
|
|
- kCCUniformSampler
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
And it will bind "kCCUniformSampler" to 0
|
|
|
|
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
void updateUniforms();
|
2013-02-27 09:38:30 +08:00
|
|
|
|
|
|
|
/** calls retrieves the named uniform location for this shader program. */
|
|
|
|
GLint getUniformLocationForName(const char* name);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniform1i only if the values are different than the previous call for this same shader program. */
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith1i(GLint location, GLint i1);
|
2013-03-27 02:20:38 +08:00
|
|
|
|
|
|
|
/** calls glUniform2i only if the values are different than the previous call for this same shader program. */
|
|
|
|
void setUniformLocationWith2i(GLint location, GLint i1, GLint i2);
|
|
|
|
|
|
|
|
/** calls glUniform3i only if the values are different than the previous call for this same shader program. */
|
|
|
|
void setUniformLocationWith3i(GLint location, GLint i1, GLint i2, GLint i3);
|
|
|
|
|
|
|
|
/** calls glUniform4i only if the values are different than the previous call for this same shader program. */
|
|
|
|
void setUniformLocationWith4i(GLint location, GLint i1, GLint i2, GLint i3, GLint i4);
|
|
|
|
|
|
|
|
/** calls glUniform2iv only if the values are different than the previous call for this same shader program. */
|
|
|
|
void setUniformLocationWith2iv(GLint location, GLint* ints, unsigned int numberOfArrays);
|
|
|
|
|
|
|
|
/** calls glUniform3iv only if the values are different than the previous call for this same shader program. */
|
|
|
|
void setUniformLocationWith3iv(GLint location, GLint* ints, unsigned int numberOfArrays);
|
|
|
|
|
|
|
|
/** calls glUniform4iv only if the values are different than the previous call for this same shader program. */
|
|
|
|
|
|
|
|
void setUniformLocationWith4iv(GLint location, GLint* ints, unsigned int numberOfArrays);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniform1f only if the values are different than the previous call for this same shader program. */
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith1f(GLint location, GLfloat f1);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniform2f only if the values are different than the previous call for this same shader program. */
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith2f(GLint location, GLfloat f1, GLfloat f2);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniform3f only if the values are different than the previous call for this same shader program. */
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith3f(GLint location, GLfloat f1, GLfloat f2, GLfloat f3);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniform4f only if the values are different than the previous call for this same shader program. */
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith4f(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniform2fv only if the values are different than the previous call for this same shader program. */
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith2fv(GLint location, GLfloat* floats, unsigned int numberOfArrays);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniform3fv only if the values are different than the previous call for this same shader program. */
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith3fv(GLint location, GLfloat* floats, unsigned int numberOfArrays);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniform4fv only if the values are different than the previous call for this same shader program. */
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith4fv(GLint location, GLfloat* floats, unsigned int numberOfArrays);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** calls glUniformMatrix4fv only if the values are different than the previous call for this same shader program. */
|
2012-11-19 17:16:06 +08:00
|
|
|
void setUniformLocationWithMatrix4fv(GLint location, GLfloat* matrixArray, unsigned int numberOfMatrices);
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
/** will update the builtin uniforms if they are different than the previous call for this same shader program. */
|
|
|
|
void setUniformsForBuiltins();
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** returns the vertexShader error log */
|
|
|
|
const char* vertexShaderLog();
|
|
|
|
/** returns the fragmentShader error log */
|
|
|
|
const char* fragmentShaderLog();
|
|
|
|
/** returns the program error log */
|
|
|
|
const char* programLog();
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// reload all shaders, this function is designed for android
|
|
|
|
// when opengl context lost, so don't call it.
|
|
|
|
void reset();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
inline const GLuint getProgram() { return _program; }
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
private:
|
2012-11-09 12:08:18 +08:00
|
|
|
bool updateUniformLocation(GLint location, GLvoid* data, unsigned int bytes);
|
2012-04-19 14:35:52 +08:00
|
|
|
const char* description();
|
|
|
|
bool compileShader(GLuint * shader, GLenum type, const GLchar* source);
|
|
|
|
const char* logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-03-15 10:42:22 +08:00
|
|
|
private:
|
2013-06-15 14:03:30 +08:00
|
|
|
GLuint _program;
|
|
|
|
GLuint _vertShader;
|
|
|
|
GLuint _fragShader;
|
|
|
|
GLint _uniforms[kCCUniform_MAX];
|
|
|
|
struct _hashUniformEntry* _hashForUniforms;
|
|
|
|
bool _usesTime;
|
2012-03-12 15:22:03 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of shaders group
|
|
|
|
/// @}
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* __CCGLPROGRAM_H__ */
|