Textures and shaders

This tutorial will be short, basically we are going to add texturing to the shader equation.
So what do we need for this one.
First we need to be able to send uniforms to the shaders, now uniforms are constants that you set and they can control many different aspects of the shader, however unlike the varying variables these variables will not interpolate across the polygons.
In this tut we will use them to send the sampler cords which is sort of stupid since it’s just a number like 0 or 1, but it has to be done.
I will use this function for this job, i got several more of these for other types of variables included in the code.

void sendUniform1i(char var[60], int v)
GLint loc = glGetUniformLocationARB(ProgramObject, var);
if (loc==-1) return; // can't find variable

glUniform1iARB(loc, v);

Then we need to load textures, fortunately i added a load texture function, it’s a nifty little thing, ok it’s a jumbled mess of crappy code, but it does work, most of the time.
Anyway any texture loading function will do it’s no biggie, i load the textures like this.


The “2” tells the loader to use trilinear filtering with ansitopic filtering.

Next up is the multi texturing code, it’s par of the rendering code so just let’s see it all at once.

if (useshader) glUseProgramObjectARB(ProgramObject);
sendUniform1i("texunit0", 0);
sendUniform1i("texunit1", 1);




if (useshader) glUseProgramObjectARB(0);

Finally the shader code itself (we only change the fragment shader code)

uniform sampler2D texunit0;
uniform sampler2D texunit1;
varying vec4 pos;

void main( void )

vec4 base = texture2D(texunit0, gl_TexCoord[0].xy);
vec4 ambi = texture2D(texunit1, gl_TexCoord[0].xy);

gl_FragColor = base * ambi;

This shader reads from booth textures and then multiplies them together, simple as always, the next one will be more complicated as we add ppl lighting, wohoo.
Download this tutorial for MSVCPP 6.0

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

WordPress Themes