Topic: Fullscreen shaders and effects.

I have recently been sent a very nice .blend file by my artist. It has some very nice effects in it... but they're made of shells... I've been wanting to stretch my legs with shaders and thought this would be a good excuse.

The first question, is there a reference for the standard shaders, which values are automatically filled?

Secondly, does Maratis have any way to apply a full screen shader easily? Should I just create a full screen quad in a UI level (/manually in code). I should have access to the depth buffer, right?

What I'm wanting to do is put a light grain on the screen, maybe volumetric fog and, if I get really carried away, volumetric lighting. All of these need to be applied to the full screen rather than per object.

Re: Fullscreen shaders and effects.

I'm looking for the same thing, I want to create some global GLSL effect like HDR, DOF, fog, bloom, etc...

Re: Fullscreen shaders and effects.

I guess when I find out, I will add it to the tutorial then too smile

Re: Fullscreen shaders and effects.

Hi,

I started an example of post processing some time ago, but I didn't had time to finish it to publish on the website,
it also need a recent version of Maratis from svn.

It is possible to use render to texture and shaders in c++ to do post processing,
here is a (work in progress) example blurring the frame :

(custom MGame::draw)
http://www.maratis3d.com/code/post-process/

Re: Fullscreen shaders and effects.

That's perfect, thank you smile

Re: Fullscreen shaders and effects.

Ho, and for your first question, in the shaders example, the "standardDSEN" shader is the same as the one hard-build into Maratis (just less optimized according to the textures really used) :

http://www.maratis3d.org/?p=548

Re: Fullscreen shaders and effects.

Right, I've finally had time during my lunch break to get this working properly. I have it loading the shader from a file and I have it (theoretically) reloading when I press spacebar. It's a bit flakey for some reason, and I don't like the fact that I have to give an absolute path for the shader filenames. Can I change M_fopen to look for files relative to MaratisPlayer, then the mproj, then global maybe?

Anyway, I'm now pulling apart the drawTexturedQuad code and I'll probably put this into a PostRenderManager type thing and stick it into the tutorial tongue The thing I don't understand though, how is the texture being passed to the shader? From what I can see, it's sending an empty texture, I assume it probably filles it with whichever texture has been passed to bindTexture. What's the easiest way to get the depth buffer passed in too?

Re: Fullscreen shaders and effects.

the texture is send by :
render->bindTexture(m_colorTextureId);

to pass the depth texture to the shader, I guess it should be something like :
render->bindTexture(m_depthTextureId, 2);

(that you access as multitexture 2)

Re: Fullscreen shaders and effects.

Thanks smile Just what I wanted smile Will get on this tomorrow

Re: Fullscreen shaders and effects.

I've been having a play around with this and I can't seem to get the depth buffer texture to display at all... I'm basically using the post processing example, then I've added the depth texture to be bound before drawing the quad

    render->bindTexture(m_colorTextureId);
    render->bindTexture(m_depthTextureId, 1);
    drawTexturedQuad(MVector2((float)screenWidth, (float)screenHeight));

and I've passed through an array of textures in the drawing itself

    int texIds[4] = { 0, 1, 2, 3 };
    render->sendUniformInt(m_postProcessor->ExposeShader(), "Texture", texIds);

(I have started refactoring a little and moved the shader loading and initialisation into an RIIA class, but uniforms aren't in there yet, hence the ExposeShader) However, I can't access it in the shader at all and I'm not sure why. The fragment shader is, right now, incredibly straight forward...

varying vec2 texcoord;
uniform sampler2D Texture[4];
                            
void main(void)
{
    vec3 color = texture2D(Texture[1], texcoord).xyz;
    
    gl_FragColor = vec4(color.x, color.y, color.z, 1.0);
}

I'm clearly doing something mind numbingly stupid, but I'm not sure what right now. (and I've tried it with binding to 2 and accessing it any way I can think of)

Re: Fullscreen shaders and effects.

hi,
you need to add the table number with sendUniformInt if it's not a single int :

int texIds[4] = { 0, 1, 2, 3 };
render->sendUniformInt(m_postProcessor->ExposeShader(), "Texture",  texIds, 4);

m_postProcessor->ExposeShader() return the FX id right ?

Re: Fullscreen shaders and effects.

in case you want to see more rendering code as example, you can have a look to the standard renderer :
http://code.google.com/p/maratis/source … nderer.cpp

maybe a bit complex but playing with shaders

Re: Fullscreen shaders and effects.

Seems like it is my mistake.
I will try to fix it tomorrow.

I'm developing app for iOS with maratis3d.
I made checkout of maratis3d.
I tried to replace MGame object in iOS example project in EAGLView.mm with object of MyGame class from Post Processing example (http://www.maratis3d.com/code/post-process/.).

        game = new MyGame();
//        new MGame();

I ran this project on the device (iPad), and it stops in the debugger in MyGame.cpp with EXC_BAD_ACCESS, because MEngine::getInstance()->getRenderingContext() returns NULL.
I tried to place

  game = new MyGame();
 engine->setGame(game);

after

[self loadMaratisProject:filename];

.
App runs with black screen.
What I'm doing wrong?
How should I replace MGame  with MyGame?

Last edited by Petr (2012-08-29 14:55:04)

Re: Fullscreen shaders and effects.

To put it after [self loadMaratisProject:filename]; is good, because MyGame use the rendering context in it's constructor.
The screen is probably black because the shader is not working, look at the xcode console output to see if there is error messages during the shader compilation.

Also try to add this to the shader in MyGame :

#ifdef GL_ES
precision highp float;
#endif

Re: Fullscreen shaders and effects.

I've changed shader to include #ifdef GL_ES ... and placed

MGame *game = new MyGame();
engine->setGame(game);

after loadMaratisProject.
Also, I set  [UIScreen mainScreen].bounds.size.height and ...width instead of  RESOL macro in MyGame.cpp  :

render->texImage(0, [UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height, M_UBYTE, M_RGB, 0);
...
render->texImage(0, [UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height, M_UBYTE, M_RGB, 0);
...
render->setViewport(0, 0,  [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height);

Program runs , there is no errors in console.
Screen remains black.
Program responds on touches - messages like "2012-08-30 10:23:20.941 example[9002:707] Updated Touch 0 at position 651, 259." appears in the console.
I will try to compare code of MGame::draw() and MyGame::draw() and change the latter.
Can you give me a quick tip how to do that correctly?

Also, when I launch program with MyGame, when the device changes its orientation, message
"Failed to make complete framebuffer object 8cdd" appears in the console.
When I launch project with MGame, all works fine and there is no such message

Last edited by Petr (2012-08-30 06:41:17)

Re: Fullscreen shaders and effects.

I  MyGame::drawTexturedQuad there are lines

int texId = 0;
render->sendUniformInt(m_fx, "Texture", &texId);

Is that right?
Or it must be

int texId = m_colorTextureId;
render->sendUniformInt(m_fx, "Texture", &texId);

?

Re: Fullscreen shaders and effects.

Hi,

int texId = 0 is correct, it specify the multitexture-id (0 to 7), the texture id is given by "bindTexture".

For the screen still being dark, try to disable the real time shadow if there is in the scene, I'm not sure iOS can manage multiple render to texture at the same time (real time shadow uses one).

I never tried post processing in iOS, I'll see if I can test it too.

Re: Fullscreen shaders and effects.

Ok, I will try

Where should I check for them?

Last edited by Petr (2012-08-30 07:59:09)

Re: Fullscreen shaders and effects.

open the level with a text editor and search for "shadow="true"",
or modify the level with Maratis editor.

Re: Fullscreen shaders and effects.

I replaced shadow="true" with shadow="false" where it occurs. Nothing changed.
I've tried to add

 render->bindFrameBuffer(1);

in MyGame::draw() before

// draw the rendered textured with a shader effect
    render->setViewport(0, 0, screenWidth, screenHeight);
    render->setClearColor(MVector3(1, 0, 0));
    render->clear(M_BUFFER_COLOR | M_BUFFER_DEPTH);
    
    set2dMode(screenWidth, screenHeight);
    
    render->bindFX(m_fx);
    render->bindTexture(m_colorTextureId);
    drawTexturedQuad(MVector2((float)screenWidth, (float)screenHeight));
    render->bindFX(0);

(1 is a framebuffer that created in ES2Renderer)
Now screen blinks with red/black color every 3-5 seconds (I think, this is due to     render->setClearColor(MVector3(1, 0, 0));
), but no game image appears on screen.
Also there is no message "Failed to make complete framebuffer object 8cdd" when device change its orientation.
Looks like that scene is not drawn into the texture.
I will check  attachFrameBufferTexture now , may be it is not valid/allowed in GL ES or iOS.

Re: Fullscreen shaders and effects.

I think I have an idea,
on iOS the current frame buffer is not 0,

in MyGame try replacing :

// finish render to texture
render->bindFrameBuffer(0);

by :

// do this before render->enableDepthTest();
unsigned int currentFrameBuffer = 0;
render->getCurrentFrameBuffer(&currentFrameBuffer);

// finish render to texture
render->bindFrameBuffer(currentFrameBuffer);

Re: Fullscreen shaders and effects.

Ok Petr, I got it working on iOS,
I found a bug in MES1Context.cpp and MES2Context.cpp, it's corrected on svn.

That plus "render->bindFrameBuffer(currentFrameBuffer);" instead of "render->bindFrameBuffer(0);" will make it work.

Re: Fullscreen shaders and effects.

I have found this bug too (GL_COLOR_ATTACHMENT0 + ((int)type - 2);), and fix it,
I also replace

render->bindFrameBuffer(0);

with

unsigned int currentFrameBuffer = 0;
render->getCurrentFrameBuffer(&currentFrameBuffer);
render->bindFrameBuffer(currentFrameBuffer);

but program continue to  run with red/black screen.
I have found that after

    render->texImage(0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height, M_UBYTE, M_DEPTH, 0);

in MyGame:MyGame() call to glGetError() returns 0x500   (1280 in dec), GL_INVALID_ENUM.
I was not able to fix it yet.

If you have a working code of post processing on iOS ,can you post it please?

Also

unsigned int currentFrameBuffer = 0;
render->getCurrentFrameBuffer(&currentFrameBuffer);
render->bindFrameBuffer(currentFrameBuffer);

sets the frame buffer to m_renderBufferId, which is used to render into textures.
I think we need to set

render->bindFrameBuffer(1);

to make it work.

Last edited by Petr (2012-08-30 13:41:53)

Re: Fullscreen shaders and effects.

I tested the renderToTexture example : http://www.maratis3d.org/download/render-to-texture.zip
(the example use a build-in feature to set a camera to render to a texture from script)

I'll see if I can test the cpp example too.

Re: Fullscreen shaders and effects.

Thank you! If you can do so it would be very good.