Here’s the list of CC_ uniforms.
http://cocos2d-x.org/documentation/programmers-guide/13/index.html
http://cocos2d-x.org/docs/programmers-guide/advanced_topics/index.html#predefined-uniforms .
Here’s a brief I’ve written, but not finished or edited enough to post on my blog yet. Not sure if this is useful, what do you think? should I continue along these lines to supplement the cocos docs?
Cocos2D Defined Uniforms
Uniforms are values that are like global variables for shaders. When applied to a shader program all Node’s that use that shader will have access to the associated uniforms and each uniform will have the same value for all vertices and fragments in that frame (hence the similarity to global values).
Attributes instead are values that are unique to each vertex. A vertex is essentially a set of attributes of various lengths of bytes.
- A standard sprite vertex contains a Position, Color, and Texture Coordinate.
- A 3D sprite vertex might contain Position, Color, Texture Coordinate(s), and Normal
(optional if texture mapped) (optional - for lighting)
- A custom Node may submit a quad command with custom attributes (up to limit)
How Engine Applies Default Uniforms
void GLProgram::setUniformsForBuiltins(...) {
//...
GLint location;
location = _builtInUniforms[UNIFORM_P_MATRIX];
setUniformLocationWithMatrix4fv(location, matrixP.m, 1);
location = _builtInUniforms[GLProgram::UNIFORM_TIME];
setUniformLocationWith4f(location, time/10.0, time, time*2, time*4);
//...
}
Matrices
CC_PMatrix - Projection matrix - Transforms coordinate from camera to screen (see guide)
CC_MVMatrix - Model-View matrix - Transforms coordinate from model to world to camera (see guide)
CC_MVPMatrix - Model-View-Projection matrix - Transforms coordinate from local to screen (see guide)
Guide - http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
CC_NormalMatrix - appears to be unused
CC_Time[4] - CPU clock timestamp in seconds multiplied by various factors
CC_SinTime[4] - Similar to CC_Time where element [3] has sin applied
CC_CosTime[4] - Same as CC_Time except elements [3] has cosine applied
setUniformLocationWith4f(..., time/10.0, time, time*2, time*4);
setUniformLocationWith4f(..., time/8.0, time/4.0, time/2.0, sinf(time));
setUniformLocationWith4f(..., time/8.0, time/4.0, time/2.0, cosf(time));
CC_TextureN - 2D Texture (sampler) that can be used to extract pixel information.
(N = four textures can be used [0…3] for CCSprite, and up to eight [0…7] for CCSprite3D)
vec4 color = Sample2D(CC_Texture0, xy);
CC_Random01[4] - Similar to CC_Time, but holds four random values using CCRANDOM_0_1()
CC_alpha_value - Used with alpha discard shader to “cut out” pixels color.a < CC_alpha_value
CC_AmbientColor - appears to be unused; would likely be filled in if there’s an ambient light in the scene
You’ll use one of these to transform your vertices in the vertex shader depending on which of these matrices you have pre-applied when you add your vertices into the GPU buffer object.
Node Applies Model-View Matrix (using default shader)
If you look at the internal code for the Node class you’ll notice that it uses the Model-View matrix to each vertex of the quad (or polygon vertex). It updates and applies the _modelViewTransform
to adjust based on the parent hierarchy as well as the camera. This is done, most likely, because then the Model-View matrix multiplication can be done once per model instead of per vertex.
Edit: relatedly I plan to add more examples to the shader cookbook as well as flesh out a similar code sample and post regarding the Material system.
Cocos2d Shader Cookbook Port?
Edit2: Added clarification to the end point about node’s default shader