前言: 这个系列是我自己在看cocos2dx源码时记录的一些比较重要的数据结构跟方法中间可能穿插些opengl的知识. 每个介绍的类之间没有固定引用顺序,仅作为个人的记录,可能会有点乱(其实主要是为了怕记不住,这次看源码的目的是搞清楚 cocos的MVPMatrix的使用)

#1 CCGLProgram CCGLProgram 是 管理opengl 管线中 GLProgram 和其相关的 着色器, 顶点属性, 统一变量等等的类, 以下是其中几个比较关键的结构体

##1.1 VertextAttrib

VertexAtttrib结构体在数据存储上于 glGetActiveAttrib获取的结构时一致的 index : 指的是vertexShader顶点着色器中 attribute 属性 , 在shader中 可以用(layout=?)指定index size:属性对应类型存储的数量 例如color = {1.0, 1.0, 1.0}, size 为 3, type 为GL_FLOAT type: 指的是属性对应的存储类型 name: 在顶点着色器中属性的名字


/**VertexAttrib is a structure to encapsulate data got from glGetActiveAttrib.*/struct VertexAttrib
{/**Index of attribute, start from 0.*/GLuint index;/**Number of Data type in the attribute, could range from 0-4.*/GLint size;/**Data type of the attribute, could be GL_FLOAT, GL_UNSIGNED_BYTE etc.*/GLenum type;/**The string name in vertex shader.*/std::string name;
};
复制代码

这个结构体主要被应用于一下opengl函数的设置

void glVertexAttribPointer( GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride,const GLvoid * pointer);
//GLboolean normalized 属性主要用于标识传入的属性的各个分量是否需要归一化, stride是相同属性的两个数据间的偏移, pointer代表的是属性的首个在buffer里的偏移
复制代码

##1.2 Uniform Uniform 主要用于存储 glGetActiveUnifom 和glGetUniformLocation 的数据

/**Uniform is a structure to encapsulate data got from glGetActiveUniform and glGetUniformLocation.*/
struct Uniform
{/**The place where the uniform placed, starts from 0.*/GLint location;/**Number of data type in attribute.*/GLint size;/**Data type of the attribute.*/GLenum type;/**String of the uniform name.*/std::string name;
};
复制代码

uniform 统一变量是在一个glprogram 通用的 统一变量空间可以被顶点着色器个片段着色器同时引用 location : 统一变量在glprogram里的位置 ,这个可用的统一变量数是有上限的 size: 相应统一变量的分量数量 type:相应统一变量的数据类型 name:相应统一变量的名字 结构体内容可被用于一下opengl函数

void glUniform*(GLint location,  GLsizei count,  const GLfloat *value)
//count:指明要更改的元素个数。如果目标uniform变量不是一个数组,那么这个值应该设为1;如果是数组,则应该设置为>=1。
//value:指定一个具有count个数值的数组指针,用来更新指定的uniform变量。
复制代码

##1.3 VertexIndex 预定义的顶点属性的对应的index, 占坑

/**Enum the preallocated vertex attribute. */enum{/**Index 0 will be used as Position.*/VERTEX_ATTRIB_POSITION,/**Index 1 will be used as Color.*/VERTEX_ATTRIB_COLOR,/**Index 2 will be used as Tex coord unit 0.*/VERTEX_ATTRIB_TEX_COORD,/**Index 3 will be used as Tex coord unit 1.*/VERTEX_ATTRIB_TEX_COORD1,/**Index 4 will be used as Tex coord unit 2.*/VERTEX_ATTRIB_TEX_COORD2,/**Index 5 will be used as Tex coord unit 3.*/VERTEX_ATTRIB_TEX_COORD3,/**Index 6 will be used as Normal.*/VERTEX_ATTRIB_NORMAL,/**Index 7 will be used as Blend weight for hardware skin.*/VERTEX_ATTRIB_BLEND_WEIGHT,/**Index 8 will be used as Blend index.*/VERTEX_ATTRIB_BLEND_INDEX,/**Index 9 will be used as tangent.*/VERTEX_ATTRIB_TANGENT,/**Index 10 will be used as Binormal.*/VERTEX_ATTRIB_BINORMAL,VERTEX_ATTRIB_MAX,// backward compatibilityVERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,};
复制代码

##1.4 UniformIndex 预定义的统一变量的对应的index, 占坑. 这里代码用到的枚举名字大家如果有学过opengl的话应该会非常熟悉

 enum{/**Ambient color.*/UNIFORM_AMBIENT_COLOR,/**Projection matrix.*/UNIFORM_P_MATRIX,/**Model view matrix.*/UNIFORM_MV_MATRIX,/**Model view projection matrix.*/UNIFORM_MVP_MATRIX,/**Normal matrix.*/UNIFORM_NORMAL_MATRIX,/**Time.*/UNIFORM_TIME,/**sin(Time).*/UNIFORM_SIN_TIME,/**cos(Time).*/UNIFORM_COS_TIME,/**Random number.*/UNIFORM_RANDOM01,/** @{* Sampler 0-3, used for texture.*/UNIFORM_SAMPLER0,UNIFORM_SAMPLER1,UNIFORM_SAMPLER2,UNIFORM_SAMPLER3,/**@}*/UNIFORM_MAX,};
复制代码

##1.4built in shader 内建的shader 字符串

  /**@name Built Shader types@{*//** ETC1 ALPHA supports for 2d */static const char* SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR;static const char* SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR_NO_MVP;static const char* SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY;static const char* SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY_NO_MVP;/**Built in shader for 2d. Support Position, Texture and Color vertex attribute.*/static const char* SHADER_NAME_POSITION_TEXTURE_COLOR;/**Built in shader for 2d. Support Position, Texture and Color vertex attribute, but without multiply vertex by MVP matrix.*/static const char* SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP;/**Built in shader for 2d. Support Position, Texture vertex attribute, but include alpha test.*///省略一大堆/**Built in shader for camera clear*/static const char* SHADER_CAMERA_CLEAR;/**end of built shader types.@}*/
复制代码

##1.5 built in uniform 内建的 uniform 默认名

  /**@name Built uniform names@{*//**Ambient Color uniform.*/static const char* UNIFORM_NAME_AMBIENT_COLOR;/**Projection Matrix uniform.*/static const char* UNIFORM_NAME_P_MATRIX;/**Model view matrix uniform.*/static const char* UNIFORM_NAME_MV_MATRIX;/**Model view projection uniform.*/static const char* UNIFORM_NAME_MVP_MATRIX;/**Normal matrix uniform.*/static const char* UNIFORM_NAME_NORMAL_MATRIX;/**Time uniform.*/static const char* UNIFORM_NAME_TIME;/**Sin time uniform.*/static const char* UNIFORM_NAME_SIN_TIME;/**Cos time uniform.*/static const char* UNIFORM_NAME_COS_TIME;/**Random number uniform.*/static const char* UNIFORM_NAME_RANDOM01;/**@{ Sampler uniform 0-3, used for textures.*/static const char* UNIFORM_NAME_SAMPLER0;static const char* UNIFORM_NAME_SAMPLER1;static const char* UNIFORM_NAME_SAMPLER2;static const char* UNIFORM_NAME_SAMPLER3;/**@}*//**Alpha test value uniform.*/static const char* UNIFORM_NAME_ALPHA_TEST_VALUE;/**end of Built uniform names@}*/
复制代码

1.6 built in attribute names

默认的内建属性名字

/**@name Built Attribute names@{*//**Attribute color.*/static const char* ATTRIBUTE_NAME_COLOR;/**Attribute position.*/static const char* ATTRIBUTE_NAME_POSITION;/**@{ Attribute Texcoord 0-3.*/static const char* ATTRIBUTE_NAME_TEX_COORD;static const char* ATTRIBUTE_NAME_TEX_COORD1;static const char* ATTRIBUTE_NAME_TEX_COORD2;static const char* ATTRIBUTE_NAME_TEX_COORD3;/**@}*//**Attribute normal.*/static const char* ATTRIBUTE_NAME_NORMAL;/**Attribute blend weight.*/static const char* ATTRIBUTE_NAME_BLEND_WEIGHT;/**Attribute blend index.*/static const char* ATTRIBUTE_NAME_BLEND_INDEX;/**Attribute blend tangent.*/static const char* ATTRIBUTE_NAME_TANGENT;/**Attribute blend binormal.*/static const char* ATTRIBUTE_NAME_BINORMAL;/**end of Built Attribute names@}*/
复制代码

1.7 一些渲染流程管线方法的包装函数

挑选几个可以展示流程的 1.创建glprogram 省略了代码里如何使用内建shader名创建byteArray的过程

bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeDefines)
{_program = glCreateProgram();CHECK_GL_ERROR_DEBUG();// convert defines here. If we do it in "compileShader" we will do it twice.// a cache for the defines could be useful, but seems like overkill at this pointstd::string replacedDefines = "";replaceDefines(compileTimeDefines, replacedDefines);_vertShader = _fragShader = 0;if (vShaderByteArray){if (!compileShader(&_vertShader, GL_VERTEX_SHADER, vShaderByteArray, replacedDefines)){CCLOG("cocos2d: ERROR: Failed to compile vertex shader");return false;}}// Create and compile fragment shaderif (fShaderByteArray){if (!compileShader(&_fragShader, GL_FRAGMENT_SHADER, fShaderByteArray, replacedDefines)){CCLOG("cocos2d: ERROR: Failed to compile fragment shader");return false;}}if (_vertShader){glAttachShader(_program, _vertShader);}CHECK_GL_ERROR_DEBUG();if (_fragShader){glAttachShader(_program, _fragShader);}_hashForUniforms.clear();CHECK_GL_ERROR_DEBUG();return true;
}复制代码

2.链接

void GLProgram::link()// 内部调用了
//void GLProgram::bindPredefinedVertexAttribs()
//绑定默认的顶点属性
//关键还是这个gl函数
//glBindAttribLocation(_program, attribute_locations[i].location, attribute_locations[i].attributeName);
复制代码

3.使用

void GLProgram::use();
//内部实际调用 glUseProgram()
//很奇怪为什么没有dettachshader, 其实链接完就可以释放点shader在cpu端的内存了
复制代码

4.更新 这里只找到对uniform统一变量的, MVPMatrix主要就是用uniform传入渲染管线的 网上的说法是cocos管理了一个变换矩阵的堆栈,用于管理和保存需要使用的矩阵数据

void GLProgram::updateUniforms()
复制代码

5.其他类似删除释放glprogram , shader内存的包装方法都在源码中很容易找到

#3GLProgramCache 管理所有的GLProgram生死存亡的类

class CC_DLL GLProgramCache : public Ref
{
public:/**Constructor.* @js ctor*/GLProgramCache();/**Destructor.* @js NA* @lua NA*/~GLProgramCache();/** returns the shared instance *//** 怀念的OC风格,现在都没人写OC了吗, 就想知道swift以后会直接兼容C++吗*/static GLProgramCache* getInstance();/** purges the cache. It releases the retained instance. */static void destroyInstance();/** @deprecated Use getInstance() instead */CC_DEPRECATED_ATTRIBUTE static GLProgramCache* sharedShaderCache();/** @deprecated Use destroyInstance() instead */CC_DEPRECATED_ATTRIBUTE static void purgeSharedShaderCache();/** loads the default shaders */void loadDefaultGLPrograms();CC_DEPRECATED_ATTRIBUTE void loadDefaultShaders() { loadDefaultGLPrograms(); }/** reload the default shaders */void reloadDefaultGLPrograms();CC_DEPRECATED_ATTRIBUTE void reloadDefaultShaders() { reloadDefaultGLPrograms(); }/** returns a GL program for a given key *//*使用 对应的char* 变量作为索引 *///例子:GLProgram *p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);GLProgram * getGLProgram(const std::string &key);CC_DEPRECATED_ATTRIBUTE GLProgram * getProgram(const std::string &key) { return getGLProgram(key); }CC_DEPRECATED_ATTRIBUTE GLProgram * programForKey(const std::string &key){ return getGLProgram(key); }/** adds a GLProgram to the cache for a given name */void addGLProgram(GLProgram* program, const std::string &key);CC_DEPRECATED_ATTRIBUTE void addProgram(GLProgram* program, const std::string &key) { addGLProgram(program, key); }/** reload default programs these are relative to light */void reloadDefaultGLProgramsRelativeToLights();private:/**@{Init and load predefined shaders.*/bool init();void loadDefaultGLProgram(GLProgram *program, int type);/**@}*//**Get macro define for lights in current openGL driver.*//*获取宏定义的light GLSL方法*/std::string getShaderMacrosForLight() const;/**Predefined shaders.*/std::unordered_map<std::string, GLProgram*> _programs;
};
复制代码

PS:第一次写可能像是直接贴代码, 实际也的确如此, 对渲染管线的流程也只是只言片语,不成逻辑,是在抱歉....如果对opengl有兴趣的话可以点击https://learnopengl.com/这里有全套的教程,简单易学.ios 开发可以github上搜索GPUImage项目作为学习opengles2.0的参考

转载于:https://juejin.im/post/5a30dd40f265da432e5bffa8

Cocos2dx源码记录(1) CCGLProgram相关推荐

  1. Cocos2dx源码记录(11) CCPrimtiveCommand,CCPrimetive

    #1 CCPrimetive 因为CCPrimtiveCommand里有用到这个就先分析下这个吧 按照字面的意思应该是由一系列顶点属性和带有顺序的顶点索引的图元面片类 /**Primitive can ...

  2. Cocos2dx源码记录(6) CCTrianglesCommand

    CCQuadCommand类继承自CCTrianglesCommand类, 因为一个4个点的封闭图形由两个三角形组成.下面先介绍父类CCTrianglesCommand 然后再简单地介绍下CCQuad ...

  3. Gem5模拟器,popnet模拟器源码记录(十五)

    在使用王小航老师课题组开源的Gem5代码进行大规模的Chiplet并行仿真时,有个关键问题是:为什么原来的gem5不可以进行大规模仿真?为什么精度不够?也是论文提出的背景: (20条消息) 傻白探索C ...

  4. 学习vue-router源码记录-1

    因为本人开发中使用的是VUE技术栈,最近也是开始源码的学习,以此记录个人理解,若行文有误,请多多指教. 1.new Router和install 在vue中我们使用vue-router时需要先进行ne ...

  5. Ubuntu16.04编译android6.0.1源码记录

    目录 目录 一.安装环境 二.下载源码 1.下载repo 2.初始化repo 3.同步源代码 关于驱动 三.编译源码 四.导入源码到AS 五.刷入真机 六.修改源码 总结: 3.同步源代码 关于驱动 ...

  6. 【GAT】图注意力网络 - 简单的源码记录

    由于和GCN代码比较相似,所以部分内容从GCN那篇博客中截取. 1 - cora数据集 GNN常用数据集之Cora数据集 2 - 源码含义记录 首先我们来整体看一下代码的组成 截图中的这一大坨为命令行 ...

  7. 安卓贴图源码---记录旋转后位置..类似in/百度魔图

    效果如图: 类似in,百度魔图,的贴图功能  核心的地方:单/多点 旋转缩放后记录各个顶点小图标位置 引用这里 http://blog.csdn.net/xiaanming/article/detai ...

  8. 远古守卫/cocos2d-x 源码/塔防游戏/高仿王国保卫战

    下载地址:下载地址 本源码高度模仿IOS游戏王国保卫战,由国外IOS商业开发教程网站raywenderlich内部工程师制作,有非常完整的关卡设计,战斗流程,长达12个关卡,各种敌兵,怪物,箭塔,炮塔 ...

  9. windows编译安卓源码记录

    环境 Windows10 + vmware17 + ubuntu22 ubuntu环境设置 装完ubuntu系统后拖拽复制文件进去验证vmtools功能情况,如果vmtools异常很麻烦,试了n多方法 ...

最新文章

  1. Python:数据提取之JSON与JsonPATH
  2. Oracle数据表和Constraint管理
  3. 从C语言的角度重构数据结构系列(十二)-C语言判断语法详解(ifswitch)
  4. [搜索]一种分词的实现(2)
  5. Linux下PDF操作与转换
  6. python语言中的单行注释语句_Python 1基础语法一(注释、行与缩进、多行语句、空行和代码组)...
  7. linux 刻录cd,Linux 刻录CD/DVD命令 growisofs/mkisofs/cdrecord
  8. 识别物体是否存在_【科学实践Vol.1】带你玩转“人脸识别”
  9. Linux学习笔记(四)账号管理之管理用户账号
  10. 【渝粤教育】国家开放大学2018年秋季 2505T学前儿童社会教育 参考试题
  11. word-break: break-all与word-wrap:break-word的区别
  12. android 调用自带地图,Android中调用百度地图
  13. 戴姆勒集团将拆分卡车业务;洲际酒店集团发布全新品牌标识;先正达集团中国创新研发中心落户南京 | 美通企业周刊...
  14. 美国NIST《可解释的人工智能的四个原则》(全文翻译)
  15. Base64加密和解密使用
  16. c语言井字棋程序设计报告,井字棋游戏(课程设计)总结报告.doc
  17. 强烈推荐 :最用心的运营数据指标解读
  18. java.sql.SQLException: sql injection violation, multi-statement not allow
  19. 被调查7小时心生怨气 男子朋友圈辱骂警察被行拘
  20. 3. IEC 61000-3 系列部分标准简介(0 公式 0 基础学习电磁兼容)

热门文章

  1. 【论文写作分析】之三《基于预训练语言模型的案件要素识别方法》
  2. 【Dual-Path-RNN-Pytorch源码分析】loss函数:SI-SNR
  3. php7-fpm webtatic,如何在 CentOS 7 中使用 Nginx 和 PHP7-FPM 安装 Nextcloud
  4. SpringBoot自定义参数验证器
  5. linux启动时间极限优化,Linux启动时间的极限优化
  6. Elasticsearch之数据建模
  7. JavaScript toFixed() 方法
  8. git分支指的是_git基础之分支含义
  9. selenium + python自动化测试环境搭建
  10. 剑灵选区界面一直正在连接服务器,如何选择服务器 选择困难症的指南