From b162bdd8d225364c74878aab5319745383631d7b Mon Sep 17 00:00:00 2001 From: satellite Date: Sun, 19 Sep 2021 18:16:25 -0400 Subject: [PATCH] Sync laptop and desktop --- .gitignore | 4 + assets/Config/settings.json | 4 +- assets/Data/creatures.json | 4 +- assets/Shaders/terrain/terrain.fs | 237 ++++++++++++++ assets/Shaders/terrain/terrain.fs.bak | 220 +++++++++++++ assets/Shaders/terrain/terrain.vs | 52 +++ .../types/collision/CollisionObjUtils.java | 3 + .../entity/types/hitbox/HitboxUtils.java | 2 + .../server/culture/religion/Religion.java | 5 +- .../game/server/culture/religion/Story.java | 15 + src/main/java/electrosphere/main/Globals.java | 3 + src/main/java/electrosphere/main/Main.java | 2 +- .../java/electrosphere/renderer/Mesh.java | 30 +- .../electrosphere/renderer/ModelUtils.java | 306 +++++++++--------- .../renderer/RenderingEngine.java | 36 ++- 15 files changed, 766 insertions(+), 157 deletions(-) create mode 100644 assets/Shaders/terrain/terrain.fs create mode 100644 assets/Shaders/terrain/terrain.fs.bak create mode 100644 assets/Shaders/terrain/terrain.vs create mode 100644 src/main/java/electrosphere/game/server/culture/religion/Story.java diff --git a/.gitignore b/.gitignore index 17f6922b..cdd04f35 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,7 @@ /Telephone-*.jar /hs_err_pid* + +.classpath +.project +.settings \ No newline at end of file diff --git a/assets/Config/settings.json b/assets/Config/settings.json index 017b0bbe..655d4b85 100644 --- a/assets/Config/settings.json +++ b/assets/Config/settings.json @@ -8,8 +8,8 @@ "graphicsFOV" : 90.0, - "graphicsPerformanceLODChunkRadius" : 2, - "graphicsPerformanceEnableVSync" : true, + "graphicsPerformanceLODChunkRadius" : 3, + "graphicsPerformanceEnableVSync" : false, "graphicsPerformanceDrawShadows" : true, "graphicsDebugDrawCollisionSpheres" : false, diff --git a/assets/Data/creatures.json b/assets/Data/creatures.json index d069a356..d0bb6dd8 100644 --- a/assets/Data/creatures.json +++ b/assets/Data/creatures.json @@ -88,8 +88,8 @@ "movementSystems" : [ { "type" : "GROUND", - "acceleration" : 0.001, - "maxVelocity" : 0.025 + "acceleration" : 0.05, + "maxVelocity" : 0.25 } ], "physicsObject" : { diff --git a/assets/Shaders/terrain/terrain.fs b/assets/Shaders/terrain/terrain.fs new file mode 100644 index 00000000..d5fc1f5e --- /dev/null +++ b/assets/Shaders/terrain/terrain.fs @@ -0,0 +1,237 @@ + + +#version 410 core +out vec4 FragColor; + +struct Material { + sampler2D diffuse; + sampler2D specular; + float shininess; +}; + +struct DirLight { + vec3 direction; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +struct PointLight { + vec3 position; + + float constant; + float linear; + float quadratic; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +struct SpotLight { + vec3 position; + vec3 direction; + float cutOff; + float outerCutOff; + + float constant; + float linear; + float quadratic; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +#define NR_POINT_LIGHTS 10 + +in vec3 FragPos; +in vec3 Normal; +in vec2 TexCoord; +in vec4 FragPosLightSpace; +in vec4 groundTexIndices; + +uniform vec3 viewPos; +uniform DirLight dirLight; +uniform PointLight pointLights[NR_POINT_LIGHTS]; +uniform SpotLight spotLight; +uniform Material material; + +//texture stuff +// uniform sampler2D ourTexture; +uniform int hasTransparency; +// uniform sampler2D specularTexture; + +//light depth map +uniform sampler2D shadowMap; + +//textures +// +// Goal is to have a texture for the current chunk and one for each nearnby chunk +// +// +// +uniform sampler2D groundTextures1; +uniform sampler2D groundTextures2; +uniform sampler2D groundTextures3; +uniform sampler2D groundTextures4; +uniform sampler2D groundTextures5; +uniform sampler2D groundTextures[10]; + +// function prototypes +vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir, vec3 texColor); +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); +vec3 blendedTextureColor(vec2 texPos, vec4 tex1, vec4 tex2, vec4 tex3, vec4 tex4); + +void main(){ + if(hasTransparency == 1){ + if(texture(material.diffuse, TexCoord).a < 0.1){ + discard; + } + } + vec3 norm = normalize(Normal); + vec3 viewDir = normalize(viewPos - FragPos); + + + + // sampler2DArray text = groundTextures; + // sampler2D test = groundTextures1; + vec4 texColor1 = texture(groundTextures[int(groundTexIndices.x * 2)], TexCoord); + vec4 texColor2 = texture(groundTextures[int(groundTexIndices.y * 2)], TexCoord); + vec4 texColor3 = texture(groundTextures[int(groundTexIndices.z * 2)], TexCoord); + vec4 texColor4 = texture(groundTextures[int(groundTexIndices.w * 2)], TexCoord); + vec3 finalTexColor = blendedTextureColor(TexCoord, texColor1, texColor2, texColor3, texColor4); + // vec4 tex2 = texture(groundTextures[int(groundTexIndices.y)], TexCoord); + // vec4 tex3 = texture2D(groundTextures[int(groundTexIndex.z * 2)], texPos); + // vec4 tex4 = texture2D(groundTextures[int(groundTexIndex.w * 2)], texPos); + + //get texture color + // vec3 texColor = vec3(0,0,1);//blendedTextureColor(texPos, groundTexIndices); + + + vec3 result = CalcDirLight(dirLight, norm, viewDir, finalTexColor); + //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); +} + +// calculates the color when using a directional light. +vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir, vec3 texColor){ + vec3 lightDir = normalize(-light.direction); + // diffuse shading + float diff = max(dot(normal, lightDir), 0.0); + // specular shading + vec3 reflectDir = reflect(-lightDir, normal); + 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 diffuse = light.diffuse * diff; + //vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord).rgb); + + + float shadow = ShadowCalculation(FragPosLightSpace, lightDir, normal); + + return ( ambient + (1.0-shadow) * diffuse ) * texColor;// + specular); +} + + +// calculates the color when using a point light. +vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir){ + vec3 lightDir = normalize(light.position - fragPos); + // diffuse shading + float diff = max(dot(normal, lightDir), 0.0); + // specular shading + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + // attenuation + float distance = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); + // combine results + vec3 ambient = light.ambient * vec4(texture(material.diffuse, TexCoord)).xyz; + vec3 diffuse = light.diffuse * diff * vec4(texture(material.diffuse, TexCoord)).xyz; + vec3 specular = light.specular * spec * vec4(texture(material.specular, TexCoord)).xyz; + ambient *= attenuation; + diffuse *= attenuation; + specular *= attenuation; + return (ambient + diffuse + specular); +} + +// calculates the color when using a spot light. +vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir) +{ + vec3 lightDir = normalize(light.position - fragPos); + // diffuse shading + float diff = max(dot(normal, lightDir), 0.0); + // specular shading + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + // attenuation + float distance = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); + // spotlight intensity + float theta = dot(lightDir, normalize(-light.direction)); + float epsilon = light.cutOff - light.outerCutOff; + float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0); + // combine results + vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoord)); + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoord)); + vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord)); + ambient *= attenuation * intensity; + 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; +} + + +vec3 blendedTextureColor(vec2 texPos, vec4 tex1, vec4 tex2, vec4 tex3, vec4 tex4){ + // int texIndex1 = int(groundTexIndex.x * 2); + // int texIndex2 = int(groundTexIndex.y * 2); + // int texIndex3 = int(groundTexIndex.z * 2); + // int texIndex4 = int(groundTexIndex.w * 2); + // vec4 tex1 = texture2D(groundTextures[int(groundTexIndex.x * 2)], texPos); + // vec4 tex2 = texture2D(groundTextures[int(groundTexIndex.y * 2)], texPos); + // vec4 tex3 = texture2D(groundTextures[int(groundTexIndex.z * 2)], texPos); + // vec4 tex4 = texture2D(groundTextures[int(groundTexIndex.w * 2)], texPos); + // float percentTex1 = (texPos.x - 1) * (texPos.y - 1); + // float percentTex2 = (texPos.x - 0) * (texPos.y - 1); + // float percentTex3 = (texPos.x - 1) * (texPos.y - 0); + // float percentTex4 = (texPos.x - 0) * (texPos.y - 0); + return vec3(0,0,0);//mix(mix(tex1,tex2,texPos.x),mix(tex3,tex4,texPos.x),texPos.y).rgb; +} \ No newline at end of file diff --git a/assets/Shaders/terrain/terrain.fs.bak b/assets/Shaders/terrain/terrain.fs.bak new file mode 100644 index 00000000..e1173905 --- /dev/null +++ b/assets/Shaders/terrain/terrain.fs.bak @@ -0,0 +1,220 @@ + + +#version 330 core +out vec4 FragColor; + +struct Material { + sampler2D diffuse; + sampler2D specular; + float shininess; +}; + +struct DirLight { + vec3 direction; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +struct PointLight { + vec3 position; + + float constant; + float linear; + float quadratic; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +struct SpotLight { + vec3 position; + vec3 direction; + float cutOff; + float outerCutOff; + + float constant; + float linear; + float quadratic; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +#define NR_POINT_LIGHTS 10 + +in vec3 FragPos; +in vec3 Normal; +in vec2 TexCoord; +in vec4 FragPosLightSpace; +in ivec4 groundTexIndices; + +uniform vec3 viewPos; +uniform DirLight dirLight; +uniform PointLight pointLights[NR_POINT_LIGHTS]; +uniform SpotLight spotLight; +uniform Material material; + +//texture stuff +// uniform sampler2D ourTexture; +uniform int hasTransparency; +// uniform sampler2D specularTexture; + +//light depth map +uniform sampler2D shadowMap; + +//textures +// +// Goal is to have a texture for the current chunk and one for each nearnby chunk +// +// +// +// uniform sampler2D groundTextures[10]; + + +// 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); +vec3 blendedTextureColor(vec2 texPos, ivec4 groundTexIndex); + +void main(){ + if(hasTransparency == 1){ + if(texture(material.diffuse, TexCoord).a < 0.1){ + discard; + } + } + vec3 norm = normalize(Normal); + vec3 viewDir = normalize(viewPos - FragPos); + + //get texture color + vec3 texColor = vec3(0,0,1);//blendedTextureColor(texPos, groundTexIndices); + + + 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); + // FragColor = vec4(result, 1);//texture(ourTexture, TexCoord);//vec4(result, 1.0); +} + +// calculates the color when using a directional light. +vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir, vec3 texColor){ + // //get texture color + // vec3 texColor = vec3(1,0,0);//blendedTextureColor(texPos, groundTexIndices); + //get light direction + vec3 lightDir = normalize(-light.direction); + // diffuse shading + float diff = max(dot(normal, lightDir), 0.0); + // specular shading + vec3 reflectDir = reflect(-lightDir, normal); + 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 diffuse = light.diffuse * diff; + //vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord).rgb); + + + float shadow = ShadowCalculation(FragPosLightSpace, lightDir, normal); + + return ( ambient + (1.0-shadow) * diffuse ) * texColor;// + specular); +} + + +// calculates the color when using a point light. +vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir){ + vec3 lightDir = normalize(light.position - fragPos); + // diffuse shading + float diff = max(dot(normal, lightDir), 0.0); + // specular shading + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + // attenuation + float distance = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); + // combine results + vec3 ambient = light.ambient * vec4(texture(material.diffuse, TexCoord)).xyz; + vec3 diffuse = light.diffuse * diff * vec4(texture(material.diffuse, TexCoord)).xyz; + vec3 specular = light.specular * spec * vec4(texture(material.specular, TexCoord)).xyz; + ambient *= attenuation; + diffuse *= attenuation; + specular *= attenuation; + return (ambient + diffuse + specular); +} + +// calculates the color when using a spot light. +vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir) +{ + vec3 lightDir = normalize(light.position - fragPos); + // diffuse shading + float diff = max(dot(normal, lightDir), 0.0); + // specular shading + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + // attenuation + float distance = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); + // spotlight intensity + float theta = dot(lightDir, normalize(-light.direction)); + float epsilon = light.cutOff - light.outerCutOff; + float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0); + // combine results + vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoord)); + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoord)); + vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord)); + ambient *= attenuation * intensity; + 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; +} + + +// vec3 blendedTextureColor(vec2 texPos, ivec4 groundTexIndex){ +// vec4 tex1 = texture2D(groundTextures[groundTexIndex.x * 2], texPos); +// vec4 tex2 = texture2D(groundTextures[groundTexIndex.y * 2], texPos); +// vec4 tex3 = texture2D(groundTextures[groundTexIndex.z * 2], texPos); +// vec4 tex4 = texture2D(groundTextures[groundTexIndex.w * 2], texPos); +// // float percentTex1 = (texPos.x - 1) * (texPos.y - 1); +// // float percentTex2 = (texPos.x - 0) * (texPos.y - 1); +// // float percentTex3 = (texPos.x - 1) * (texPos.y - 0); +// // float percentTex4 = (texPos.x - 0) * (texPos.y - 0); +// return mix(mix(tex1,tex2,texPos.x),mix(tex3,tex4,texPos.x),texPos.y).rgb; +// } \ No newline at end of file diff --git a/assets/Shaders/terrain/terrain.vs b/assets/Shaders/terrain/terrain.vs new file mode 100644 index 00000000..d07fbb70 --- /dev/null +++ b/assets/Shaders/terrain/terrain.vs @@ -0,0 +1,52 @@ +//Vertex Shader +#version 410 core + + + +//input buffers +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; +layout (location = 4) in vec2 aTex; +layout (location = 5) in ivec4 aGroundTexIndices; + + +//coordinate space transformation matrices +uniform mat4 transform; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; +uniform mat4 lightSpaceMatrix; + + + +//output buffers +out vec3 Normal; +out vec3 FragPos; +out vec2 TexCoord; +out vec4 FragPosLightSpace; +out vec4 groundTexIndices; + + + + +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; + + //push texure indices to fragment shader + groundTexIndices = groundTexIndices; + + //shadow map stuff + FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0); + + + //set final position with opengl space + gl_Position = projection * view * model * FinalVertex; +} diff --git a/src/main/java/electrosphere/entity/types/collision/CollisionObjUtils.java b/src/main/java/electrosphere/entity/types/collision/CollisionObjUtils.java index eab00f55..9badb5b0 100644 --- a/src/main/java/electrosphere/entity/types/collision/CollisionObjUtils.java +++ b/src/main/java/electrosphere/entity/types/collision/CollisionObjUtils.java @@ -38,6 +38,7 @@ public class CollisionObjUtils { rVal.putData(EntityDataStrings.DATA_STRING_SCALE, scale); rVal.putData(EntityDataStrings.COLLISION_ENTITY_COLLISION_OBJECT, planeObject); rVal.putData(EntityDataStrings.COLLISION_ENTITY_COLLIDABLE, collidable); + rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true); Globals.entityManager.registerEntity(rVal); return rVal; @@ -63,6 +64,7 @@ public class CollisionObjUtils { rVal.putData(EntityDataStrings.DATA_STRING_SCALE, scale); rVal.putData(EntityDataStrings.COLLISION_ENTITY_COLLISION_OBJECT, cubeObject); rVal.putData(EntityDataStrings.COLLISION_ENTITY_COLLIDABLE, collidable); + rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true); Globals.entityManager.registerEntity(rVal); return rVal; @@ -87,6 +89,7 @@ public class CollisionObjUtils { rVal.putData(EntityDataStrings.DATA_STRING_SCALE, scale); rVal.putData(EntityDataStrings.COLLISION_ENTITY_COLLISION_OBJECT, cubeObject); rVal.putData(EntityDataStrings.COLLISION_ENTITY_COLLIDABLE, collidable); + rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true); Globals.entityManager.registerEntity(rVal); return rVal; diff --git a/src/main/java/electrosphere/entity/types/hitbox/HitboxUtils.java b/src/main/java/electrosphere/entity/types/hitbox/HitboxUtils.java index b73a17d6..eff836c5 100644 --- a/src/main/java/electrosphere/entity/types/hitbox/HitboxUtils.java +++ b/src/main/java/electrosphere/entity/types/hitbox/HitboxUtils.java @@ -32,6 +32,7 @@ public class HitboxUtils { rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent); rVal.putData(EntityDataStrings.HITBOX_DATA, data); rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0)); + rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true); Globals.hitboxManager.registerHitbox(rVal); return rVal; } @@ -47,6 +48,7 @@ public class HitboxUtils { rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent); rVal.putData(EntityDataStrings.HITBOX_DATA, data); rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0)); + rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true); Globals.hitboxManager.registerHitbox(rVal); return rVal; } diff --git a/src/main/java/electrosphere/game/server/culture/religion/Religion.java b/src/main/java/electrosphere/game/server/culture/religion/Religion.java index 1ee0094d..d80e1829 100644 --- a/src/main/java/electrosphere/game/server/culture/religion/Religion.java +++ b/src/main/java/electrosphere/game/server/culture/religion/Religion.java @@ -1,5 +1,8 @@ package electrosphere.game.server.culture.religion; +import electrosphere.game.server.character.diety.Diety; +import java.util.List; + public class Religion { - + List dietyList; } diff --git a/src/main/java/electrosphere/game/server/culture/religion/Story.java b/src/main/java/electrosphere/game/server/culture/religion/Story.java new file mode 100644 index 00000000..dab53bf7 --- /dev/null +++ b/src/main/java/electrosphere/game/server/culture/religion/Story.java @@ -0,0 +1,15 @@ +package electrosphere.game.server.culture.religion; + +/** + * + * @author satellite + */ +public class Story { + static enum StoryType { + FABLE, //a story including animals/things that explains a moral + MYTH, //an explanation for some fact + PARABLE, //a story strictly starring humans that explains a moral + } + + StoryType type; +} diff --git a/src/main/java/electrosphere/main/Globals.java b/src/main/java/electrosphere/main/Globals.java index 902c24ca..9cd8465c 100644 --- a/src/main/java/electrosphere/main/Globals.java +++ b/src/main/java/electrosphere/main/Globals.java @@ -172,6 +172,7 @@ public class Globals { public static TextureMap textureMapDefault; public static ShaderProgram defaultMeshShader; + public static ShaderProgram terrainShaderProgram; public static Menu currentMenu; @@ -320,6 +321,8 @@ public class Globals { loadingBox = WidgetUtils.createVerticallyAlignedTextBox(520, 100, 50, 7, 1, "LOADING", true); //init default shaderProgram defaultMeshShader = ShaderProgram.smart_assemble_shader(false,true); + //init terrain shader program + terrainShaderProgram = ShaderProgram.loadSpecificShader("/Shaders/terrain/terrain.vs", "/Shaders/terrain/terrain.fs"); //init skybox assetManager.registerModelToSpecificString(RenderUtils.createSkyboxModel(null), AssetDataStrings.ASSET_STRING_SKYBOX_BASIC); //init models diff --git a/src/main/java/electrosphere/main/Main.java b/src/main/java/electrosphere/main/Main.java index 0cd96b05..85a75f2f 100644 --- a/src/main/java/electrosphere/main/Main.java +++ b/src/main/java/electrosphere/main/Main.java @@ -115,7 +115,7 @@ public class Main { Globals.initGlobals(); //debug: create terrain/world viewer -// TerrainViewer.runViewer(); + TerrainViewer.runViewer(); //create the drawing context Globals.renderingEngine = new RenderingEngine(); diff --git a/src/main/java/electrosphere/renderer/Mesh.java b/src/main/java/electrosphere/renderer/Mesh.java index c5297f00..0ed60c05 100644 --- a/src/main/java/electrosphere/renderer/Mesh.java +++ b/src/main/java/electrosphere/renderer/Mesh.java @@ -4,11 +4,13 @@ import electrosphere.entity.CameraEntityUtils; import electrosphere.main.Globals; import electrosphere.main.Main; import electrosphere.renderer.light.LightBuffer; +import electrosphere.renderer.texture.Texture; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import org.joml.Matrix4f; import org.joml.Vector3f; import org.lwjgl.BufferUtils; @@ -71,6 +73,10 @@ public class Mesh { boolean hasBones = true; public boolean hasTextureCoords = true; + public List textureList; + public boolean useTextureList; + public String textureListArrayUniformName; + public float vertexMinX; public float vertexMaxX; public float vertexMinY; @@ -572,7 +578,7 @@ public class Mesh { glEnableVertexAttribArray(4); } - public void bufferCustomAttribArray(FloatBuffer buffer, int bufferDimension, int attribIndex){ + public void bufferCustomFloatAttribArray(FloatBuffer buffer, int bufferDimension, int attribIndex){ int customBuffer = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, customBuffer); GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); @@ -580,6 +586,20 @@ public class Mesh { glEnableVertexAttribArray(attribIndex); } + public void bufferCustomIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){ + int customBuffer = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, customBuffer); + GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); + glVertexAttribPointer(attribIndex, bufferDimension, GL_INT, false, 0, 0); + glEnableVertexAttribArray(attribIndex); + } + + public void setTextureList(List textureList, String uniformName){ + this.textureList = textureList; + useTextureList = true; + textureListArrayUniformName = uniformName; + } + public void set_material(Material input){ this.material = input; } @@ -724,7 +744,7 @@ public class Mesh { } } - if(useMaterial){ + if(useMaterial && !useTextureList){ if(material == null){ Globals.materialDefault.apply_material(0,1); GL20.glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "hasTransparency"), 0); @@ -738,6 +758,12 @@ public class Mesh { } } + if(useTextureList){ + for(Texture texture : textureList){ + + } + } + glBindVertexArray(vertexArrayObject); diff --git a/src/main/java/electrosphere/renderer/ModelUtils.java b/src/main/java/electrosphere/renderer/ModelUtils.java index ead44b4b..3d96c0ac 100644 --- a/src/main/java/electrosphere/renderer/ModelUtils.java +++ b/src/main/java/electrosphere/renderer/ModelUtils.java @@ -16,153 +16,153 @@ import static org.lwjgl.opengl.GL30.glGenVertexArrays; * @author satellite */ public class ModelUtils { - public static Model createTerrainModel(float[][] heightfield, int stride){ - Model rVal = new Model(); - rVal.meshes = new ArrayList(); - Mesh m = new Mesh(); - int width = heightfield.length; - int height = heightfield[0].length; - - int actualWidth = (int)Math.ceil(1.0 * width / stride); - int actualHeight = (int)Math.ceil(1.0 * height / stride); - -// System.out.println(actualWidth + " " + actualHeight); - -// System.out.println((actualWidth - 1) * (actualHeight - 1)); - - FloatBuffer vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 3); - FloatBuffer normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 3); - IntBuffer faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); - FloatBuffer texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 2); - for(int x = 0; x < width; x = x + stride){ - for(int y = 0; y < height; y = y + stride){ - //deal with vertex - vertices.put(x); - vertices.put(heightfield[x][y]); - vertices.put(y); - //deal with normal - if(x / stride < actualWidth - 1){ - if(y / stride < actualHeight - 1){ - float hL; - if(x > 0){ - hL = heightfield[x-1][y]; - } else { - hL = heightfield[x][y]; - } - float hR = heightfield[x+1][y]; - float hD = heightfield[x][y+1]; - float hU; - if(y > 0){ - hU = heightfield[x][y-1]; - } else { - hU = heightfield[x][y]; - } - Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); - Normal.normalize(); - normals.put(Normal.x); - normals.put(Normal.y); - normals.put(Normal.z); - } else { - float hL; - if(x > 0){ - hL = heightfield[x-1][y]; - } else { - hL = heightfield[x][y]; - } - float hR = heightfield[x+1][y]; - float hD = heightfield[x][y]; - float hU = heightfield[x][y-1]; - Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); - Normal.normalize(); - normals.put(Normal.x); - normals.put(Normal.y); - normals.put(Normal.z); - } - } else { - if(y / stride < actualHeight - 1){ - float hL = heightfield[x-1][y]; - float hR = heightfield[x][y]; - float hD = heightfield[x][y+1]; - float hU; - if(y > 0){ - hU = heightfield[x][y-1]; - } else { - hU = heightfield[x][y]; - } - Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); - Normal.normalize(); - normals.put(Normal.x); - normals.put(Normal.y); - normals.put(Normal.z); - } else { - float hL = heightfield[x-1][y]; - float hR = heightfield[x][y]; - float hD = heightfield[x][y]; - float hU = heightfield[x][y-1]; - Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); - Normal.normalize(); - normals.put(Normal.x); - normals.put(Normal.y); - normals.put(Normal.z); - } - } - //deal with texture coordinates - if(x / stride % 2 == 0){ - if(y / stride % 2 == 0){ - texture_coords.put(0); - texture_coords.put(0); - } else { - texture_coords.put(0); - texture_coords.put(1); - } - } else { - if(y / stride % 2 == 0){ - texture_coords.put(1); - texture_coords.put(0); - } else { - texture_coords.put(1); - texture_coords.put(1); - } - } - //deal with faces - if(x / stride < actualWidth - 1 && y / stride < actualHeight - 1){ - faces.put((x / stride + 0) * actualHeight + (y / stride + 0)); - faces.put((x / stride + 0) * actualHeight + (y / stride + 1)); - faces.put((x / stride + 1) * actualHeight + (y / stride + 0)); - faces.put((x / stride + 1) * actualHeight + (y / stride + 0)); - faces.put((x / stride + 0) * actualHeight + (y / stride + 1)); - faces.put((x / stride + 1) * actualHeight + (y / stride + 1)); - } - } - } - vertices.flip(); - normals.flip(); - faces.flip(); - texture_coords.flip(); - - m.vertexArrayObject = glGenVertexArrays(); - glBindVertexArray(m.vertexArrayObject); - //buffer vertices - m.buffer_vertices(vertices, 3); - //buffer normals - m.buffer_normals(normals, 3); - //buffer faces - m.buffer_faces(faces); - //buffer texture coords - m.buffer_texture_coords(texture_coords, 2); - m.shader = ShaderProgram.smart_assemble_shader(false,true); - glBindVertexArray(0); - m.parent = rVal; - - Material groundMat = new Material(); - Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); - groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); - groundMat.set_specular("/Textures/Ground/Dirt1.png"); - m.set_material(groundMat); - - rVal.meshes.add(m); - return rVal; - } +// public static Model createTerrainModel(float[][] heightfield, int stride){ +// Model rVal = new Model(); +// rVal.meshes = new ArrayList(); +// Mesh m = new Mesh(); +// int width = heightfield.length; +// int height = heightfield[0].length; +// +// int actualWidth = (int)Math.ceil(1.0 * width / stride); +// int actualHeight = (int)Math.ceil(1.0 * height / stride); +// +//// System.out.println(actualWidth + " " + actualHeight); +// +//// System.out.println((actualWidth - 1) * (actualHeight - 1)); +// +// FloatBuffer vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 3); +// FloatBuffer normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 3); +// IntBuffer faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); +// FloatBuffer texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 2); +// for(int x = 0; x < width; x = x + stride){ +// for(int y = 0; y < height; y = y + stride){ +// //deal with vertex +// vertices.put(x); +// vertices.put(heightfield[x][y]); +// vertices.put(y); +// //deal with normal +// if(x / stride < actualWidth - 1){ +// if(y / stride < actualHeight - 1){ +// float hL; +// if(x > 0){ +// hL = heightfield[x-1][y]; +// } else { +// hL = heightfield[x][y]; +// } +// float hR = heightfield[x+1][y]; +// float hD = heightfield[x][y+1]; +// float hU; +// if(y > 0){ +// hU = heightfield[x][y-1]; +// } else { +// hU = heightfield[x][y]; +// } +// Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); +// Normal.normalize(); +// normals.put(Normal.x); +// normals.put(Normal.y); +// normals.put(Normal.z); +// } else { +// float hL; +// if(x > 0){ +// hL = heightfield[x-1][y]; +// } else { +// hL = heightfield[x][y]; +// } +// float hR = heightfield[x+1][y]; +// float hD = heightfield[x][y]; +// float hU = heightfield[x][y-1]; +// Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); +// Normal.normalize(); +// normals.put(Normal.x); +// normals.put(Normal.y); +// normals.put(Normal.z); +// } +// } else { +// if(y / stride < actualHeight - 1){ +// float hL = heightfield[x-1][y]; +// float hR = heightfield[x][y]; +// float hD = heightfield[x][y+1]; +// float hU; +// if(y > 0){ +// hU = heightfield[x][y-1]; +// } else { +// hU = heightfield[x][y]; +// } +// Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); +// Normal.normalize(); +// normals.put(Normal.x); +// normals.put(Normal.y); +// normals.put(Normal.z); +// } else { +// float hL = heightfield[x-1][y]; +// float hR = heightfield[x][y]; +// float hD = heightfield[x][y]; +// float hU = heightfield[x][y-1]; +// Vector3f Normal = new Vector3f(hL - hR, 2.0f, hD - hU); +// Normal.normalize(); +// normals.put(Normal.x); +// normals.put(Normal.y); +// normals.put(Normal.z); +// } +// } +// //deal with texture coordinates +// if(x / stride % 2 == 0){ +// if(y / stride % 2 == 0){ +// texture_coords.put(0); +// texture_coords.put(0); +// } else { +// texture_coords.put(0); +// texture_coords.put(1); +// } +// } else { +// if(y / stride % 2 == 0){ +// texture_coords.put(1); +// texture_coords.put(0); +// } else { +// texture_coords.put(1); +// texture_coords.put(1); +// } +// } +// //deal with faces +// if(x / stride < actualWidth - 1 && y / stride < actualHeight - 1){ +// faces.put((x / stride + 0) * actualHeight + (y / stride + 0)); +// faces.put((x / stride + 0) * actualHeight + (y / stride + 1)); +// faces.put((x / stride + 1) * actualHeight + (y / stride + 0)); +// faces.put((x / stride + 1) * actualHeight + (y / stride + 0)); +// faces.put((x / stride + 0) * actualHeight + (y / stride + 1)); +// faces.put((x / stride + 1) * actualHeight + (y / stride + 1)); +// } +// } +// } +// vertices.flip(); +// normals.flip(); +// faces.flip(); +// texture_coords.flip(); +// +// m.vertexArrayObject = glGenVertexArrays(); +// glBindVertexArray(m.vertexArrayObject); +// //buffer vertices +// m.buffer_vertices(vertices, 3); +// //buffer normals +// m.buffer_normals(normals, 3); +// //buffer faces +// m.buffer_faces(faces); +// //buffer texture coords +// m.buffer_texture_coords(texture_coords, 2); +// m.shader = ShaderProgram.loadSpecificShader("/Shaders/terrain/terrain.vs", "/Shaders/terrain/terrain.fs"); +// glBindVertexArray(0); +// m.parent = rVal; +// +// Material groundMat = new Material(); +// Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); +// groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); +// groundMat.set_specular("/Textures/Ground/Dirt1.png"); +// m.set_material(groundMat); +// +// rVal.meshes.add(m); +// return rVal; +// } public static Model createTerrainModelPrecomputedShader(float[][] heightfield, ShaderProgram program, int stride){ Model rVal = new Model(); @@ -183,6 +183,7 @@ public class ModelUtils { FloatBuffer normals; IntBuffer faces; FloatBuffer texture_coords; + IntBuffer textureIndices; if(stride * actualWidth > width){ int drawWidth = actualWidth + 1; int drawHeight = actualHeight + 1; @@ -190,11 +191,13 @@ public class ModelUtils { normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); faces = BufferUtils.createIntBuffer((drawWidth - 1) * (drawHeight - 1) * 2 * 3); texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 8); + textureIndices = BufferUtils.createIntBuffer(drawWidth * drawHeight * 16); } else { vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 8); + textureIndices = BufferUtils.createIntBuffer(actualWidth * actualHeight * 16); } int incrementer = 0; @@ -268,6 +271,13 @@ public class ModelUtils { texture_coords.put(1); texture_coords.put(1); + for(int i = 0; i < 4 ; i++){ + textureIndices.put(0); + textureIndices.put(1); + textureIndices.put(2); + textureIndices.put(3); + } + //deal with faces if(1.0f * x / stride < actualWidth - 1 && 1.0f * y / stride < actualHeight - 1){ @@ -297,6 +307,8 @@ public class ModelUtils { m.buffer_faces(faces); //buffer texture coords m.buffer_texture_coords(texture_coords, 2); + //texture indices + m.bufferCustomIntAttribArray(textureIndices, 4, 5); m.shader = program; glBindVertexArray(0); m.parent = rVal; diff --git a/src/main/java/electrosphere/renderer/RenderingEngine.java b/src/main/java/electrosphere/renderer/RenderingEngine.java index e8e07d09..35aab284 100644 --- a/src/main/java/electrosphere/renderer/RenderingEngine.java +++ b/src/main/java/electrosphere/renderer/RenderingEngine.java @@ -17,6 +17,7 @@ import electrosphere.renderer.framebuffer.FramebufferUtils; import electrosphere.renderer.framebuffer.Renderbuffer; import electrosphere.renderer.texture.Texture; import electrosphere.renderer.ui.Widget; +import java.nio.FloatBuffer; import org.joml.Matrix4f; import org.joml.Quaternionf; import org.joml.Vector3d; @@ -39,10 +40,19 @@ import static org.lwjgl.glfw.GLFW.glfwTerminate; import static org.lwjgl.glfw.GLFW.glfwWindowHint; import org.lwjgl.glfw.GLFWWindowSizeCallbackI; import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.GL11; 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_DEPTH_BUFFER_BIT; import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; +import static org.lwjgl.opengl.GL11.GL_EXP2; +import static org.lwjgl.opengl.GL11.GL_FOG; +import static org.lwjgl.opengl.GL11.GL_FOG_COLOR; +import static org.lwjgl.opengl.GL11.GL_FOG_DENSITY; +import static org.lwjgl.opengl.GL11.GL_FOG_END; +import static org.lwjgl.opengl.GL11.GL_FOG_MODE; +import static org.lwjgl.opengl.GL11.GL_FOG_START; +import static org.lwjgl.opengl.GL11.GL_LINEAR; import static org.lwjgl.opengl.GL11.GL_NEAREST; import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA; import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA; @@ -55,6 +65,7 @@ import static org.lwjgl.opengl.GL11.glClearColor; 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.glFogf; import static org.lwjgl.opengl.GL11.glViewport; import static org.lwjgl.opengl.GL13.GL_TEXTURE0; import static org.lwjgl.opengl.GL13.GL_TEXTURE1; @@ -98,8 +109,8 @@ public class RenderingEngine { //Initializes opengl glfwInit(); //Gives hints to glfw to control how opengl will be used - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); Allows you to make the background transparent // glfwWindowHint(GLFW_OPACITY, 23); @@ -160,6 +171,27 @@ public class RenderingEngine { Globals.shadowMapTextureLoc = lightDepthBuffer.getTexture(); // glEnable(GL_CULL_FACE); // enabled for shadow mapping + // + //Fog + // + //enable fog +// glEnable(GL_FOG); +// //set the equation to use for fog +// glFogf(GL_FOG_MODE,GL_LINEAR); +//// glFogf(GL_FOG_MODE,GL_EXP2); +// //set the density of the fog +// glFogf(GL_FOG_DENSITY,1.0f); +// //these are applicable for the linear equation +// glFogf(GL_FOG_START,0.8f); +// glFogf(GL_FOG_END,1.0f); +// //fog color +// FloatBuffer fogColor = FloatBuffer.allocate(4); +// fogColor.put(1.0f); +// fogColor.put(1.0f); +// fogColor.put(1.0f); +// fogColor.put(1.0f); +// fogColor.flip(); +// GL11.glFogfv(GL_FOG_COLOR, fogColor); // // Projection and View matrix creation