# Multitexturing with four texture units...?

This topic is 4487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hiya! I've just stepped into the wonderful world of multitexturing... but I'm having quite a hard time getting the "blending" right... My problem is that I dont know how to set GL_TEXTUREi_ARB to have the corresponding amount of texture so that all four add up to 1... :( * I want the terrain to blend between four textures. * The textures are all loaded and works fine separately. * I have a "heightmap" , well sort of, that contains the amount of the various textures each vertex should contain (four total textures become a total of 1). Also, I need to set this value for every vertex, would that be too slow and how can this be done? Currently I'm using...
	// TEXTURE-UNIT #0:
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[Terrain1].texID);
glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);

// TEXTURE-UNIT #1:
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[Terrain2].texID);
glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_COMBINE_EXT);

... Plus two more texture units...

...but if I add more than two textures colors become either washed out or too dark... This can easily be fixed if I learn how to set the mix-values to those already calculated in my map! :D Hope someone understands me and can help! :D

##### Share on other sites
To rephrase myself;

How can I set the blending amount of each texture per vertex?
lets say a vertex has Terrain1,Terrain2,Terrain3,Terrain4 and each of these are a number low enough so that the total sum is 1.

how can I draw Texture1 with Terrain1 "strength" and Texture2 with Terrain2 "strength" and so on..?

Anyone?

I'm really not sure what to even look for...

##### Share on other sites
You can't do this just with the GL_ARB_texture_env_combine combine modes. You will need to use either GL_ATI_texture_env_combine3 and GL_MODULATE_ADD_ATI combine mode, depending on which your card supports. I've never used the nvidia extension so I'll give the example using ati's. It should be similar enough though, just check the extension specs.
//blend* are floats with the correct blending amounts for GL_TEXTURE*float tex0const = {blend0, blend0, blend0, blend0};float tex1const = {blend1, blend1, blend1, blend1};float tex2const = {blend2, blend2, blend2, blend2};float tex3const = {blend3, blend3, blend3, blend3};//Set up texunit0 with regular modulate combine mode to get (tex0*blend0)glActiveTexture(GL_TEXTURE0);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, tex0);glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT);glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, tex0const);//Set texunit1 with GL_MODULATE_ADD_ATI to get (tex1*blend1)+prevglActiveTexture(GL_TEXTURE1);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, tex1);glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE_ADD_ATI);glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT);glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, tex1const);//Set texunit2 and texunit3 similarly to get (tex2*blend2)+prev//and (tex3*blend3)+prev

I didn't test that code so there might be little errors, but that's the jist of what you need. See the specs for more info on how to use these extensions.

Now with all that said, by far the easiest way to get what you want is to use fragment shaders. Then all you would need to do is
color = tex0*blend0 + tex1*blend1 + tex2*blend2 + tex3*blend3;
but I'm not sure if you've used shaders before or are up to learning about them just yet.

##### Share on other sites
Check this site, which is an awsome resource for doing exactly what you want to do. However, rather than using the heightmap to store your blend factors, I would go with using the vertex color array. This has the added benefit of allowing you to generate the blend factors procedurally, so you may change them on the fly if need be. After pouring over the above article and all of the included resources and source code, if you you still have problems, PM me.