use of org.asassecreations.engine.math.vector.Vec3 in project Voxel_Game by ASasseCreations.
the class BlockRaycast method cast.
public static final Vec3 cast() {
int numberOfIterations = 0;
final Vec3 currentPosition = new Vec3(GameCamera.position);
final Vec3 currentDirection = new Vec3(ray());
currentDirection.normalise();
currentDirection.scale(CAST_SIZE);
Vec3 blockIntersection = null;
while (blockIntersection == null) {
currentPosition.x += currentDirection.x;
currentPosition.y += currentDirection.y;
currentPosition.z += currentDirection.z;
final BlockModeled block = Chunk.getBlock(currentPosition.x, currentPosition.y, currentPosition.z);
if (block != null) {
blockIntersection = new Vec3(currentPosition.x, currentPosition.y, currentPosition.z);
break;
}
numberOfIterations++;
if (numberOfIterations >= MAX_ITERATIONS)
break;
}
return blockIntersection;
}
use of org.asassecreations.engine.math.vector.Vec3 in project Voxel_Game by ASasseCreations.
the class Chunk method occlude.
// Occlude terrain
public final boolean occlude() {
if (!generated || operation == null || !operation.equals(ChunkOperation.OCCLUDING)) {
operation = null;
return false;
}
calculateOcclusion();
calculateSideOcclusion();
final List<Vec3> lights = new ArrayList<>();
{
final List<Chunk> chunksToCheck = new ArrayList<>();
Chunk cc = null;
if ((cc = Chunk.getChunk(x - 1, z - 1)) != null)
chunksToCheck.add(cc);
if ((cc = Chunk.getChunk(x - 1, z + 0)) != null)
chunksToCheck.add(cc);
if ((cc = Chunk.getChunk(x - 1, z + 1)) != null)
chunksToCheck.add(cc);
if ((cc = Chunk.getChunk(x + 0, z - 1)) != null)
chunksToCheck.add(cc);
if ((cc = Chunk.getChunk(x + 0, z + 0)) != null)
chunksToCheck.add(cc);
if ((cc = Chunk.getChunk(x + 0, z + 1)) != null)
chunksToCheck.add(cc);
if ((cc = Chunk.getChunk(x + 1, z - 1)) != null)
chunksToCheck.add(cc);
if ((cc = Chunk.getChunk(x + 1, z + 0)) != null)
chunksToCheck.add(cc);
if ((cc = Chunk.getChunk(x + 1, z + 1)) != null)
chunksToCheck.add(cc);
cc = null;
for (final Chunk c : chunksToCheck) for (int y = 0; y < V_SIZE; y++) for (int z = 0; z < H_SIZE; z++) for (int x = 0; x < H_SIZE; x++) {
final BlockModeled b = c.blocks[x][y][z];
if (b == null)
continue;
if (!b.visible)
continue;
if (b.preset.equals(Block.BLOCKS.get("lamp"))) {
final Vec3 pos = new Vec3(x + .5f, y + .5f, z + .5f);
Vec3.add(pos, new Vec3(c.x * H_SIZE, 0, c.z * H_SIZE), pos);
lights.add(pos);
}
}
}
final float[] dist = new float[lights.size()];
for (int y = 0; y < V_SIZE; y++) for (int z = 0; z < H_SIZE; z++) for (int x = 0; x < H_SIZE; x++) {
final BlockModeled b = blocks[x][y][z];
if (b == null)
continue;
if (!b.visible)
continue;
if (b.preset.equals(Block.BLOCKS.get("lamp"))) {
b.brightness = 1f;
continue;
}
final Vec3 Bpos = new Vec3(x + .5f, y + .5f, z + .5f);
Vec3.add(Bpos, new Vec3(this.x * H_SIZE, 0, this.z * H_SIZE), Bpos);
for (int i = 0; i < lights.size(); i++) {
final Vec3 Lpos = lights.get(i);
dist[i] = Mathf.distance(Lpos, Bpos);
}
float total = 0;
for (final float distance : dist) total += Mathf.constrain(Mathf.map(distance, 1, 8, 1, 0), 0f, 1f);
b.brightness = total;
}
// calculate each block light value
occluded = true;
operation = null;
return true;
}
use of org.asassecreations.engine.math.vector.Vec3 in project Voxel_Game by ASasseCreations.
the class ChunkSystem method checkLeftClick.
public static final void checkLeftClick(final Inventory inventory) {
final Vec3 block = BlockRaycast.cast();
if (block != null) {
final Chunk chunk = Chunk.getChunk((int) Math.floor(block.x / Chunk.H_SIZE), (int) Math.floor(block.z / Chunk.H_SIZE));
if (chunk != null) {
final Vector3f blockToSet = Chunk.getBlockCoordinates(block.x, block.y, block.z);
if (chunk.blocks[(int) blockToSet.x][(int) blockToSet.y][(int) blockToSet.z] != null && !chunk.blocks[(int) blockToSet.x][(int) blockToSet.y][(int) blockToSet.z].preset.breakable)
return;
inventory.addBlock(chunk.blocks[(int) blockToSet.x][(int) blockToSet.y][(int) blockToSet.z].preset);
chunk.blocks[(int) blockToSet.x][(int) blockToSet.y][(int) blockToSet.z] = null;
chunk.modified.put(new Vector3f((int) blockToSet.x, (int) blockToSet.y, (int) blockToSet.z), -1);
{
final int cx = chunk.x;
final int cz = chunk.z;
addToQueues(chunk);
addToQueues(Chunk.getChunk(cx - 1, cz));
addToQueues(Chunk.getChunk(cx + 1, cz));
addToQueues(Chunk.getChunk(cx, cz - 1));
addToQueues(Chunk.getChunk(cx, cz + 1));
}
}
}
}
use of org.asassecreations.engine.math.vector.Vec3 in project Voxel_Game by ASasseCreations.
the class GameRenderer method render.
public static final void render(final Light sun, final Player player, final Inventory inventory) {
// Scale the sky color so that the addition of light rays look good
final Vec3 skyColor = Settings.CURRENT_SKY_COLOR.asVec3();
skyColor.scale(.5f);
// Set the clear color
Renderer.clearColor(new Color(skyColor));
// Calculate sun direction
final Vec3 sunDirection = new Vec3(sun.position);
sunDirection.negate();
sunDirection.normalise();
// Calculate sun position on the screen
final Vec2 point = get2dPoint(SunRenderer.LIGHT_POSITION, GameCamera.view, GameCamera.projection_primary, framebufferLightScatter.width, framebufferLightScatter.height);
// Calculate camera rotation vector
final Vec3 cameraRotation = BlockRaycast.ray();
cameraRotation.normalise();
// Calculate the strength of the light rays based on how much you are
// facing the sun
float strength = Vec3.dot(cameraRotation, sunDirection);
strength = Mathf.constrain(strength, 0, 1);
// Start rendering to the chunk framebuffer
framebufferChunks.bind();
Renderer.clear();
// Render the sun
SunRenderer.begin();
SunRenderer.render(sun);
SunRenderer.end();
// Render the chunks
ChunkRenderer.begin(sun);
ChunkRenderer.render();
ChunkRenderer.end();
// Render the clouds
if (UserSettings.RENDER_CLOUDS) {
CloudRenderer.begin(sun);
CloudRenderer.render(new Vec3(player.position.x, 100, player.position.z), 300);
CloudRenderer.end();
}
// Render the hand
if (UserSettings.RENDER_HAND) {
HandRenderer.begin();
HandRenderer.render(sun);
HandRenderer.end();
}
// Start rendering to the light ray framebuffer
framebufferLightScatter.bind();
// Bind everything for the godray rendering
GL30.glBindVertexArray(model.id);
GL20.glEnableVertexAttribArray(0);
// Load texture of the entire scene rendered in black
StrictTexture.activeTexture(0);
StrictTexture.bindTexture(textureChunkSceneBlack);
// Draw the light rays
lightShader.start();
lightShader.loadCenter(point);
lightShader.loadStrength(strength);
lightShader.loadRes(framebufferLightScatter.width, framebufferLightScatter.height);
// Draw
GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, model.vertexCount);
// Unbind light ray stuff
lightShader.stop();
GL30.glBindVertexArray(0);
GL20.glDisableVertexAttribArray(0);
// Bind main framebuffer for use of combining the color attachments
// together
framebufferChunks.bind();
GL20.glEnableVertexAttribArray(0);
GL30.glBindVertexArray(model.id);
// Bind textures
StrictTexture.activeTexture(0);
StrictTexture.bindTexture(textureChunkScene);
StrictTexture.activeTexture(1);
StrictTexture.bindTexture(textureLightScatter);
// Combine color attachments together
combine.start();
GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, model.vertexCount);
combine.stop();
// Unbind
GL30.glBindVertexArray(0);
GL20.glDisableVertexAttribArray(0);
// Blit the result over to the screen
framebufferChunks.blit(null, GL11.GL_COLOR_BUFFER_BIT, GL11.GL_LINEAR);
// Return to the default framebuffer for guis
FrameBufferObject.unbind();
// Render the minimap
Minimap.render(sun);
// Render other guis
GameStateGuis.render(inventory);
}
use of org.asassecreations.engine.math.vector.Vec3 in project Voxel_Game by ASasseCreations.
the class GameRenderer method get2dPoint.
private static final Vec2 get2dPoint(final Vec3 point3D, final Mat4 viewMatrix, final Mat4 projectionMatrix, final int width, final int height) {
final Vec4 clipSpacePos = Mat4.transform(projectionMatrix, Mat4.transform(viewMatrix, new Vec4(point3D.x, point3D.y, point3D.z, 1.0f), null), null);
final Vec3 ndcSpacePos = new Vec3(clipSpacePos.x / clipSpacePos.w, clipSpacePos.y / clipSpacePos.w, clipSpacePos.z / clipSpacePos.w);
final Vec2 vector = new Vec2(ndcSpacePos.x, ndcSpacePos.y);
Vec2.add(vector, new Vec2(1, 1), vector);
vector.x /= 2f;
vector.y /= 2f;
return vector;
}
Aggregations