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 false 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
|
|
|
|
|
|
|
#include "CCGLProgram.h"
|
2012-03-15 10:42:22 +08:00
|
|
|
#include "ccGLStateCache.h"
|
2012-03-12 15:22:03 +08:00
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "CCFileUtils.h"
|
2012-03-15 10:42:22 +08:00
|
|
|
#include "support/data_support/uthash.h"
|
2012-03-12 15:22:03 +08:00
|
|
|
#include "CCString.h"
|
2012-04-17 17:55:26 +08:00
|
|
|
// extern
|
|
|
|
#include "kazmath/GL/matrix.h"
|
2012-03-15 10:42:22 +08:00
|
|
|
#include "kazmath/kazmath.h"
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
typedef struct _hashUniformEntry
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
GLvoid* value; // value
|
|
|
|
unsigned int location; // Key
|
|
|
|
UT_hash_handle hh; // hash entry
|
2012-03-15 10:42:22 +08:00
|
|
|
} tHashUniformEntry;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
CCGLProgram::CCGLProgram()
|
2012-03-15 10:54:07 +08:00
|
|
|
: m_uProgram(0)
|
|
|
|
, m_uVertShader(0)
|
|
|
|
, m_uFragShader(0)
|
|
|
|
, m_pHashForUniforms(NULL)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
memset(m_uUniforms, 0, sizeof(m_uUniforms));
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCGLProgram::~CCGLProgram()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOGINFO("cocos2d: %s %d deallocing 0x%X", __FUNCTION__, __LINE__, this);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// there is no need to delete the shaders. They should have been already deleted.
|
|
|
|
CCAssert( m_uVertShader == 0, "Vertex Shaders should have been already deleted");
|
|
|
|
CCAssert( m_uFragShader == 0, "Vertex Shaders should have been already deleted");
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_uProgram)
|
|
|
|
{
|
|
|
|
ccGLDeleteProgram(m_uProgram);
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
tHashUniformEntry *current_element, *tmp;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Purge uniform hash
|
|
|
|
HASH_ITER(hh, m_pHashForUniforms, current_element, tmp) {
|
|
|
|
HASH_DEL(m_pHashForUniforms, current_element);
|
|
|
|
free(current_element->value);
|
|
|
|
free(current_element);
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 10:42:22 +08:00
|
|
|
bool CCGLProgram::initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uProgram = glCreateProgram();
|
2012-04-17 17:55:26 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uVertShader = m_uFragShader = 0;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( vShaderByteArray ) {
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!compileShader(&m_uVertShader, GL_VERTEX_SHADER, vShaderByteArray)) {
|
|
|
|
CCLOG("cocos2d: ERROR: Failed to compile vertex shader");
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Create and compile fragment shader
|
|
|
|
if( fShaderByteArray ) {
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!compileShader(&m_uFragShader, GL_FRAGMENT_SHADER, fShaderByteArray)) {
|
|
|
|
CCLOG("cocos2d: ERROR: Failed to compile fragment shader");
|
|
|
|
}
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( m_uVertShader ) {
|
|
|
|
glAttachShader(m_uProgram, m_uVertShader);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( m_uFragShader ) {
|
|
|
|
glAttachShader(m_uProgram, m_uFragShader);
|
|
|
|
}
|
|
|
|
m_pHashForUniforms = NULL;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-03-15 10:42:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCGLProgram::initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename)
|
|
|
|
{
|
2012-06-08 16:22:57 +08:00
|
|
|
const GLchar * vertexSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(vShaderFilename))->getCString();
|
|
|
|
const GLchar * fragmentSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fShaderFilename))->getCString();
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return initWithVertexShaderByteArray(vertexSource, fragmentSource);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCGLProgram::description()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return CCString::stringWithFormat("<CCGLProgram = %08X | Program = %i, VertexShader = %i, FragmentShader = %i>", this, m_uProgram, m_uVertShader, m_uFragShader)->getCString();
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 10:42:22 +08:00
|
|
|
bool CCGLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
GLint status;
|
2012-03-15 10:42:22 +08:00
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
if (!source)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*shader = glCreateShader(type);
|
|
|
|
glShaderSource(*shader, 1, &source, NULL);
|
2012-04-17 17:55:26 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
glCompileShader(*shader);
|
2012-04-17 17:55:26 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
|
2012-04-17 17:55:26 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! status ) {
|
|
|
|
if( type == GL_VERTEX_SHADER )
|
|
|
|
CCLOG("cocos2d: %s", vertexShaderLog() );
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: %s", fragmentShaderLog() );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-15 10:42:22 +08:00
|
|
|
return ( status == GL_TRUE );
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::addAttribute(const char* attributeName, GLuint index)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
glBindAttribLocation(m_uProgram,
|
|
|
|
index,
|
|
|
|
attributeName);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::updateUniforms()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Since sample most probably won't change, set it to 0 now.
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uUniforms[kCCUniformMVPMatrix] = glGetUniformLocation(m_uProgram, kCCUniformMVPMatrix_s);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uUniforms[kCCUniformSampler] = glGetUniformLocation(m_uProgram, kCCUniformSampler_s);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->use();
|
|
|
|
this->setUniformLocationWith1i( m_uUniforms[kCCUniformSampler], 0 );
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCGLProgram::link()
|
|
|
|
{
|
2012-03-15 10:54:07 +08:00
|
|
|
glLinkProgram(m_uProgram);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-17 18:01:15 +08:00
|
|
|
#if DEBUG
|
2012-04-19 14:35:52 +08:00
|
|
|
GLint status;
|
2012-03-15 10:54:07 +08:00
|
|
|
glValidateProgram(m_uProgram);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-03-15 10:54:07 +08:00
|
|
|
glGetProgramiv(m_uProgram, GL_LINK_STATUS, &status);
|
2012-03-12 15:22:03 +08:00
|
|
|
if (status == GL_FALSE) {
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("cocos2d: ERROR: Failed to link program: %i", m_uProgram);
|
|
|
|
if( m_uVertShader )
|
|
|
|
glDeleteShader( m_uVertShader );
|
|
|
|
if( m_uFragShader )
|
|
|
|
glDeleteShader( m_uFragShader );
|
|
|
|
ccGLDeleteProgram( m_uProgram );
|
|
|
|
m_uVertShader = m_uFragShader = m_uProgram = 0;
|
2012-03-12 15:22:03 +08:00
|
|
|
return false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-17 18:01:15 +08:00
|
|
|
#endif
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-03-15 10:54:07 +08:00
|
|
|
if (m_uVertShader)
|
|
|
|
glDeleteShader(m_uVertShader);
|
|
|
|
if (m_uFragShader)
|
|
|
|
glDeleteShader(m_uFragShader);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uVertShader = m_uFragShader = 0;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::use()
|
|
|
|
{
|
2012-03-15 10:54:07 +08:00
|
|
|
ccGLUseProgram(m_uProgram);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCGLProgram::logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc)
|
|
|
|
{
|
|
|
|
GLint logLength = 0, charsWritten = 0;
|
|
|
|
|
|
|
|
infoFunc(object, GL_INFO_LOG_LENGTH, &logLength);
|
|
|
|
if (logLength < 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
char *logBytes = (char*)malloc(logLength);
|
|
|
|
logFunc(object, logLength, &charsWritten, logBytes);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCString* log = CCString::stringWithCString(logBytes);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
free(logBytes);
|
2012-04-14 19:11:57 +08:00
|
|
|
return log->getCString();
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCGLProgram::vertexShaderLog()
|
|
|
|
{
|
2012-03-15 10:54:07 +08:00
|
|
|
return this->logForOpenGLObject(m_uVertShader, (GLInfoFunction)&glGetShaderiv, (GLLogFunction)&glGetShaderInfoLog);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCGLProgram::fragmentShaderLog()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return this->logForOpenGLObject(m_uFragShader, (GLInfoFunction)&glGetShaderiv, (GLLogFunction)&glGetShaderInfoLog);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCGLProgram::programLog()
|
|
|
|
{
|
2012-03-15 10:54:07 +08:00
|
|
|
return this->logForOpenGLObject(m_uProgram, (GLInfoFunction)&glGetProgramiv, (GLLogFunction)&glGetProgramInfoLog);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
// Uniform cache
|
|
|
|
|
|
|
|
bool CCGLProgram::updateUniformLocation(unsigned int location, GLvoid* data, unsigned int bytes)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool updated = true;
|
|
|
|
tHashUniformEntry *element = NULL;
|
|
|
|
HASH_FIND_INT(m_pHashForUniforms, &location, element);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! element ) {
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
element = (tHashUniformEntry*)malloc( sizeof(*element) );
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// key
|
|
|
|
element->location = location;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// value
|
|
|
|
element->value = malloc( bytes );
|
|
|
|
memcpy(element->value, data, bytes );
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
HASH_ADD_INT(m_pHashForUniforms, location, element);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( memcmp( element->value, data, bytes) == 0 )
|
|
|
|
updated = false;
|
|
|
|
else
|
|
|
|
memcpy( element->value, data, bytes );
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return updated;
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationWith1i(unsigned int location, GLint i1)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool updated = updateUniformLocation(location, &i1, sizeof(i1)*1);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniform1i( (GLint)location, i1);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationWith1f(unsigned int location, GLfloat f1)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool updated = updateUniformLocation(location, &f1, sizeof(f1)*1);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniform1f( (GLint)location, f1);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationWith2f(unsigned int location, GLfloat f1, GLfloat f2)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat floats[2] = {f1,f2};
|
|
|
|
bool updated = updateUniformLocation(location, floats, sizeof(floats));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniform2f( (GLint)location, f1, f2);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationWith3f(unsigned int location, GLfloat f1, GLfloat f2, GLfloat f3)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat floats[3] = {f1,f2,f3};
|
|
|
|
bool updated = updateUniformLocation(location, floats, sizeof(floats));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniform3f( (GLint)location, f1, f2, f3);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationWith4f(unsigned int location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat floats[4] = {f1,f2,f3,f4};
|
|
|
|
bool updated = updateUniformLocation(location, floats, sizeof(floats));
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniform4f( (GLint)location, f1, f2, f3,f4);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationWith2fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool updated = updateUniformLocation(location, floats, sizeof(float)*2*numberOfArrays);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniform2fv( (GLint)location, (GLsizei)numberOfArrays, floats );
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationWith3fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool updated = updateUniformLocation(location, floats, sizeof(float)*3*numberOfArrays);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniform3fv( (GLint)location, (GLsizei)numberOfArrays, floats );
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationWith4fv(unsigned int location, GLfloat* floats, unsigned int numberOfArrays)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool updated = updateUniformLocation(location, floats, sizeof(float)*4*numberOfArrays);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniform4fv( (GLint)location, (GLsizei)numberOfArrays, floats );
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformLocationwithMatrix4fv(unsigned int location, GLfloat* matrixArray, unsigned int numberOfMatrices)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool updated = updateUniformLocation(location, matrixArray, sizeof(float)*16*numberOfMatrices);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( updated )
|
|
|
|
{
|
|
|
|
glUniformMatrix4fv( (GLint)location, (GLsizei)numberOfMatrices, GL_FALSE, matrixArray);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::setUniformForModelViewProjectionMatrix()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kmMat4 matrixP;
|
|
|
|
kmMat4 matrixMV;
|
|
|
|
kmMat4 matrixMVP;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLGetMatrix(KM_GL_PROJECTION, &matrixP );
|
|
|
|
kmGLGetMatrix(KM_GL_MODELVIEW, &matrixMV );
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kmMat4Multiply(&matrixMVP, &matrixP, &matrixMV);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
setUniformLocationwithMatrix4fv(m_uUniforms[kCCUniformMVPMatrix], matrixMVP.mat, 1);
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLProgram::reset()
|
|
|
|
{
|
|
|
|
m_uVertShader = m_uFragShader = 0;
|
|
|
|
memset(m_uUniforms, 0, sizeof(m_uUniforms));
|
|
|
|
|
|
|
|
|
|
|
|
// it is already deallocated by android
|
|
|
|
//ccGLDeleteProgram(m_uProgram);
|
|
|
|
m_uProgram = 0;
|
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
tHashUniformEntry *current_element, *tmp;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Purge uniform hash
|
|
|
|
HASH_ITER(hh, m_pHashForUniforms, current_element, tmp)
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
HASH_DEL(m_pHashForUniforms, current_element);
|
|
|
|
free(current_element->value);
|
|
|
|
free(current_element);
|
|
|
|
}
|
2012-04-17 17:55:26 +08:00
|
|
|
m_pHashForUniforms = NULL;
|
2012-03-15 10:42:22 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
NS_CC_END
|