您的位置:首页 > 移动开发 > Cocos引擎

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: