我正在尝试在使用RenderTexture创建的纹理上绘制一条贝塞尔线,但该线未显示,只有纹理
this是我遵循的教程
Bezier.h:
class Bezier : public Node
{
private:
Color4F genRandomBrightColor();
public:
Sprite* create(float width, float height);
};
Bezier.cpp:
Sprite* Bezier::create(float width, float height){
auto rt = RenderTexture::create(width, height);
auto randomColor = genRandomBrightColor();
//rt->begin();
rt->beginWithClear(randomColor.r, randomColor.g, randomColor.b, randomColor.a);
//draw bezier line
int segments = 50;
//tried with Vec2 too
Vertex2F vertices[51];
Color4F colors[51];
float t = 0;
Point startPoint = Point(0, CCRANDOM_0_1()*height);
Point anchor1 = Point(CCRANDOM_0_1()*width / 2, CCRANDOM_0_1()*height);
Point anchor2 = Point((CCRANDOM_0_1()*width / 2) + (width / 2), CCRANDOM_0_1()*height);
Point endPoint = Point(width, CCRANDOM_0_1()*height);
//this i copied from DrawNode so it should be good
for (int i = 0; i < segments; i++){
colors[i] = Color4F::WHITE;
vertices[i] = Vertex2F(powf(1 - t, 3) * startPoint.x + 3.0f * powf(1 - t, 2) * t * anchor1.x + 3.0f * (1 - t) * t * t * anchor2.x + t * t * t * endPoint.x,
powf(1 - t, 3) * startPoint.y + 3.0f * powf(1 - t, 2) * t * anchor1.y + 3.0f * (1 - t) * t * t * anchor2.y + t * t * t * endPoint.y);
t += 1.0f / segments;
}
vertices[segments] = Vertex2F(endPoint.x, endPoint.y);
//////////////////////////////////////////////////////////////////////////
auto shaderProgram = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_COLOR);
setShaderProgram(shaderProgram);
CC_NODE_DRAW_SETUP();
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, segments);
rt->end();
auto sprite = Sprite::createWithTexture(rt->getSprite()->getTexture());
return sprite;
}
Color4F Bezier::genRandomBrightColor(){
while (true){
float r = CCRANDOM_0_1();
float g = CCRANDOM_0_1();
float b = CCRANDOM_0_1();
if ((r < 0.25) && (g > 0.5) && (b > 0.75) || (r > 0.75) && (g > 0.5) && (b<0.25)){
return Color4F(r,g,b,1);
}
}
}
GameScene.cpp:
#include <Bezier.h>
....
auto dl = new Bezier();
auto sprite = dl->create(visibleSize.width, visibleSize.height/2);
sprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 4 + origin.y));
this->addChild(sprite);
这是屏幕截图:http://postimg.org/image/cvy46wtwv/
任何帮助将不胜感激! PS:我没有使用DrawNode的功能,因为我想更多地了解这个
编辑:GOT IT!我需要使用CUSTOM COMMAND OpenGl code is not working for cocos2dx- 3.0 with VS2013 CPP答案 0 :(得分:0)
在函数创建结束时,将addchild(sprite)放到这样:
auto sprite = Sprite::createWithTexture(rt->getSprite()->getTexture());
addChild(sprite);
return sprite;