Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm currently making a cube style game. With chunks being drawn with VBOs. I'd like to know if there is a way to create an overlay texture on top of the existing texture without the need to rebuild the VBO. This is required to show activity in a cube (think of Minecraft when destroying a block and the little cracks start spreading).

I believe these are called "decals" but I've not found how to use them with VBOs. So how do I draw decals on OpenGL VBO drawn cubes?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Try to render your VBO with two textures: one as the diffuse map (the block texture) and one showing the crack.

You can combine them in a shader:

// GLSL fragment shader

#version 130

uniform sampler2D texDiffuse;
uniform sampler2D texCrack;
uniform float uniCrackAmount; // between 0 and 1

in vec2 vertTexCoord;

out vec4 fragColor;

void main()
{
    vec4 color_diffuse = texture(texDiffuse, vertTexCoord);
    vec4 color_crack = texture(texCrack, vertTexCoord);

    fragColor = color_diffuse + (color_crack * uniCrackAmount);
}
share|improve this answer
    
I could see that working if I was only drawing one cube. But how am I supposed to only draw the cracked texture on the faces of the cube I want, out of hundreds of cubes? Note that it could need to be multiple cubes as well. –  GameDev-er Jun 22 '12 at 17:00
1  
Use a vertex attribute. Give each vertex of a cube a float that tells the shader to draw a certain amount of cracked texture on top of it. –  knight666 Jun 22 '12 at 17:26
    
So the vertex attribute goes into the VBO? Seems like it's not a very good solution. That could add thousands of floats, just for the handful of floats I'd want to utilize. Any non shader options? –  GameDev-er Jun 22 '12 at 17:40
1  
Well, you wanted to render thousands of cubes, so whatever solution you come up with, it's going to cost a bit of memory. ;) –  knight666 Jun 22 '12 at 18:06
1  
Thousands of floats is not a huge memory overhead. We're talking kilobytes here; this isn't 1996. Sometimes trading extra memory usage is a fair exchange for increased performance or flexibility. –  Jimmy Shelter Jun 22 '12 at 18:13
show 4 more comments

For a non-shader solution you could draw in two batches. First of all you flag the cubes in the current scene that need the overlay texture - this can be done during your scene tree traversal or other scene setup. Then draw the regular cubes using your normal draw routine. Finally draw the overlay-textured cubes using multitexturing - you can experiment with a decal or interpolate TexEnv for this.

The VBO setup would just use the same set of texcoords for both texture units; something like this (not intended as copy-and-paste code):

glClientActiveTexture (GL_TEXTURE0);
glEnableClientState (GL_TEXTURE_COORD_POINTER);
glTexCoordPointer (2, GL_FLOAT, sizeof (myvertextype), (void *) 12);

glClientActiveTexture (GL_TEXTURE1);
glEnableClientState (GL_TEXTURE_COORD_POINTER);
glTexCoordPointer (2, GL_FLOAT, sizeof (myvertextype), (void *) 12);

// drawing stuff

glClientActiveTexture (GL_TEXTURE1);
glDisableClientState (GL_TEXTURE_COORD_POINTER);

glClientActiveTexture (GL_TEXTURE0);
glDisableClientState (GL_TEXTURE_COORD_POINTER);

But otherwise - yeah - the shader-based solution would be preferable. Aside from those ClientActiveTexture calls going away (which is always a good thing) you've got much better control over the type of blend you do between the two textures.

share|improve this answer
    
Thanks mh01 I'd vote plus for your answer but I don't have the rep. So all I can say for now is thanks! –  GameDev-er Jun 22 '12 at 18:38
    
And LO! You have rep. –  Jimmy Shelter Jun 22 '12 at 20:04
    
Just 9 more points and I'll have enough! Thanks. –  GameDev-er Jun 22 '12 at 20:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.