3
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

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);
}
\$\endgroup\$
10
  • \$\begingroup\$ 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. \$\endgroup\$
    – GameDev-er
    Commented Jun 22, 2012 at 17:00
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – knight666
    Commented Jun 22, 2012 at 17:26
  • \$\begingroup\$ 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? \$\endgroup\$
    – GameDev-er
    Commented Jun 22, 2012 at 17:40
  • 1
    \$\begingroup\$ Well, you wanted to render thousands of cubes, so whatever solution you come up with, it's going to cost a bit of memory. ;) \$\endgroup\$
    – knight666
    Commented Jun 22, 2012 at 18:06
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 22, 2012 at 18:13
2
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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! \$\endgroup\$
    – GameDev-er
    Commented Jun 22, 2012 at 18:38
  • \$\begingroup\$ And LO! You have rep. \$\endgroup\$ Commented Jun 22, 2012 at 20:04
  • \$\begingroup\$ Just 9 more points and I'll have enough! Thanks. \$\endgroup\$
    – GameDev-er
    Commented Jun 22, 2012 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .