Fix #5
1 parent 7c747ab commit 1caf9bbd284a687601319ce71f2f6611abb01b95
@cory cory authored on 30 Jan 2023
Showing 9 changed files
View
76
cube.json 0 → 100644
{
"collections": {
"name":"Main",
"subCollections":
[
{
"name":"collection1",
"objects":[
{
"name":"Cube",
"position":
[0,0,0],
"rotation":
[0,0,0],
"points":
[[-30.0, -10.0, -10.0],
[-30.0, -10.0, 10.0],
[-30.0, 10.0, -10.0],
[-30.0, 10.0, 10.0],
[-10.0, -10.0, -10.0],
[-10.0, -10.0, 10.0],
[-10.0, 10.0, -10.0],
[-10.0, 10.0, 10.0]
]
,"faceList":[
[0, 2, 3, 1],
[4, 5, 7, 6],
[0, 1, 5, 4],
[1, 3, 7, 5],
[3, 2, 6, 7],
[2, 0, 4, 6]],
"textureList":
["red", "green", "blue", "cyan", "magenta", "yellow"],
"uvPointsList":
[
[[0, 0], [100, 0], [100, 100], [0, 100]],
[[0, 0], [100, 0], [100, 100], [0, 100]],
[[0, 0], [100, 0], [100, 100], [0, 100]],
[[0, 0], [100, 0], [100, 100], [0, 100]],
[[0, 0], [100, 0], [100, 100], [0, 100]],
[[0, 0], [100, 0], [100, 100], [0, 100]]
]
}
]
}
]
},
"textures":[
{
"name":"red",
"type":"solid",
"color":"FF0000"
},{
"name":"green",
"type":"solid",
"color":"00FF00"
},{
"name":"blue",
"type":"solid",
"color":"0000FF"
},{
"name":"cyan",
"type":"solid",
"color":"00FFFF"
},{
"name":"magenta",
"type":"solid",
"color":"FF00FF"
},{
"name":"yellow",
"type":"solid",
"color":"FFFF00"
}
]
}
View
217
src/main/java/uk/org/floop/epq3d/App.java
package uk.org.floop.epq3d;
 
import javax.swing.*;
import java.awt.*;
 
class App {
static ObjectCollection mainCollection;
private static void initWindow() {
 
public static void main(String[] args) {
// invokeLater() is used here to prevent our graphics processing from
// blocking the GUI. https://stackoverflow.com/a/22534931/4655368
// this is a lot of boilerplate code that you shouldn't be too concerned about.
// just know that when main runs it will call initWindow() once.
// will call initWindow() once.
SwingUtilities.invokeLater(App::initWindow);
}
 
public static void initObjects(){
mainCollection = new ObjectCollection();
JsonReader Jason = new JsonReader("cube.json");
Jason.getObjects();
mainCollection = Jason.mainCollection;
mainCollection.initialiseAll();
//textures = Jason.textures;
//mainCollection = new ObjectCollection();
 
Texture testTexture;
/*try {
//testTexture = ImageIO.read(new File("/home/cory/Screenshot from 2022-06-06 18-52-12.png"));
testTexture = new Texture(ImageIO.read(new File("/home/cory/Screenshot from 2022-09-26 13-05-40.png")));
} catch (IOException e) {
throw new RuntimeException(e);
}*/
Texture red = new Texture(Color.red);
Texture green = new Texture(Color.GREEN);
Texture blue = new Texture(Color.BLUE);
Texture cyan = new Texture(Color.CYAN);
Texture magenta = new Texture(Color.magenta);
Texture yellow = new Texture(Color.yellow);
 
 
mainCollection.addObject(new Object3d(new PointComp[]{
new PointComp(-30,-10,-10),
new PointComp(-30,-10,10),
new PointComp(-30,10,-10),
new PointComp(-30,10,10),
new PointComp(-10,-10,-10),
new PointComp(-10,-10,10),
new PointComp(-10,10,-10),
new PointComp(-10,10,10)
 
}, new int[][]{
{0,2,3,1},
{4,5,7,6},
{0,1,5,4},
{1,3,7,5},
{3,2,6,7},
{2,0,4,6},
}, new Point2D[][]{
{new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
{new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
{new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
{new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
{new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
{new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
{new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
},
false,
new Texture[]{red, green, blue, cyan, magenta, yellow},
true));
// Texture testTexture;
// /*try {
// //testTexture = ImageIO.read(new File("/home/cory/Screenshot from 2022-06-06 18-52-12.png"));
// testTexture = new Texture(ImageIO.read(new File("/home/cory/Screenshot from 2022-09-26 13-05-40.png")));
// } catch (IOException e) {
// throw new RuntimeException(e);
// }*/
// Texture red = new Texture("red", Color.red);
// Texture green = new Texture("green", Color.GREEN);
// Texture blue = new Texture("blue", Color.BLUE);
// Texture cyan = new Texture(Color.CYAN);
// Texture magenta = new Texture(Color.magenta);
// Texture yellow = new Texture(Color.yellow);
//
//
// mainCollection.addObject(new Object3d(new PointComp[]{
// new PointComp(-10,10,10),
// new PointComp(-30,-10,-10),
// new PointComp(-30,-10,10),
// new PointComp(-30,10,-10),
// new PointComp(-30,10,10),
// new PointComp(-10,-10,-10),
// new PointComp(-10,-10,10),
// new PointComp(10,10,10),
// new PointComp(10,-10,10),
// new PointComp(0,0,24)
// new PointComp(-10,10,-10),
// new PointComp(-10,10,10)
//
// }, new int[][]{
// {0,2,3,1},
// {1,0,4},
// {0,2,4},
// {2,3,4},
// {3,1,4}
// }, true));
mainCollection.addObject(new Object3d(new PointComp[]{
new PointComp(0,0,0),
new PointComp(20,0,0),
new PointComp(0,10,0),
new PointComp(0,0,10),
}, new int[][]{
{0,1,2},
{0,2,3},
{0,3,1},
{2,1,3}
}, new Point2D[][]{
{new Point2D(0,0), new Point2D(100,0), new Point2D(0,100)},
{new Point2D(0,0), new Point2D(100,0), new Point2D(0,100)},
{new Point2D(0,0), new Point2D(100,0), new Point2D(0,100)},
},
false,
new Texture[]{red, blue, cyan, yellow},
true));
// {4,5,7,6},
// {0,1,5,4},
// {1,3,7,5},
// {3,2,6,7},
// {2,0,4,6},
// }, new Point2D[][]{
// {new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
// {new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
// {new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
// {new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
// {new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
// {new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
// {new Point2D(0, 0), new Point2D(100, 0), new Point2D(100, 100), new Point2D(0, 100)},
// },
// false,
// new Texture[]{red, green, blue, cyan, magenta, yellow},
// true));
//// mainCollection.addObject(new Object3d(new PointComp[]{
//// new PointComp(-10,10,10),
//// new PointComp(-10,-10,10),
//// new PointComp(10,10,10),
//// new PointComp(10,-10,10),
//// new PointComp(0,0,24)
//// }, new int[][]{
//// {0,2,3,1},
//// {1,0,4},
//// {0,2,4},
//// {2,3,4},
//// {3,1,4}
//// }, true));
// mainCollection.addObject(new Object3d(new PointComp[]{
// new PointComp(0,0,0),
// new PointComp(20,0,0),
// new PointComp(0,10,0),
// new PointComp(0,0,10),
// }, new int[][]{
// {0,1,2},
// {0,2,3},
// {0,3,1},
// {2,1,3}
// }, new Point2D[][]{
// {new Point2D(0,0), new Point2D(100,0), new Point2D(0,100)},
// {new Point2D(0,0), new Point2D(100,0), new Point2D(0,100)},
// {new Point2D(0,0), new Point2D(100,0), new Point2D(0,100)},
// },
// false,
// new Texture[]{red, blue, cyan, yellow},
// true));
}
}
View
10
src/main/java/uk/org/floop/epq3d/HTTPPost.java
package uk.org.floop.epq3d;
 
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
 
public class HTTPPost {
public void invokePost(JsonWriter json) {
/* public void invokePost(JsonWriter json) {
 
try {
String requestBody = json.file.toJSONString();
HttpClient client = HttpClient.newHttpClient();
// var objectMapper = new ObjectMapper();
// String requestBody = objectMapper.writeValueAsString(values);
// return requestBody;
// }
 
*/
}
 
View
109
src/main/java/uk/org/floop/epq3d/JsonReader.java 0 → 100644
package uk.org.floop.epq3d;
 
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 
import java.awt.*;
import java.io.FileReader;
import java.io.IOException;
import java.util.Objects;
 
// Program for print data in JSON format.
public class JsonReader {
JSONObject file;
Texture[] textures;
ObjectCollection mainCollection;
public JsonReader(String filePath) {
JSONParser parser = new JSONParser();
Object obj;
try {
obj = parser.parse(new FileReader(filePath));
} catch (IOException | ParseException e) {
throw new RuntimeException(e);
}
file = (JSONObject)obj;
}
public void getObjects(){
textures = getTextures((JSONArray) file.get("textures"));
mainCollection = separateCollection((JSONObject) file.get("collections"), textures);
file.get("collection");
}
private ObjectCollection separateCollection(JSONObject json, Texture[] textures){
ObjectCollection result = new ObjectCollection();
 
// first, get the sub collections of the object and separate them recursively.
JSONArray subCollections = (JSONArray) json.get("subCollections");
if(subCollections != null) for (Object collection: subCollections) {
result.addCollection(separateCollection((JSONObject) collection, textures));
}
// next, get the name of the collection
result.name = json.get("name").toString();
 
// next, get the objects contained in this collection
JSONArray objects3d = (JSONArray) json.get("objects");
if(objects3d != null) for(Object objectJson: objects3d){
JSONObject object = (JSONObject)objectJson;
JSONArray pointsJson = (JSONArray) object.get("points");
// pack points
PointComp[] points = new PointComp[pointsJson.size()];
for (int i = 0; i < pointsJson.size(); i+=1){
JSONArray jsonPoint = (JSONArray) pointsJson.get(i);
points[i] = new PointComp(
(double) jsonPoint.get(0),
(double) jsonPoint.get(1),
(double) jsonPoint.get(2)
);
 
}
 
// make faceList, UV list and texture lists
// (the indices should match up, if they don't something went wrong)
JSONArray faceListJson = (JSONArray) object.get("faceList");
JSONArray uvPointsListJson = (JSONArray) object.get("uvPointsList");
JSONArray texturesNamesJson = (JSONArray) object.get("textureList");
 
int[][] faceList = new int[faceListJson.size()][];
Point2D[][] uvPointsList = new Point2D[faceListJson.size()][];
Texture[] texturesList = new Texture[faceListJson.size()];
 
for (int i = 0; i < faceListJson.size(); i+=1) {
JSONArray jsonFace = (JSONArray) faceListJson.get(i);
JSONArray jsonUV = (JSONArray) uvPointsListJson.get(i);
String jsonTextureName = (String) texturesNamesJson.get(i);
 
faceList[i] = new int[jsonFace.size()];
uvPointsList[i] = new Point2D[jsonFace.size()];
 
for (int j = 0; j < jsonFace.size(); j += 1){
faceList[i][j] = Integer.parseInt(String.valueOf(jsonFace.get(j)));
uvPointsList[i][j] = new Point2D(
Integer.parseInt(String.valueOf(((JSONArray)jsonUV.get(j)).get(0))),
Integer.parseInt(String.valueOf(((JSONArray)jsonUV.get(j)).get(1)))
);
}
 
// terrible linear search but idc because this code only runs once
for (Texture texture : textures) {
if (Objects.equals(texture.name, jsonTextureName)) {
texturesList[i] = texture;
break;
}
}
}
result.addObject(new Object3d(points, faceList, uvPointsList, false, texturesList, true));
}
return result;
}
public Texture[] getTextures(JSONArray textures){
Texture[] result = new Texture[textures.size()];
for(int i = 0; i < textures.size(); i += 1){
JSONObject JsonTexture = (JSONObject) textures.get(i);
if(Objects.equals(JsonTexture.get("type"), "solid")){
result[i] = new Texture(JsonTexture.get("name").toString(), Color.decode('#' + (String) JsonTexture.get("color")));
}
}
return result;
}
}
View
4
src/main/java/uk/org/floop/epq3d/Object3d.java
textures = _textures;
 
if(initialise){initialise();}
}
 
 
public void invalidate(){
for (Face face:
faces) {
face.invalidate();
faces[face].points[point] = points[faceList[face][point]];
}
faces[face].hasEdges = hasEdges;
faces[face].texture = textures[face];
faces[face].UVPoints = uvPoints[0];
faces[face].UVPoints = uvPoints[face];
faces[face].initialise();
}
// init bounding sphere
double distance = 0;
View
src/main/java/uk/org/floop/epq3d/ObjectCollection.java
View
src/main/java/uk/org/floop/epq3d/Screen.java
View
src/main/java/uk/org/floop/epq3d/Texture.java
View
src/main/java/uk/org/floop/epq3d/testing.java