cocos2dx给DrawNode的shader传递Texture2D
2016-02-25 20:35
405 查看
要给一个DrawNode设置GLProgram,并且在shader中读取纹理信息,如下:
uniform sampler2D uTexture;
void main(){
gl_FragColor = texture2D(uTexture, v_texCoord);
}
在程序运行后发现中发现gl_FragColor 不正确,原因在于DrawNode绘制图形时写入的纹理坐标均为0.
因此,需要重写DrawNode的draw** 函数。
如drawSolidRect为例,代码如下
class VRMaskDrawNode : public DrawNode
void VRMaskDrawNode::drawSolidRect(const Vec2 & origin, const Vec2 & destination, const Color4F & color)
{
Vec2 verts[] = {
origin,
Vec2(destination.x, origin.y),
destination,
Vec2(origin.x, destination.y)
};
int numberOfPoints = 4;
auto triangle_count = numberOfPoints - 2;
auto vertex_count = 3 * triangle_count;
ensureCapacity(vertex_count);
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
V2F_C4B_T2F_Triangle *cursor = triangles;
V2F_C4B_T2F_Triangle tmp1 = {
{ verts[0], Color4B(color), Tex2F(0.0f, 0.0f) },
{ verts[1], Color4B(color), Tex2F(1.0f, 0.0f) },
{ verts[2], Color4B(color), Tex2F(1.0f, 1.0f) },
};
V2F_C4B_T2F_Triangle tmp2 = {
{ verts[0], Color4B(color), Tex2F(0.0f, 0.0f) },
{ verts[2], Color4B(color),Tex2F(1.0f, 1.0f) },
{ verts[3], Color4B(color), Tex2F(0.0f, 1.0f) },
};
*cursor++ = tmp1;
*cursor++ = tmp2;
_bufferCount += vertex_count;
_dirty = true;
}
uniform sampler2D uTexture;
void main(){
gl_FragColor = texture2D(uTexture, v_texCoord);
}
在程序运行后发现中发现gl_FragColor 不正确,原因在于DrawNode绘制图形时写入的纹理坐标均为0.
因此,需要重写DrawNode的draw** 函数。
如drawSolidRect为例,代码如下
class VRMaskDrawNode : public DrawNode
void VRMaskDrawNode::drawSolidRect(const Vec2 & origin, const Vec2 & destination, const Color4F & color)
{
Vec2 verts[] = {
origin,
Vec2(destination.x, origin.y),
destination,
Vec2(origin.x, destination.y)
};
int numberOfPoints = 4;
auto triangle_count = numberOfPoints - 2;
auto vertex_count = 3 * triangle_count;
ensureCapacity(vertex_count);
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
V2F_C4B_T2F_Triangle *cursor = triangles;
V2F_C4B_T2F_Triangle tmp1 = {
{ verts[0], Color4B(color), Tex2F(0.0f, 0.0f) },
{ verts[1], Color4B(color), Tex2F(1.0f, 0.0f) },
{ verts[2], Color4B(color), Tex2F(1.0f, 1.0f) },
};
V2F_C4B_T2F_Triangle tmp2 = {
{ verts[0], Color4B(color), Tex2F(0.0f, 0.0f) },
{ verts[2], Color4B(color),Tex2F(1.0f, 1.0f) },
{ verts[3], Color4B(color), Tex2F(0.0f, 1.0f) },
};
*cursor++ = tmp1;
*cursor++ = tmp2;
_bufferCount += vertex_count;
_dirty = true;
}
相关文章推荐
- cocos2dx实现经典飞机大战
- cocos2d-x lua 3.6 解析json
- cocos2dx-js3.9 手动绑定JS到C++
- Cocos2.2.3文件夹的创建与递归删除
- COCOS2DX 3.0 优化提升渲染速度 Auto-batching
- cocos2d-x 优化(纹理渲染优化、资源缓存、内存优化)
- WIN7 以下创建cocos2d-x3.0+lua项目
- cocos2dx-js 文件加密 quickcocos2dx3.3文件加密
- cocos-js project.json modules
- Cocos2d-x 3.x序列帧动画
- cocos2dx xcode下面编译注意问题
- Cocos 2.2.3 zip文件解压缩
- cocos2d-js销毁计时器
- Cocos2d-x 3.x进度条
- Cocos 2.2.3资源在线下载
- cocos2dx shader分析之CCGLProgram
- Cocos2d-x 3.x图元绘制
- cocos2d-x LUA项目设置记录
- Cocos2d-x 3.x文件操作
- Cocos2D-X2.2.3学习笔记10(几何图形)