Search code examples
c++cocos2d-x

Rendering to a CCRenderTexture not working


I'm trying to use CCRenderTexture to create a heightmap to use with the Terrain class. I don't know if this is the best way to do it, I'm a newb to both opengl and cocos2d-x, so please bear with me.

auto* renderTexHeightMap = CCRenderTexture::create(width, height);
renderTexHeightMap->begin();
glRasterPos2i(0, 0);
glDrawPixels(width, height, GL_RGB, GL_FLOAT, pixelBuffer);
renderTexHeightMap->end();

renderTexHeightMap->saveToFile("heightmap.jpg", false);

I know that pixelBuffer contains the data that I want (greyscale pixel data), but whenever I call CCRenderTexture::saveToFile all I get is a black picture. What am I missing?


Solution

  • rendertexture will delay one frame to render ,so you need to saveToFile at next frame,my english not good ,do you anderstand? you can use DelayTime to do it or another way my way: my code type is lua

    local function save()
      renderTexture:saveToFile("heightmap.jpg",false)
    end
    local callfunc = cc.CallFunc:create(save)
    local dela = cc.DelayTime:create(0.01)
    local seq = cc.Sequence:create(dela,callfunc)
    node:runAction(seq)