LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Skatty on June 16, 2013, 14:31:05

Title: VBO textured cube
Post by: Skatty on June 16, 2013, 14:31:05
Hello - again : p

I need a bit of help. I have VBO method for cube (atm built from 6 quads). I defined also grass texture but i have a problem: only 2 (front and back) walls are well textured and all rest is blurry. How fix that? Also, how i can define on example 3 different textures for 1 quad ? (1 texture for top, 1 for bottom and 1 for sides)
Here's screen of running:
(http://i.imgur.com/dxui6s4.png)
And a source:
static void drawVertexBufferObject()
        {
            // create geometry buffers
        //cbuffer - color
        //vbuffer - vectors
        //tbuffer - texture
       
           FloatBuffer vBuffer = BufferUtils.createFloatBuffer(72);
           //BACK FACE
           vBuffer.put(0.0f).put(0.0f).put(0.0f);
           vBuffer.put(1.0f).put(0.0f).put(0.0f);
           vBuffer.put(1.0f).put(1.0f).put(0.0f);
           vBuffer.put(0.0f).put(1.0f).put(0.0f);
           
           //FRONT FACE
           vBuffer.put(0.0f).put(0.0f).put(1.0f);
           vBuffer.put(1.0f).put(0.0f).put(1.0f);
           vBuffer.put(1.0f).put(1.0f).put(1.0f);
           vBuffer.put(0.0f).put(1.0f).put(1.0f);
           
           //RIGHT FACE
           vBuffer.put(1.0f).put(0.0f).put(0.0f);
           vBuffer.put(1.0f).put(1.0f).put(0.0f);
           vBuffer.put(1.0f).put(1.0f).put(1.0f);
           vBuffer.put(1.0f).put(0.0f).put(1.0f);
           
           //LEFT FACE
           vBuffer.put(0.0f).put(0.0f).put(0.0f);
           vBuffer.put(0.0f).put(0.0f).put(1.0f);
           vBuffer.put(0.0f).put(1.0f).put(1.0f);
           vBuffer.put(0.0f).put(1.0f).put(0.0f);
           
           //TOP FACE
           vBuffer.put(0.0f).put(1.0f).put(0.0f);
           vBuffer.put(1.0f).put(1.0f).put(0.0f);
           vBuffer.put(1.0f).put(1.0f).put(1.0f);
           vBuffer.put(0.0f).put(1.0f).put(1.0f);
           
           //BOTTOM FACE
           vBuffer.put(0.0f).put(0.0f).put(0.0f);
           vBuffer.put(1.0f).put(0.0f).put(0.0f);
           vBuffer.put(1.0f).put(0.0f).put(1.0f);
           vBuffer.put(0.0f).put(0.0f).put(1.0f);

           vBuffer.flip();
           
           
           
           FloatBuffer tBuffer = BufferUtils.createFloatBuffer(72);
           //BACK FACE
           tBuffer.put(0.0f).put(0.0f).put(0.0f);
           tBuffer.put(1.0f).put(0.0f).put(0.0f);
           tBuffer.put(1.0f).put(1.0f).put(0.0f);
           tBuffer.put(0.0f).put(1.0f).put(0.0f);
           
           //FRONT FACE
           tBuffer.put(0.0f).put(0.0f).put(1.0f);
           tBuffer.put(1.0f).put(0.0f).put(1.0f);
           tBuffer.put(1.0f).put(1.0f).put(1.0f);
           tBuffer.put(0.0f).put(1.0f).put(1.0f);
           
           //RIGHT FACE
           tBuffer.put(1.0f).put(0.0f).put(0.0f);
           tBuffer.put(1.0f).put(1.0f).put(0.0f);
           tBuffer.put(1.0f).put(1.0f).put(1.0f);
           tBuffer.put(1.0f).put(0.0f).put(1.0f);
           
           //LEFT FACE
           tBuffer.put(0.0f).put(0.0f).put(0.0f);
           tBuffer.put(0.0f).put(0.0f).put(1.0f);
           tBuffer.put(0.0f).put(1.0f).put(1.0f);
           tBuffer.put(0.0f).put(1.0f).put(0.0f);
           
           //TOP FACE
           tBuffer.put(0.0f).put(1.0f).put(0.0f);
           tBuffer.put(1.0f).put(1.0f).put(0.0f);
           tBuffer.put(1.0f).put(1.0f).put(1.0f);
           tBuffer.put(0.0f).put(1.0f).put(1.0f);
           
           //BOTTOM FACE
           tBuffer.put(0.0f).put(0.0f).put(0.0f);
           tBuffer.put(1.0f).put(0.0f).put(0.0f);
           tBuffer.put(1.0f).put(0.0f).put(1.0f);
           tBuffer.put(0.0f).put(0.0f).put(1.0f);

           tBuffer.flip();
           //

           IntBuffer ib = BufferUtils.createIntBuffer(2);

           glGenBuffersARB(ib);
           int vHandle = ib.get(0);
           int tHandle = ib.get(1);

           glEnableClientState(GL_VERTEX_ARRAY);
           glEnableClientState(GL_TEXTURE_COORD_ARRAY);
           
           glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle);
           glBufferDataARB(GL_ARRAY_BUFFER_ARB, vBuffer, GL_STATIC_DRAW_ARB);
           glVertexPointer(3, GL_FLOAT, /* stride */3 << 2, 0L);
           
           glBindBufferARB(GL_ARRAY_BUFFER_ARB, tHandle);
           glBufferDataARB(GL_ARRAY_BUFFER_ARB, tBuffer, GL_STATIC_DRAW_ARB);
           glTexCoordPointer(3, GL_FLOAT, 0, 0);
       
           glBindTexture(GL11.GL_TEXTURE_2D, texture1.getTextureID());
           glDrawArrays(GL_QUADS, 0, 24 /* elements */);

           glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

           glDisableClientState(GL_VERTEX_ARRAY);

           // cleanup VBO handles
           ib.put(0, vHandle);
           ib.put(1, tHandle);
           grass.release();
           glDeleteBuffersARB(ib);
        }
Title: Re: VBO textured cube
Post by: Fool Running on June 18, 2013, 12:40:56
Quote from: Skatty on June 16, 2013, 14:31:05
... only 2 (front and back) walls are well textured and all rest is blurry. How fix that?
That's caused by you specifying the texture coordinates as 3D texture coordinates instead of 2D (i.e. only 2 values per coordinate). Think of it as putting wallpaper on the cube (where the wallpaper is the texture).

Quote from: Skatty on June 16, 2013, 14:31:05
Also, how i can define on example 3 different textures for 1 quad ? (1 texture for top, 1 for bottom and 1 for sides)
Short version:
You can't.

Long version:
You can do some tricky stuff to make it work.
One option is to use a texture atlas (tile sheet). This is where you put a bunch of smaller textures into a large texture and just change the texture coordinates to choose the smaller texture you want to use (this is the fastest option).

The second option is to bind multiple textures and use multitexturing to choose which texture is actually visible on each face of the cube. Here you would need to specify multiple texture coordinates (one for each texture) on each vertex. You would probably also need a custom shader for this to work well. Because of the overhead of multitexturing, this probably isn't a good option. But it would allow you to have full-resolution textures on each side.

There are probably other options I haven't thought of.  :P
Title: Re: VBO textured cube
Post by: Skatty on June 18, 2013, 15:56:46
Thank you very much!

QuoteThat's caused by you specifying the texture coordinates as 3D texture coordinates instead of 2D

So i have to delete Z vector from texture coords?
Title: Re: VBO textured cube
Post by: Cornix on June 18, 2013, 17:08:03
Quote from: Skatty on June 18, 2013, 15:56:46[...]
So i have to delete Z vector from texture coords?
Yes.
Title: Re: VBO textured cube
Post by: Fool Running on June 18, 2013, 17:09:23
Quote from: Skatty on June 18, 2013, 15:56:46
Thank you very much!

QuoteThat's caused by you specifying the texture coordinates as 3D texture coordinates instead of 2D

So i have to delete Z vector from texture coords?
Yes, but you also need to change the x and y coords to be like they are all being put on a 2D plane. e.g. For the right face, you would remove the current x coordinate because they are all 1.0. This will leave you with x,y coordinates that should be more what you are looking for.

EDIT: Cornix beat me. :P
Title: Re: VBO textured cube
Post by: Skatty on June 19, 2013, 10:27:07
So i have now:

           //RIGHT FACE
           tBuffer.put(0.0f).put(0.0f);
           tBuffer.put(1.0f).put(0.0f);
           tBuffer.put(1.0f).put(1.0f);
           tBuffer.put(0.0f).put(1.0f);


but it look like:

(http://i.imgur.com/ByR5TGD.jpg)
Title: Re: VBO textured cube
Post by: Fool Running on June 19, 2013, 13:35:59
Did you change your call to glTexCoordPointer to only have the 2 texture coordinates instead of 3 (i.e. change it to glTexCoordPointer(2, GL_FLOAT, 0, 0); )?
Title: Re: VBO textured cube
Post by: Skatty on June 19, 2013, 19:48:53
Ok. All walls are textured but 2 side walls are fliped badly. How i can fix that?
Title: Re: VBO textured cube
Post by: Fool Running on June 20, 2013, 16:17:59
Just change your texture coordinates to flip it the correct way.