patternjavaMinor
Making a voxel engine
Viewed 0 times
enginemakingvoxel
Problem
I've decided a while ago to make my own voxel engine. To start, I coded my own basic 3D engine in Java using minimal OpenGL bindings for things such as SRGB ect. I set up my own VBA and VBOS and had it working with .obj files. I already have transformations and Quaternions set up.
I've recently started to implement my attempt at a voxel engine. Last night I set up a very crude jury-rigged attempt that actually worked well enough with perlin noise to draw random 1x1x1 blocks on the screen that resembled a world.
The issue is that I've used immediate mode and block-by block-rendering with no chunks. So today I've decided to start writing my chunk class, and was hoping someone could tell me if it seems correctly set up.
Edit: I forgot to mention that I currently am trying to figure out how to handle vertices, so that's why there is no code.
```
package cam.base.engine;
public class Chunk
{
public static final int X_CHUNK_SIZE = 16;
public static final int Y_CHUNK_SIZE = 128;
public static final int Z_CHUNK_SIZE = 16;
public int chunkXNumber;
public int chunkZNumber;
private Block [][][] blocks = new Block[X_CHUNK_SIZE][Y_CHUNK_SIZE][Z_CHUNK_SIZE];
public Chunk(int chunkXNumber, int chunkZNumber)
{
this.chunkXNumber = chunkXNumber;
this.chunkZNumber = chunkZNumber;
createBlocks(chunkXNumber, chunkZNumber);
Vertex [][][] vertices = createVertices(getActiveBlocks(), chunkXNumber, chunkZNumber);
int[] = createIndices(vertices);
}
private void createBlocks(int chunkXNum, int chunkZNum)
{
for(int i = 1; i .2)
{
if(yVal >= 80)
{
material = 0;
}
else
{
material = 1;
}
}
else
{
if(yVal >= 64)
{
material = 0;
}
else
{
material = 2;
}
I've recently started to implement my attempt at a voxel engine. Last night I set up a very crude jury-rigged attempt that actually worked well enough with perlin noise to draw random 1x1x1 blocks on the screen that resembled a world.
The issue is that I've used immediate mode and block-by block-rendering with no chunks. So today I've decided to start writing my chunk class, and was hoping someone could tell me if it seems correctly set up.
Edit: I forgot to mention that I currently am trying to figure out how to handle vertices, so that's why there is no code.
```
package cam.base.engine;
public class Chunk
{
public static final int X_CHUNK_SIZE = 16;
public static final int Y_CHUNK_SIZE = 128;
public static final int Z_CHUNK_SIZE = 16;
public int chunkXNumber;
public int chunkZNumber;
private Block [][][] blocks = new Block[X_CHUNK_SIZE][Y_CHUNK_SIZE][Z_CHUNK_SIZE];
public Chunk(int chunkXNumber, int chunkZNumber)
{
this.chunkXNumber = chunkXNumber;
this.chunkZNumber = chunkZNumber;
createBlocks(chunkXNumber, chunkZNumber);
Vertex [][][] vertices = createVertices(getActiveBlocks(), chunkXNumber, chunkZNumber);
int[] = createIndices(vertices);
}
private void createBlocks(int chunkXNum, int chunkZNum)
{
for(int i = 1; i .2)
{
if(yVal >= 80)
{
material = 0;
}
else
{
material = 1;
}
}
else
{
if(yVal >= 64)
{
material = 0;
}
else
{
material = 2;
}
Solution
-
-
It could be simply
-
In the
-
-
The code should follow the Code Conventions for the Java Programming Language.
-
Some of the loops runs from
-
The two continue is unnecessary here:
createBlocks actually fills the blocks array, creation happens before that, so this name is a little bit misleading. I'd move the array creation inside the method and use it as a return value:private Block[][][] blocks;
public Chunk(int chunkXNumber, int chunkZNumber) {
..
blocks = createBlocks(chunkXNumber, chunkZNumber);
}
private Block[][][] createBlocks(int chunkXNum, int chunkZNum) {
final Block[][][] blocks = new Block[X_CHUNK_SIZE][Y_CHUNK_SIZE][Z_CHUNK_SIZE];
// loops here
return blocks;
}-
private boolean checkBlockActive(byte material) {
if (material == 0) {
return false;
}
else {
return true;
}
}It could be simply
private boolean checkBlockActive(byte material) {
return material != 0;
}-
In the
getMaterial method you could use multiple returns and get rid of the result variable:private byte getMaterial(final float density, final int yVal) {
if (density > .2) {
if (yVal >= 80) {
return 0;
} else {
return 1;
}
} else {
if (yVal >= 64) {
return 0;
} else {
return 2;
}
}
}-
160.0f is used multiple times and it's a magic number. It would deserve a named constant.-
The code should follow the Code Conventions for the Java Programming Language.
-
Some of the loops runs from
1 to `-
The two continue is unnecessary here:
for (int k = 1; k <= Z_CHUNK_SIZE; k++) {
if (activeBlocks[i - 1][j - 1][k - 1]) {
if (verticesActive[i - 1][j - 1][k - 1] == false) {
vertices[i - 1][j - 1][k - 1] = new Vertex(new Vector3(i * chunkXNum, j, k * chunkZNum));
}
continue;
}
continue;
}Code Snippets
private Block[][][] blocks;
public Chunk(int chunkXNumber, int chunkZNumber) {
..
blocks = createBlocks(chunkXNumber, chunkZNumber);
}
private Block[][][] createBlocks(int chunkXNum, int chunkZNum) {
final Block[][][] blocks = new Block[X_CHUNK_SIZE][Y_CHUNK_SIZE][Z_CHUNK_SIZE];
// loops here
return blocks;
}private boolean checkBlockActive(byte material) {
if (material == 0) {
return false;
}
else {
return true;
}
}private boolean checkBlockActive(byte material) {
return material != 0;
}private byte getMaterial(final float density, final int yVal) {
if (density > .2) {
if (yVal >= 80) {
return 0;
} else {
return 1;
}
} else {
if (yVal >= 64) {
return 0;
} else {
return 2;
}
}
}for (int k = 1; k <= Z_CHUNK_SIZE; k++) {
if (activeBlocks[i - 1][j - 1][k - 1]) {
if (verticesActive[i - 1][j - 1][k - 1] == false) {
vertices[i - 1][j - 1][k - 1] = new Vertex(new Vector3(i * chunkXNum, j, k * chunkZNum));
}
continue;
}
continue;
}Context
StackExchange Code Review Q#33812, answer score: 5
Revisions (0)
No revisions yet.