PDA

View Full Version : multiple shaders , rendertargts, bad result



lobbel
04-16-2009, 07:08 AM
Hello,

i have a strange result when combining shaders. I loaded two shaders.
A perpixel lightning and a glow-shader. Seperatly both are working.
If I only perform the perpixel lightning it works, and if i only perform
the glow shader it works as well.
But when i try to combine those shaders something goes wrong.
I use the glutSolidTeapot in my example. Applying only one shader
(perpixel light, or glow) on the teapot i can rotate and move it.
Applying these two effects i cant rotate the teapot. I only see the
teapot pretty close on the top.

please have a look at the draw method

static GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT2_EXT};
RTT.BeginMRT(2, mrt); // render original teapot in target 0 and 2
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
shader2.SetParameterModelViewProjectionMatrix(CSha derObject::VERTEX,"modelViewProj");
shader2.SetParameterModelViewMatrixInv(CShaderObje ct::VERTEX,"modelViewInv");
shader2.SetParameterFloat3v(CShaderObject::VERTEX,"lightPosition",m_Camera.GetCameraPosition().Array());
shader2.SetParameterFloat3v(CShaderObject::VERTEX,"eyePosition",m_Camera.GetCameraPosition().Array());

shader2.Begin();
//glColor3f(0.95,0.57,0.25);
::glutSolidTeapot(1.0);
shader1.End();
RTT.End();

RTT.Begin(1);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
cgfx.CgFxSetupSampler("SrcSampler",RTT.GetTextureID(0));
cgfx.CgFxBegin("pass0");
RTT.DrawFullscreenQuad();
cgfx.CgFxEnd();
RTT.End();

RTT.Begin(0);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
cgfx.CgFxSetupSampler("SrcSampler",RTT.GetTextureID(1));
cgfx.CgFxBegin("pass1");
RTT.DrawFullscreenQuad();
cgfx.CgFxEnd();
RTT.End();

// draw teapot texture
RTT.EnableGLBlendColor();
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
cgfx.CgFxSetupSampler("SrcSampler",RTT.GetTextureID(2));
cgfx.CgFxBegin("pass3");
RTT.DrawFullscreenQuad();
cgfx.CgFxEnd();

// glow texture
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
cgfx.CgFxSetupSampler("TmpSampler",RTT.GetTextureID(0));
cgfx.CgFxBegin("pass2");
RTT.DrawFullscreenQuad();
cgfx.CgFxEnd();
RTT.DisableGLBlendColor();

RTT.Begin()

////////////////////////////////////////
// unbind texture
////////////////////////////////////////
glBindTexture(GL_TEXTURE_2D, 0);
////////////////////////////////////////
// bind framebuffer
////////////////////////////////////////
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Framebuffer);
////////////////////////////////////////
// bind renderbuffer
////////////////////////////////////////
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Framebuffer);
////////////////////////////////////////
// save current attribs
////////////////////////////////////////
glPushAttrib(SaveAttrib);
////////////////////////////////////////
// align viewport
////////////////////////////////////////
glViewport(0, 0, m_uiWidth, m_uiHeight);
////////////////////////////////////////
// set active attachment (target)
////////////////////////////////////////
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT+Target);
////////////////////////////////////////
// clear buffers
////////////////////////////////////////
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
////////////////////////////////////////
// fill cleared buffer with values
////////////////////////////////////////
glClearColor(0,0,0,0);

RTT.End()

////////////////////////////////////////
// unbind framebuffer
////////////////////////////////////////
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
////////////////////////////////////////
// pop attribs
////////////////////////////////////////
glPopAttrib();


I guess it has something to do with matrix transformation. But I dont understand how it could effect the other shader.
I also checked out if the glow shader is the reason for my problem, but it isnt. I wrote a simple passthrough shader.
And its the same result. Perpixel lightning + passthrough = wrong result. Teapot is not moving.

here are the links to pictures showing you what im talking about
http://rapidshare.com/files/222047298/glow.jpg
http://rapidshare.com/files/222047562/perpixel.jpg
http://rapidshare.com/files/222047694/perpixel_and_glow.jpg

Any ideas ?

thanks,
cmos

ephtracy
04-16-2009, 08:25 PM
some of your codes made me a little confused :



shader2.Begin();
//glColor3f(0.95,0.57,0.25);
::glutSolidTeapot(1.0);
shader1.End();

begin with 'shader2' but end with 'shader1'?



// bind framebuffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Framebuffer);

// bind renderbuffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Framebuffer);

did you really bind 'Renderbuffer'?

lobbel
04-17-2009, 01:11 AM
Hi there, thanks for your reply. I fixed the appearence of the two glBindFramebufferEXT. Now im binding a framebuffer and a renderbuffer.

So, here is also the updated code. The names of the shaders were confusig. Now shader1 performs the per pixel lightning and shader2 is a simple passthrough shader. So, the result is the same as before.


RTT.Begin(0);
glPolygonMode( GL_FRONT_AND_BACK,
GL_FILL); shader1.SetParameterModelViewProjectionMatrix(CSha derObject::VERTEX,"modelViewProj"); shader1.SetParameterModelViewMatrixInv(CShaderObje ct::VERTEX,"modelViewInv"); shader1.SetParameterFloat3v(CShaderObject::VERTEX,"lightPosition",m_Camera.GetCameraPosition().Array()); shader1.SetParameterFloat3v(CShaderObject::VERTEX,"eyePosition",m_Camera.GetCameraPosition().Array());
shader1.Begin();
::glutSolidTeapot(1.0);
shader1.End();
RTT.End();

glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
shader2.SetParameterSampler(CShaderObject::FRAGMEN T,"texture",RTT.GetTextureID(0));
shader2.Begin();
RTT.DrawFullscreenQuad();
shader2.End();



greetings,
cmos

lobbel
04-17-2009, 02:11 AM
Ok, whether there is a bug with the Cg runtime or I did something wrong. It's workign now.

I sawpped these lines


// pass those first shader1.SetParameterFloat3v(CShaderObject::VERTEX,"eyePosition",m_Camera.GetCameraPosition().Array());
shader1.SetParameterFloat3v(CShaderObject::VERTEX,"lightPosition",m_Camera.GetCameraPosition().Array());

// matrix last
shader1.SetParameterModelViewProjectionMatrix(CSha derObject::VERTEX,"modelViewProj");
shader1.SetParameterModelViewMatrixInv(CShaderObje ct::VERTEX,"modelViewInv");


it should make no different which order passing the uniforms to a shader. but in this case it does. i dont know why.

any suggestions anyway ?

greetings,
cmos

ephtracy
04-17-2009, 10:55 AM
i also don't think the order of passing uniforms is the reason.

i have no idea how you implemented your 'CShaderObject' class,
does the 'shader.SetParameter[...]' functions equivalent to 'glUniform[...]' functions?

i would active the shader program first before passing uniforms to it.



shader2.Begin();
shader2.SetParameterModelViewProjectionMatrix(...) ;
shader2.SetParameterModelViewMatrixInv(...);
...
shader2.End();


all right, i really have no more ideas about this:)