Jump to content

Pass textures to post shaders


photo

Recommended Posts

Hi,

I'm trying to pass multiple textures to post shader in runtime. The whole idea is to imitate delayed rendering (when new image is rendering upon old image after several milliseconds have passed). After some research using documentation and forum I've found Screenshot and TextureRender samples. But I still can't understand how the parameter passing works.

There is this chunk of code (Screenshot.cpp):

void screenshot_callback(Renderer *renderer)
    {
        if (Game::get()->getTime() < next_screenshot)
            return;

        next_screenshot += SCREENSHOT_TIMEOUT;

        RenderState *render_state = RenderState::get();

        TexturePtr color = renderer->getTextureColor();
        
        TextureRenderPtr texture_render = renderer->getPostTextureRender();

        render_state->saveState();
        render_state->clearStates();
        texture_render->setColorTexture(0, texture);
        texture_render->enable();
        {
            Render::get()->renderPostMaterial("render_copy_2d", color);
        }
        texture_render->disable();
        texture_render->unbindColorTexture();
        render_state->restoreState();
    }

I do not understand the dependency of color and texture variables and what they represent in code.

Also there is another question: how does texture variable "understand" that it needs to get the whole window screenshot? The only initialization given in the same file (Screenshot.cpp) is:

void init_video_resources()
    {
        if (texture && (texture->getWidth() != sprite->getWidth() || texture->getHeight() != sprite->getHeight()))
            texture.clear();

        if (!texture)
        {
            texture = Texture::create();
            texture->create2D(sprite->getWidth(), sprite->getHeight(), Texture::FORMAT_RGBA8, Texture::USAGE_RENDER | Texture::FILTER_POINT);

            sprite->setRender(texture, !Render::get()->isFlipped());
        }
    }

So it is not clear when texture is getting its contents...

 

Or maybe there is different approach that I don't see as an answer to my problem. I would be very grateful if you could help me out with it.

Edited by naumova.natalya
Link to comment

The "color" texture is the colour target of the main Renderer (see the line where "color" is created), and the screenshot_callback() method is invoked exactly at the _end_ of the main Renderer's rendering - that's what CALLBACK_END means in the addCallback() call elsewhere in the file. So at the point when the above code runs, "color" contains the final colour output of the main render.

texture_render->setColorTexture(0, texture);
texture_render->enable();
{
	Render::get()->renderPostMaterial("render_copy_2d", color);

These three lines invoke the "render_copy_2d" material post-process to render a copy of "color" into "texture". This first line sets "texture" as the colour target for the framebuffer called "texture_render", which the second line then enables as the active render target so that the third line can render a copy of the "color" texture into it.

  • Like 1
Link to comment
×
×
  • Create New...