Skip to content

Commit 6c9d46d

Browse files
Added custom models and fized some bugs
1 parent 7994711 commit 6c9d46d

File tree

11 files changed

+115
-33
lines changed

11 files changed

+115
-33
lines changed

src/main/java/io/github/startforkiller/jminecraft/engine/Engine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class Engine implements Runnable {
66

77
private final Window window;
8-
private final Timer timer;
8+
public final Timer timer;
99
private final IGameLogic gameLogic;
1010

1111
private final MouseInput mouseInput;

src/main/java/io/github/startforkiller/jminecraft/engine/data/Chunk.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Chunk extends Mesh {
1313
private LinkedList<Float> shadingValues = null;
1414

1515
public final Vector3f chunkPosition;
16+
public final Vector3f realPosition;
1617

1718
private int meshIndexCounter = 0;
1819

@@ -24,6 +25,7 @@ public Chunk(World world, Vector3f chunkPosition) {
2425

2526
this.world = world;
2627
this.chunkPosition = chunkPosition;
28+
this.realPosition = new Vector3f(chunkPosition).mul(16);
2729
this.blocks = new int[16][16][16];
2830
}
2931

@@ -54,6 +56,16 @@ public void addFace(int face, BlockType blockType, float x, float y, float z) {
5456
for (int i = 0; i < shadingVals.length; i++) this.shadingValues.add(i, shadingVals[i]);
5557
}
5658

59+
public int getBlock(int x, int y, int z) {
60+
int blockTypeID = this.blocks[x][y][z];
61+
62+
if(blockTypeID == 0) return 0;
63+
64+
BlockType blockType = world.blockTypes.get(blockTypeID);
65+
if(blockType.isTransparent()) return 0;
66+
else return blockTypeID;
67+
}
68+
5769
public void updateMesh() {
5870
this.positions = new LinkedList<>();
5971
this.texCoords = new LinkedList<>();
@@ -70,45 +82,45 @@ public void updateMesh() {
7082
if(blockNumber > 0) {
7183
BlockType blockType = this.world.blockTypes.get(blockNumber);
7284

73-
float x = this.chunkPosition.x + localX;
74-
float y = this.chunkPosition.y + localY;
75-
float z = this.chunkPosition.z + localZ;
85+
float x = this.realPosition.x + localX;
86+
float y = this.realPosition.y + localY;
87+
float z = this.realPosition.z + localZ;
7688

7789
if(blockType.isCube()) {
7890
if (localX == 0) {
79-
if (this.blocks[localX + 1][localY][localZ] == 0) addFace(0, blockType, x, y, z);
91+
if (getBlock(localX + 1, localY, localZ) == 0) addFace(0, blockType, x, y, z);
8092
if (this.world.getBlockNumber(x - 1, y, z) == 0) addFace(1, blockType, x, y, z);
8193
} else {
8294
if (localX == 15) {
8395
if (this.world.getBlockNumber(x + 1, y, z) == 0) addFace(0, blockType, x, y, z);
8496
} else {
85-
if (this.blocks[localX + 1][localY][localZ] == 0) addFace(0, blockType, x, y, z);
97+
if (getBlock(localX + 1, localY, localZ) == 0) addFace(0, blockType, x, y, z);
8698
}
87-
if (this.blocks[localX - 1][localY][localZ] == 0) addFace(1, blockType, x, y, z);
99+
if (getBlock(localX - 1, localY, localZ) == 0) addFace(1, blockType, x, y, z);
88100
}
89101

90102
if (localY == 0) {
91-
if (this.blocks[localX][localY + 1][localZ] == 0) addFace(2, blockType, x, y, z);
103+
if (getBlock(localX, localY + 1, localZ) == 0) addFace(2, blockType, x, y, z);
92104
if (this.world.getBlockNumber(x, y - 1, z) == 0) addFace(3, blockType, x, y, z);
93105
} else {
94106
if (localY == 15) {
95107
if (this.world.getBlockNumber(x, y + 1, z) == 0) addFace(2, blockType, x, y, z);
96108
} else {
97-
if (this.blocks[localX][localY + 1][localZ] == 0) addFace(2, blockType, x, y, z);
109+
if (getBlock(localX, localY + 1, localZ) == 0) addFace(2, blockType, x, y, z);
98110
}
99-
if (this.blocks[localX][localY - 1][localZ] == 0) addFace(3, blockType, x, y, z);
111+
if (getBlock(localX, localY - 1, localZ) == 0) addFace(3, blockType, x, y, z);
100112
}
101113

102114
if (localZ == 0) {
103-
if (this.blocks[localX][localY][localZ + 1] == 0) addFace(4, blockType, x, y, z);
115+
if (getBlock(localX, localY, localZ + 1) == 0) addFace(4, blockType, x, y, z);
104116
if (this.world.getBlockNumber(x, y, z - 1) == 0) addFace(5, blockType, x, y, z);
105117
} else {
106118
if (localZ == 15) {
107119
if (this.world.getBlockNumber(x, y, z + 1) == 0) addFace(4, blockType, x, y, z);
108120
} else {
109-
if (this.blocks[localX][localY][localZ + 1] == 0) addFace(4, blockType, x, y, z);
121+
if (getBlock(localX, localY, localZ + 1) == 0) addFace(4, blockType, x, y, z);
110122
}
111-
if (this.blocks[localX][localY][localZ - 1] == 0) addFace(5, blockType, x, y, z);
123+
if (getBlock(localX, localY, localZ - 1) == 0) addFace(5, blockType, x, y, z);
112124
}
113125
} else {
114126
for(int i = 0; i < blockType.getVertexPositions().length; i++) {

src/main/java/io/github/startforkiller/jminecraft/engine/data/TextureManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public TextureManager(int textureWidth, int textureHeight, int maxTextures) {
3636
textureArray = glGenTextures();
3737
glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray);
3838

39+
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3940
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4041

4142
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, textureWidth, textureHeight, maxTextures, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

src/main/java/io/github/startforkiller/jminecraft/engine/data/World.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.startforkiller.jminecraft.engine.data;
22

3+
import io.github.startforkiller.jminecraft.engine.Timer;
34
import io.github.startforkiller.jminecraft.engine.Window;
5+
import io.github.startforkiller.jminecraft.engine.data.models.CactusModel;
46
import io.github.startforkiller.jminecraft.engine.data.models.CubeModel;
57
import io.github.startforkiller.jminecraft.engine.data.models.PlantModel;
68
import io.github.startforkiller.jminecraft.game.Renderer;
@@ -57,14 +59,24 @@ public World() {
5759
blockTypes.add(new BlockType(textureManager, "rose", new HashMap<String, String>() {{
5860
put("all", "rose");
5961
}}, new PlantModel()));
62+
blockTypes.add(new BlockType(textureManager, "cactus", new HashMap<String, String>() {{
63+
put("top", "cactus_top");
64+
put("bottom", "cactus_bottom");
65+
put("sides", "cactus_side");
66+
}}, new CactusModel()));
67+
blockTypes.add(new BlockType(textureManager, "dead_bush", new HashMap<String, String>() {{
68+
put("all", "dead_bush");
69+
}}, new PlantModel()));
6070

6171
textureManager.generateMipMaps();
6272

63-
int[] firstRnd = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 10};
64-
int[] secondRnd = {0, 3};
65-
int[] thirdRnd = {0, 0, 1};
73+
int[] firstRnd = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 11};
74+
int[] secondRnd = {0, 6};
75+
int[] thirdRnd = {0, 0, 5};
6676
Random random = new Random();
6777

78+
Timer timer = new Timer();
79+
timer.init();
6880
for(int x = 0; x < 8; x++) {
6981
for(int z = 0; z < 8; z++) {
7082
Chunk currentChunk = new Chunk(this, new Vector3f(x - 4, -1, z - 4));
@@ -75,34 +87,30 @@ public World() {
7587
if(j == 15) currentChunk.blocks[i][j][k] = firstRnd[random.nextInt(firstRnd.length)];
7688
else if(j > 12) currentChunk.blocks[i][j][k] = secondRnd[random.nextInt(secondRnd.length)];
7789
else currentChunk.blocks[i][j][k] = thirdRnd[random.nextInt(thirdRnd.length)];
90+
//currentChunk.blocks[i][j][k] = 1;
7891
}
7992
}
8093
}
81-
currentChunk.updateMesh();
8294

8395
chunks.put(currentChunk.chunkPosition, currentChunk);
8496
}
8597
}
8698

87-
/*chunks.put(new Vector3f(0, 0, 0), new Chunk(this, new Vector3f(0, 0, 0)));
88-
Chunk chunk = chunks.get(new Vector3f(0, 0, 0));
89-
for(int x = 0; x < 16; x++) {
90-
for(int y = 0; y < 16; y++) {
91-
for(int z = 0; z < 16; z++) {
92-
chunk.blocks[x][y][z] = 1;
93-
}
94-
}
95-
}*/
99+
for(Chunk chunk : chunks.values()) {
100+
chunk.updateMesh();
101+
}
96102

97-
//chunk.updateMesh();
103+
System.out.println(timer.getElapsedTime());
98104
}
99105

100106
public int getBlockNumber(float x, float y, float z) {
101107
Vector3f chunkPosition = new Vector3f((float)Math.floor(x / 16), (float)Math.floor(y / 16), (float)Math.floor(z / 16));
102108

103-
if(chunks.get(chunkPosition) == null) return 0;
109+
if(chunks.get(chunkPosition) == null) {
110+
return 0;
111+
}
104112

105-
return chunks.get(chunkPosition).blocks[(int)(getUnsignedInt((int)x) % 16)][(int)(getUnsignedInt((int)y) % 16)][(int)(getUnsignedInt((int)z) % 16)];
113+
return chunks.get(chunkPosition).getBlock((int)(getUnsignedInt((int)x) % 16), (int)(getUnsignedInt((int)y) % 16), (int)(getUnsignedInt((int)z) % 16));
106114
}
107115

108116
public void render(Renderer renderer, Window window, Camera camera) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.github.startforkiller.jminecraft.engine.data.models;
2+
3+
public class CactusModel implements DataModel {
4+
5+
private static float[][] vertexPositions = new float[][]{
6+
{ 0.4375f, 0.5000f, 0.5000f, 0.4375f, -0.5000f, 0.5000f, 0.4375f, -0.5000f, -0.5000f, 0.4375f, 0.5000f, -0.5000f},//# right
7+
{-0.4375f, 0.5000f, -0.5000f, -0.4375f, -0.5000f, -0.5000f, -0.4375f, -0.5000f, 0.5000f, -0.4375f, 0.5000f, 0.5000f}, // left
8+
{ 0.5000f, 0.5000f, 0.5000f, 0.5000f, 0.5000f, -0.5000f, -0.5000f, 0.5000f, -0.5000f, -0.5000f, 0.5000f, 0.5000f}, // top
9+
{-0.5000f, -0.5000f, 0.5000f, -0.5000f, -0.5000f, -0.5000f, 0.5000f, -0.5000f, -0.5000f, 0.5000f, -0.5000f, 0.5000f}, // bottom
10+
{-0.5000f, 0.5000f, 0.4375f, -0.5000f, -0.5000f, 0.4375f, 0.5000f, -0.5000f, 0.4375f, 0.5000f, 0.5000f, 0.4375f}, // front
11+
{ 0.5000f, 0.5000f, -0.4375f, 0.5000f, -0.5000f, -0.4375f, -0.5000f, -0.5000f, -0.4375f, -0.5000f, 0.5000f, -0.4375f} // back
12+
};
13+
14+
private static float[][] texCoords = new float[][]{
15+
{0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f},
16+
{0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f},
17+
{0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f},
18+
{0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f},
19+
{0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f},
20+
{0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f}
21+
};
22+
23+
private static float[][] shadingValues = new float[][] {
24+
{0.6f, 0.6f, 0.6f, 0.6f},
25+
{0.6f, 0.6f, 0.6f, 0.6f},
26+
{1.0f, 1.0f, 1.0f, 1.0f},
27+
{0.4f, 0.4f, 0.4f, 0.4f},
28+
{0.8f, 0.8f, 0.8f, 0.8f},
29+
{0.8f, 0.8f, 0.8f, 0.8f}
30+
};
31+
32+
@Override
33+
public float[][] getVertexPositions() {
34+
return vertexPositions;
35+
}
36+
37+
@Override
38+
public float[][] getTexCoords() {
39+
return texCoords;
40+
}
41+
42+
@Override
43+
public float[][] getShadingValues() {
44+
return shadingValues;
45+
}
46+
47+
@Override
48+
public boolean isTransparent() {
49+
return true;
50+
}
51+
52+
@Override
53+
public boolean isCube() {
54+
return false;
55+
}
56+
}

src/main/java/io/github/startforkiller/jminecraft/game/Renderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ public void render(Window window, TextureManager textureManager, GameItem[] game
7474
shaderProgram.unbind();
7575
}
7676

77+
public final static Matrix4f identityMatrix = new Matrix4f().identity();
78+
7779
public void render(Window window, TextureManager textureManager, Map<Vector3f, Chunk> chunks, Camera camera) {
7880
renderCommonPart(window, textureManager, camera);
7981

8082
for(Chunk chunk : chunks.values()) {
81-
Matrix4f modelMatrix = transformation.getModelMatrix(new Vector3f(chunk.chunkPosition).mul(15, 15, 15));
82-
83-
shaderProgram.setUniform("modelMatrix", modelMatrix);
83+
shaderProgram.setUniform("modelMatrix", identityMatrix);
8484
chunk.render();
8585
}
8686

src/main/resources/fragment.glsl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ out vec4 fragColor;
77
uniform sampler2DArray textureArraySampler;
88

99
void main() {
10-
fragColor = texture(textureArraySampler, interpolatedTexCoords) * interpolatedShadingValues;
10+
vec4 textureColour = texture(textureArraySampler, interpolatedTexCoords);
11+
fragColor = textureColour * interpolatedShadingValues;
12+
13+
if(textureColour.a == 0.0) {
14+
discard;
15+
}
1116
}
825 Bytes
Loading
1015 Bytes
Loading
1.02 KB
Loading
739 Bytes
Loading

0 commit comments

Comments
 (0)