OpenGL lighting

This commit is contained in:
Halbear 2026-08-29 19:42:11 +01:00
parent 5518dae1d7
commit f7127e0878
28 changed files with 873 additions and 188 deletions

View file

@ -66,7 +66,8 @@ public class OpenGLRenderer implements Renderer{
Logger.debug("OpenGL version: [{}]", glGetString(GL_VERSION));
Logger.debug("GLSL version: [{}]", glGetString(GL_SHADING_LANGUAGE_VERSION));
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
glClearColor(0.6f, 0.7f, 1.0f, 0.0f);
if(!LoggedNullCapabilities){
Logger.debug("Creating OpenGL Context On Render Thread");
LoggedNullCapabilities = true;

View file

@ -124,7 +124,7 @@ public class ConvexMesh {
int[][] e = {{0,1},{1,2},{2,3},{3,0},{4,5},{5,6},{6,7},{7,4},{0,4},{1,5},{2,6},{3,7}};
mesh.Edges.addAll(Arrays.asList(e));
if(!PrimaryRuntime.IsServer && !RenderThread.Headless) {
PrimaryRuntime.GetEngineInstance().scene().AddActor(new VisualisedCollisionActor3D("VISUALISEDACTOR" + mesh.MeshID.toString(), mesh.Position, mesh.MeshID));
// PrimaryRuntime.GetEngineInstance().scene().AddActor(new VisualisedCollisionActor3D("VISUALISEDACTOR" + mesh.MeshID.toString(), mesh.Position, mesh.MeshID));
}
return mesh;
}

View file

@ -99,6 +99,12 @@ public class GameCore implements GameLogic {
public InitData Initialise(EngineInstance engineInstance) {
Scene scene = (Scene) engineInstance.scene();
List<ModelData> models = new ArrayList<>();
ModelData SponzaData = ModelLoader.LoadModel("resources/models/Cafe/exterior.json");
List<MaterialData> SponzaMaterial = ModelLoader.LoadMaterials("resources/models/Cafe/exterior_mat.json");
ModelData SponzaData1 = ModelLoader.LoadModel("resources/models/Cafe/interior.json");
List<MaterialData> SponzaMaterial1= ModelLoader.LoadMaterials("resources/models/Cafe/interior_mat.json");
scene.AddActor(new Actor3D("Sponza1", SponzaData.ID(), new Vector3f(0,0f,-5f)).SetScale(0.1f));
scene.AddActor(new Actor3D("Sponza2", SponzaData1.ID(), new Vector3f(0,0f,-5f)).SetScale(0.1f));
MelonaData = ModelLoader.LoadModel("resources/models/cube/Cube.json");
CubeModelID = MelonaData.ID();
List<MaterialData> MelonaMat = ModelLoader.LoadMaterials("resources/models/cube/Cube_mat.json");
@ -141,6 +147,10 @@ public class GameCore implements GameLogic {
}
boolean createOfflinePlayer = !PrimaryRuntime.IsServer && !ClientSideNetworkUtils.Connected;
materials.addAll(SponzaMaterial);
materials.addAll(SponzaMaterial1);
models.add(SponzaData);
models.add(SponzaData1);
materials.addAll(MelonaMat);
materials.addAll(CollisionVisualisationMat);
materials.addAll(MelonaMaterial);
@ -169,13 +179,47 @@ public class GameCore implements GameLogic {
guiTextures.add(guiTexture);
}
scene.GetLightingManager().GetAmbientLightColour().set(1.0f, 0.9f, 0.75f);
scene.GetLightingManager().SetAmbientLightIntensity(0.6f);
SkyLight = new Light(new Vector3f(1.5f, 1.20f, 1.0f),new Vector3f(0.0f, -1.0f, 0.3f), true, 2.00f);
//scene.GetLightingManager().GetAmbientLightColour().set(1.0f, 0.9f, 0.75f);
//scene.GetLightingManager().SetAmbientLightIntensity(0.6f);
scene.GetLightingManager().GetAmbientLightColour().set(0.3f, 0.35f, 0.5f);
scene.GetLightingManager().SetAmbientLightIntensity(0.01f);
// SkyLight = new Light(new Vector3f(1.5f, 1.20f, 1.0f),new Vector3f(0.0f, -1.0f, 0.3f), true, 2.00f);
SkyLight = new Light(new Vector3f(0.25f, 1.0f, 1.5f),new Vector3f(0.0f, -1.0f, 0.0f), true, 1.70f);
SkyLight.SetType(Light.LightType.Directional);
List<ILight> lights = new ArrayList<>();
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-433, 445f, -424f).div(10.0f),false,3000.0f+ (float)(Math.random() * 100.0)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(-210, 445f, 460f).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(-966, 445f, 204f).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(-176, 445f, 1000f).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(777, 445f, 858).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(376, 445f, -2136).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,2.0f),new Vector3f(2163, 445f, 1880).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(3334, 445f, 2405).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(3509, 445f, 1825).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(3877, 445f, 3378).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(4925, 445f, 3430).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f+ (float)Math.random(),1.5f,1.0f),new Vector3f(-2083, 445f, -1826).div(10.0f),false,700.0f + (float)(Math.random() * 150.0f)));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(2076, 410, 1272).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(2446, 465, 2303).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-842, 410, -1141).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-207, 410, -1830).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(899, 460, -2015).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(1775, 460, -3448).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(1800, 460, -4154).div(10.0f),false,1000.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-1727, 410, -387).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-1198, 410, -964).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-1778, 410, -1502).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-2692, 333, -2109).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-2849, 333, -1724).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-2019, 410, -729).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-1727, 410, -389).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(1.9f + (float)Math.random(),1.5f,1.0f),new Vector3f(-1337, 410, 122).div(10.0f),false,100.0f));
lights.add(new Light(new Vector3f(0.75f,2.0f,0.75f),new Vector3f(1086, 425, -3375).div(10.0f),false,500.0f));
lights.add(SkyLight);
ILight[] lightArr = new ILight[lights.size()];
@ -188,7 +232,7 @@ public class GameCore implements GameLogic {
}
//if(PrimaryRuntime.IsServer) {
permutation.GeneratePermutationArray(5783904701859L);
GenerateChunk(new Vector3i(0, 0, 0), 16);
//GenerateChunk(new Vector3i(0, 0, 0), 16);
for(int i = 0; i < 20; i++){
SpawnNewActor(engineInstance, new Vector3f(-5 + (float)Math.random() * 10,10 + (float)Math.random() * 10, -5 + (float)Math.random() * 10), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
}
@ -293,7 +337,7 @@ public class GameCore implements GameLogic {
return player;
}
public void SpawnNewActor(EngineInstance engineInstance, Vector3f Position, Vector3f Rotation, Vector3f Direction){
public Actor3D SpawnNewActor(EngineInstance engineInstance, Vector3f Position, Vector3f Rotation, Vector3f Direction){
var scene = engineInstance.scene();
Camera camera = scene.GetCamera();
SpawnPosition.set(Position);
@ -314,6 +358,7 @@ public class GameCore implements GameLogic {
RigidPhysicsController physicsController = (RigidPhysicsController) Melona.GetPhysicsController();
//RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).LinearVelocity.set(0,0,-20f -(40f * (float)Math.random()));
scene.AddActor(Melona);
return Melona;
}
public void SpawnNewActorEditorComponent(EngineInstance engineInstance, Vector3f Position, Vector3f Rotation, Vector3f Direction){

View file

@ -12,4 +12,14 @@ public interface ILight {
public void SetColour(Vector3f Colour);
public void SetPositon(float Position);
public void SetDirectional(boolean Directional);
public Light.Attenuation GetAttenuation();
public Light.LightType GetType();
public void SetType(Light.LightType type);
public void SetAttenuation(Light.Attenuation attenuation);
public Vector3f getConeDirection();
public float getCutOff();
public float getCutOffAngle();
public void setConeDirection(float x, float y, float z);
public void setConeDirection(Vector3f coneDirection);
}

View file

@ -1,9 +1,14 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene.InputEvents;
import net.halbear.Terrain4J.EngineCore.Input.KeyboardInput;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidBody;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidPhysicsController;
import net.halbear.Terrain4J.EngineCore.Main.GameCore;
import net.halbear.Terrain4J.EngineCore.Main.PrimaryRuntime;
import net.halbear.Terrain4J.EngineCore.Main.Scene.*;
import net.halbear.Terrain4J.EngineCore.Main.Scene.Actor.Actor3D;
import net.halbear.Terrain4J.EngineCore.Main.Scene.Actor.PlayerActor3D;
import org.joml.Vector3f;
import static org.lwjgl.glfw.GLFW.*;
@ -28,8 +33,16 @@ public class DebugCameraController implements IPlayerInputEvents {
if(input.keyPressed(GLFW_KEY_SPACE)){
camera.MoveUp(MovementDist);
}
if(input.keySinglePress(GLFW_KEY_F3)){
Scene.GUI_MODE = (Scene.GUI_MODE + 1) % 2;
}
if(input.keyPressed(GLFW_KEY_LEFT_CONTROL)){
camera.Sprint(true);
} else camera.Sprint(false);
if(input.keySinglePress(GLFW_KEY_F)){
for(int i = 0; i < 20; i++){
Actor3D Melona = GameCore.SpawnNewActor(PrimaryRuntime.GetEngineInstance(), new Vector3f(-5 + (float)Math.random() * 10,10 + (float)Math.random() * 10, -5 + (float)Math.random() * 10), camera.GetRotation(), camera.GetDirection());
}
}
}
}

View file

@ -4,18 +4,90 @@ import org.joml.Vector3f;
public class Light implements ILight{
public LightType GetType() {
return type;
}
public void SetType(LightType type) {
this.type = type;
}
public enum LightType{
Spot,
Point,
Directional,
Ambient
}
private Attenuation attenuation;
private final Vector3f Colour;
private final Vector3f Position;
private boolean Directional;
private float Intensity;
private boolean Directional = false;
private float Intensity = 0;
private LightType type = LightType.Point;
private Vector3f coneDirection = new Vector3f();
private float cutOff = 0;
private float cutOffAngle = 0;
public Light(Vector3f Colour, Vector3f Position, boolean Directional, float Intensity){
attenuation = new Attenuation(0, 0, 1);
this.Colour = Colour;
this.Position = Position;
this.Directional = Directional;
this.Intensity = Intensity;
}
public Light(Vector3f Colour, Vector3f Position, LightType type, float Intensity){
attenuation = new Attenuation(0, 0, 1);
this.Colour = Colour;
this.Position = Position;
this.Directional = type == LightType.Directional;
this.type = type;
this.Intensity = Intensity;
}
public Light(Vector3f Colour, Vector3f Position, Vector3f coneDirection, float cutOffAngle, float Intensity){
attenuation = new Attenuation(0, 0, 1);
this.Colour = Colour;
this.Position = Position;
this.Directional = false;
this.type = LightType.Spot;
this.Intensity = Intensity;
}
public Vector3f getConeDirection() {
return coneDirection;
}
public float getCutOff() {
return cutOff;
}
public float getCutOffAngle() {
return cutOffAngle;
}
public void setConeDirection(float x, float y, float z) {
coneDirection.set(x, y, z);
}
public void setConeDirection(Vector3f coneDirection) {
this.coneDirection = coneDirection;
}
public final void setCutOffAngle(float cutOffAngle) {
this.cutOffAngle = cutOffAngle;
cutOff = (float) Math.cos(Math.toRadians(cutOffAngle));
}
public Attenuation GetAttenuation() {
return attenuation;
}
public void SetAttenuation(Attenuation attenuation) {
this.attenuation = attenuation;
}
@Override
public Vector3f GetColour() {
return Colour;
@ -55,4 +127,41 @@ public class Light implements ILight{
public void SetDirectional(boolean Directional) {
this.Directional = Directional;
}
public static class Attenuation {
private float constant;
private float exponent;
private float linear;
public Attenuation(float constant, float linear, float exponent) {
this.constant = constant;
this.linear = linear;
this.exponent = exponent;
}
public float getConstant() {
return constant;
}
public float getExponent() {
return exponent;
}
public float getLinear() {
return linear;
}
public void setConstant(float constant) {
this.constant = constant;
}
public void setExponent(float exponent) {
this.exponent = exponent;
}
public void setLinear(float linear) {
this.linear = linear;
}
}
}

View file

@ -8,10 +8,15 @@ import java.util.List;
public class GLMaterial {
private final List<GLMesh> meshList;
private String texturePath;
String NormalTexturePath;
boolean hasMetalRoughnessMap;
boolean hasNormalMap;
String MetalRoughnessMap;
Vector4f DiffuseColour;
float Roughness;
float Metallic;
public static final Vector4f DEFAULT_COLOUR = new Vector4f(0.0f, 0.0f, 0.0f, 1.0f);
private Vector4f DiffuseColour;
public GLMaterial() {
meshList = new ArrayList<>();
DiffuseColour = DEFAULT_COLOUR;
@ -39,4 +44,44 @@ public class GLMaterial {
public void SetDiffuseColour(Vector4f diffuseColour) {
this.DiffuseColour = diffuseColour;
}
public String GetNormalTexturePath() {
return NormalTexturePath;
}
public void SetNormalTexturePath(String texturePath) {
this.NormalTexturePath = texturePath;
}
public boolean HasNormalMap() {
return hasNormalMap;
}
public void HasNormalMap(boolean Set){this.hasNormalMap = Set;}
public String GetRoughnessTexturePath() {
return MetalRoughnessMap;
}
public void SetRoughnessTexturePath(String texturePath) {
this.MetalRoughnessMap = texturePath;
}
public boolean HasRoughnessMap() {
return hasMetalRoughnessMap;
}
public void HasRoughnessMap(boolean Set){this.hasMetalRoughnessMap = Set;}
public float GetRoughness() {
return Roughness;
}
public void SetRoughness(float roughness) {
this.Roughness = roughness;
}
public float GetMetallic() {
return Metallic;
}
public void SetMetallic(float metallic) {
this.Metallic = metallic;
}
}

View file

@ -1,151 +0,0 @@
//package net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.GlModels;
//
//import net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.Images.Texture;
//import net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.Images.TextureCache;
//import net.halbear.Terrain4J.EngineCore.RenderingAPI.Universal.Interfaces.ITextureCache;
//import org.joml.Vector4f;
//import org.lwjgl.PointerBuffer;
//import org.lwjgl.assimp.*;
//import org.lwjgl.system.MemoryStack;
//
//import java.io.File;
//import java.nio.IntBuffer;
//import java.util.ArrayList;
//import java.util.List;
//
//import static org.lwjgl.assimp.Assimp.*;
//
//public class GLModelLoader {
// private GLModelLoader() {
// }
//
//
//
// public static GLModel loadModel(String ModelId, String ModelPath, TextureCache textureCache) {
// return loadModel(ModelId, ModelPath, textureCache, aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices |
// aiProcess_Triangulate | aiProcess_FixInfacingNormals | aiProcess_CalcTangentSpace | aiProcess_LimitBoneWeights |
// aiProcess_PreTransformVertices);
//
// }
//
// public static GLModel loadModel(String ModelId, String ModelPath, TextureCache textureCache, int Flags) {
// File file = new File(ModelPath);
// if (!file.exists()) {
// throw new RuntimeException("Model path does not exist: " + ModelPath);
// }
// String ModelDir = file.getParent();
//
// AIScene aiScene = aiImportFile(ModelPath, Flags);
// if (aiScene == null) {
// throw new RuntimeException("Error loading model: " + ModelPath);
// }
//
// int MaterialCount = aiScene.mNumMaterials();
// List<GLMaterial> materialList = new ArrayList<>();
// for (int i = 0; i < MaterialCount; i++) {
// AIMaterial aiMaterial = AIMaterial.create(aiScene.mMaterials().get(i));
// materialList.add(ProcessMaterial(aiMaterial, ModelDir, textureCache));
// }
//
// int MeshCount = aiScene.mNumMeshes();
// PointerBuffer aiMeshes = aiScene.mMeshes();
// GLMaterial defaultMaterial = new GLMaterial();
// for (int i = 0; i < MeshCount; i++) {
// AIMesh aiMesh = AIMesh.create(aiMeshes.get(i));
// GLMesh mesh = ProcessMesh(aiMesh);
// int materialIdx = aiMesh.mMaterialIndex();
// GLMaterial material;
// if (materialIdx >= 0 && materialIdx < materialList.size()) {
// material = materialList.get(materialIdx);
// } else {
// material = defaultMaterial;
// }
// material.GetMeshList().add(mesh);
// }
//
// if (!defaultMaterial.GetMeshList().isEmpty()) {
// materialList.add(defaultMaterial);
// }
//
// return new GLModel(ModelId, materialList);
// }
//
// private static GLMaterial ProcessMaterial(AIMaterial aiMaterial, String modelDir, TextureCache textureCache) {
// GLMaterial Material = new GLMaterial();
// try (MemoryStack stack = MemoryStack.stackPush()) {
// AIColor4D color = AIColor4D.create();
//
// int result = aiGetMaterialColor(aiMaterial, AI_MATKEY_COLOR_DIFFUSE, aiTextureType_NONE, 0,
// color);
// if (result == aiReturn_SUCCESS) {
// Material.SetDiffuseColour(new Vector4f(color.r(), color.g(), color.b(), color.a()));
// }
//
// AIString aiTexturePath = AIString.calloc(stack);
// aiGetMaterialTexture(aiMaterial, aiTextureType_DIFFUSE, 0, aiTexturePath, (IntBuffer) null,
// null, null, null, null, null);
// String TexturePath = aiTexturePath.dataString();
// if (TexturePath != null && TexturePath.length() > 0) {
// Material.SetTexturePath(modelDir + File.separator + new File(TexturePath).getName());
// textureCache.CreateTexture(Material.GetTexturePath());
// Material.SetDiffuseColour(GLMaterial.DEFAULT_COLOUR);
// }
//
// return Material;
// }
// }
// private static GLMesh ProcessMesh(AIMesh aiMesh) {
// float[] vertices = ProcessVertices(aiMesh);
// float[] textCoords = ProcessTextCoords(aiMesh);
// int[] indices = ProcessIndices(aiMesh);
//
// if (textCoords.length == 0) {
// int numElements = (vertices.length / 3) * 2;
// textCoords = new float[numElements];
// }
//
// return new GLMesh(vertices, textCoords, indices);
// }
//
// private static int[] ProcessIndices(AIMesh aiMesh) {
// List<Integer> indices = new ArrayList<>();
// int FaceCount = aiMesh.mNumFaces();
// AIFace.Buffer aiFaces = aiMesh.mFaces();
// for (int i = 0; i < FaceCount; i++) {
// AIFace aiFace = aiFaces.get(i);
// IntBuffer buffer = aiFace.mIndices();
// while (buffer.remaining() > 0) {
// indices.add(buffer.get());
// }
// }
// return indices.stream().mapToInt(Integer::intValue).toArray();
// }
//
// private static float[] ProcessTextCoords(AIMesh aiMesh) {
// AIVector3D.Buffer TextureCoordBuffer = aiMesh.mTextureCoords(0);
// if (TextureCoordBuffer == null) {
// return new float[]{};
// }
// float[] data = new float[TextureCoordBuffer.remaining() * 2];
// int index = 0;
// while (TextureCoordBuffer.remaining() > 0) {
// AIVector3D TextureCoord = TextureCoordBuffer.get();
// data[index++] = TextureCoord.x();
// data[index++] = 1 - TextureCoord.y();
// }
// return data;
// }
//
// private static float[] ProcessVertices(AIMesh aiMesh) {
// AIVector3D.Buffer VertexBuffer = aiMesh.mVertices();
// float[] data = new float[VertexBuffer.remaining() * 3];
// int index = 0;
// while (VertexBuffer.remaining() > 0) {
// AIVector3D TextureCoord = VertexBuffer.get();
// data[index++] = TextureCoord.x();
// data[index++] = TextureCoord.y();
// data[index++] = TextureCoord.z();
// }
// return data;
// }
//}

View file

@ -1,9 +1,12 @@
package net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.Rendering;
import imgui.extension.imguizmo.flag.Mode;
import net.halbear.Terrain4J.EngineCore.Logic.Rendering.VulkanContext;
import net.halbear.Terrain4J.EngineCore.Main.Scene.Actor.Actor3D;
import net.halbear.Terrain4J.EngineCore.Main.Scene.Actor.Actor3DElement;
import net.halbear.Terrain4J.EngineCore.Main.Scene.ILight;
import net.halbear.Terrain4J.EngineCore.Main.Scene.IScene;
import net.halbear.Terrain4J.EngineCore.Main.Scene.Light;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.GlModels.GLMaterial;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.GlModels.GLMesh;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.GlModels.GLModel;
@ -14,24 +17,31 @@ import net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.Shader.ShaderProgram
import net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.Shader.UniformsMap;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Universal.Interfaces.ITextureCache;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Rendering.Pipeline.Images.ITexture;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Util.VulkanBuffer;
import net.halbear.Terrain4J.EngineCore.RenderingAPI.Vulkan.Util.VulkanUtils;
import net.halbear.Terrain4J.EngineCore.ServerNetworking.ClientSideNetworkUtils;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.joml.Vector4f;
import org.lwjgl.system.MemoryUtil;
import org.tinylog.Logger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.glActiveTexture;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER;
import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
public class ForwardRenderer implements SceneRender{
private static final int MAX_LIGHTS = 32;
private final ShaderProgram shaderProgram;
private UniformsMap uniformsMap;
@ -48,8 +58,32 @@ public class ForwardRenderer implements SceneRender{
uniformsMap.CreateUniform("projectionMatrix");
uniformsMap.CreateUniform("modelMatrix");
uniformsMap.CreateUniform("textureSampler");
uniformsMap.CreateUniform("normalSampler");
uniformsMap.CreateUniform("roughnessSampler");
uniformsMap.CreateUniform("viewMatrix");
uniformsMap.CreateUniform("sceneInfo.camPos");
uniformsMap.CreateUniform("sceneInfo.ambientLightIntensity");
uniformsMap.CreateUniform("sceneInfo.ambientLightColor");
uniformsMap.CreateUniform("sceneInfo.LightCount");
uniformsMap.CreateUniform("material.diffuse");
uniformsMap.CreateUniform("material.hasTexture");
uniformsMap.CreateUniform("material.hasNormalMap");
uniformsMap.CreateUniform("material.hasRoughMap");
uniformsMap.CreateUniform("material.roughnessFactor");
uniformsMap.CreateUniform("material.metallicFactor");
for (int i = 0; i < MAX_LIGHTS; i++) {
String name = "lights[" + i + "]";
uniformsMap.CreateUniform(name + ".position");
uniformsMap.CreateUniform(name + ".lightType");
uniformsMap.CreateUniform(name + ".intensity");
uniformsMap.CreateUniform(name + ".color");
uniformsMap.CreateUniform(name + ".conedir");
uniformsMap.CreateUniform(name + ".cutoff");
uniformsMap.CreateUniform(name + ".attenuation.constant");
uniformsMap.CreateUniform(name + ".attenuation.linear");
uniformsMap.CreateUniform(name + ".attenuation.exponent");
}
}
@Override
@ -60,9 +94,17 @@ public class ForwardRenderer implements SceneRender{
@Override
public void Render(IScene scene, ModelsCache modelsCache, TextureCache textureCache) {
shaderProgram.Bind();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
uniformsMap.SetUniform("projectionMatrix", scene.GetProjection().GetProjectionMatrix());
uniformsMap.SetUniform("viewMatrix", scene.GetCamera().GetViewMatrix());
uniformsMap.SetUniform("sceneInfo.camPos", scene.GetCamera().GetPosition());
uniformsMap.SetUniform("sceneInfo.ambientLightIntensity", scene.GetLightingManager().GetAmbientLightIntensity());
uniformsMap.SetUniform("sceneInfo.ambientLightColor", scene.GetLightingManager().GetAmbientLightColour());
uniformsMap.SetUniform("textureSampler", 0);
uniformsMap.SetUniform("normalSampler", 1);
uniformsMap.SetUniform("roughnessSampler", 2);
UpdateLights(scene);
Map<String,GLModel> models = modelsCache.GetModelMap();
for (Actor3D actor : scene.GetActors()) {
if(actor instanceof Actor3DElement &&( !((Actor3DElement) actor).RenderOnTopOfPlayer && ClientSideNetworkUtils.OwnsActor(((Actor3DElement) actor).GetParentID()) || new Vector3f().set(actor.GetPosition()).sub(scene.GetCamera().GetPosition()).absolute().lengthSquared() < 4f)) continue;
@ -71,9 +113,21 @@ public class ForwardRenderer implements SceneRender{
if(model == null) continue;
model.GetMeshList().stream().forEach(mesh -> {
GLMaterial material = modelsCache.GetMaterial(mesh.MaterialID());
uniformsMap.SetUniform("material.diffuse", material.GetDiffuseColour());
uniformsMap.SetUniform("material.hasTexture", 1);
uniformsMap.SetUniform("material.hasNormalMap", material.HasNormalMap() ? 1 : 0);
uniformsMap.SetUniform("material.hasRoughMap", material.HasRoughnessMap() ? 1 : 0);
uniformsMap.SetUniform("material.roughnessFactor", material.GetRoughness());
uniformsMap.SetUniform("material.metallicFactor", material.GetMetallic());
ITexture texture = textureCache.GetTexture(material.GetTexturePath());
glActiveTexture(GL_TEXTURE0);
texture.Bind();
texture = textureCache.GetTexture(material.HasNormalMap() ? material.GetNormalTexturePath() : TextureCache.DEFAULT_TEXTURE_PATH);
glActiveTexture(GL_TEXTURE1);
texture.Bind();
texture = textureCache.GetTexture(material.HasRoughnessMap() ? material.GetRoughnessTexturePath() : TextureCache.DEFAULT_TEXTURE_PATH);
glActiveTexture(GL_TEXTURE2);
texture.Bind();
uniformsMap.SetUniform("material.diffuse", material.GetDiffuseColour());
glBindVertexArray(mesh.GetVaoID());
uniformsMap.SetUniform("modelMatrix", actor.GetModelMatrix());
@ -85,4 +139,62 @@ public class ForwardRenderer implements SceneRender{
shaderProgram.UnBind();
}
public void UpdateLights( IScene scene){
ILight[] lights = scene.GetLightingManager().GetLights();
Matrix4f viewMatrix = scene.GetCamera().GetViewMatrix();
int lightCount = Math.min(lights != null ? lights.length : 0, MAX_LIGHTS);
uniformsMap.SetUniform("sceneInfo.LightCount", lightCount);
ILight light;
for(int i = 0; i < lightCount; i++){
if (i < MAX_LIGHTS) {
light =lights[i];;
} else {
light = null;
}
String name = "lights[" + i + "]";
UpdateLight(light, name, viewMatrix);
}
}
private void UpdateLight(ILight light, String prefix, Matrix4f viewMatrix) {
Vector4f aux = new Vector4f();
Vector3f lightPosition = new Vector3f();
Vector3f color = new Vector3f();
float intensity = 0.0f;
float constant = 0.0f;
float linear = 0.0f;
float exponent = 0.0f;
int LightType = 0;
Vector3f ConeDir = new Vector3f(0,0,0);
float Cutoff = 0f;
if (light != null) {
switch(light.GetType()){
case Point -> LightType = 0;
case Directional -> LightType = 1;
case Ambient -> LightType = 2;
case Spot -> LightType = 3;
}
aux.set(light.GetPosition(), 1);
aux.mul(viewMatrix);
lightPosition.set(aux.x, aux.y, aux.z);
color.set(light.GetColour());
intensity = light.GetIntensity();
Light.Attenuation attenuation = light.GetAttenuation();
constant = attenuation.getConstant();
linear = attenuation.getLinear();
exponent = attenuation.getExponent();
ConeDir = light.getConeDirection();
Cutoff = light.getCutOff();
}
uniformsMap.SetUniform(prefix + ".position", lightPosition);
uniformsMap.SetUniform(prefix + ".lightType", LightType);
uniformsMap.SetUniform(prefix + ".intensity", intensity);
uniformsMap.SetUniform(prefix + ".color", color);
uniformsMap.SetUniform(prefix + ".conedir", ConeDir);
uniformsMap.SetUniform(prefix + ".cutoff", Cutoff);
uniformsMap.SetUniform(prefix + ".attenuation.constant", constant);
uniformsMap.SetUniform(prefix + ".attenuation.linear", linear);
uniformsMap.SetUniform(prefix + ".attenuation.exponent", exponent);
}
}

View file

@ -71,7 +71,10 @@ public class ShaderProgram {
public void LinkShaderProgram(List<Integer> ShaderModules){
glLinkProgram(ProgramID);
if(glGetProgrami(ProgramID, GL_LINK_STATUS) == GL_FALSE){
Logger.error("Failed to link shader program: [{}]", glGetProgramInfoLog(ProgramID, 1024));
String infoLog = glGetProgramInfoLog(ProgramID, 1024);
ShaderModules.forEach(ShaderID -> glDetachShader(ProgramID, ShaderID));
ShaderModules.forEach(GL30::glDeleteShader);
throw new RuntimeException("Failed to link shader program: " + infoLog);
}
ShaderModules.forEach(ShaderID -> glDetachShader(ProgramID, ShaderID));

View file

@ -2,6 +2,7 @@ package net.halbear.Terrain4J.EngineCore.RenderingAPI.OpenGL.Shader;
import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector3f;
import org.joml.Vector4f;
import org.lwjgl.system.MemoryStack;
@ -53,4 +54,12 @@ public class UniformsMap {
public void SetUniform(String uniformName, Vector2f value) {
glUniform2f(GetUniformLocation(uniformName), value.x, value.y);
}
public void SetUniform(String uniformName, float value) {
glUniform1f(GetUniformLocation(uniformName), value);
}
public void SetUniform(String uniformName, Vector3f value) {
glUniform3f(GetUniformLocation(uniformName), value.x, value.y, value.z);
}
}

View file

@ -18,7 +18,7 @@ import java.util.UUID;
import static org.lwjgl.vulkan.VK10.VK_FORMAT_R8G8B8A8_SRGB;
public class TextureCache implements ITextureCache {
public static final int MAX_TEXTURES = 128;
public static final int MAX_TEXTURES = 256;
private final IndexedLinkedHashMap<String, ITexture> TextureMap;
private final List<String> ActualTextures;