Cocos2d-x中使用OpenGL ES2.0编写shader
这几天在看子龙山人的关于OpenGL的文章,先依葫芦画瓢,能看到些东西,才能慢慢深入了解,当入门文章不错,但是其中遇到的一些问题,折腾了一些时间,为了方便和我一样的小白们,在这篇文章中进行写补充。
OpenGL ES2.0教程:编写自己的shader(2)--子龙山人
文章中关于VBO和VAO部分的代码的写法,我进行了些修改,将重复使用的变量定义全局变量,并修改初始化数据位置,防止在onDraw方法中重复的Malloc,致使内存泄露,具体的修改在代码注释中会说明。
源码如下:
HelloWorldScene.h
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init() override;
CREATE_FUNC(HelloWorld);
virtual void visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
void onDraw();
private:
CustomCommand _command;
float _position[];
float _color[];
GLProgram* glProgram;
GLuint vao;//定义VAO缓冲
GLuint positionVBO;
GLuint colorVBO;
GLuint positionLocation;
GLuint colorLocation;
};
#include "HelloWorldScene.h" Scene* HelloWorld::createScene()
{
auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene;
} void HelloWorld::visit(Renderer *renderer, const Mat4 &transform,uint32_t parentFlags)
{
Layer::visit(renderer, transform, parentFlags);
_command.init(_globalZOrder);
_command.func = CC_CALLBACK_0(HelloWorld::onDraw, this);
Director::getInstance()->getRenderer()->addCommand(&_command);
} void HelloWorld::onDraw()
{
//使用已经在init中初始化的glProgram
glProgram->use();
glProgram->setUniformsForBuiltins(); //获取VAO(OSX和ISO中默认开启,windows中默认不开启)
glBindVertexArray(vao); //获取VBO
glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
//填充坐标数据
glBufferData(GL_ARRAY_BUFFER, sizeof(_position), _position, GL_STATIC_DRAW);
positionLocation = glGetAttribLocation(glProgram->getProgram(), "a_position");
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, , GL_FLOAT, GL_FALSE, , (GLvoid*)); //获取VBO
glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
//填充颜色数据
glBufferData(GL_ARRAY_BUFFER, sizeof(_color), _color, GL_STATIC_DRAW);
colorLocation = glGetAttribLocation(glProgram->getProgram(), "a_color");
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(colorLocation, , GL_FLOAT, GL_FALSE, , (GLvoid*)); //使用VAO
glBindVertexArray(vao);
//渲染
glDrawArrays(GL_TRIANGLES, , );
//清空VAO
glBindVertexArray(); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(, );
CHECK_GL_ERROR_DEBUG();
} bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
} //准备数据
auto size = Director::getInstance()->getWinSize();
float position[] = { , , size.width, , size.width / , size.height};
float color[] = { , , , , , , , , , , , };
for (int i = ; i < sizeof(position); i++) {
_position[i] = position[i];
}
for (int i = ; i < sizeof(color); i++) {
_color[i] = color[i];
} //vert和frag文件和原文一样,直接放在Classes目录就可以读取到(环境:XCode)
glProgram = new GLProgram;
glProgram->initWithFilenames("myVertextShader.vert","myFragmentShader.frag");
glProgram->link();
glProgram->updateUniforms();
this->setGLProgram(glProgram); //glGen的系列方法会不断的Malloc内存空间,写在onDraw中会导致内存泄漏
glGenVertexArrays(, &vao);
glGenBuffers(, &positionVBO);
glGenBuffers(, &colorVBO);
return true;
}
运行结果:

Cocos2d-x中使用OpenGL ES2.0编写shader的更多相关文章
- Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤
原文地址: Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤 - 网络资源是无限的 - 博客频道 - CSDN.NET http://blog.csdn.net/fen ...
- OpenGL ES2.0 入门经典例子
原文链接地址:http://www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial 免责申明(必读!):本博客提供的所有教程的翻译原稿 ...
- OpenGL ES2.0入门详解
引自:http://blog.csdn.net/wangyuchun_799/article/details/7736928 1.决定你要支持的OpenGL ES的版本.目前,OpenGL ES包含 ...
- iOS开发——图形编程OC篇&OpenGL ES2.0编程步骤
OpenGL ES2.0编程步骤 OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL 三维图形 API 的子集,针对手机.PDA和游戏主机等嵌入式设备而设 ...
- Android +NDK+eclipse+opengl ES2.0 开启深度測试
參考:https://www.opengl.org/discussion_boards/showthread.php/172736-OpenGL-ES-Depth-Buffer-Problem 环境: ...
- OpenGL ES2.0 基本编程
1. EGL OpenGL ES命令须要一个rendering context和一个drawing surface. Rendering Context: 保存当前的OpenGL ES状态. Draw ...
- OPENGL ES2.0如何不使用glActiveTexture而显示多个图片
https://www.oschina.net/question/253717_72107 用opengl es 2.0显示多个图片的话,我只会一种方式,先将图片生成纹理,然后用下面的方式渲染 // ...
- OpenGL ES2.0编程三步曲 -转
原地址:http://blog.csdn.net/myarrow/article/details/7707943 1. 保存全局变量的数据结构 以下例子程序均基于Linux平台. typedef st ...
- cocos2d-js Shader系列1:cocos2d-js Shader和OpenGL ES2.0
cocos2d的Shader也就是差不多直接跟GPU打交道了,跟Flash的Stage3D(AGAL)类似,不过没有AGAL这么恶心,不需要直接编写汇编语言.而Fragment Shader又跟Fla ...
随机推荐
- 如何解决ajax跨域问题(转)
由 于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决 ajax的跨域问题.本篇将讲述一个小白从遇到跨域不 ...
- Linux Shell 03 条件测试
条件测试 方式一:在Bash中 test命令和[]是等价的. test命令: if test $n1 -eq $n2 then echo "The two number are equal& ...
- 第八篇 :微信公众平台开发实战Java版之如何网页授权获取用户基本信息
第一部分:微信授权获取基本信息的介绍 我们首先来看看官方的文档怎么说: 如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑. 关于网页授权回调域 ...
- Myeclipse6.0 在线安装svn插件
一.官网地址 1.SVN 官方网站:http://subversion.apache.org/ 二.安装步骤 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→F ...
- hdu 4635 Strongly connected 强连通缩点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...
- zoj 3229 Shoot the Bullet(无源汇上下界最大流)
题目:Shoot the Bullet 收藏:http://www.tuicool.com/articles/QRr2Qb 把每一天看成一个点,每个女孩也看成一个点,增加源和汇s.t,源向每一天连上[ ...
- SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- Mobile Prototype Dev Res Collection(Unity原型开发资源储备)
资源储备 本文针对mobile原型开发阶段的资源收集 在做移动端的开发时,当有灵感想做些东西时,若是此时缺少美术资源和可用的脚本,此刻会有些纠结,今天在Assets Store上Mark了一些移动端开 ...
- linux --备份oracle
1.exp\imp 导入导出命令使用exp username/pwd@sid file=path.dmp owner=user 不导出表数据:rows=n举例:exp iflashbuy/qwerwh ...
- 复习做UWP时涉及到的几种加密签名相关
本人菜鸟一枚,大学里凭兴趣学了一点WP的皮毛,后来又幸运(或者不幸)的进了一家专注于Windows生态的公司做了一段时间的UWP.在博客园写点自己遇到的东西,作为分享,也作为自己的备忘,如果有错误的地 ...