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); 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){ public void set_material(Material input){
this.material = input; this.material = input;
} }

View File

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