要给一个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
{
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中为DrawNode应用GLProgram shader,并解决DrawNode纹理坐标为0导致的问题。通过重写drawSolidRect方法,实现了在shader中正确读取和使用Texture2D。

1444

被折叠的 条评论
为什么被折叠?



