Skip to content

Commit

Permalink
Merge pull request #1 from callumnewlands/checkpointing
Browse files Browse the repository at this point in the history
Adds checkpointing and "out of forest" output
  • Loading branch information
callumnewlands authored Jun 19, 2021
2 parents 50deb51 + 6204f15 commit f4b59ec
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ target/
# Project exclude paths
/target/
/frames/
/checkpoints/

# Large files
*.hdr
*.hdr
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ recommended to download them and save them inside the ```/resources/textures/```
| 5 | (use config file) | Toggle depth output |
| 6 | (use config file) | Toggle shadows |
| 7 | (use config file) | Toggle leaf translucency |
| 9 | | Print current position and direction |
| 9 | C | Create a checkpoint yaml file for the current position and direction |
| Mouse | I | Look up |
| Mouse | K | Look down |
| Mouse | J | Look left |
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;
Expand Down Expand Up @@ -978,13 +980,14 @@ private void pollKeys() {
lightingPassShader.setUniform("translucencyEnabled", parameters.lighting.translucency.enabled);
}
if (glfwGetKey(window, GLFW_KEY_9) == GLFW_PRESS) {
System.out.println("Pos: " + camera.getPosition() + " Dir: " + camera.getDirection());
checkpoint();
}
}

private void blockAndProcessInputStream() {
final float LOOK_OFFSET = parameters.input.stdin.lookOffset * 30f / parameters.input.stdin.fps;
try {
glfwPollEvents();
String s = inputStream.readLine().toUpperCase();
for (char c : s.toCharArray()) {
switch (c) {
Expand All @@ -998,6 +1001,7 @@ private void blockAndProcessInputStream() {
case 'K' -> camera.processMouseMovement(0, -LOOK_OFFSET);
case 'J' -> camera.processMouseMovement(-LOOK_OFFSET, 0);
case 'L' -> camera.processMouseMovement(LOOK_OFFSET, 0);
case 'C' -> checkpoint();
case 'X' -> exit();
}
quadtree.setSeedPoint(camera.getPosition().x, camera.getPosition().z);
Expand All @@ -1007,6 +1011,27 @@ private void blockAndProcessInputStream() {
}
}

private void checkpoint() {
String fileName = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".yaml";

Parameters currentParams = ParameterLoader.getParameters();
Parameters.Camera cameraParams = new Parameters.Camera();
cameraParams.setVerticalMovement(currentParams.camera.verticalMovement);
cameraParams.setStartDirection(camera.getDirection());
cameraParams.setStartPosition(camera.getPosition());
Parameters newParams = currentParams.toBuilder().camera(cameraParams).build();
try {
File dir = new File("./checkpoints");
dir.mkdir();
ParameterLoader.outputParameters("./checkpoints/" + fileName, newParams);
System.out.println("Checkpoint: " + fileName + " created");
} catch (IOException e) {
System.out.println("Error: Unable to checkpoint:");
e.printStackTrace();
}
System.out.println("Pos: " + camera.getPosition() + " Dir: " + camera.getDirection());
}

private void outputFrame(String type) {
float[] array = new float[windowWidth * windowHeight * 3];
glReadBuffer(GL_FRONT);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/params/ParameterLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import java.io.IOException;
import java.util.List;
import java.util.Random;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.joml.Vector3f;

Expand All @@ -53,6 +56,11 @@ public static Parameters loadParameters(String filePath) throws IOException {
return parameters;
}

public static void outputParameters(String filePath, Parameters parameters) throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.writeValue(new File(filePath), parameters);
}

public static Parameters getParameters() {
if (parameters == null) {
parameters = new Parameters();
Expand Down Expand Up @@ -86,4 +94,15 @@ public Vector3f deserialize(JsonParser jsonParser, DeserializationContext deseri
}
}

static class Vec3Serializer extends JsonSerializer<Vector3f> {

@Override
public void serialize(Vector3f vector3f, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartArray();
jsonGenerator.writeNumber(vector3f.x);
jsonGenerator.writeNumber(vector3f.y);
jsonGenerator.writeNumber(vector3f.z);
jsonGenerator.writeEndArray();
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/params/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.joml.Vector3f;

@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Setter
public class Parameters {

Expand Down Expand Up @@ -110,8 +114,10 @@ public static class Collisions {
@Setter
public static class Camera {
@JsonDeserialize(using = ParameterLoader.Vec3Deserializer.class)
@JsonSerialize(using = ParameterLoader.Vec3Serializer.class)
public Vector3f startPosition = new Vector3f(0, 3.3f, 0);
@JsonDeserialize(using = ParameterLoader.Vec3Deserializer.class)
@JsonSerialize(using = ParameterLoader.Vec3Serializer.class)
public Vector3f startDirection = new Vector3f(0, 0, 1);
public boolean verticalMovement = true;
}
Expand Down Expand Up @@ -139,6 +145,7 @@ public Texture(String diffuse) {
@Setter
public static class ColourFilter {
@JsonDeserialize(using = ParameterLoader.Vec3Deserializer.class)
@JsonSerialize(using = ParameterLoader.Vec3Serializer.class)
public Vector3f colour = new Vector3f(1, 0, 1);
public float mixFactor = 0f;
public boolean expMix = false;
Expand Down Expand Up @@ -349,9 +356,11 @@ public static class Sun {
public boolean autoPosition = true;
public int numSides = 10;
@JsonDeserialize(using = ParameterLoader.Vec3Deserializer.class)
@JsonSerialize(using = ParameterLoader.Vec3Serializer.class)
public Vector3f strength = new Vector3f(3.8f, 3.3f, 3.2f);
public float scale = 20f;
@JsonDeserialize(using = ParameterLoader.Vec3Deserializer.class)
@JsonSerialize(using = ParameterLoader.Vec3Serializer.class)
public Vector3f position = new Vector3f(50.0f, 200.0f, -50.0f);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/small.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ random:
seed: -1

input:
manual: true
manual: false
stdin:
enabled: false
enabled: true

output:
window:
Expand All @@ -22,7 +22,7 @@ ecosystemSimulation:
numIterations: 200

terrain:
width: 75.0
width: 35.0

quadtree:
levels: 3
Expand Down

0 comments on commit f4b59ec

Please sign in to comment.