From 57c2fa1e6152e9a043952a2ae2b4cd67c7bd9fe2 Mon Sep 17 00:00:00 2001 From: austin Date: Sat, 9 Oct 2021 16:22:33 -0400 Subject: [PATCH] Terrain splatting hopefully working --- .gitignore | 4 +- assets/Shaders/terrain/terrain.fs | 56 +++++++++++++++---- assets/Shaders/terrain/terrain.vs | 6 +- .../game/client/cells/DrawCell.java | 5 +- .../game/client/cells/DrawCellManager.java | 1 + .../terrain/manager/ClientTerrainManager.java | 13 +++++ .../java/electrosphere/renderer/Mesh.java | 22 ++++++-- .../electrosphere/renderer/ModelUtils.java | 35 ++++++++---- .../renderer/RenderingEngine.java | 14 ++--- .../renderer/texture/Texture.java | 6 ++ 10 files changed, 127 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index cdd04f35..9bce6baf 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,6 @@ .classpath .project -.settings \ No newline at end of file +.settings + +.vscode \ No newline at end of file diff --git a/assets/Shaders/terrain/terrain.fs b/assets/Shaders/terrain/terrain.fs index 64853af6..85aae85e 100644 --- a/assets/Shaders/terrain/terrain.fs +++ b/assets/Shaders/terrain/terrain.fs @@ -72,12 +72,12 @@ uniform sampler2D shadowMap; // // // -uniform sampler2D groundTextures1; -uniform sampler2D groundTextures2; -uniform sampler2D groundTextures3; -uniform sampler2D groundTextures4; -//fifth texture unit is for shadow map -uniform sampler2D groundTextures5; +// uniform sampler2D groundTextures1; +// uniform sampler2D groundTextures2; +// uniform sampler2D groundTextures3; +// uniform sampler2D groundTextures4; +// //fifth texture unit is for shadow map +// uniform sampler2D groundTextures5; //this is for bindable ground textures uniform sampler2D groundTextures[10]; @@ -87,6 +87,25 @@ 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); +vec4 getTextureColor(int index, vec2 coord){ + if(index == 0){ + return texture(groundTextures[0], coord); + } + if(index == 1){ + return texture(groundTextures[1], coord); + } + if(index == 2){ + return texture(groundTextures[2], coord); + } + if(index == 3){ + return texture(groundTextures[3], coord); + } + if(index == 4){ + return texture(groundTextures[4], coord); + } + // return vec3(1,1,1); + return vec4(0,0,0,0); +} void main(){ if(hasTransparency == 1){ @@ -101,11 +120,28 @@ void main(){ // sampler2DArray text = groundTextures; // sampler2D test = groundTextures1; - vec4 texColor1 = texture(groundTextures[groundTexIndices.x * 2], TexCoord); - vec4 texColor2 = texture(groundTextures[groundTexIndices.y * 2], TexCoord); - vec4 texColor3 = texture(groundTextures[groundTexIndices.z * 2], TexCoord); - vec4 texColor4 = texture(groundTextures[groundTexIndices.w * 2], TexCoord); + + vec4 texColor1 = getTextureColor(groundTexIndices.x, TexCoord); + vec4 texColor2 = getTextureColor(groundTexIndices.y, TexCoord); + vec4 texColor3 = getTextureColor(groundTexIndices.z, TexCoord); + vec4 texColor4 = getTextureColor(groundTexIndices.w, TexCoord); + // vec4 texColor1 = texture(groundTextures[groundTexIndices.x], TexCoord); + // vec4 texColor2 = texture(groundTextures[groundTexIndices.y], TexCoord); + // vec4 texColor3 = texture(groundTextures[groundTexIndices.z], TexCoord); + // vec4 texColor4 = texture(groundTextures[groundTexIndices.w], TexCoord); + // vec4 texColor1 = texture(groundTextures[0], TexCoord); + // vec4 texColor2 = texture(groundTextures[1], TexCoord); + // vec4 texColor3 = texture(groundTextures[1], TexCoord); + // vec4 texColor4 = texture(groundTextures[1], TexCoord); vec3 finalTexColor = blendedTextureColor(TexCoord, texColor1, texColor2, texColor3, texColor4); + // vec3 finalTexColor = vec3(0,0,0); + // vec3 finalTexColor = mix(mix(texColor1,texColor2,TexCoord.x),mix(texColor3,texColor4,TexCoord.x),TexCoord.y).xyz;//blendedTextureColor(TexCoord, texColor1, texColor2, texColor3, texColor4); + // if(groundTexIndices.x != 1 || groundTexIndices.y != 0 || groundTexIndices.z != 0 || groundTexIndices.w != 0){ + // finalTexColor = vec3(1,0,0); + // } + // vec3 finalTexColor = vec3(groundTexIndices.x,groundTexIndices.y,groundTexIndices.z); + // vec3 finalTexColor = vec3(1.0,0,0); + // 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); diff --git a/assets/Shaders/terrain/terrain.vs b/assets/Shaders/terrain/terrain.vs index 57b506b3..ac0a4332 100644 --- a/assets/Shaders/terrain/terrain.vs +++ b/assets/Shaders/terrain/terrain.vs @@ -7,7 +7,7 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aNormal; layout (location = 4) in vec2 aTex; -layout (location = 5) in ivec4 aGroundTexIndices; +layout (location = 5) in vec4 aGroundTexIndices; //coordinate space transformation matrices @@ -41,7 +41,9 @@ void main() { TexCoord = aTex; //push texure indices to fragment shader - groundTexIndices = aGroundTexIndices; + groundTexIndices = ivec4(int(aGroundTexIndices.x),int(aGroundTexIndices.y),int(aGroundTexIndices.z),int(aGroundTexIndices.w)); + // groundTexIndices = vec4(int(aGroundTexIndices.x),int(aGroundTexIndices.y),int(aGroundTexIndices.z),int(aGroundTexIndices.w)); + // groundTexIndices = ivec4(0,1,2,3); //shadow map stuff FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0); diff --git a/src/main/java/electrosphere/game/client/cells/DrawCell.java b/src/main/java/electrosphere/game/client/cells/DrawCell.java index 530a215e..e9c726cf 100644 --- a/src/main/java/electrosphere/game/client/cells/DrawCell.java +++ b/src/main/java/electrosphere/game/client/cells/DrawCell.java @@ -28,6 +28,7 @@ public class DrawCell { int cellY; float[][] heightmap; + float[][] texturemap; int dynamicInterpolationRatio; @@ -70,6 +71,7 @@ public class DrawCell { int cellX, int cellY, float[][] heightmap, + float[][] texturemap, int dynamicInterpolationRatio, ShaderProgram program ){ @@ -79,6 +81,7 @@ public class DrawCell { rVal.program = program; rVal.dynamicInterpolationRatio = dynamicInterpolationRatio; rVal.heightmap = heightmap; + rVal.texturemap = texturemap; return rVal; } @@ -90,7 +93,7 @@ public class DrawCell { if(modelEntity != null){ Globals.entityManager.deregisterEntity(modelEntity); } - Model terrainModel = ModelUtils.createTerrainModelPrecomputedShader(heightmap, program, stride); + Model terrainModel = ModelUtils.createTerrainModelPrecomputedShader(heightmap, texturemap, program, stride); Mesh terrainMesh = terrainModel.meshes.get(0); terrainMesh.useTextureList = true; terrainMesh.textureList.add(groundTextureOne); diff --git a/src/main/java/electrosphere/game/client/cells/DrawCellManager.java b/src/main/java/electrosphere/game/client/cells/DrawCellManager.java index 8574db94..a7ab77c1 100644 --- a/src/main/java/electrosphere/game/client/cells/DrawCellManager.java +++ b/src/main/java/electrosphere/game/client/cells/DrawCellManager.java @@ -157,6 +157,7 @@ public class DrawCellManager { currentCellX, currentCellY, clientTerrainManager.getHeightmapAtPoint(currentCellX, currentCellY), + clientTerrainManager.getTextureMapAtPoint(currentCellX, currentCellY), clientWorldData.getDynamicInterpolationRatio(), program ); diff --git a/src/main/java/electrosphere/game/client/terrain/manager/ClientTerrainManager.java b/src/main/java/electrosphere/game/client/terrain/manager/ClientTerrainManager.java index 9cb46bb1..5dba14e9 100644 --- a/src/main/java/electrosphere/game/client/terrain/manager/ClientTerrainManager.java +++ b/src/main/java/electrosphere/game/client/terrain/manager/ClientTerrainManager.java @@ -184,6 +184,19 @@ public class ClientTerrainManager { return rVal; } + public float[][] getTextureMapAtPoint(int x, int y){ + float[][] rVal = new float[clientWorldData.getDynamicInterpolationRatio()][clientWorldData.getDynamicInterpolationRatio()]; + rVal[1][1] = 1; + rVal[2][1] = 1; + rVal[3][1] = 1; + rVal[4][1] = 1; + rVal[5][1] = 1; + rVal[5][2] = 1; + rVal[6][1] = 1; + rVal[6][2] = 1; + return rVal; + } + public void ejectLoadedChunks(){ List chunksToEject = new LinkedList(); for(LoadingChunk chunk : loadingChunkCache.getChunks()){ diff --git a/src/main/java/electrosphere/renderer/Mesh.java b/src/main/java/electrosphere/renderer/Mesh.java index fec15827..21f42ae8 100644 --- a/src/main/java/electrosphere/renderer/Mesh.java +++ b/src/main/java/electrosphere/renderer/Mesh.java @@ -594,6 +594,14 @@ public class Mesh { glEnableVertexAttribArray(attribIndex); } + public void bufferCustomUIntAttribArray(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_UNSIGNED_INT, false, 0, 0); + glEnableVertexAttribArray(attribIndex); + } + public void setTextureList(List textureList, String uniformName){ this.textureList = textureList; useTextureList = true; @@ -767,18 +775,24 @@ public class Mesh { if(useShadowMap){ glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, Globals.shadowMapTextureLoc); - glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "shadowMap"), 3); + glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "shadowMap"), 4); } if(useTextureList){ int i = 0; // glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures"), 5); +// for(int j = 1; j < 15; j++){ +// textureList.get(0).bind(j); +// } for(Texture texture : textureList){ -// System.out.println("groundTextures[" + i*2 + "]" + "=>" + (5+i*2)); - texture.bind(5+i*2); - glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures[" + i*2 + "]"),5+i*2); +// System.out.println(texture.getPath() + " => groundTextures[" + i + "]" + "=>" + (i)); + texture.bind(5+i); + glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures[" + i + "]"),5+i); i++; } + for(int j = i; j < 10; j++){ + glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures[" + j + "]"),5+j); + } // glActiveTexture(GL_TEXTURE0); } diff --git a/src/main/java/electrosphere/renderer/ModelUtils.java b/src/main/java/electrosphere/renderer/ModelUtils.java index 6aecda2b..9348fc85 100644 --- a/src/main/java/electrosphere/renderer/ModelUtils.java +++ b/src/main/java/electrosphere/renderer/ModelUtils.java @@ -164,7 +164,7 @@ public class ModelUtils { // return rVal; // } - public static Model createTerrainModelPrecomputedShader(float[][] heightfield, ShaderProgram program, int stride){ + public static Model createTerrainModelPrecomputedShader(float[][] heightfield, float[][] texturemap, ShaderProgram program, int stride){ Model rVal = new Model(); rVal.meshes = new ArrayList(); Mesh m = new Mesh(); @@ -183,7 +183,7 @@ public class ModelUtils { FloatBuffer normals; IntBuffer faces; FloatBuffer texture_coords; - IntBuffer textureIndices; + FloatBuffer textureIndices; if(stride * actualWidth > width){ int drawWidth = actualWidth + 1; int drawHeight = actualHeight + 1; @@ -191,13 +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); + textureIndices = BufferUtils.createFloatBuffer(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); + textureIndices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 16); } int incrementer = 0; @@ -271,11 +271,25 @@ 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); + if(x + stride < width - 1 && y + stride < height - 1){ +// texturemap[x+stride][y+stride]; + for(int i = 0; i < 4 ; i++){ +// textureIndices.put(1); +// textureIndices.put(0); +// textureIndices.put(0); +// textureIndices.put(0); + textureIndices.put(texturemap[x][y]); + textureIndices.put(texturemap[x+stride][y]); + textureIndices.put(texturemap[x][y+stride]); + textureIndices.put(texturemap[x+stride][y+stride]); + } + } else { + for(int i = 0; i < 4 ; i++){ + textureIndices.put(0); + textureIndices.put(0); + textureIndices.put(0); + textureIndices.put(0); + } } @@ -296,6 +310,7 @@ public class ModelUtils { normals.flip(); faces.flip(); texture_coords.flip(); + textureIndices.flip(); m.vertexArrayObject = glGenVertexArrays(); glBindVertexArray(m.vertexArrayObject); @@ -308,7 +323,7 @@ public class ModelUtils { //buffer texture coords m.buffer_texture_coords(texture_coords, 2); //texture indices - m.bufferCustomIntAttribArray(textureIndices, 4, 5); + m.bufferCustomFloatAttribArray(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 35aab284..02272879 100644 --- a/src/main/java/electrosphere/renderer/RenderingEngine.java +++ b/src/main/java/electrosphere/renderer/RenderingEngine.java @@ -122,13 +122,13 @@ public class RenderingEngine { glfwTerminate(); } //set resize callback - GLFW.glfwSetWindowSizeCallback​(Globals.window, new GLFWWindowSizeCallbackI(){ - @Override - public void invoke(long window, int width, int height){ - Globals.WINDOW_HEIGHT = height; - Globals.WINDOW_WIDTH = width; - } - }); +// GLFW.glfwSetWindowSizeCallback​(Globals.window, new GLFWWindowSizeCallbackI(){ +// @Override +// public void invoke(long window, int width, int height){ +// Globals.WINDOW_HEIGHT = height; +// Globals.WINDOW_WIDTH = width; +// } +// }); //Makes the window that was just created the current OS-level window context glfwMakeContextCurrent(Globals.window); //Maximize it diff --git a/src/main/java/electrosphere/renderer/texture/Texture.java b/src/main/java/electrosphere/renderer/texture/Texture.java index 39954fbb..75e982e4 100644 --- a/src/main/java/electrosphere/renderer/texture/Texture.java +++ b/src/main/java/electrosphere/renderer/texture/Texture.java @@ -29,10 +29,12 @@ public class Texture { int width; int height; boolean hasTransparency; + String path = ""; public Texture(){ } public Texture(String path){ + this.path = path; //generate the texture object on gpu texture_pointer = glGenTextures(); //bind the new texture @@ -135,4 +137,8 @@ public class Texture { public boolean isTransparent(){ return hasTransparency; } + + public String getPath(){ + return path; + } }