cocos2dx给DrawNode的shader传递Texture2D

来源:互联网 发布:五轴五联动好编程吗 编辑:程序博客网 时间:2024/05/20 05:58

要给一个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;
}



0 0
原创粉丝点击