我试图在每个onTouchMoved调用上绘制一个连续的样条线, 该功能应该类似于iOS游戏中使用的线条图 - 飞行控制。
我以下列方式使用它:
构造函数init:
conPointsArray = new PointArray();
conPointsArray->initWithCapacity(DEF_ARRAY_SIZE);
lineDrawer = DrawNode::create();
onTouchMoved回调用法:
void line::onTouchMoved(cocos2d::Touch *Touch, cocos2d::Event *Event)
{
conPointsArray->addControlPoint(Touch->getLocation());
lineDrawer->drawCardinalSpline((conPointsArray), 0.5f, 100, Color4F::BLUE);
CCLOG("on touch moved x: %f y %f", Touch->getLocation().x, Touch->getLocation().y);
}
但是应用程序总是在某个openGL函数中崩溃。
我认为它的原因是因为数组不断变化而且openGL遇到了麻烦(事实上我总是发送相同的数组这是一个坏主意,但只是为了看看它是如何工作的),所以我将绘图调用移动到onTouchEnded,实际上,该线被绘制,但只是(在预期的情况下)onTouch操作结束后。
此问题的解决方案/最佳做法是什么?
修改
这是代码崩溃的openGL函数:
void DrawNode::onDrawGLLine(const Mat4 &transform, uint32_t flags)
{
auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR);
glProgram->use();
glProgram->setUniformsForBuiltins(transform);
if (_dirtyGLLine)
{
glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
_dirtyGLLine = false;
}
if (Configuration::getInstance()->supportsShareableVAO())
{
GL::bindVAO(_vaoGLLine);
}
else
{
glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
// vertex
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
// color
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
// texcood
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
}
glLineWidth(2);
glDrawArrays(GL_LINES, 0, _bufferCountGLLine); <----------- CRASH HERE
glBindBuffer(GL_ARRAY_BUFFER, 0);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLLine);
CHECK_GL_ERROR_DEBUG();
}