Hallo ich benutze momentan Vertexbuffer für meine Voxel-Engine (LWJGL). Ich habe zwar die Möglichkeit Farben hinzuzufügen aber ich würde gerne Texturen dadrauf sehen und wenn möglich scharfe Texturen. Momentan benutze ich die Texturen und Texte von Slick würde aber gerne auf andere Weise an die Texturen und den Text kommen. Ich möchte wenigstens eine Textur auf meinen Blöcken sehen .
Also hier mein Chunk-Code:
Ich hoffe ihr könnt mir weiterhelfen
MFG faustdonner
Also hier mein Chunk-Code:
Java:
import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.Color;
public class Chunk {
public static final int CHUNK_WIDTH = 16;
public static final int CHUNK_HEIGHT = 64;
public static final int CHUNK_DEPTH = 16;
private int VBOVertexHandle;
private int VBOColorHandle;
public int[][][] blocks;
private boolean draw = false;
public int startX, startY, startZ;
public int x, z;
public Chunk(Point pos, boolean generate, boolean draw) {
this.x = pos.y;
this.z = pos.x;
this.draw = draw;
this.startX = (int) (z * CHUNK_WIDTH * Block.BLOCK_SIZE);
this.startY = 0;
this.startZ = (int) (x * CHUNK_DEPTH * Block.BLOCK_SIZE);
blocks = new int[CHUNK_WIDTH][CHUNK_HEIGHT][CHUNK_DEPTH];
if(draw) {
VBOColorHandle = GL15.glGenBuffers();
VBOVertexHandle = GL15.glGenBuffers();
}
if(generate && draw) {
generate();
}
else if(generate) {
generateWithoutDraw();
}
}
private void generate() {
for(int x = 0; x < CHUNK_WIDTH; x++) {
for(int y = 0; y < CHUNK_HEIGHT; y++) {
for(int z = 0; z < CHUNK_DEPTH; z++) {
if(y > 32) {
blocks[x][y][z] = Block.air.ID;
}
else if(y == 32) {
blocks[x][y][z] = Block.grass.ID;
}
else if(y < 32 && y >= 32 - 4) {
blocks[x][y][z] = Block.dirt.ID;
}
else {
blocks[x][y][z] = Block.stone.ID;
}
}
}
}
VBOColorHandle = GL15.glGenBuffers();
VBOVertexHandle = GL15.glGenBuffers();
FloatBuffer VertexPositionData = BufferUtils.createFloatBuffer((CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) * 6 * 12);
FloatBuffer VertexColorData = BufferUtils.createFloatBuffer((CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) * 6 * 12);
for(int x = 0; x < CHUNK_WIDTH; x++) {
for(int y = 0; y < CHUNK_HEIGHT; y++) {
for(int z = 0; z < CHUNK_DEPTH; z++) {
try {
if(blocks[x][y][z] == 0) throw new NullPointerException();
VertexPositionData.put(Block.CreateBlock(new Point(this.x, this.z), new Vector3f((float) startX + x * Block.BLOCK_SIZE, (float) startY + y * Block.BLOCK_SIZE, (float) startZ + z * Block.BLOCK_SIZE)));
Color color = BlockMaterial.getBlockMaterialById(Block.getBlockById(blocks[x][y][z]).blockMaterial).texture;
VertexColorData.put(CreateCubeVertexCol(new float[] {color.r, color.g, color.b}));
} catch(NullPointerException e) {
VertexPositionData.put(new float[] {});
VertexColorData.put(CreateCubeVertexCol(new float[] {}));
}
}
}
}
VertexColorData.flip();
VertexPositionData.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, VBOColorHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexColorData, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
onChange();
}
private void generateWithoutDraw() {
for(int x = 0; x < CHUNK_WIDTH; x++) {
for(int y = 0; y < CHUNK_HEIGHT; y++) {
for(int z = 0; z < CHUNK_DEPTH; z++) {
if(y > 32) {
blocks[x][y][z] = Block.air.ID;
}
else if(y == 32) {
blocks[x][y][z] = Block.grass.ID;
}
else if(y < 32 && y >= 32 - 4) {
blocks[x][y][z] = Block.dirt.ID;
}
else {
blocks[x][y][z] = Block.stone.ID;
}
}
}
}
onChange();
}
private float[] CreateCubeVertexCol(float[] CubeColorArray) {
float[] cubeColors = new float[CubeColorArray.length * 4 * 6];
for (int i = 0; i < cubeColors.length; i++) {
cubeColors[i] = CubeColorArray[i % CubeColorArray.length];
}
return cubeColors;
}
public void paint() {
if(!draw) return;
GL11.glPushMatrix();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
GL11.glColorPointer(3, GL11.GL_FLOAT, 0, 0L);
GL11.glDrawArrays(GL11.GL_QUADS, 0, CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH * 24);
GL11.glPopMatrix();
}
public boolean hasCoordinate(int blockX, int blockZ) {
for(int x = 0; x < CHUNK_WIDTH; x++) {
for(int z = 0; z < CHUNK_DEPTH; z++) {
if(x + this.x * CHUNK_WIDTH == blockX && z + this.z * CHUNK_DEPTH == blockZ) return true;
}
}
return false;
}
public void run() {
}
public void onChange() {
save();
}
public String save() {
String sep = System.getProperty("file.separator");
File chunkFile = new File("."+sep+"worlds"+sep+World.worldName+sep+"region"+sep+x+"."+z+".cbr");
String data = "";
for(int x = 0; x < CHUNK_WIDTH; x++) {
for(int y = 0; y < CHUNK_HEIGHT; y++) {
for(int z = 0; z < CHUNK_DEPTH; z++) {
if(blocks[x][y][z] != Block.air.ID) {
data += blocks[x][y][z] + ":";
}
else {
data += ":";
}
}
}
}
try {
FileWriter fw = new FileWriter(chunkFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);
bw.close();
} catch(IOException e) {
e.printStackTrace();
}
return data;
}
public static Chunk load(int chunkX, int chunkZ, boolean draw) {
String sep = System.getProperty("file.separator");
Chunk chunk = new Chunk(new Point(chunkX, chunkZ), false, draw);
File chunkFile = new File("."+sep+"worlds"+sep+World.worldName+sep+"region"+sep+chunkX+"."+chunkZ+".cbr");
try {
FileReader fr = new FileReader(chunkFile);
BufferedReader br = new BufferedReader(fr);
String[] coords = br.readLine().split(":");
int i = 0;
for(int x = 0; x < CHUNK_WIDTH; x++) {
for(int y = 0; y < CHUNK_HEIGHT; y++) {
for(int z = 0; z < CHUNK_DEPTH; z++) {
try {
chunk.blocks[x][y][z] = Integer.valueOf(coords[i]);
} catch(Exception e) {
chunk.blocks[x][y][z] = Block.air.ID;
}
i++;
}
}
}
br.close();
} catch(IOException e) {}
return chunk;
}
public boolean isDraw() {
return draw;
}
public void setDraw(boolean draw) {
if(draw) {
VBOColorHandle = GL15.glGenBuffers();
VBOVertexHandle = GL15.glGenBuffers();
FloatBuffer VertexPositionData = BufferUtils.createFloatBuffer((CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) * 6 * 12);
FloatBuffer VertexColorData = BufferUtils.createFloatBuffer((CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) * 6 * 12);
for(int x = 0; x < CHUNK_WIDTH; x++) {
for(int y = 0; y < CHUNK_HEIGHT; y++) {
for(int z = 0; z < CHUNK_DEPTH; z++) {
try {
if(blocks[x][y][z] == 0) throw new NullPointerException();
VertexPositionData.put(Block.CreateBlock(new Point(this.x, this.z), new Vector3f((float) startX + x * Block.BLOCK_SIZE, (float) startY + y * Block.BLOCK_SIZE, (float) startZ + z * Block.BLOCK_SIZE)));
Color color = BlockMaterial.getBlockMaterialById(Block.getBlockById(blocks[x][y][z]).blockMaterial).texture;
VertexColorData.put(CreateCubeVertexCol(new float[] {color.r, color.g, color.b}));
} catch(NullPointerException e) {
VertexPositionData.put(new float[] {});
VertexColorData.put(CreateCubeVertexCol(new float[] {}));
}
}
}
}
VertexColorData.flip();
VertexPositionData.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, VBOColorHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexColorData, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
this.draw = draw;
}
}
Ich hoffe ihr könnt mir weiterhelfen
MFG faustdonner