OpenGl代码不适用于cocos2dx- 3.0与VS2013 CPP
OpenGl code is not working for cocos2dx- 3.0 with VS2013 CPP
想要创建一个纹理贴图,但似乎这段代码不工作。我认为openGL部分不工作。也许我错过了一些东西,我正试图使小翅膀像地图和通过教程:http://www.raywenderlich.com/33266/how-to-create-dynamic-textures-with-ccrendertexture-in-cocos2d-2-x。下面的代码假设给出条纹背景的输出请检查链接。
RenderTexture* rt = RenderTexture::create(textureWidth, textureHeight);
rt->beginWithClear(color1.r, color1.g, color1.b, color1.a);
Point* vertices = (Point*)calloc(nStrips * 6, sizeof(Point));;
Color4F* colors = (Color4F*)calloc(nStrips * 6, sizeof(Color4F));
setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
CC_NODE_DRAW_SETUP();
int nVertices = 0;
float x1 = -textureHeight;
float x2;
float y1 = textureHeight;
float y2 = 0;
float dx = textureWidth / nStrips;
float stripeWidth = dx / 2;
for (int i = 0; i<nStrips; i++) {
x2 = x1 + textureHeight;
vertices[nVertices] = Point(x1, y1);
colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);
vertices[nVertices] = Point(x1 + stripeWidth, y1);
colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);
vertices[nVertices] = Point(x2, y2);
colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);
vertices[nVertices] = vertices[nVertices - 2];
colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);
vertices[nVertices] = vertices[nVertices - 2];
colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);
vertices[nVertices] = Point(x2 + stripeWidth, y2);
colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);
x1 += dx;
}
Sprite* noise = Sprite::create("Noise.png");
BlendFunc blendFunc;
blendFunc.src = GL_DST_COLOR;
blendFunc.dst = GL_ZERO;
noise->setBlendFunc(blendFunc);
noise->setPosition(Point(textureWidth / 2, textureHeight / 2));
noise->visit();
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices);
rt->end();
return Sprite::createWithTexture(rt->getSprite()->getTexture());
下面的代码片段应该可以解决您的问题:
Sprite* HelloWorld::spriteWithColor(cocos2d::Color4F bgColor, float textureWidth, float textureHeight)
{
RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);
rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
// 3: Draw into the texture
//Caution!!! if you want to use your own opengl draw call ,you must wrap them with a custom command
_customCommand.init(rt->getGlobalZOrder());
_customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw,this);
auto renderer = Director::getInstance()->getRenderer();
renderer->addCommand(&_customCommand);
//draw into texture
_noiseSprite->setBlendFunc({GL_DST_COLOR, GL_ZERO});
_noiseSprite->setPosition(Vec2(textureWidth/2, textureHeight/2));
_noiseSprite->visit();
rt->end();
return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
void HelloWorld::onDraw()
{
this->setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
this->getGLProgram()->use();
this->getGLProgram()->setUniformsForBuiltins();
Size winSize = Director::getInstance()->getVisibleSize();
float gradientAlpha = 0.7;
Vec2 vertices[4];
Color4F colors[4];
int nVertices = 0;
vertices[nVertices] = Vec2(0, 0);
colors[nVertices++] = (Color4F){0, 0, 0, 0 };
vertices[nVertices] = Vec2(winSize.width, 0);
colors[nVertices++] = (Color4F){0, 0, 0, 0};
vertices[nVertices] = Vec2(0, winSize.height);
colors[nVertices++] = (Color4F){0, 0, 0, gradientAlpha};
vertices[nVertices] = Vec2(winSize.width, winSize.height);
colors[nVertices++] = (Color4F){0, 0, 0, gradientAlpha};
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_COLOR | GL::VERTEX_ATTRIB_FLAG_POSITION);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 内置函数可查看CPP中的成员变量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- Cpp-Tuple使用带有变量的get
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何通过cpp程序运行shell脚本
- 使用2个键的cpp-stl::优先级队列排序不正确
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我需要知道编译器如何在cpp中使用析构函数
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 无法在UE4中包含BP类到CPP类
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- OpenGl代码不适用于cocos2dx- 3.0与VS2013 CPP