Monday, May 24, 2010

Adrian Got Shadow Mapping

I've finally finished adding another feather in the HD league. Shadows are a good way of showing the object positions etc in the world. I've finally added the ShadowMapping pass to our codebase and have actually cleaned up the code a little to treat the Shaders as objects rather than just the GLuint handles. Any way It's far from perfect, the aliasing effect is pretty evident.. the depth map is pretty high res (equal to the size of the rendering buffer) but the artifacts are there because of the huge frustum size that i had to specify for the light so as to fit the whole map.

Any way It's a good start. Ill' be looking out for ways to fix this but for now I think this is a good feature to have. Finally one of the games i created.. has shadows :D

It took me a while to get the thing working.. i was stuck with various silly problems.. but im' glad i got it done. For some tech details.. here is the sequence that i use to get the shadow rendering done. (Note .. the shadow map is rendered before every frame even for the static objects (map) but that's the only solution when we don't have a proper terrain etc to do shadow mapping offline and use that.)

  • Create FrameBuffer with DepthBuffer only
  • Enable and Set PolygonOffset(1.0, 1.0) // generally used value.
  • Render The scene you want to get a shadow of (map + hero) from lights perspective
  • Store the light matrix.(Light_MVP)
  • Disable PolygonOffset
  • Use Vertex and Fragment shaders for setting the color based on shadow map. (depth buffer sent as texture and is the ShadowMap)
  • Send the Light_MVP to shader using mat4 uniform variable. (not using the texture matrix hack, coz this is cleaner to me).
You can check out the code @code.google.com/p/adrian

Sunday, May 9, 2010

Post Processing

If you have seen my prev post, you already know iv'e been hard at work creating something using the GLSL Shaders. Well iv'e been adding a Post-Processing pipeline to Adrian's graphics engine. And i'm really excited about the results iv'e achieved with it .. So without further ado.. here are the screenies..



I know the toon shader has been done a million times now.. but the special thing about this one is ... it is done as a post-process step instead of while rendering. I know it sux.. but if you can achieve one more effect during post processing its just icing on the cake .. isn't it? :D

Saturday, May 8, 2010

GLSL MultiTexture CheckList

Most of the tutorials about glsl cover the GPU side of things and generally leave out the host (CPU) side of the code to your imagination.. well not exactly .. but they assume you get it right. Turns out.. its' not that straightforward afterall.. or atleast not for me. So Here is a check list you need to see for getting multi texturing (multiple uniform variables in Fragment shader) to work.

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex0id); // general texture bind call we all know and love.
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex1id);

glUseProgram(program); //program is the shader program you create using glCreateProgram() call.
GLint h0 = glGetUniformLocation(program, "tex0");
GLint h1 = glGetUnifromLocation(program, "tex1");
glUnifrom1i(h0, 0); //first texture sampler
glUniform1i(h1, 1); //second texture sampler.
The code for Fragment Shader corresponding to the above cpu code is as follows
uniform sampler2D tex0;
uniform sampler2D tex1;
void main()
{
gl_FragColor = texture2D(tex0, gl_TexCoord[0].st) + texture2D(tex1, gl_TexCoord[0].st);
}
So Now the check list:
  • Use glActiveTexture for each texture unit
  • Use 0, 1, .. texture unit index while assigning values to uniform values.
  • Call glUseProgram() before trying to set uniform values.
  • Bind texture with texture id for each glActiveTexture.
I know this might look silly but iv'e seen a lot of people asking for this particular problem.. and sadly.. I struggled to get this right too... and this is a post to make sure i dont' ever have to go through that pain again...