Saturday, May 08, 2010

Minute Mistakes Franatic Frustration

I've been working on updating our good old game "Adrian". Trying to update the graphics engine to use shaders, frame buffers and all that stuff. Actually the other day i found a wonderful game dev blog which discussed various graphics engine tweaks, implementations etc. I've been eager to work on something with shaders.. thought Adrian might be the best bet. So i quickly started writing some basic code for handling multiple pass rendering and some basic code for handling frame buffer objects etc. I've read a lot about glsl .. even done some examples (simple ones) before so i did not think it would hard to get stuff running.. boy was i wrong.. I struggled to get multiple textures to work in my shaders.. and the reason?.. well let me put it in code...
//vertex shader
void main()
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexcoord0;

//fragment shader
uniform sampler2D tex0;
uniform sampler2D tex1;
void main()
gl_FragColor = texture2D(tex0, gl_TexCooord[0].st) * texture2D(tex1, glTexCoord[0].st);
Looks simple right?? now how do you send / bind the uniform variables to two different textures from host program??
glBindTexture(GL_TEXTURE_2D, tex0_id);
glBindTexture(GL_TEXTURE_2D, tex1_id);

GLint h0 = glGetUniformLocation(program, "tex0");
glUniform1i(h0, 0);
GLint h1 = glGetUniformLocation(program, "tex1");
glUniform1i(h1, 1);

now the most obscure stuff is setting the sampler handle values to 0, 1, .. corresponding to the texture you bind it to..(GL_TEXTURE0, 1.. ) instead of the texture id itself.. and now most ridiculously frustrating part.. you need to call glUseProgram(program) before you can set these variables. Most people got the other stuff wrong.. but i was stuck with this problem for like 12hrs or so... actually i just figured out about this problem and thought i should document it somewhere before i forget it again.. so there you go.. if you want to tread in the glsl waters.. you better be careful.. they are very .very.. weird.. :D
Post a Comment