Hi, i have some problems with texturing a simple cube
i found a topic in the forum (http://hub.jmonkeyengine.org/groups/graphics/forum/topic/texture-mapping-problems-with-tex-coordinates/) but it doesn’t solve my problem.
I use 24 vertexes for my cube, here is my code:
[java]//Build Cube
//front face
float [] frontvert = {0,0,0, 1,0,0, 0,1,0, 1,1,0};
//back face
float [] backvert = {0,0,-1, 1,0,-1, 0,1,-1, 1,1,-1};
//left face
float [] leftvert = {0,0,-1, 0,0,0, 0,1,-1, 0,1,0};
//right face
float [] rightvert = {1,0,0, 1,0,-1, 1,1,0, 1,1,-1};
//top face
float [] topvert = {0,1,0, 1,1,0, 0,1,-1, 1,1,-1};
//bottom face
float [] bottomvert = {0,0,0, 1,0,0, 0,0,-1, 1,0,-1};
// Indexes. We define the order in which mesh should be constructed
int [] indexes = {1,3,2, 2,0,1, 6,7,5, 5,4,6, 5,7,3, 3,1,5, 2,6,4, 4,0,2, 3,7,6, 6,2,3, 4,5,1, 1,0,4};
if (this.getBuffer(Type.Position) == null) {
FloatBuffer bufferPos = BufferUtils.createFloatBuffer(243);
this.setBuffer(Type.Position, 3, bufferPos);
FloatBuffer bufferTex = BufferUtils.createFloatBuffer(243);
this.setBuffer(Type.TexCoord, 3, bufferTex);
IntBuffer bufferInd = BufferUtils.createIntBuffer(36);
this.setBuffer(Type.Index, 1, bufferInd);
}
FloatBuffer buffer = (FloatBuffer) this.getBuffer(Type.Position).getData();
buffer.put(frontvert);
buffer.put(backvert);
buffer.put(leftvert);
buffer.put(rightvert);
buffer.put(topvert);
buffer.put(bottomvert);
FloatBuffer buffer2 = (FloatBuffer) this.getBuffer(Type.TexCoord).getData();
buffer2.put(frontvert);
buffer2.put(backvert);
buffer2.put(leftvert);
buffer2.put(rightvert);
buffer2.put(topvert);
buffer2.put(bottomvert);
IntBuffer buffer3 = (IntBuffer) this.getBuffer(Type.Index).getData();
buffer3.put(indexes);
this.updateBound();
[/java]
Look at com.jme3.scene.shape.Box for an example on how to do it.
Since you are using negative texture coordinates then you will need to make sure that your texture has wrapping enabled. Or fix the texture coordinates to be 0 to 1.
bufferTex and buffer2 seem wrong.
a) bufferTex : u-v coordinates are only 2 not 3.
[java]
this.setBuffer(Type.TexCoord, 2, bufferTex);
[/java]
b) buffer2 : i would copy paste the same u-v coordinates (the ones of the front face)
[java]
float left = 0;
float right = 1;
float top = 1;
float bot = 0;
float[] frontUV = { left, top, right, top, right, bot, left, bot };
buffer2.put(frontUV );
buffer2.put(frontUV );
buffer2.put(frontUV );
buffer2.put(frontUV );
buffer2.put(frontUV );
buffer2.put(frontUV );
[/java]
i don’t know if that helps
^^^ Yes, of course. That’s true also. For some of your faces the texture coordinates will have the same x values and for some the same y.
…good catch, @tralala
ok i have it^^
my problem were the wrong buffertex (thx tralala) and i have took only indexes from 0…7 but I need 0…23
thx for your help
an another question, how can I delete some floats from the buffer to delete vertices. I can add them with buffer.put but it seems there is no method to delete them, maybe I have to set a new buffer?
i get the buffer, and resent all data (updated).
a) you cannot delete things from a buffer. You can however replace them with something else.
here is my example from my sprite lib:
[java]
private void updatePositions()
{
if (positionBufferChanged)
{
VertexBuffer pvb = mesh.getBuffer(VertexBuffer.Type.Position);
FloatBuffer positions = (FloatBuffer) pvb.getData();
positions.rewind();
for (int i = 0; i < sprites.size(); i++)
{
Vector3f position = sprites.get(i).getPosition();
positions.put(position.x).put(position.y).put(position.z);
}
positions.flip();
pvb.updateData(positions);
}
}
[/java]