Search code examples
javaopengl3dlwjgl

How can I NOT apply a texture to part of the model?


I have a simple 16 x 16 grid to which I apply a single texture. The texture file is divided into four parts - each a different color. By default each square is colored green (upper left part of the file). If you click a square I apply the red portion (upper right part of the file). Now I want to make the square disappear entirely when clicked. I suppose I can use a transparent texture but I was hoping I wouldn't have to so as to avoid loading / reloading two different texture files.

Here is the code I use to update the texture vbo:

  //I don't bother offsetting my changes.  I simply update the 'UVs'
  //array and then copy the entire thing to the floatbuffer.   
  public void updateTexture(int offset)
  {

      //populate the texture buffer
      //fbtex is a floatbuffer (16x16x8 in size). UVs is an array, same size.
      fbtex.put( UVs );
      fbtex.rewind();

      glBindBuffer(GL_ARRAY_BUFFER, vboHandles.get(TEXTURE_IDX)); //the texture data
      glBufferSubData(GL_ARRAY_BUFFER, offset, fbtex);
      fbtex.clear(); //don't need this anymore  
  }

The VBO will contain up to 256 instances of my co-ords for Green:

public float[] UV_0 = { 0.02f,0.02f, 
                        0.02f,0.24f,
                        0.24f,0.24f,
                        0.24f,0.02f};

or less if includes a few of my co-ords for Red:

public float[] UV_1 = { 0.24f,0.02f, 
                        0.48f,0.02f,
                        0.48f,0.24f,
                        0.24f,0.24f};

Is there anything I can do to the VBO data to draw a section invisible? So the objects in the background can be seen for example?


Solution

  • You can just not render parts of the VBO. Normally you draw the entire data with something like

    glDrawArrays(GL_TRIANGLES, 0, numElements);
    

    glDrawArrays takes a first and count parameter which you can use to render only a part of the VBO. So if you wanted to not render some data, you would render all data before and then after this data in two draw calls.