Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

105 Neutral

About Smoove

  • Rank
  1. Smoove

    Glow Shader

    Hi,   I found this topic, which is related to my question, but does not answer it... So I thought it'd be the right place to ask !   I am trying to achieve Glowing in a 3D environment using Direct X 9. To get started I made a very simple scene: camera faces a flat, white square. No lights.   To understand and implement the effect I used mostly these two sources: http://devmaster.net/posts/3100/shader-effects-glow-and-bloom http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/     Now before I start copy&pasting large chunks of code, I thought I'd explain my understanding of the algorithm and let you guys go "NO! You got it completely wrong!" ... :)   Main steps:   1. Render the scene to a empty 2D texture 2. Blur the resulting 2D texture 3. Render the scene to the back buffer as usual, applying the previously created texture     Key things according to my understanding:   1. The render-to-texture stage uses a custom pixel shader that is responsible for blurring it i.e. once rendering is finished, the "image" produced is already blurry. There is no need of back processing the texture pixels at C++ level. 2. This shader uses the "gaussian" method to create the blur effect, but that is almost irrelevant; what matters is to take each pixel and to spread its color to the surrounding pixels with a very low alpha: some pixel will thus stack more color than others (typically less on the edges)  and therefore have a higher alpha value (again, lower on the edges) 3. Finally rendering the scene using the produced, blurred texture is done with transparency so that my square appears covered  by a "cloud" that goes beyond its edges.   Note that this last point confuses me: I render my square (to the backbuffer) with texture coordinates of 0 to 1, so I would have thought that the blurred texture would be contained within the limits of the quad, no?     I am more than happy to share the entire code, if necessary, but here are the key sections, as a quick glance:     The Quad definition: Vertex vertices[] = { {-1.0f, 1.0f, 0.0f, 0.0f, 0.0f}, {-1.0f,-1.0f, 0.0f, 0.0f, 1.0f}, { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f}, { 1.0f,-1.0f, 0.0f, 1.0f, 1.0f} }; Rendering code: // Render to texture device->SetRenderTarget(0, textureSurface); device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0); device->BeginScene();  device->SetTransform(D3DTS_WORLD, &(R * T)); device->SetTransform(D3DTS_PROJECTION, &PT);     effect->Begin(&passes, 0);     for (UINT p = 0; p < passes; p++)     {         effect->SetTechnique("Blur");         effect->SetMatrix("WorldViewProj", &((R * T) * V * PT));         effect->BeginPass(p);         device->SetFVF(FvF);         device->SetStreamSource(0, buffer, 0, sizeof(Vertex));         device->SetTexture(0, NULL);         device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);         effect->EndPass();     }     effect->End(); device->EndScene();         // Render to back buffer device->SetRenderTarget(0, backbufferSurface); device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(000, 000, 000), 1.0f, 0); device->BeginScene();  device->SetTransform(D3DTS_WORLD, &(R * T)); device->SetTransform(D3DTS_PROJECTION, &PB); device->SetFVF(FvF); device->SetStreamSource(0, buffer, 0, sizeof(Vertex)); device->SetTexture(0,renderingTexture); device->SetRenderState(D3DRS_ALPHABLENDENABLE,true); device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTCOLOR); //D3DBLEND_INVSRCALPHA); device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE); device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); device->EndScene(); device->Present(NULL, NULL, NULL, NULL); Pixel shader for the render-to-texture phase: PS_OUTPUT ps_main( in PS_INPUT In ) { float offset[5] = { 0.0, 1.0, 2.0, 3.0, 4.0 }; float weight[5] = { 0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162 };          PS_OUTPUT Out = {float4(0.0,0.0,0.0,0.0)};          for (int i = 0; i < 5; i ++)      {         Out.Color += tex2D( Tex0, ( In.Texture + float2(0.0, offset[i]) ) / 1024.0 ) * weight[i];         Out.Color += tex2D( Tex0, ( In.Texture - float2(0.0, offset[i]) ) / 1024.0 ) * weight[i];     }          return Out; } I hope this makes all sense. Do let me know, if there is anything unclear.   Remark: the code above does not give me a completely incorrect result. The main square appears textured with a slightly transparent inner square.   Any help/advice/comment would be much appreciated! Many thanks! Philippe
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!