Skip to content

Commit

Permalink
Added lighting and normal mapping to the lwjgl renderer (#386)
Browse files Browse the repository at this point in the history
* Added light classes

* Added lighting functionality

* Added conditional lighting

* Set options to lighting

* changed media m3d

* 3d settings

* settings changed
  • Loading branch information
net-cscience-raphael committed Feb 26, 2024
1 parent 5280763 commit 443c1ec
Show file tree
Hide file tree
Showing 37 changed files with 1,691 additions and 479 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum MediaType {
VIDEO(0, "v", "video"),
IMAGE(1, "i", "image"),
AUDIO(2, "a", "audio"),
MODEL3D(3, "m", "3dmodel"),
MODEL3D(3, "m", "m3d"),
TEXTUREMODEL3D(5, "mt", "3dtexturemodel"),
IMAGE_SEQUENCE(4, "is", "imagesequence"),
UNKNOWN(99, "u", "unknown");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public interface IModel {
*/
List<Vector3f> getAllNormals();

boolean usesNonDefaultTexture();

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import org.vitrivr.cineast.core.data.m3d.texturemodel.util.MinimalBoundingBox;

/**
* The Material contains all meshes and the texture that are drawn with on the meshes Further it contains the diffuse color of the material
* The Material contains all meshes and the texture that are drawn with on the meshes Further it contains the diffuse
* color of the material
*/
public class Material {

Expand All @@ -25,6 +26,7 @@ public class Material {
* Texture that drawn on all meshes
*/
private Texture texture;
private Texture normalMapTexture;

/**
* DEFAULT_COLOR is black and 100% opaque
Expand All @@ -34,7 +36,11 @@ public class Material {
/**
* diffuseColor is the color that is drawn on the meshes when no texture is present
*/
private Vector4f diffuseColor;
private final Vector4f diffuseColor;

private final Vector4f ambientColor;
private float reflectance;
private final Vector4f specularColor;

/**
* Empty material that can be used as a placeholder.
Expand All @@ -47,7 +53,10 @@ public class Material {
public Material() {
this.meshes = new ArrayList<>();
this.texture = new Texture();
this.normalMapTexture = null;
this.diffuseColor = DEFAULT_COLOR;
this.ambientColor = new Vector4f(0.0f, 0.0f, 0.0f, 1.0f);;
this.specularColor = DEFAULT_COLOR;
}

/**
Expand Down Expand Up @@ -116,6 +125,26 @@ public void setTexture(Texture texture) {
this.texture = texture;
}


public boolean hasNonDefaultTexture(){
return !this.texture.isDefault();
}

/**
* @param texture sets the texture to this material
*/
public void setNormalTexture(Texture texture) {
this.normalMapTexture = texture;
}

public Texture getNormalMapTexture() {
return this.normalMapTexture;
}

public boolean hasNormalMapTexture() {
return this.normalMapTexture != null;
}

/**
* @return the diffuse color of this material
*/
Expand All @@ -127,19 +156,42 @@ public Vector4f getDiffuseColor() {
* @param diffuseColor sets the diffuse color of this material
*/
public void setDiffuseColor(Vector4f diffuseColor) {
this.diffuseColor = diffuseColor;
this.diffuseColor.set(diffuseColor);
}


public Vector4f getAmbientColor() {
return this.ambientColor;
}

public void setAmbientColor(Vector4f ambientColor) {
this.ambientColor.set(ambientColor);
}

public float getReflectance() {
return this.reflectance;
}

public void setReflectance(float reflectance) {
this.reflectance = reflectance;
}

public Vector4f getSpecularColor() {
return this.specularColor;
}

public void setSpecularColor(Vector4f specularColor) {
this.specularColor.set(specularColor);
}

/**
* closes all resources the material uses
* Calls close on all containing classes
* closes all resources the material uses Calls close on all containing classes
*/
public void close() {
this.meshes.forEach(Mesh::close);
this.meshes.clear();
this.texture.close();
this.texture = null;
this.diffuseColor = null;
LOGGER.trace("Closed Material");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,21 @@ public class Mesh {

/**
* List of all vertices normals in the mesh
* TODO: not used yet, will be used for vertex shading
*/
@SuppressWarnings("all")
private final float[] normals;


/**
* List of all vertices normals in the mesh
*/
private final float[] tangents;

/**
* List of all vertices normals in the mesh
*/
private final float[] bitangents;


/**
* MinimalBoundingBox that encloses the mesh
*/
Expand All @@ -84,12 +94,15 @@ public class Mesh {
* @param textureCoordinates List of all texture coordinates in the mesh
* @param idx List of all vertices ids.
*/
public Mesh(float[] positions, float[] normals, float[] textureCoordinates, int[] idx) {
public Mesh(float[] positions, float[] normals, float[] tangents, float[] bitangents, float[] textureCoordinates, int[] idx) {
//Stores all the data
this.positions = positions;
this.idx = idx;
this.numVertices = idx.length;
this.normals = normals;
this.tangents = tangents;
this.bitangents = bitangents;

// List to store results of face normals calculation
this.facenormals = new ArrayList<>(this.numVertices / 3);
//this.areas = new ArrayList<>(positions.length / 3);
Expand All @@ -98,7 +111,7 @@ public Mesh(float[] positions, float[] normals, float[] textureCoordinates, int[
// Calculate face normals
// ic increments by 3 because a face is defined by 3 vertices
for (var ic = 0; ic < this.idx.length; ic += 3) {
if (normals == null) {
if (normals == null || normals.length == 0) {
// Add zero vector if there are no vertex normals
this.facenormals.add(new Vector3f(0f, 0f, 0f));
} else {
Expand All @@ -111,7 +124,7 @@ public Mesh(float[] positions, float[] normals, float[] textureCoordinates, int[
var vn2 = new Vector3f(normals[idx[ic + 1] * 3], normals[idx[ic + 1] * 3 + 1], normals[idx[ic + 1] * 3 + 2]);
var vn3 = new Vector3f(normals[idx[ic + 2] * 3], normals[idx[ic + 2] * 3 + 1], normals[idx[ic + 2] * 3 + 2]);
// Instance the face normal
var fn = new Vector3f(0, 0, 0);
var fn = new Vector3f(0.0F, 0.0F, 0.0F);
// Calculate the direction of the face normal by averaging the three vertex normals
fn.add(vn1).add(vn2).add(vn3).div(3).normalize();
// Instance the face area
Expand Down Expand Up @@ -163,10 +176,20 @@ public int[] getIdx() {
/**
* @return list containing all face normals
*/
public List<Vector3f> getNormals() {
public List<Vector3f> getFaceNormals() {
return this.facenormals;
}

public float[] getVerticesNormals() {
return this.normals;
}
public float[] getTangents() {
return this.tangents;
}
public float[] getBitangents() {
return this.bitangents;
}

/**
* @return the MinimalBoundingBox which contains the scaling factor to norm and the translation to origin (0,0,0)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joml.Vector3f;
import org.joml.Vector4f;
import org.vitrivr.cineast.core.data.m3d.texturemodel.util.MinimalBoundingBox;

/**
Expand Down Expand Up @@ -109,7 +108,7 @@ public List<Material> getMaterials() {
@Override
public List<Vector3f> getAllNormals() {
var normals = new ArrayList<Vector3f>();
this.materials.forEach(m -> m.getMeshes().forEach(mesh -> normals.addAll(mesh.getNormals())));
this.materials.forEach(m -> m.getMeshes().forEach(mesh -> normals.addAll(mesh.getFaceNormals())));
return normals;
}

Expand All @@ -123,4 +122,12 @@ public void close(){
this.entities.clear();
LOGGER.trace("Closed model {}", this.id);
}

public boolean usesNonDefaultTexture() {
var nonDefaultTexture = false;
for (var material : this.materials) {
nonDefaultTexture |= material.hasNonDefaultTexture();
}
return nonDefaultTexture;
}
}
Loading

0 comments on commit 443c1ec

Please sign in to comment.