Terrain splatting hopefully working

This commit is contained in:
austin 2021-10-09 16:22:33 -04:00
parent 95829bb847
commit 57c2fa1e61
10 changed files with 127 additions and 35 deletions

4
.gitignore vendored
View File

@ -14,4 +14,6 @@
.classpath .classpath
.project .project
.settings .settings
.vscode

View File

@ -72,12 +72,12 @@ uniform sampler2D shadowMap;
// //
// //
// //
uniform sampler2D groundTextures1; // uniform sampler2D groundTextures1;
uniform sampler2D groundTextures2; // uniform sampler2D groundTextures2;
uniform sampler2D groundTextures3; // uniform sampler2D groundTextures3;
uniform sampler2D groundTextures4; // uniform sampler2D groundTextures4;
//fifth texture unit is for shadow map // //fifth texture unit is for shadow map
uniform sampler2D groundTextures5; // uniform sampler2D groundTextures5;
//this is for bindable ground textures //this is for bindable ground textures
uniform sampler2D groundTextures[10]; 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); vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal); float ShadowCalculation(vec4 fragPosLightSpace, vec3 lightDir, vec3 normal);
vec3 blendedTextureColor(vec2 texPos, vec4 tex1, vec4 tex2, vec4 tex3, vec4 tex4); 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(){ void main(){
if(hasTransparency == 1){ if(hasTransparency == 1){
@ -101,11 +120,28 @@ void main(){
// sampler2DArray text = groundTextures; // sampler2DArray text = groundTextures;
// sampler2D test = groundTextures1; // sampler2D test = groundTextures1;
vec4 texColor1 = texture(groundTextures[groundTexIndices.x * 2], TexCoord);
vec4 texColor2 = texture(groundTextures[groundTexIndices.y * 2], TexCoord); vec4 texColor1 = getTextureColor(groundTexIndices.x, TexCoord);
vec4 texColor3 = texture(groundTextures[groundTexIndices.z * 2], TexCoord); vec4 texColor2 = getTextureColor(groundTexIndices.y, TexCoord);
vec4 texColor4 = texture(groundTextures[groundTexIndices.w * 2], 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 = 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 tex2 = texture(groundTextures[int(groundTexIndices.y)], TexCoord);
// vec4 tex3 = texture2D(groundTextures[int(groundTexIndex.z * 2)], texPos); // vec4 tex3 = texture2D(groundTextures[int(groundTexIndex.z * 2)], texPos);
// vec4 tex4 = texture2D(groundTextures[int(groundTexIndex.w * 2)], texPos); // vec4 tex4 = texture2D(groundTextures[int(groundTexIndex.w * 2)], texPos);

View File

@ -7,7 +7,7 @@
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal; layout (location = 1) in vec3 aNormal;
layout (location = 4) in vec2 aTex; layout (location = 4) in vec2 aTex;
layout (location = 5) in ivec4 aGroundTexIndices; layout (location = 5) in vec4 aGroundTexIndices;
//coordinate space transformation matrices //coordinate space transformation matrices
@ -41,7 +41,9 @@ void main() {
TexCoord = aTex; TexCoord = aTex;
//push texure indices to fragment shader //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 //shadow map stuff
FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0); FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0);

View File

@ -28,6 +28,7 @@ public class DrawCell {
int cellY; int cellY;
float[][] heightmap; float[][] heightmap;
float[][] texturemap;
int dynamicInterpolationRatio; int dynamicInterpolationRatio;
@ -70,6 +71,7 @@ public class DrawCell {
int cellX, int cellX,
int cellY, int cellY,
float[][] heightmap, float[][] heightmap,
float[][] texturemap,
int dynamicInterpolationRatio, int dynamicInterpolationRatio,
ShaderProgram program ShaderProgram program
){ ){
@ -79,6 +81,7 @@ public class DrawCell {
rVal.program = program; rVal.program = program;
rVal.dynamicInterpolationRatio = dynamicInterpolationRatio; rVal.dynamicInterpolationRatio = dynamicInterpolationRatio;
rVal.heightmap = heightmap; rVal.heightmap = heightmap;
rVal.texturemap = texturemap;
return rVal; return rVal;
} }
@ -90,7 +93,7 @@ public class DrawCell {
if(modelEntity != null){ if(modelEntity != null){
Globals.entityManager.deregisterEntity(modelEntity); 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); Mesh terrainMesh = terrainModel.meshes.get(0);
terrainMesh.useTextureList = true; terrainMesh.useTextureList = true;
terrainMesh.textureList.add(groundTextureOne); terrainMesh.textureList.add(groundTextureOne);

View File

@ -157,6 +157,7 @@ public class DrawCellManager {
currentCellX, currentCellX,
currentCellY, currentCellY,
clientTerrainManager.getHeightmapAtPoint(currentCellX, currentCellY), clientTerrainManager.getHeightmapAtPoint(currentCellX, currentCellY),
clientTerrainManager.getTextureMapAtPoint(currentCellX, currentCellY),
clientWorldData.getDynamicInterpolationRatio(), clientWorldData.getDynamicInterpolationRatio(),
program program
); );

View File

@ -184,6 +184,19 @@ public class ClientTerrainManager {
return rVal; 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(){ public void ejectLoadedChunks(){
List<LoadingChunk> chunksToEject = new LinkedList(); List<LoadingChunk> chunksToEject = new LinkedList();
for(LoadingChunk chunk : loadingChunkCache.getChunks()){ for(LoadingChunk chunk : loadingChunkCache.getChunks()){

View File

@ -594,6 +594,14 @@ public class Mesh {
glEnableVertexAttribArray(attribIndex); 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<Texture> textureList, String uniformName){ public void setTextureList(List<Texture> textureList, String uniformName){
this.textureList = textureList; this.textureList = textureList;
useTextureList = true; useTextureList = true;
@ -767,18 +775,24 @@ public class Mesh {
if(useShadowMap){ if(useShadowMap){
glActiveTexture(GL_TEXTURE3); glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, Globals.shadowMapTextureLoc); glBindTexture(GL_TEXTURE_2D, Globals.shadowMapTextureLoc);
glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "shadowMap"), 3); glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "shadowMap"), 4);
} }
if(useTextureList){ if(useTextureList){
int i = 0; int i = 0;
// glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures"), 5); // glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures"), 5);
// for(int j = 1; j < 15; j++){
// textureList.get(0).bind(j);
// }
for(Texture texture : textureList){ for(Texture texture : textureList){
// System.out.println("groundTextures[" + i*2 + "]" + "=>" + (5+i*2)); // System.out.println(texture.getPath() + " => groundTextures[" + i + "]" + "=>" + (i));
texture.bind(5+i*2); texture.bind(5+i);
glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures[" + i*2 + "]"),5+i*2); glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures[" + i + "]"),5+i);
i++; i++;
} }
for(int j = i; j < 10; j++){
glUniform1i(glGetUniformLocation(Globals.renderingEngine.getActiveShader().shaderProgram, "groundTextures[" + j + "]"),5+j);
}
// glActiveTexture(GL_TEXTURE0); // glActiveTexture(GL_TEXTURE0);
} }

View File

@ -164,7 +164,7 @@ public class ModelUtils {
// return rVal; // 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(); Model rVal = new Model();
rVal.meshes = new ArrayList(); rVal.meshes = new ArrayList();
Mesh m = new Mesh(); Mesh m = new Mesh();
@ -183,7 +183,7 @@ public class ModelUtils {
FloatBuffer normals; FloatBuffer normals;
IntBuffer faces; IntBuffer faces;
FloatBuffer texture_coords; FloatBuffer texture_coords;
IntBuffer textureIndices; FloatBuffer textureIndices;
if(stride * actualWidth > width){ if(stride * actualWidth > width){
int drawWidth = actualWidth + 1; int drawWidth = actualWidth + 1;
int drawHeight = actualHeight + 1; int drawHeight = actualHeight + 1;
@ -191,13 +191,13 @@ public class ModelUtils {
normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12); normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12);
faces = BufferUtils.createIntBuffer((drawWidth - 1) * (drawHeight - 1) * 2 * 3); faces = BufferUtils.createIntBuffer((drawWidth - 1) * (drawHeight - 1) * 2 * 3);
texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 8); texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 8);
textureIndices = BufferUtils.createIntBuffer(drawWidth * drawHeight * 16); textureIndices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 16);
} else { } else {
vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12);
normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12); normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 12);
faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3); faces = BufferUtils.createIntBuffer((actualWidth - 1) * (actualHeight - 1) * 2 * 3);
texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 8); texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 8);
textureIndices = BufferUtils.createIntBuffer(actualWidth * actualHeight * 16); textureIndices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 16);
} }
int incrementer = 0; int incrementer = 0;
@ -271,11 +271,25 @@ public class ModelUtils {
texture_coords.put(1); texture_coords.put(1);
texture_coords.put(1); texture_coords.put(1);
for(int i = 0; i < 4 ; i++){ if(x + stride < width - 1 && y + stride < height - 1){
textureIndices.put(0); // texturemap[x+stride][y+stride];
textureIndices.put(1); for(int i = 0; i < 4 ; i++){
textureIndices.put(2); // textureIndices.put(1);
textureIndices.put(3); // 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(); normals.flip();
faces.flip(); faces.flip();
texture_coords.flip(); texture_coords.flip();
textureIndices.flip();
m.vertexArrayObject = glGenVertexArrays(); m.vertexArrayObject = glGenVertexArrays();
glBindVertexArray(m.vertexArrayObject); glBindVertexArray(m.vertexArrayObject);
@ -308,7 +323,7 @@ public class ModelUtils {
//buffer texture coords //buffer texture coords
m.buffer_texture_coords(texture_coords, 2); m.buffer_texture_coords(texture_coords, 2);
//texture indices //texture indices
m.bufferCustomIntAttribArray(textureIndices, 4, 5); m.bufferCustomFloatAttribArray(textureIndices, 4, 5);
m.shader = program; m.shader = program;
glBindVertexArray(0); glBindVertexArray(0);
m.parent = rVal; m.parent = rVal;

View File

@ -122,13 +122,13 @@ public class RenderingEngine {
glfwTerminate(); glfwTerminate();
} }
//set resize callback //set resize callback
GLFW.glfwSetWindowSizeCallback(Globals.window, new GLFWWindowSizeCallbackI(){ // GLFW.glfwSetWindowSizeCallback(Globals.window, new GLFWWindowSizeCallbackI(){
@Override // @Override
public void invoke(long window, int width, int height){ // public void invoke(long window, int width, int height){
Globals.WINDOW_HEIGHT = height; // Globals.WINDOW_HEIGHT = height;
Globals.WINDOW_WIDTH = width; // Globals.WINDOW_WIDTH = width;
} // }
}); // });
//Makes the window that was just created the current OS-level window context //Makes the window that was just created the current OS-level window context
glfwMakeContextCurrent(Globals.window); glfwMakeContextCurrent(Globals.window);
//Maximize it //Maximize it

View File

@ -29,10 +29,12 @@ public class Texture {
int width; int width;
int height; int height;
boolean hasTransparency; boolean hasTransparency;
String path = "";
public Texture(){ public Texture(){
} }
public Texture(String path){ public Texture(String path){
this.path = path;
//generate the texture object on gpu //generate the texture object on gpu
texture_pointer = glGenTextures(); texture_pointer = glGenTextures();
//bind the new texture //bind the new texture
@ -135,4 +137,8 @@ public class Texture {
public boolean isTransparent(){ public boolean isTransparent(){
return hasTransparency; return hasTransparency;
} }
public String getPath(){
return path;
}
} }