Skip to content

Commit

Permalink
Merge pull request #4 from akhunovaa/feature/render-speed-optimization
Browse files Browse the repository at this point in the history
Feature/render speed optimization
  • Loading branch information
akhunovaa committed Oct 13, 2021
2 parents 9c1aa77 + c1ad38b commit bfaa4cb
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 389 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -12,14 +12,14 @@ mvn -P {your OS name} clean install

## Start/Run:

`java -jar cubecraft-0.1.0.jar `
`java -jar cubecraft-0.1.2.jar `


## for MacOS users add VM option:

`-XstartOnFirstThread `

`java -jar cubecraft-0.1.0.jar -XstartOnFirstThread`
`java -jar cubecraft-0.1.2.jar -XstartOnFirstThread`

[![Video clip](https://s3.amazonaws.com/culga-games-images/uploads/images/cube-craft.jpg)](https://www.youtube.com/watch?v=16pI2U7xM0E "Everything Is AWESOME")

Expand Down
24 changes: 15 additions & 9 deletions assets/models/cube.obj
Expand Up @@ -46,20 +46,26 @@ vn 0.000000 0.000000 1.000000
vn -1.000000 0.000000 0.000000
vn 0.000000 0.000000 -1.000000

f 11/1/1 17/2/1 13/3/1
f 24/4/2 22/5/2 20/6/2

f 15/1/3 19/7/3 12/4/3
# front
f 2/2/4 6/1/4 18/4/4
f 6/1/4 21/8/4 18/4/4

f 3/2/5 7/9/5 23/7/5
# back
f 1/4/6 4/2/6 8/1/6
f 5/7/6 1/4/6 8/1/6

# top
f 9/10/1 11/1/1 13/3/1
f 16/2/2 24/4/2 20/6/2
f 11/1/1 17/2/1 13/3/1

f 10/2/3 15/1/3 12/4/3
f 2/2/4 6/1/4 18/4/4
# bottom
f 16/2/2 24/4/2 20/6/2
f 24/4/2 22/5/2 20/6/2

# right
f 3/2/5 7/9/5 23/7/5
f 14/4/5 3/2/5 23/7/5
f 5/7/6 1/4/6 8/1/6

# left
f 15/1/3 19/7/3 12/4/3
f 10/2/3 15/1/3 12/4/3
5 changes: 3 additions & 2 deletions pom.xml
Expand Up @@ -15,7 +15,7 @@
</developers>

<properties>
<project.versioning>0.1.1</project.versioning>
<project.versioning>0.1.2</project.versioning>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand All @@ -25,7 +25,7 @@
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>

<lwjgl.version>3.3.0-SNAPSHOT</lwjgl.version>
<lwjgl.version>3.2.3</lwjgl.version>
<swt.version>4.3</swt.version>
<joml.version>1.10.0</joml.version>
<version.version>1.18.20</version.version>
Expand Down Expand Up @@ -175,6 +175,7 @@
<directory>${test.resources.dir}</directory>
</testResource>
</testResources>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/mycubecraft/Settings.java
Expand Up @@ -2,7 +2,7 @@

public final class Settings {

public static final String WINDOW_TITLE = "Cubecraft Game Engine| JAVA | Developed by Azat Akhunov";
public static final String WINDOW_TITLE = "Cubecraft Game | JAVA | Engine | Developed by Azat Akhunov";

public static final int WIDTH = 1024;
public static final int HEIGHT = 768;
Expand Down
77 changes: 20 additions & 57 deletions src/main/java/ru/mycubecraft/core/Mesh.java
Expand Up @@ -69,20 +69,16 @@ public Mesh(float[] positions, float[] textCoords,
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, textCoordsBuffer, GL15.GL_STREAM_DRAW);
GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 0, 0);
//
// // Vertex normals VBO
// vboId = GL15.glGenBuffers();
// vboIdList.add(vboId);
// vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length);
// if (vecNormalsBuffer.capacity() > 0) {
// vecNormalsBuffer.put(normals).flip();
// } else {
// vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length);
// vecNormalsBuffer.put(positions).flip();
// }
// GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
// GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vecNormalsBuffer, GL15.GL_STREAM_DRAW);
// GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, 0, 0);

// Vertex normals VBO
vboId = GL15.glGenBuffers();
vboIdList.add(vboId);
vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length);
vecNormalsBuffer.put(normals).flip();

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vecNormalsBuffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, 0, 0);

// Index VBO
vboId = GL15.glGenBuffers();
Expand Down Expand Up @@ -121,39 +117,6 @@ public int getVertexCount() {
return vertexCount;
}

// public void renderList(List<GameItem> gameItems, Consumer<GameItem> consumer) {
// initRender();
//
// for (GameItem gameItem : gameItems) {
// // Set up data required by GameItem
// consumer.accept(gameItem);
// // Render this game item
// glDrawElements(GL_TRIANGLES, vertexCount, GL_UNSIGNED_INT, 0);
// }
//
// endRender();
// }

// private void initRender() {
// Texture texture = material.getTexture();
// if (texture != null) {
// // Activate firs texture bank
// glActiveTexture(GL_TEXTURE0);
// // Bind the texture
// glBindTexture(GL_TEXTURE_2D, texture.getId());
// }
//
// // Draw the mesh
// glBindVertexArray(vaoId);
// }

// private void endRender() {
// // Restore state
// glBindVertexArray(0);
//
// glBindTexture(GL_TEXTURE_2D, 0);
// }

public void render() {
Texture texture = material.getTexture();
if (texture != null) {
Expand All @@ -162,20 +125,19 @@ public void render() {
// Bind the texture
glBindTexture(GL_TEXTURE_2D, texture.getId());
}
// Texture normalMap = material.getNormalMap();
// if (normalMap != null) {
// // Activate first texture bank
// glActiveTexture(GL_TEXTURE1);
// // Bind the texture
// glBindTexture(GL_TEXTURE_2D, normalMap.getId());
// }
Texture normalMap = material.getNormalMap();
if (normalMap != null) {
// Activate first texture bank
glActiveTexture(GL_TEXTURE1);
// Bind the texture
glBindTexture(GL_TEXTURE_2D, normalMap.getId());
}

// Draw the mesh
GL30.glBindVertexArray(vaoId);

GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
// GL20.glEnableVertexAttribArray(2);
GL20.glEnableVertexAttribArray(2);

glDrawElements(GL_TRIANGLES, vertexCount, GL_UNSIGNED_INT, 0);

Expand All @@ -194,9 +156,10 @@ public void setMaterial(Material material) {


public void cleanUp() {
// Restore state
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
// GL20.glDisableVertexAttribArray(2);
GL20.glDisableVertexAttribArray(2);

// Delete the VBOs
glBindBuffer(GL_ARRAY_BUFFER, 0);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ru/mycubecraft/data/Contact.java
Expand Up @@ -8,7 +8,7 @@ public class Contact implements Comparable<Contact> {
public final float t;
/* The collision normal */
public int nx, ny, nz;
/* The global position of the collided voxel */
/* The global position of the collided block */
public int x, y, z;

public Contact(float t, int x, int y, int z) {
Expand All @@ -24,6 +24,6 @@ public int compareTo(Contact o) {
}

public String toString() {
return "{" + x + "|" + y + "|" + z + " " + nx + " " + ny + " " + nz + " @ " + t + "}";
return "{The global position of the collided block: x=" + x + " y=" + y + " z=" + z + " The collision normal: nx=" + nx + " ny=" + ny + " nz=" + nz + " @ The collision time: " + t + "}";
}
}
2 changes: 1 addition & 1 deletion src/main/java/ru/mycubecraft/data/Hud.java
Expand Up @@ -44,7 +44,7 @@ public Hud() {
public void buildHud() {
fontTexture.buildTexture();

String version = "ALPHA 0.1.0";
String version = "ALPHA 0.1.2";
String fpsText = "FPS: 0";
String coordinatesText = "X: 0.00 Y: 0.00 Z: 0.00";
String createdChunksSizeText = "CHUNKS: 0";
Expand Down
Expand Up @@ -2,25 +2,19 @@

import org.joml.FrustumIntersection;
import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector3f;
import ru.mycubecraft.Settings;
import ru.mycubecraft.core.GameItem;
import ru.mycubecraft.renderer.Camera;

import java.util.ArrayList;

public class FrustumCullingFilter {

private final Matrix4f prjViewMatrix;
private final Vector2f nearFar;
private final FrustumIntersection frustumInt;
private final float closestDistance = Settings.Z_FAR;

public FrustumCullingFilter() {
prjViewMatrix = new Matrix4f();
frustumInt = new FrustumIntersection();
nearFar = new Vector2f();
}

public void updateFrustum(Matrix4f projMatrix, Matrix4f viewMatrix) {
Expand All @@ -31,11 +25,11 @@ public void updateFrustum(Matrix4f projMatrix, Matrix4f viewMatrix) {
frustumInt.set(prjViewMatrix);
}

public void filter(ArrayList<GameItem> gameItems, Camera camera) {
gameItems.parallelStream().forEach(item -> filter(item, camera));
public void filter(ArrayList<GameItem> gameItems) {
gameItems.parallelStream().forEach(this::filter);
}

public void filter(GameItem gameItem, Camera camera) {
public void filter(GameItem gameItem) {
float boundingRadius;
Vector3f gameItemPosition;
if (gameItem != null && !gameItem.isDisableFrustumCulling()) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/ru/mycubecraft/renderer/Camera.java
Expand Up @@ -14,9 +14,9 @@ public class Camera {
public Vector3f rotation;

public Camera() {
this.position = new Vector4f(29.0f, 130.0f, 29.0f, 1);
this.position = new Vector4f(40.0f, 200f, 29.0f, 1);
this.rotation = new Vector3f(32.0f, 9.0f, 0.0f);
viewMatrix = new Matrix4f();
this.viewMatrix = new Matrix4f();
}

public Matrix4f getViewMatrix() {
Expand Down Expand Up @@ -62,14 +62,14 @@ public void moveRotation(double offsetX, double offsetY, double offsetZ) {
}

public void movePosition(float offsetX, float offsetY, float offsetZ) {
if (offsetZ != 0) {
// if (offsetZ != 0) {
position.x += (float) Math.sin(Math.toRadians(rotation.y)) * -1.0f * offsetZ;
position.z += (float) Math.cos(Math.toRadians(rotation.y)) * offsetZ;
}
if (offsetX != 0) {
// }
// if (offsetX != 0) {
position.x += (float) Math.sin(Math.toRadians(rotation.y - 90)) * -1.0f * offsetX;
position.z += (float) Math.cos(Math.toRadians(rotation.y - 90)) * offsetX;
}
// }
position.y += offsetY;
}

Expand Down
8 changes: 2 additions & 6 deletions src/main/java/ru/mycubecraft/renderer/Renderer.java
Expand Up @@ -10,8 +10,6 @@
import ru.mycubecraft.engine.SkyBox;
import ru.mycubecraft.engine.graph.DirectionalLight;
import ru.mycubecraft.engine.graph.FrustumCullingFilter;
import ru.mycubecraft.engine.graph.PointLight;
import ru.mycubecraft.engine.graph.SpotLight;
import ru.mycubecraft.scene.Scene;
import ru.mycubecraft.util.AssetPool;
import ru.mycubecraft.window.Window;
Expand Down Expand Up @@ -60,7 +58,7 @@ public void clear() {
public void render(World world, Camera camera, Scene scene, IHud hud, Vector3f ambientLight) {
clear();
filteredItems.clear();

Window window = Window.getInstance();
if (window.isResized()) {
glViewport(0, 0, window.getWidth(), window.getHeight());
Expand All @@ -77,8 +75,6 @@ public void render(World world, Camera camera, Scene scene, IHud hud, Vector3f a

private void renderScene(World world, Scene scene, Vector3f ambientLight) {

Camera camera = scene.getCamera();

boolean frustumCulling = true;
Matrix4f projectionMatrix = transformation.getProjectionMatrix();
Matrix4f viewMatrix = transformation.getViewMatrix();
Expand All @@ -100,7 +96,7 @@ private void renderScene(World world, Scene scene, Vector3f ambientLight) {
.filter(Block::isVisible)
.filter(block -> {
GameItem gameItem = block.getGameCubeItem();
frustumFilter.filter(gameItem, camera);
frustumFilter.filter(gameItem);
return gameItem.isInsideFrustum();
}).forEach(block -> filteredItems.add(block.getGameCubeItem()));
}
Expand Down

0 comments on commit bfaa4cb

Please sign in to comment.