First pass at shadows
This commit is contained in:
parent
79cad3a473
commit
152aac2492
@ -0,0 +1,9 @@
|
||||
package electrosphere.entity.collision;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author amaterasu
|
||||
*/
|
||||
public class CollisionAnimation {
|
||||
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package electrosphere.entity.collision;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author amaterasu
|
||||
*/
|
||||
public class CollisionSphere {
|
||||
|
||||
}
|
||||
@ -168,6 +168,4 @@ public class MovementTree {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -86,8 +86,12 @@ public class Globals {
|
||||
//Array that holds all models that need to be shoved to the gpu
|
||||
public static Matrix4f viewMatrix = new Matrix4f();
|
||||
public static Matrix4f projectionMatrix;
|
||||
public static Matrix4f lightDepthMatrix = new Matrix4f();
|
||||
|
||||
|
||||
public static int shadowMapTextureLoc = 0;
|
||||
public static int depthMapShaderProgramLoc = 0;
|
||||
|
||||
|
||||
public static Texture textureDiffuseDefault;
|
||||
public static Texture textureSpecularDefault;
|
||||
|
||||
@ -164,6 +164,24 @@ public class Main {
|
||||
// Entity unitCube = EntityUtils.spawnDrawableEntity(unitCubeModelPath);
|
||||
// EntityUtils.getEntityPosition(unitCube).set(playerStartRealX - 0.5f,10,playerStartRealY - 0.5f);
|
||||
|
||||
// String goundPlaneModelPath = "Models/groundplanemassiveuv.fbx";
|
||||
// Entity groundPlane = EntityUtils.spawnDrawableEntity(goundPlaneModelPath);
|
||||
// EntityUtils.getEntityPosition(groundPlane).set(10f,2f,10f);
|
||||
// EntityUtils.getEntityRotation(groundPlane).rotateAxis((float)Math.PI/2, new Vector3f(1,0,0));
|
||||
// EntityUtils.getEntityScale(groundPlane).set(5);
|
||||
|
||||
String unitsphereModelPath = "Models/unitsphere.fbx";
|
||||
Entity unitsphere = EntityUtils.spawnDrawableEntity(unitsphereModelPath);
|
||||
EntityUtils.getEntityPosition(unitsphere).set(10f,2f,10f);
|
||||
EntityUtils.getEntityScale(unitsphere).set(1);
|
||||
|
||||
for(int i = 0; i < 10; i++){
|
||||
Random rand = new Random();
|
||||
Entity creature = CreatureUtils.spawnBasicCreature(0, 0.01f, 0.01f);
|
||||
EntityUtils.getEntityPosition(creature).set(rand.nextFloat() * 10, rand.nextFloat() * 10, rand.nextFloat() * 10);
|
||||
EntityUtils.getEntityScale(creature).set(0.01f);
|
||||
}
|
||||
|
||||
RenderUtils.recaptureScreen();
|
||||
|
||||
///
|
||||
|
||||
@ -75,4 +75,19 @@ public class Actor {
|
||||
}
|
||||
}
|
||||
|
||||
public void drawForDepthBuffer(){
|
||||
Model model = Globals.assetManager.fetchModel(modelPath);
|
||||
if(model != null){
|
||||
if(animation != null){
|
||||
model.playAnimation(animation);
|
||||
model.incrementTime(0.001);
|
||||
model.incrementTime(animationTime);
|
||||
if(model.currentAnimation == null){
|
||||
playingAnimation = false;
|
||||
}
|
||||
}
|
||||
model.drawForDepthBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -464,9 +464,15 @@ public class Mesh {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
glBindVertexArray(vertexArrayObject);
|
||||
|
||||
|
||||
glActiveTexture(GL_TEXTURE3);
|
||||
glBindTexture(GL_TEXTURE_2D, Globals.shadowMapTextureLoc);
|
||||
glUniform1i(glGetUniformLocation(shader.shaderProgram, "shadowMap"), 3);
|
||||
|
||||
|
||||
|
||||
//
|
||||
@ -497,6 +503,7 @@ public class Mesh {
|
||||
glUniformMatrix4fv(shader.shaderVertexViewLoc, false, Globals.viewMatrix.get(new float[16]));
|
||||
glUniformMatrix4fv(shader.shaderVertexProjectionLoc, false, Globals.projectionMatrix.get(new float[16]));
|
||||
glUniform3fv(shader.shaderVertexViewPosLoc, CameraEntityUtils.getCameraEye(Globals.playerCamera).get(BufferUtils.createFloatBuffer(3)));
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader.shaderProgram, "lightSpaceMatrix"), false, Globals.lightDepthMatrix.get(new float[16]));
|
||||
|
||||
//
|
||||
//
|
||||
@ -567,4 +574,50 @@ public class Mesh {
|
||||
public void set_material(Material input){
|
||||
this.material = input;
|
||||
}
|
||||
|
||||
|
||||
public void drawForDepthBuffer(){
|
||||
/*
|
||||
|
||||
!!!!!!!!!!!!!!!!!THERE IS NO SHADER PROGRAM HERE!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
The shader program is set in the main render function to make things easier
|
||||
|
||||
*/
|
||||
|
||||
|
||||
glBindVertexArray(vertexArrayObject);
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
//Handle bones
|
||||
//
|
||||
if(bones != null && !bones.isEmpty()){
|
||||
glUniform1i(glGetUniformLocation(Globals.depthMapShaderProgramLoc, "numBones"), bones.size());
|
||||
glUniform1i(glGetUniformLocation(Globals.depthMapShaderProgramLoc, "hasBones"), 1);
|
||||
Iterator<String> boneIterator = bone_id_list.iterator();
|
||||
float bufferarray[] = new float[16];
|
||||
int incrementer = 0;
|
||||
while (boneIterator.hasNext()){
|
||||
Bone currentBone = parent.boneMap.get(boneIterator.next());
|
||||
Matrix4f currentMat = new Matrix4f(currentBone.final_transform);
|
||||
currentMat.get(bufferarray);
|
||||
String currentUniform = "bones[" + incrementer + "]";
|
||||
GL20.glUniformMatrix4fv(glGetUniformLocation(Globals.depthMapShaderProgramLoc, currentUniform), false, bufferarray);
|
||||
incrementer++;
|
||||
}
|
||||
} else {
|
||||
glUniform1i(glGetUniformLocation(Globals.depthMapShaderProgramLoc, "hasBones"), 0);
|
||||
}
|
||||
|
||||
//buffer model/view/proj matrices
|
||||
glUniformMatrix4fv(glGetUniformLocation(Globals.depthMapShaderProgramLoc, "model"), false, parent.modelMatrix.get(new float[16]));
|
||||
glUniformMatrix4fv(glGetUniformLocation(Globals.depthMapShaderProgramLoc, "lightSpaceMatrix"), false, Globals.lightDepthMatrix.get(new float[16]));
|
||||
|
||||
|
||||
GL11.glDrawElements(GL_TRIANGLES, elementCount, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -335,4 +335,16 @@ public class Model {
|
||||
update_node_transform(current_node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void drawForDepthBuffer(){
|
||||
if(node_map != null && !node_map.isEmpty()){
|
||||
update_node_transform(root_anim_node);
|
||||
}
|
||||
Iterator<Mesh> mesh_Iterator = meshes.iterator();
|
||||
while(mesh_Iterator.hasNext()){
|
||||
Mesh currentMesh = mesh_Iterator.next();
|
||||
currentMesh.drawForDepthBuffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,11 +43,14 @@ import static org.lwjgl.glfw.GLFW.glfwTerminate;
|
||||
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
|
||||
import org.lwjgl.opengl.GL;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import static org.lwjgl.opengl.GL11.GL_BACK;
|
||||
import static org.lwjgl.opengl.GL11.GL_BLEND;
|
||||
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
|
||||
import static org.lwjgl.opengl.GL11.GL_CULL_FACE;
|
||||
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
|
||||
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
|
||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||
import static org.lwjgl.opengl.GL11.GL_FRONT;
|
||||
import static org.lwjgl.opengl.GL11.GL_INT;
|
||||
import static org.lwjgl.opengl.GL11.GL_LEQUAL;
|
||||
import static org.lwjgl.opengl.GL11.GL_LESS;
|
||||
@ -62,11 +65,15 @@ import static org.lwjgl.opengl.GL11.glBindTexture;
|
||||
import static org.lwjgl.opengl.GL11.glBlendFunc;
|
||||
import static org.lwjgl.opengl.GL11.glClear;
|
||||
import static org.lwjgl.opengl.GL11.glClearColor;
|
||||
import static org.lwjgl.opengl.GL11.glCullFace;
|
||||
import static org.lwjgl.opengl.GL11.glDisable;
|
||||
import static org.lwjgl.opengl.GL11.glDrawArrays;
|
||||
import static org.lwjgl.opengl.GL11.glEnable;
|
||||
import static org.lwjgl.opengl.GL11.glViewport;
|
||||
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
|
||||
import static org.lwjgl.opengl.GL13.GL_TEXTURE1;
|
||||
import static org.lwjgl.opengl.GL13.GL_TEXTURE2;
|
||||
import static org.lwjgl.opengl.GL13.GL_TEXTURE3;
|
||||
import static org.lwjgl.opengl.GL13.glActiveTexture;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
|
||||
@ -112,10 +119,13 @@ public class RenderUtils {
|
||||
|
||||
public static final int GL_DEFAULT_FRAMEBUFFER = 0;
|
||||
public static final int GL_DEFAULT_RENDERBUFFER = 0;
|
||||
static Framebuffer fbo;
|
||||
static Renderbuffer rbo;
|
||||
static Framebuffer screenFramebuffer;
|
||||
static Renderbuffer screenRenderbuffer;
|
||||
static int screenTextureVAO;
|
||||
static int screenTextureShaders;
|
||||
static ShaderProgram screenTextureShaders;
|
||||
|
||||
static ShaderProgram lightDepthShaderProgram;
|
||||
static Framebuffer lightDepthBuffer;
|
||||
|
||||
static int createScreenTextureVAO(){
|
||||
int rVal = glGenVertexArrays();
|
||||
@ -219,14 +229,20 @@ public class RenderUtils {
|
||||
|
||||
//init screen rendering quadrant
|
||||
screenTextureVAO = createScreenTextureVAO();
|
||||
initScreenTextureShaderProgram();
|
||||
// initScreenTextureShaderProgram();
|
||||
screenTextureShaders = ShaderProgram.loadSpecificShader("/Shaders/screentexture/simple1/simple1.vs", "/Shaders/screentexture/simple1/simple1.fs");
|
||||
// screenTextureShaders = ShaderProgram.loadSpecificShader("/Shaders/screentexture/drawDepthBuffer/drawDepthBuffer.vs", "/Shaders/screentexture/drawDepthBuffer/drawDepthBuffer.fs");
|
||||
|
||||
//generate framebuffers
|
||||
fbo = FramebufferUtils.generateScreensizeTextureFramebuffer();
|
||||
screenFramebuffer = FramebufferUtils.generateScreensizeTextureFramebuffer();
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, GL_DEFAULT_FRAMEBUFFER);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, GL_DEFAULT_RENDERBUFFER);
|
||||
|
||||
|
||||
lightDepthShaderProgram = ShaderProgram.loadSpecificShader("/Shaders/lightDepth/lightDepth.vs", "/Shaders/lightDepth/lightDepth.fs");
|
||||
Globals.depthMapShaderProgramLoc = lightDepthShaderProgram.shaderProgram;
|
||||
lightDepthBuffer = FramebufferUtils.generateDepthBuffer();
|
||||
Globals.shadowMapTextureLoc = lightDepthBuffer.getTexture();
|
||||
// glEnable(GL_CULL_FACE); // enabled for shadow mapping
|
||||
|
||||
|
||||
//
|
||||
@ -492,8 +508,73 @@ public class RenderUtils {
|
||||
|
||||
public static void drawScreen(){
|
||||
|
||||
|
||||
//
|
||||
//first pass: generate depth map
|
||||
//
|
||||
//set the viewport to shadow map size
|
||||
glViewport(0, 0, 4096, 4096);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
lightDepthBuffer.bind();
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
//set matrices for light render
|
||||
float near_plane = 0.001f, far_plane = 100.5f;
|
||||
Matrix4f lightProjection = new Matrix4f().setOrtho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);//glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);
|
||||
Matrix4f lightView = new Matrix4f().lookAt(
|
||||
new Vector3f(-20.0f, 40.0f, -10.0f),
|
||||
new Vector3f( 0.0f, 0.0f, 0.0f),
|
||||
new Vector3f( 0.0f, 1.0f, 0.0f)
|
||||
);
|
||||
Globals.lightDepthMatrix = new Matrix4f(lightProjection).mul(lightView);
|
||||
// Globals.lightDepthMatrix = new Matrix4f(Globals.projectionMatrix).mul(Globals.viewMatrix);
|
||||
|
||||
glUseProgram(lightDepthShaderProgram.shaderProgram);
|
||||
glUniformMatrix4fv(glGetUniformLocation(lightDepthShaderProgram.shaderProgram, "lightSpaceMatrix"), false, Globals.lightDepthMatrix.get(new float[16]));
|
||||
|
||||
// glCullFace(GL_FRONT);
|
||||
|
||||
//
|
||||
// D R A W A L L E N T I T I E S
|
||||
//
|
||||
Matrix4f modelTransformMatrix = new Matrix4f();
|
||||
for(Entity currentEntity : Globals.entityManager.getDrawable()){
|
||||
//fetch actor
|
||||
Actor currentActor = EntityUtils.getEntityActor(currentEntity);
|
||||
//increment animations
|
||||
if(currentActor.getCurrentAnimation() != null){
|
||||
currentActor.incrementAnimationTime(deltaTime * 500);
|
||||
}
|
||||
//calculate and apply model transform
|
||||
modelTransformMatrix.identity();
|
||||
modelTransformMatrix.translate(new Vector3f(EntityUtils.getEntityPosition(currentEntity)).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
|
||||
modelTransformMatrix.rotate(EntityUtils.getEntityRotation(currentEntity));
|
||||
modelTransformMatrix.scale(EntityUtils.getEntityScale(currentEntity));
|
||||
currentActor.applyModelMatrix(modelTransformMatrix);
|
||||
//draw
|
||||
currentActor.drawForDepthBuffer();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//bind default framebuffer
|
||||
glBindFramebuffer(GL_FRAMEBUFFER,0);
|
||||
//reset texture
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
//reset the viewport to screen size
|
||||
glViewport(0, 0, 1920, 1080);
|
||||
//resume culling backface
|
||||
// glCullFace(GL_BACK);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//bind screen fbo
|
||||
fbo.bind();
|
||||
screenFramebuffer.bind();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
///
|
||||
@ -507,14 +588,15 @@ public class RenderUtils {
|
||||
//
|
||||
// D R A W A L L E N T I T I E S
|
||||
//
|
||||
Matrix4f modelTransformMatrix = new Matrix4f();
|
||||
modelTransformMatrix = new Matrix4f();
|
||||
for(Entity currentEntity : Globals.entityManager.getDrawable()){
|
||||
//fetch actor
|
||||
Actor currentActor = EntityUtils.getEntityActor(currentEntity);
|
||||
//increment animations
|
||||
if(currentActor.getCurrentAnimation() != null){
|
||||
currentActor.incrementAnimationTime(deltaTime * 500);
|
||||
}
|
||||
//this is incremented in the shadow calculations
|
||||
// if(currentActor.getCurrentAnimation() != null){
|
||||
// currentActor.incrementAnimationTime(deltaTime * 500);
|
||||
// }
|
||||
//calculate and apply model transform
|
||||
modelTransformMatrix.identity();
|
||||
modelTransformMatrix.translate(new Vector3f(EntityUtils.getEntityPosition(currentEntity)).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
|
||||
@ -558,13 +640,18 @@ public class RenderUtils {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glActiveTexture(GL_TEXTURE3);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
|
||||
//render full screen quad
|
||||
glUseProgram(screenTextureShaders);
|
||||
glUseProgram(screenTextureShaders.shaderProgram);
|
||||
glBindVertexArray(screenTextureVAO);
|
||||
glBindTexture(GL_TEXTURE_2D, fbo.getTexture());
|
||||
glBindTexture(GL_TEXTURE_2D, screenFramebuffer.getTexture());
|
||||
// glBindTexture(GL_TEXTURE_2D, lightDepthBuffer.getTexture());
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
||||
|
||||
@ -576,51 +663,4 @@ public class RenderUtils {
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
|
||||
static void initScreenTextureShaderProgram(){
|
||||
int vertexShader;
|
||||
int fragmentShader;
|
||||
//Creates a new shader object and assigns its 'pointer' to the integer "vertexShader"
|
||||
vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
//This alerts openGL to the presence of a vertex shader and points the shader at its source
|
||||
glShaderSource(vertexShader, Utilities.readBakedResourceToString(Main.class.getResourceAsStream("/Shaders/screentexture/simple1/simple1.vs")));
|
||||
//Compiles the source for the vertex shader object
|
||||
glCompileShader(vertexShader);
|
||||
//The following tests if the vertex shader compiles successfully
|
||||
int success;
|
||||
success = glGetShaderi(vertexShader, GL_COMPILE_STATUS);
|
||||
if (success != GL_TRUE) {
|
||||
System.out.println("Vertex Shader failed to compile!");
|
||||
throw new RuntimeException(GL20.glGetShaderInfoLog(vertexShader));
|
||||
}
|
||||
//Creates and opengl object for a fragment shader and assigns its 'pointer' to the integer fragmentShader
|
||||
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
//This points the opengl shadder object to its proper source
|
||||
glShaderSource(fragmentShader, Utilities.readBakedResourceToString(Main.class.getResourceAsStream("/Shaders/screentexture/simple1/simple1.fs")));
|
||||
//This compiles the shader object
|
||||
glCompileShader(fragmentShader);
|
||||
//This tests for the success of the compile attempt
|
||||
success = glGetShaderi(fragmentShader, GL_COMPILE_STATUS);
|
||||
if (success != GL_TRUE) {
|
||||
System.out.println("Fragment Shader failed to compile!");
|
||||
throw new RuntimeException(GL20.glGetShaderInfoLog(fragmentShader));
|
||||
}
|
||||
//This creates a shader program opengl object and assigns its 'pointer' to the integer shaderProgram
|
||||
screenTextureShaders = glCreateProgram();
|
||||
//This attaches the vertex and fragment shaders to the program
|
||||
glAttachShader(screenTextureShaders, vertexShader);
|
||||
glAttachShader(screenTextureShaders, fragmentShader);
|
||||
//This links the program to the GPU (I think its to the GPU anyway)
|
||||
glLinkProgram(screenTextureShaders);
|
||||
//Tests for the success of the shader program creation
|
||||
success = glGetProgrami(screenTextureShaders, GL_LINK_STATUS);
|
||||
if (success != GL_TRUE) {
|
||||
throw new RuntimeException(glGetProgramInfoLog(screenTextureShaders));
|
||||
}
|
||||
|
||||
//Deletes the individual shader objects to free up memory
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,17 +1,30 @@
|
||||
package electrosphere.renderer.framebuffer;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.GL_DEPTH_COMPONENT;
|
||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||
import static org.lwjgl.opengl.GL11.GL_LINEAR;
|
||||
import static org.lwjgl.opengl.GL11.GL_NEAREST;
|
||||
import static org.lwjgl.opengl.GL11.GL_NONE;
|
||||
import static org.lwjgl.opengl.GL11.GL_REPEAT;
|
||||
import static org.lwjgl.opengl.GL11.GL_RGB;
|
||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
|
||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_BORDER_COLOR;
|
||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
|
||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
|
||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_S;
|
||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_T;
|
||||
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
|
||||
import static org.lwjgl.opengl.GL11.glBindTexture;
|
||||
import static org.lwjgl.opengl.GL11.glDrawBuffer;
|
||||
import static org.lwjgl.opengl.GL11.glGenTextures;
|
||||
import static org.lwjgl.opengl.GL11.glReadBuffer;
|
||||
import static org.lwjgl.opengl.GL11.glTexImage2D;
|
||||
import static org.lwjgl.opengl.GL11.glTexParameterfv;
|
||||
import static org.lwjgl.opengl.GL11.glTexParameteri;
|
||||
import static org.lwjgl.opengl.GL13.GL_CLAMP_TO_BORDER;
|
||||
import static org.lwjgl.opengl.GL30.GL_COLOR_ATTACHMENT0;
|
||||
import static org.lwjgl.opengl.GL30.GL_DEPTH24_STENCIL8;
|
||||
import static org.lwjgl.opengl.GL30.GL_DEPTH_ATTACHMENT;
|
||||
import static org.lwjgl.opengl.GL30.GL_DEPTH_STENCIL_ATTACHMENT;
|
||||
import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER;
|
||||
import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER_COMPLETE;
|
||||
@ -63,4 +76,42 @@ public class FramebufferUtils {
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 1920, 1080);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
static int depthMapWidth = 4096;
|
||||
static int depthMapHeight = 4096;
|
||||
|
||||
public static Framebuffer generateDepthBuffer(){
|
||||
Framebuffer buffer = new Framebuffer();
|
||||
buffer.bind();
|
||||
|
||||
|
||||
//texture
|
||||
int texture = glGenTextures();
|
||||
glBindTexture(GL_TEXTURE_2D,texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapWidth, depthMapHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT , NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
||||
float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
||||
buffer.setTexture(texture);
|
||||
|
||||
|
||||
|
||||
//bind texture to fbo
|
||||
int mipMapLevel = 0;
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture, mipMapLevel);
|
||||
glDrawBuffer(GL_NONE);
|
||||
glReadBuffer(GL_NONE);
|
||||
|
||||
|
||||
|
||||
//check make sure compiled
|
||||
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
|
||||
System.out.println("Framebuffer is not complete!");
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
src/main/resources/Models/unitsphere.fbx
Normal file
BIN
src/main/resources/Models/unitsphere.fbx
Normal file
Binary file not shown.
@ -49,6 +49,7 @@ struct SpotLight {
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoord;
|
||||
in vec4 FragPosLightSpace;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform DirLight dirLight;
|
||||
@ -57,14 +58,19 @@ uniform SpotLight spotLight;
|
||||
uniform Material material;
|
||||
|
||||
//texture stuff
|
||||
uniform sampler2D ourTexture;
|
||||
// uniform sampler2D ourTexture;
|
||||
uniform int hasTransparency;
|
||||
// uniform sampler2D specularTexture;
|
||||
|
||||
//light depth map
|
||||
uniform sampler2D shadowMap;
|
||||
|
||||
|
||||
// function prototypes
|
||||
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
|
||||
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
|
||||
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
|
||||
float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal);
|
||||
|
||||
void main(){
|
||||
if(hasTransparency == 1){
|
||||
@ -75,11 +81,14 @@ void main(){
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
|
||||
|
||||
|
||||
vec3 result = CalcDirLight(dirLight, norm, viewDir);
|
||||
//for(int i = 0; i < NR_POINT_LIGHTS; i++){
|
||||
// result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
|
||||
//}
|
||||
//result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
|
||||
|
||||
FragColor = vec4(result, texture(material.diffuse, TexCoord).a);//texture(ourTexture, TexCoord);//vec4(result, 1.0);
|
||||
}
|
||||
|
||||
@ -93,10 +102,14 @@ vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir){
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
// combine results
|
||||
vec3 texColor = texture(material.diffuse, TexCoord).rgb;
|
||||
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoord).rgb);
|
||||
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoord).rgb);
|
||||
vec3 ambient = light.ambient;
|
||||
vec3 diffuse = light.diffuse * diff;
|
||||
//vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord).rgb);
|
||||
return (ambient + diffuse);// + specular);
|
||||
|
||||
|
||||
float shadow = ShadowCalculation(FragPosLightSpace, lightDir, normal);
|
||||
|
||||
return ( ambient + (1.0-shadow) * diffuse ) * texColor;// + specular);
|
||||
}
|
||||
|
||||
|
||||
@ -145,4 +158,34 @@ vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
||||
diffuse *= attenuation * intensity;
|
||||
specular *= attenuation * intensity;
|
||||
return (ambient + diffuse + specular);
|
||||
}
|
||||
|
||||
|
||||
float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal){
|
||||
|
||||
// perform perspective divide
|
||||
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
|
||||
|
||||
//transform to NDC
|
||||
projCoords = projCoords * 0.5 + 0.5;
|
||||
|
||||
//get closest depth from light's POV
|
||||
float closestDepth = texture(shadowMap, projCoords.xy).r;
|
||||
|
||||
//get depth of current fragment
|
||||
float currentDepth = projCoords.z;
|
||||
|
||||
//calculate bias
|
||||
float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
|
||||
|
||||
//calculate shadow value
|
||||
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
|
||||
|
||||
if(projCoords.z > 1.0){
|
||||
shadow = 0.0;
|
||||
}
|
||||
|
||||
// shadow = currentDepth;
|
||||
|
||||
return shadow;
|
||||
}
|
||||
@ -16,6 +16,7 @@ uniform mat4 transform;
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform mat4 lightSpaceMatrix;
|
||||
|
||||
//bone related variables
|
||||
const int MAX_WEIGHTS = 4;
|
||||
@ -30,27 +31,41 @@ uniform int numBones;
|
||||
out vec3 Normal;
|
||||
out vec3 FragPos;
|
||||
out vec2 TexCoord;
|
||||
out vec4 FragPosLightSpace;
|
||||
|
||||
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
void main() {
|
||||
|
||||
|
||||
|
||||
//calculate bone transform
|
||||
mat4 BoneTransform = (bones[int(aIndex[0])] * aWeights[0]);
|
||||
BoneTransform = BoneTransform + (bones[int(aIndex[1])] * aWeights[1]);
|
||||
BoneTransform = BoneTransform + (bones[int(aIndex[2])] * aWeights[2]);
|
||||
BoneTransform = BoneTransform + (bones[int(aIndex[3])] * aWeights[3]);
|
||||
|
||||
|
||||
//apply bone transform to position vectors
|
||||
vec4 FinalVertex = BoneTransform * vec4(aPos, 1.0);
|
||||
vec4 FinalNormal = BoneTransform * vec4(aNormal, 1.0);
|
||||
|
||||
|
||||
//make sure the W component is 1.0
|
||||
FinalVertex = vec4(FinalVertex.xyz, 1.0);
|
||||
FinalNormal = vec4(FinalNormal.xyz, 1.0);
|
||||
|
||||
|
||||
//push frag, normal, and texture positions to fragment shader
|
||||
FragPos = vec3(model * FinalVertex);
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
TexCoord = aTex;
|
||||
|
||||
//shadow map stuff
|
||||
FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0);
|
||||
|
||||
|
||||
//set final position with opengl space
|
||||
gl_Position = projection * view * model * FinalVertex;
|
||||
}
|
||||
@ -14,6 +14,7 @@ uniform mat4 transform;
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform mat4 lightSpaceMatrix;
|
||||
|
||||
|
||||
|
||||
@ -21,19 +22,27 @@ uniform mat4 projection;
|
||||
out vec3 Normal;
|
||||
out vec3 FragPos;
|
||||
out vec2 TexCoord;
|
||||
out vec4 FragPosLightSpace;
|
||||
|
||||
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
void main() {
|
||||
//normalize posiiton and normal
|
||||
vec4 FinalVertex = vec4(aPos, 1.0);
|
||||
vec4 FinalNormal = vec4(aNormal, 1.0);
|
||||
|
||||
|
||||
//push frag, normal, and texture positions to fragment shader
|
||||
FragPos = vec3(model * FinalVertex);
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
TexCoord = aTex;
|
||||
|
||||
|
||||
//shadow map stuff
|
||||
FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0);
|
||||
|
||||
|
||||
//set final position with opengl space
|
||||
gl_Position = projection * view * model * FinalVertex;
|
||||
}
|
||||
|
||||
8
src/main/resources/Shaders/lightDepth/lightDepth.fs
Normal file
8
src/main/resources/Shaders/lightDepth/lightDepth.fs
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
#version 330 core
|
||||
|
||||
void main()
|
||||
{
|
||||
// gl_FragDepth = gl_FragCoord.z;
|
||||
}
|
||||
33
src/main/resources/Shaders/lightDepth/lightDepth.vs
Normal file
33
src/main/resources/Shaders/lightDepth/lightDepth.vs
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 2) in vec4 aWeights;
|
||||
layout (location = 3) in vec4 aIndex;
|
||||
|
||||
uniform mat4 lightSpaceMatrix;
|
||||
uniform mat4 model;
|
||||
|
||||
//bone related variables
|
||||
const int MAX_WEIGHTS = 4;
|
||||
const int MAX_BONES = 100;
|
||||
uniform mat4 bones[MAX_BONES];
|
||||
uniform int numBones;
|
||||
uniform int hasBones;
|
||||
|
||||
void main()
|
||||
{
|
||||
mat4 BoneTransform = (bones[int(aIndex[0])] * aWeights[0]);
|
||||
BoneTransform = BoneTransform + (bones[int(aIndex[1])] * aWeights[1]);
|
||||
BoneTransform = BoneTransform + (bones[int(aIndex[2])] * aWeights[2]);
|
||||
BoneTransform = BoneTransform + (bones[int(aIndex[3])] * aWeights[3]);
|
||||
//apply bone transform to position vectors
|
||||
vec4 FinalVertex = BoneTransform * vec4(aPos, 1.0);
|
||||
//normalize w component
|
||||
FinalVertex = vec4(FinalVertex.xyz, 1.0);
|
||||
|
||||
if(hasBones == 0){
|
||||
FinalVertex = vec4(aPos, 1.0);
|
||||
}
|
||||
|
||||
gl_Position = lightSpaceMatrix * model * FinalVertex;
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform sampler2D depthMap;
|
||||
|
||||
void main()
|
||||
{
|
||||
float depthValue = texture(depthMap, TexCoords).r;
|
||||
FragColor = vec4(vec3(depthValue), 1.0);
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec2 aPos;
|
||||
layout (location = 1) in vec2 aTexCoords;
|
||||
|
||||
out vec2 TexCoords;
|
||||
|
||||
void main(){
|
||||
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
|
||||
TexCoords = aTexCoords;
|
||||
}
|
||||
@ -17,6 +17,12 @@
|
||||
"Textures/Wheat1stretch.png",
|
||||
"Textures/Wheat1stretch.png"
|
||||
]
|
||||
},
|
||||
"Models/unitsphere.fbx": {
|
||||
"Sphere": [
|
||||
"Textures/transparent_blue.png",
|
||||
"Textures/transparent_blue.png"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
src/main/resources/Textures/transparent_blue.png
Normal file
BIN
src/main/resources/Textures/transparent_blue.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 195 B |
Loading…
Reference in New Issue
Block a user