Skip to content

Commit

Permalink
Merge pull request #8 from Gaia3D/develop
Browse files Browse the repository at this point in the history
For version 1.4.4 release
  • Loading branch information
znkim committed Mar 5, 2024
2 parents 91b6f34 + 24e0150 commit 6c5f273
Show file tree
Hide file tree
Showing 60 changed files with 1,882 additions and 679 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class GaiaBufferDataSet implements Serializable {
private int id = -1;
private String guid = "no_guid";
private int materialId;
public GaiaMaterial material = null;

GaiaBoundingBox boundingBox = null;
GaiaRectangle texcoordBoundingRectangle = null;
Expand Down Expand Up @@ -155,35 +154,33 @@ public GaiaPrimitive toPrimitive() {
public void clear() {
buffers.forEach((key, value) -> value.clear());
buffers.clear();

material.clear();
material = null;
boundingBox = null;
texcoordBoundingRectangle = null;
transformMatrix = null;
preMultipliedTransformMatrix = null;
}

public GaiaBufferDataSet clone() throws CloneNotSupportedException {
public GaiaBufferDataSet clone() {
GaiaBufferDataSet clone = new GaiaBufferDataSet();
clone.setId(this.id);
clone.setGuid(this.guid);
clone.setMaterialId(this.materialId);
clone.setMaterial(this.material.clone());
if (this.boundingBox != null) {
clone.setBoundingBox(this.boundingBox.clone());
}
if (this.texcoordBoundingRectangle != null) {
clone.setTexcoordBoundingRectangle(this.texcoordBoundingRectangle.clone());
}
if (this.transformMatrix != null) {
clone.setTransformMatrix((Matrix4d) this.transformMatrix.clone());
clone.setTransformMatrix(new Matrix4d(this.transformMatrix));
}
if (this.preMultipliedTransformMatrix != null) {
clone.setPreMultipliedTransformMatrix((Matrix4d) this.preMultipliedTransformMatrix.clone());
clone.setPreMultipliedTransformMatrix(new Matrix4d(this.preMultipliedTransformMatrix));
}
for (Map.Entry<AttributeType, GaiaBuffer> entry : this.buffers.entrySet()) {
clone.buffers.put(entry.getKey(), entry.getValue().clone());
GaiaBuffer buffer = entry.getValue();
GaiaBuffer clonedBuffer = buffer.clone();
clone.buffers.put(entry.getKey(), clonedBuffer);
}
return clone;
}
Expand Down
72 changes: 45 additions & 27 deletions core/src/main/java/com/gaia3d/basic/exchangable/GaiaSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.gaia3d.basic.types.AttributeType;
import com.gaia3d.basic.types.FormatType;
import com.gaia3d.basic.types.TextureType;
import com.gaia3d.util.ImageResizer;
import com.gaia3d.util.ImageUtils;
import com.gaia3d.util.io.BigEndianDataInputStream;
import com.gaia3d.util.io.BigEndianDataOutputStream;
import lombok.AllArgsConstructor;
Expand All @@ -19,13 +19,9 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.joml.Matrix4d;
import org.joml.Quaterniond;
import org.joml.Vector2d;
import org.joml.Vector3d;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -47,10 +43,10 @@ public class GaiaSet implements Serializable{
List<GaiaBufferDataSet> bufferDatas;
List<GaiaMaterial> materials;

Vector3d position;
Vector3d scale;
Quaterniond quaternion;
private Matrix4d transformMatrix;
//Vector3d position;
//Vector3d scale;
//Quaterniond quaternion;
//private Matrix4d transformMatrix;

byte isBigEndian = 0;
String projectName;
Expand All @@ -61,8 +57,8 @@ public class GaiaSet implements Serializable{

public GaiaSet(Path path) {
readFile(path);
this.transformMatrix = new Matrix4d();
this.transformMatrix.identity();
//this.transformMatrix = new Matrix4d();
//this.transformMatrix.identity();
}

public GaiaSet(GaiaScene gaiaScene) {
Expand All @@ -84,9 +80,13 @@ public GaiaBoundingBox getBoundingBox() {
}

public Path writeFile(Path path, int serial) {
String tempFile = projectName + "_" + serial + "." + FormatType.TEMP.getExtension();
File output = new File(path.toAbsolutePath().toString(), tempFile);
try (BigEndianDataOutputStream stream = new BigEndianDataOutputStream(new BufferedOutputStream(new FileOutputStream(output), 32768))) {
String tempFileName = projectName + "_" + serial + "." + FormatType.TEMP.getExtension();

int dividedNumber = serial / 10000;
Path tempDir = path.resolve(this.projectName).resolve(String.valueOf(dividedNumber));
tempDir.toFile().mkdirs();
File tempFile = tempDir.resolve(tempFileName).toFile();
try (BigEndianDataOutputStream stream = new BigEndianDataOutputStream(new BufferedOutputStream(new FileOutputStream(tempFile), 32768))) {
stream.writeByte(isBigEndian);
stream.writeText(projectName);
stream.writeInt(materials.size());
Expand All @@ -96,25 +96,23 @@ public Path writeFile(Path path, int serial) {
for (GaiaMaterial material : materials) {
Map<TextureType, List<GaiaTexture>> materialTextures = material.getTextures();
List<GaiaTexture> diffuseTextures = materialTextures.get(TextureType.DIFFUSE);
if (!diffuseTextures.isEmpty()) {
if (diffuseTextures != null && !diffuseTextures.isEmpty()) {
GaiaTexture texture = materialTextures.get(TextureType.DIFFUSE).get(0);
Path parentPath = texture.getParentPath();
String diffusePath = texture.getPath();
texture.setPath(this.projectName + File.separator + diffusePath);
texture.setPath(diffusePath);
String imagePath = parentPath + File.separator + diffusePath;

Path imageTempPath = path.resolve("images").resolve(this.projectName);
Path imageTempPath = tempDir.resolve("images");
imageTempPath.toFile().mkdir();

Path outputPath = imageTempPath.resolve(diffusePath);

/*outputPath.toFile().getAbsolutePath();
BufferedImage bufferedImage = ImageIO.read(new File(imagePath));
ImageResizer imageResizer = new ImageResizer();
BufferedImage resizedImage = imageResizer.resizeImageGraphic2D(bufferedImage, 16, 16);
gaiaTextureArchive.addTexture(outputPath.toFile().getAbsolutePath(), resizedImage);*/

FileUtils.copyFile(new File(imagePath), outputPath.toFile());
File inputPathFile = new File(imagePath);
inputPathFile = ImageUtils.getChildFile(parentPath.toFile(), diffusePath);
File outputPathFile = outputPath.toFile();
if (!outputPathFile.exists()) {
FileUtils.copyFile(inputPathFile, outputPath.toFile());
}
}
material.write(stream);
}
Expand All @@ -124,8 +122,9 @@ public Path writeFile(Path path, int serial) {
}
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
return output.toPath();
return tempFile.toPath();
}

public void readFile(Path path) {
Expand Down Expand Up @@ -160,7 +159,7 @@ public void readFile(Path path) {
GaiaMaterial materialById = materials.stream()
.filter(material -> material.getId() == materialId)
.findFirst().orElseThrow();
bufferDataSet.setMaterial(materialById);
bufferDataSet.setMaterialId(materialById.getId());
GaiaRectangle texcoordBoundingRectangle = calcTexcoordBoundingRectangle(bufferDataSet);
bufferDataSet.setTexcoordBoundingRectangle(texcoordBoundingRectangle);
bufferDataSets.add(bufferDataSet);
Expand Down Expand Up @@ -210,6 +209,25 @@ public void deleteTextures() {
materials.forEach(GaiaMaterial::deleteTextures);
}

public GaiaSet clone(){
GaiaSet gaiaSet = new GaiaSet();
gaiaSet.setBufferDatas(new ArrayList<>());
for (GaiaBufferDataSet bufferData : this.bufferDatas) {
gaiaSet.getBufferDatas().add(bufferData.clone());
}
gaiaSet.setMaterials(new ArrayList<>());
for (GaiaMaterial material : this.materials) {
gaiaSet.getMaterials().add(material.clone());
}
gaiaSet.setIsBigEndian(this.isBigEndian);
gaiaSet.setProjectName(this.projectName);
gaiaSet.setFilePath(this.filePath);
gaiaSet.setFolderPath(this.folderPath);
gaiaSet.setProjectFolderPath(this.projectFolderPath);
gaiaSet.setOutputDir(this.outputDir);
return gaiaSet;
}

public void clear() {
this.bufferDatas.forEach(GaiaBufferDataSet::clear);
this.bufferDatas.clear();
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/com/gaia3d/basic/structure/GaiaFace.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gaia3d.basic.structure;

import com.gaia3d.util.GeometryUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -86,6 +87,10 @@ public void clear() {
faceNormal = null;
}

public GaiaFace clone() {
return new GaiaFace(indices.clone(), new Vector3d(faceNormal));
}

public boolean hasCoincidentIndices(GaiaFace face) {
for (int i = 0; i < indices.length; i++) {
for (int j = 0; j < face.getIndices().length; j++) {
Expand All @@ -96,4 +101,18 @@ public boolean hasCoincidentIndices(GaiaFace face) {
}
return false;
}

public double calculateArea(List<GaiaVertex> vertices) {
double area = 0.0;
for (int i = 0; i < indices.length; i+=3) {
int indices1 = indices[i];
int indices2 = indices[i + 1];
int indices3 = indices[i + 2];
GaiaVertex vertex1 = vertices.get(indices1);
GaiaVertex vertex2 = vertices.get(indices2);
GaiaVertex vertex3 = vertices.get(indices3);
area += GeometryUtils.getTriangleArea(vertex1, vertex2, vertex3);
}
return area;
}
}
9 changes: 9 additions & 0 deletions core/src/main/java/com/gaia3d/basic/structure/GaiaMesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,13 @@ public void clear() {
this.primitives.forEach(GaiaPrimitive::clear);
this.primitives.clear();
}

public GaiaMesh clone() {
GaiaMesh gaiaMesh = new GaiaMesh();
gaiaMesh.setPrimitives(new ArrayList<>());
for (GaiaPrimitive primitive : this.primitives) {
gaiaMesh.getPrimitives().add(primitive.clone());
}
return gaiaMesh;
}
}
30 changes: 26 additions & 4 deletions core/src/main/java/com/gaia3d/basic/structure/GaiaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.joml.Matrix4d;
import org.joml.Vector2d;
import org.joml.Vector3d;
Expand All @@ -26,6 +27,7 @@
* @since 1.0.0
* @see <a href="https://en.wikipedia.org/wiki/Scene_graph">Scene graph</a>
*/
@Slf4j
@Getter
@Setter
@NoArgsConstructor
Expand Down Expand Up @@ -155,15 +157,15 @@ public void recalculateTransform() {
}

public void toGaiaBufferSets(List<GaiaBufferDataSet> bufferSets, Matrix4d parentTransformMatrix) {
Matrix4d transformMatrix = new Matrix4d(this.transformMatrix);
Matrix4d sumTransformMatrix = new Matrix4d(this.transformMatrix);
if (parentTransformMatrix != null) {
parentTransformMatrix.mul(transformMatrix, transformMatrix);
parentTransformMatrix.mul(sumTransformMatrix, sumTransformMatrix);
}
for (GaiaMesh mesh : this.getMeshes()) {
mesh.toGaiaBufferSets(bufferSets, transformMatrix);
mesh.clone().toGaiaBufferSets(bufferSets, sumTransformMatrix);
}
for (GaiaNode child : this.getChildren()) {
child.toGaiaBufferSets(bufferSets, transformMatrix);
child.toGaiaBufferSets(bufferSets, sumTransformMatrix);
}
}

Expand All @@ -176,6 +178,19 @@ public void translate(Vector3d translation) {
}
}

public GaiaNode clone() {
GaiaNode clone = new GaiaNode();
clone.setName(this.name);
clone.setTransformMatrix(new Matrix4d(this.transformMatrix));
for (GaiaMesh mesh : this.meshes) {
clone.getMeshes().add(mesh.clone());
}
for (GaiaNode child : this.children) {
clone.getChildren().add(child.clone());
}
return clone;
}

public void clear() {
this.parent = null;
this.name = null;
Expand All @@ -187,4 +202,11 @@ public void clear() {
this.meshes.clear();
this.children.clear();
}

public void extractMeshes(List<GaiaMesh> resultMeshes) {
resultMeshes.addAll(this.getMeshes());
for (GaiaNode child : this.getChildren()) {
child.extractMeshes(resultMeshes);
}
}
}
34 changes: 18 additions & 16 deletions core/src/main/java/com/gaia3d/basic/structure/GaiaPrimitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import org.lwjgl.opengl.GL20;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.*;

/**
* A class that represents a primitive of a Gaia object.
Expand All @@ -42,7 +40,7 @@ public class GaiaPrimitive implements Serializable {
private List<GaiaVertex> vertices = new ArrayList<>();
private List<GaiaSurface> surfaces = new ArrayList<>();

private GaiaMaterial material = null;
//private GaiaMaterial material = null;

public GaiaBoundingBox getBoundingBox(Matrix4d transform) {
GaiaBoundingBox boundingBox = new GaiaBoundingBox();
Expand Down Expand Up @@ -77,16 +75,6 @@ public GaiaRectangle getTexcoordBoundingRectangle(GaiaRectangle texcoordBounding
return texcoordBoundingRectangle;
}

public void translateTexCoordsToPositiveQuadrant()
{
int surfacesCount = surfaces.size();
for(int i=0; i<surfacesCount; i++)
{
GaiaSurface surface = surfaces.get(i);
surface.translateTexCoordsToPositiveQuadrant(this.vertices);
}
}

public void calculateNormal() {
for (GaiaSurface surface : surfaces) {
surface.calculateNormal(this.vertices);
Expand All @@ -101,7 +89,8 @@ public int[] getIndices() {
return resultIndices;
}

public GaiaBufferDataSet toGaiaBufferSet(Matrix4d transformMatrix) {
public GaiaBufferDataSet toGaiaBufferSet(Matrix4d transformMatrixOrigin) {
Matrix4d transformMatrix = new Matrix4d(transformMatrixOrigin);
Matrix3d rotationMatrix = new Matrix3d();
transformMatrix.get3x3(rotationMatrix);
// normalize the rotation matrix
Expand Down Expand Up @@ -272,7 +261,7 @@ public GaiaBufferDataSet toGaiaBufferSet(Matrix4d transformMatrix) {
gaiaBufferDataSet.setBoundingBox(boundingBox);

//assign material. Son 2023.07.17
gaiaBufferDataSet.setMaterial(this.material);
//gaiaBufferDataSet.setMaterial(this.material);
return gaiaBufferDataSet;
}

Expand All @@ -291,4 +280,17 @@ public void clear() {
this.vertices.clear();
this.surfaces.clear();
}

public GaiaPrimitive clone() {
GaiaPrimitive gaiaPrimitive = new GaiaPrimitive();
gaiaPrimitive.setAccessorIndices(this.accessorIndices);
gaiaPrimitive.setMaterialIndex(this.materialIndex);
for (GaiaVertex vertex : this.vertices) {
gaiaPrimitive.getVertices().add(vertex.clone());
}
for (GaiaSurface surface : this.surfaces) {
gaiaPrimitive.getSurfaces().add(surface.clone());
}
return gaiaPrimitive;
}
}
Loading

0 comments on commit 6c5f273

Please sign in to comment.