我目前正在开发类似于Minecraft的Voxel引擎。最近我一直试图在系统中实现纹理。我的系统使用纹理图集和坐标系统,但是我遇到了一些问题,例如程序在启动时崩溃只是" Java没有响应"如果它确实启动,则块没有纹理。那么我将如何修复我的代码来解决这些错误并正确实现纹理。谢谢!
Chunk Class:
public class Chunk extends RenderBlock{
byte CHUNK_SIZE = 32;
private Block[][][] BlockType = new Block[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
static final float CUBE_LENGTH= RenderBlock.CUBE_LENGTH;
private static Texture texture = TextureLoad.getTexture();
private int VBOTextureHandle;
private int VBOVertexHandle;
//Texture Handle
public void drawChunk() {
GL11.glPushMatrix();
GL11.glEnable(GL15.GL_VERTEX_ARRAY_BUFFER_BINDING);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
GL11.glEnableClientState(GL11.GL_TEXTURE_2D);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOTextureHandle);
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0L);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
texture.bind();
GL13.glActiveTexture(VBOTextureHandle);
GL11.glDrawArrays(GL11.GL_QUADS, 0, 24 * CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE);
GL11.glPopMatrix();
}
public Chunk(int x, int y, int z, HeightMap height) {
for(int chunkx=0; chunkx<CHUNK_SIZE; chunkx++) {
for(int chunky=0; chunky<CHUNK_SIZE; chunky++) {
for(int chunkz=0; chunkz<CHUNK_SIZE; chunkz++) {
BlockType[chunkx][chunky][chunkz] = new BlockDirt();
}
}
}
VBOTextureHandle = GL15.glGenBuffers();
VBOVertexHandle = GL15.glGenBuffers();
makeChunk(x, y, z, height);
}
public void makeChunk(int startx, int starty, int startz, HeightMap map) {
VBOVertexHandle = GL15.glGenBuffers();
VBOTextureHandle = GL15.glGenBuffers();
FloatBuffer VertexPositionData = BufferUtils.createFloatBuffer(CHUNK_SIZE*CHUNK_SIZE*CHUNK_SIZE*(3 * 4 * 6));
FloatBuffer VertexTextureData = BufferUtils.createFloatBuffer(CHUNK_SIZE*CHUNK_SIZE*CHUNK_SIZE*(4 * 4 * 6));
for(int x=0; x<CHUNK_SIZE; x++) {
for(int y=0; y<CHUNK_SIZE; y++) {
for(int z=0; z<CHUNK_SIZE; z++) {
VertexPositionData.put(putVertices((float) (startx + x)* CUBE_LENGTH, (float) (starty + y) * CUBE_LENGTH, (float) (startz + z) * CUBE_LENGTH));
VertexTextureData.put(TexturePos(getTexturePosX(BlockType[x][y][z]), getTexturePosY(BlockType[x][y][z])));
}
}
}
VertexPositionData.flip();
VertexTextureData.flip();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexPositionData,
GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOTextureHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexTextureData,
GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
}
RenderCube Class
public class RenderBlock extends Block{
static final public float CUBE_LENGTH=2;
Block block = new Block();
//verticies
public float[] putVertices(float x, float y, float z) {
float offset = CUBE_LENGTH / 2;
return new float[] {
// BOTTOM QUAD(DOWN=+Y)
x + offset, y + offset, z,
x - offset, y + offset, z,
x - offset, y + offset, z - CUBE_LENGTH,
x + offset, y + offset, z - CUBE_LENGTH,
// TOP!
x + offset, y - offset, z - CUBE_LENGTH,
x - offset, y - offset, z - CUBE_LENGTH,
x - offset, y - offset, z,
x + offset, y - offset, z,
// FRONT QUAD
x + offset, y + offset, z - CUBE_LENGTH,
x - offset, y + offset, z - CUBE_LENGTH,
x - offset, y - offset, z - CUBE_LENGTH,
x + offset, y - offset, z - CUBE_LENGTH,
// BACK QUAD
x + offset, y - offset, z,
x - offset, y - offset, z,
x - offset, y + offset, z,
x + offset, y + offset, z,
// LEFT QUAD
x - offset, y + offset, z - CUBE_LENGTH,
x - offset, y + offset, z,
x - offset, y - offset, z,
x - offset, y - offset, z - CUBE_LENGTH,
// RIGHT QUAD
x + offset, y + offset, z,
x + offset, y + offset, z - CUBE_LENGTH,
x + offset, y - offset, z - CUBE_LENGTH,
x + offset, y - offset, z };
}
public float[] TexturePos(int posx, int posy) {
return new float[]{
posx,posy,
posx+32,posy,
posx+32,posy+32,
posx,posy+32
};
}
}
答案 0 :(得分:0)
尝试更改
GL11.glEnableClientState(GL11.GL_TEXTURE_2D);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOTextureHandle);
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0L);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
texture.bind();
到
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOTextureHandle);
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0L);
此外,当使用多个纹理,或一些纹理形状和其他空白或只是彩色,
之后GL11.glDrawArrays(GL11.GL_QUADS, 0, 24 * CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE);
添加
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
我不确定这是否有效,我还没有测试过,而且我不知道这是否是导致错误的原因,但这应该有助于渲染纹理。无论如何它对我有用。