Work on terrain mesh towards splatting

This commit is contained in:
satellite 2021-08-08 14:26:23 -04:00
parent f862c104cc
commit 5b3c99b2ff
2 changed files with 145 additions and 96 deletions

View File

@ -572,6 +572,14 @@ public class Mesh {
glEnableVertexAttribArray(4);
}
public void bufferCustomAttribArray(FloatBuffer 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_FLOAT, false, 0, 0);
glEnableVertexAttribArray(attribIndex);
}
public void set_material(Material input){
this.material = input;
}

View File

@ -186,116 +186,99 @@ public class ModelUtils {
if(stride * actualWidth > width){
int drawWidth = actualWidth + 1;
int drawHeight = actualHeight + 1;
vertices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 3);
normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 3);
vertices = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12);
normals = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 12);
faces = BufferUtils.createIntBuffer((drawWidth - 1) * (drawHeight - 1) * 2 * 3);
texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 2);
texture_coords = BufferUtils.createFloatBuffer(drawWidth * drawHeight * 8);
} else {
vertices = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 3);
normals = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 3);
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 * 2);
texture_coords = BufferUtils.createFloatBuffer(actualWidth * actualHeight * 8);
}
for(int x = 0; x < width; x = x + stride){
for(int y = 0; y < height; y = y + stride){
int incrementer = 0;
int numFaces = (actualWidth - 1) * (actualHeight - 1) * 2 * 3;
for(int x = 0; x < width - 1; x = x + stride){
for(int y = 0; y < height - 1; y = y + stride){
//deal with vertex
//0,0
vertices.put(x);
vertices.put(heightfield[x][y]);
vertices.put(y);
//1,0
vertices.put(x + 1);
vertices.put(heightfield[x+1][y]);
vertices.put(y);
//0,1
vertices.put(x);
vertices.put(heightfield[x][y+1]);
vertices.put(y + 1);
//1,1
vertices.put(x + 1);
vertices.put(heightfield[x+1][y+1]);
vertices.put(y + 1);
//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);
}
}
Vector3f normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y);
normals.put(normal.x);
normals.put(normal.y);
normals.put(normal.z);
normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + 1, y);
normals.put(normal.x);
normals.put(normal.y);
normals.put(normal.z);
normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x, y + 1);
normals.put(normal.x);
normals.put(normal.y);
normals.put(normal.z);
normal = calculateTerrainNormal(heightfield, actualWidth, actualHeight, stride, x + 1, y + 1);
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);
}
}
// if(x / stride % 2 == 0){
// if(y / stride % 2 == 0){
// texture_coords.put(0);
// texture_coords.put(0);
// texture_coords.put(1);
// texture_coords.put(0);
// texture_coords.put(0);
// texture_coords.put(1);
// texture_coords.put(1);
// texture_coords.put(1);
// } 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);
// }
// }
texture_coords.put(0);
texture_coords.put(0);
texture_coords.put(1);
texture_coords.put(0);
texture_coords.put(0);
texture_coords.put(1);
texture_coords.put(1);
texture_coords.put(1);
//deal with faces
if(1.0f * x / stride < actualWidth - 1 && 1.0f * 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));
faces.put(incrementer * 4 + 0);
faces.put(incrementer * 4 + 1);
faces.put(incrementer * 4 + 2);
faces.put(incrementer * 4 + 1);
faces.put(incrementer * 4 + 2);
faces.put(incrementer * 4 + 3);
}
incrementer++;
}
}
@ -330,6 +313,64 @@ public class ModelUtils {
return rVal;
}
static Vector3f calculateTerrainNormal(float[][] heightfield, int actualWidth, int actualHeight, int stride, int x, int y){
Vector3f rVal = new Vector3f();
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];
}
rVal = new Vector3f(hL - hR, 2.0f, hD - hU);
rVal.normalize();
} 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];
rVal = new Vector3f(hL - hR, 2.0f, hD - hU);
rVal.normalize();
}
} 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];
}
rVal = new Vector3f(hL - hR, 2.0f, hD - hU);
rVal.normalize();
} else {
float hL = heightfield[x-1][y];
float hR = heightfield[x][y];
float hD = heightfield[x][y];
float hU = heightfield[x][y-1];
rVal = new Vector3f(hL - hR, 2.0f, hD - hU);
rVal.normalize();
}
}
return rVal;
}
public static Model createUnitCube(){
Model rVal = new Model();