Skip to content

Commit

Permalink
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (skybox is now properly rendered)
Browse files Browse the repository at this point in the history
  • Loading branch information
minecraft8997 committed Nov 21, 2023
1 parent e26ff14 commit e3cb228
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 146 deletions.
105 changes: 7 additions & 98 deletions core/src/ru/mclord/classic/Block.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
package ru.mclord.classic;

import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.utils.Disposable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class Block implements Disposable {
public interface PermissionChecker {
boolean doIHaveThisPermission();
Expand Down Expand Up @@ -45,9 +32,6 @@ public boolean doIHaveThisPermission() {
}
}

private static final BlendingAttribute ALPHA =
new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

// All of these fields can be directly used by the
// game while plugins will be required to use getters
/* package-private */ final short id;
Expand Down Expand Up @@ -129,90 +113,15 @@ public void initGraphics() {
if (model != null) return;

TextureManager manager = TextureManager.getInstance();
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.begin();
modelBuilder.node();

modelBuilder.part(
Helper.FRONT_SIDE_NAME,
GL20.GL_TRIANGLES,
Helper.ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(manager.getTexture(frontTextureId), false)))
).rect(-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0f, 0f, -0.5f
);

modelBuilder.part(
Helper.BACK_SIDE_NAME,
GL20.GL_TRIANGLES,
Helper.ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(manager.getTexture(backTextureId), true)))
).rect(-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0f, 0f, 0.5f
model = Helper.constructBlock(0.5f,
manager.getTexture(frontTextureId),
manager.getTexture(backTextureId),
manager.getTexture(bottomTextureId),
manager.getTexture(topTextureId),
manager.getTexture(leftTextureId),
manager.getTexture(rightTextureId)
);

modelBuilder.part(
Helper.BOTTOM_SIDE_NAME,
GL20.GL_TRIANGLES,
Helper.ATTR,
new Material(TextureAttribute
.createDiffuse(manager.getTexture(bottomTextureId)))
).rect(-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0f, -0.5f, 0f
);

modelBuilder.part(
Helper.TOP_SIDE_NAME,
GL20.GL_TRIANGLES,
Helper.ATTR,
new Material(TextureAttribute
.createDiffuse(manager.getTexture(topTextureId)))
).rect(-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0f, 0.5f, 0f
);

modelBuilder.part(
Helper.LEFT_SIDE_NAME,
GL20.GL_TRIANGLES,
Helper.ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(manager.getTexture(leftTextureId), false)))
).rect(-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0f, 0f
);

modelBuilder.part(
Helper.RIGHT_SIDE_NAME,
GL20.GL_TRIANGLES,
Helper.ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(manager.getTexture(rightTextureId), false)))
).rect(0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0f, 0f
);
model = modelBuilder.end();

for (Material material : model.materials) material.set(ALPHA);
}

public final Model getModel() {
Expand Down
116 changes: 116 additions & 0 deletions core/src/ru/mclord/classic/Helper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package ru.mclord.classic;

import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ScreenUtils;

Expand All @@ -12,6 +19,9 @@ public class Helper {

public static final int ATTR = VertexAttributes.Usage.Position |
VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates;
private static final BlendingAttribute ALPHA =
new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

public static final String RIGHT_SIDE_NAME = "right";
public static final String LEFT_SIDE_NAME = "left";
public static final String TOP_SIDE_NAME = "top";
Expand All @@ -26,6 +36,112 @@ public static void clearDepthRGB(int r, int g, int b) {
ScreenUtils.clear(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f, true);
}

public static boolean containsIgnoreCase(String str, String[] array) {
// System.out.println("Searching for \"" + str + "\" in " + Arrays.toString(array));
for (String element : array) {
if (str.equalsIgnoreCase(element)) return true;
}

return false;
}

public static Model constructBlock(
float size,
Texture front,
Texture back,
Texture bottom,
Texture top,
Texture left,
Texture right
) {
TextureManager manager = TextureManager.getInstance();
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.begin();
modelBuilder.node();

modelBuilder.part(
FRONT_SIDE_NAME,
GL20.GL_TRIANGLES,
ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(front, false)))
).rect(-size, -size, -size,
-size, size, -size,
size, size, -size,
size, -size, -size,
0.0f, 0.0f, -size
);

modelBuilder.part(
BACK_SIDE_NAME,
GL20.GL_TRIANGLES,
ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(back, true)))
).rect(-size, size, size,
-size, -size, size,
size, -size, size,
size, size, size,
0.0f, 0.0f, size
);

modelBuilder.part(
BOTTOM_SIDE_NAME,
GL20.GL_TRIANGLES,
ATTR,
new Material(TextureAttribute.createDiffuse(bottom))
).rect(-size, -size, size,
-size, -size, -size,
size, -size, -size,
size, -size, size,
0.0f, -size, 0.0f
);

modelBuilder.part(
TOP_SIDE_NAME,
GL20.GL_TRIANGLES,
ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(top, false)))
).rect(-size, size, -size,
-size, size, size,
size, size, size,
size, size, -size,
0.0f, size, 0.0f
);

modelBuilder.part(
LEFT_SIDE_NAME,
GL20.GL_TRIANGLES,
ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(left, false)))
).rect(-size, -size, size,
-size, size, size,
-size, size, -size,
-size, -size, -size,
-size, 0.0f, 0.0f
);

modelBuilder.part(
RIGHT_SIDE_NAME,
GL20.GL_TRIANGLES,
ATTR,
new Material(TextureAttribute.createDiffuse(manager
.rotate90Texture(right, false)))
).rect(size, -size, -size,
size, size, -size,
size, size, size,
size, -size, size,
size, 0.0f, 0.0f
);
Model model = modelBuilder.end();

for (Material material : model.materials) material.set(ALPHA);

return model;
}

public static String getStacktrace(Throwable t) {
StringWriter writer0 = new StringWriter();
PrintWriter writer = new PrintWriter(writer0);
Expand Down
18 changes: 12 additions & 6 deletions core/src/ru/mclord/classic/InGameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import ru.mclord.classic.events.CustomizeEnvironmentEvent;

public class InGameScreen implements Screen {
Expand All @@ -19,14 +17,14 @@ public class InGameScreen implements Screen {
private ModelBatch modelBatch;
private McLordFirstPersonCameraController cameraController;
private Level level;
private final Skybox skybox;
private Environment environment;
private PerspectiveCamera camera;
private final float fov;
private final float cameraFar;

private InGameScreen() {
fov = Float.parseFloat(McLordClassic.getProperty("fov"));
cameraFar = Float.parseFloat(McLordClassic.getProperty("cameraFar"));
skybox = new Skybox();
}

public static InGameScreen getInstance() {
Expand Down Expand Up @@ -65,12 +63,17 @@ public void show() {
// camera.position.set(player.spawnLocation.x, player.spawnLocation.y, player.spawnLocation.z);
camera.position.set(-1.0f, -1.0f, -1.0f);
camera.near = 0.35f; // todo might be not the best value
camera.far = cameraFar;
camera.far = 1000000000.0f;
camera.update();

level.initGraphics();

if (TextureManager.getInstance().skyboxPresented) {
float size = Math.max(Math.max(level.sizeX, level.sizeY), level.sizeZ);
skybox.setSize(size);
skybox.initGraphics();
}
cameraController = new McLordFirstPersonCameraController(camera);

Gdx.input.setInputProcessor(cameraController);
Gdx.input.setCursorCatched(true);
}
Expand All @@ -82,6 +85,9 @@ public void render(float delta) {
Helper.clearDepthRGB(17, 137, 217);

modelBatch.begin(camera);
if (skybox.isReady()) {
skybox.render(modelBatch, camera);
}
level.render(modelBatch, environment);
modelBatch.end();
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/ru/mclord/classic/McLordClassic.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.mclord.classic;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
import ru.mclord.classic.events.DisconnectEvent;
import ru.mclord.classic.events.LevelDownloadingFinishedEvent;
import ru.mclord.classic.events.PlayerSpawnEvent;
Expand Down Expand Up @@ -127,6 +128,7 @@ public Object addTask(Runnable task, boolean subscribe) {
}

@Override
@SuppressWarnings("deprecation")
public void create() {
System.out.println("Loading texture pack");
String configTexturePack = gameProperties.getProperty("texturePack");
Expand All @@ -142,6 +144,8 @@ public void create() {
networkingThread = new NetworkingThread();
networkingThread.start();

DefaultShader.defaultCullFace = 0; // make it possible to render skybox

setScreen(LoadingScreen.getInstance());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public void update(float deltaTime) {
private boolean collision() {
if (xRay) return false;

// too bad, fixme at some point
int x = (int) (camera.position.x);
int y = (int) (camera.position.y);
int z = (int) (camera.position.z);
Expand Down
23 changes: 23 additions & 0 deletions core/src/ru/mclord/classic/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.mclord.classic;

public class Pair<A, B> {
private final A first;
private final B second;

private Pair(A first, B second) {
this.first = first;
this.second = second;
}

public static <A, B> Pair<A, B> of(A first, B second) {
return new Pair<>(first, second);
}

public A getFirst() {
return first;
}

public B getSecond() {
return second;
}
}
Loading

0 comments on commit e3cb228

Please sign in to comment.