Renderer/src/main/java/electrosphere/client/terrain/cells/DrawCell.java
2023-06-08 12:27:19 -04:00

135 lines
5.2 KiB
Java

package electrosphere.client.terrain.cells;
import org.joml.Vector3d;
import org.joml.Vector3i;
import org.ode4j.ode.DBody;
import electrosphere.client.terrain.cache.ChunkData;
import electrosphere.collision.CollisionEngine;
import electrosphere.engine.Globals;
import electrosphere.entity.ClientEntityUtils;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.types.terrain.TerrainChunk;
import electrosphere.renderer.ShaderProgram;
import electrosphere.renderer.texture.Texture;
import electrosphere.server.datacell.Realm;
/**
*
* @author satellite
*/
public class DrawCell {
//the position of the draw cell in world coordinates
Vector3i worldPos;
ChunkData data;
Entity modelEntity;
ShaderProgram program;
DBody physicsObject;
static Texture groundTextureOne = new Texture("/Textures/Ground/Dirt1.png");
static Texture groundTextureTwo = new Texture("/Textures/Ground/Dirt1.png");
static Texture groundTextureThree = new Texture("/Textures/Ground/Dirt1.png");
static Texture groundTextureFour = new Texture("/Textures/Ground/Dirt1.png");
static {
// groundTextureOne = new Texture("/Textures/Ground/GrassTileable.png");
// groundTextureTwo = new Texture("/Textures/Ground/Dirt1.png");
// groundTextureThree = new Texture("/Textures/Ground/Dirt1.png");
// groundTextureFour = new Texture("/Textures/Ground/Dirt1.png");
}
DrawCell(){
}
/**
* Constructs a drawcell object
*/
public static DrawCell generateTerrainCell(
Vector3i worldPos,
ChunkData data,
ShaderProgram program
){
DrawCell rVal = new DrawCell();
rVal.worldPos = worldPos;
rVal.program = program;
rVal.data = data;
return rVal;
}
/**
* Generates a drawable entity based on this chunk
* @param stride The stride between indices used to generate "sparse" meshes
*/
public void generateDrawableEntity(){
if(modelEntity != null){
Globals.clientScene.deregisterEntity(modelEntity);
}
modelEntity = TerrainChunk.clientCreateTerrainChunkEntity(data.getVoxelWeight(), data.getVoxelType());
// TerrainChunk.createTerrainChunkEntity();
// Model terrainModel = RenderUtils.createMinimizedTerrainModelPrecomputedShader(heightmap, texturemap, program, stride);
// Mesh terrainMesh = terrainModel.meshes.get(0);
// List<Texture> textureList = new LinkedList<Texture>();
// textureList.add(groundTextureOne);
// textureList.add(groundTextureTwo);
// textureList.add(groundTextureThree);
// textureList.add(groundTextureFour);
// List<String> uniformList = new LinkedList<String>();
// uniformList.add("groundTextures[0]");
// uniformList.add("groundTextures[1]");
// uniformList.add("groundTextures[2]");
// uniformList.add("groundTextures[3]");
// String terrainModelPath = Globals.assetManager.registerModel(terrainModel);
// modelEntity = EntityCreationUtils.createClientSpatialEntity();
// EntityCreationUtils.makeEntityDrawable(modelEntity, terrainModelPath);
// EntityUtils.getActor(modelEntity).addTextureMask(new ActorTextureMask("terrain",textureList,uniformList));
// modelEntity.putData(EntityDataStrings.TERRAIN_IS_TERRAIN, true);
// modelEntity.putData(EntityDataStrings.DRAW_CAST_SHADOW, true);
// LoggerInterface.loggerRenderer.INFO("New cell @ " + cellX * dynamicInterpolationRatio + "," + cellY * dynamicInterpolationRatio);
// EntityUtils.getPosition(modelEntity).set(getRealPos());
ClientEntityUtils.initiallyPositionEntity(modelEntity, getRealPos());
}
public void retireCell(){
EntityUtils.cleanUpEntity(modelEntity);
}
protected Vector3d getRealPos(){
return new Vector3d(
worldPos.x * ChunkData.CHUNK_SIZE,
worldPos.y * ChunkData.CHUNK_SIZE,
worldPos.z * ChunkData.CHUNK_SIZE
);
}
// public void generatePhysics(){
// //if we're in no-graphics mode, need to generate the entity
// if(modelEntity == null){
// modelEntity = EntityCreationUtils.createClientSpatialEntity();
// modelEntity.putData(EntityDataStrings.TERRAIN_IS_TERRAIN, true);
// EntityUtils.getPosition(modelEntity).set(new Vector3f(cellX * dynamicInterpolationRatio, 0.0f, cellY * dynamicInterpolationRatio));
// }
// //then actually perform the attach
// physicsObject = PhysicsUtils.attachTerrainRigidBody(modelEntity,heightmap,false);
// Globals.clientSceneWrapper.getCollisionEngine().registerPhysicsEntity(modelEntity);
// // System.out.println("generate physics");
// }
public void destroy(){
Realm realm = Globals.realmManager.getEntityRealm(modelEntity);
if(realm != null){
CollisionEngine collisionEngine = Globals.realmManager.getEntityRealm(modelEntity).getCollisionEngine();
collisionEngine.destroyEntityThatHasPhysics(modelEntity);
}
EntityUtils.cleanUpEntity(modelEntity);
}
}