123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #include "client/World.h"
- #include "client/Player.h"
- #include "data/Array.h"
- #include "math/Frustum.h"
- #include "math/View.h"
- #include "rendering/VertexBuffer.h"
- #include "rendering/Window.h"
- #include "utils/Logger.h"
- static constexpr int WORLD_SIZE = 16;
- static Array<bool, WORLD_SIZE * WORLD_SIZE * WORLD_SIZE> world(true);
- static bool dirtyVertexBuffer = true;
- static int vertices = 0;
- static Frustum frustum(80.0f, 0.1f, 1000.0f);
- Shader World::shader;
- static VertexBuffer vertexBuffer;
- bool World::init() {
- Error e =
- shader.compile("resources/shader/test.vs", "resources/shader/test.fs");
- if(e.has()) {
- e.message.printLine();
- return true;
- }
- vertexBuffer.init(VertexBuffer::Attributes().addFloat(3).addFloat(3));
- return false;
- }
- IntVector3 World::getSize() {
- return IntVector3(WORLD_SIZE, WORLD_SIZE, WORLD_SIZE);
- }
- void World::set(int x, int y, int z, bool b) {
- if(x < 0 || x >= WORLD_SIZE || y < 0 || y >= WORLD_SIZE || z < 0 ||
- z >= WORLD_SIZE) {
- return;
- }
- world[x * WORLD_SIZE * WORLD_SIZE + y * WORLD_SIZE + z] = b;
- dirtyVertexBuffer = true;
- }
- bool World::isAir(int x, int y, int z) {
- if(x < 0 || x >= WORLD_SIZE || y < 0 || y >= WORLD_SIZE || z < 0 ||
- z >= WORLD_SIZE) {
- return true;
- }
- return !world[x * WORLD_SIZE * WORLD_SIZE + y * WORLD_SIZE + z];
- }
- static void addTriangle(Buffer& buffer, const Vector3& a, const Vector3& b,
- const Vector3& c) {
- Vector3 normal = (b - a).cross(c - a);
- buffer.add(a).add(normal).add(b).add(normal).add(c).add(normal);
- }
- static void addCube(Buffer& buffer, int x, int y, int z) {
- if(World::isAir(x, y, z)) {
- return;
- }
- Vector3 v000(x, y, z);
- Vector3 v001(x, y, z + 1);
- Vector3 v010(x, y + 1, z);
- Vector3 v011(x, y + 1, z + 1);
- Vector3 v100(x + 1, y, z);
- Vector3 v101(x + 1, y, z + 1);
- Vector3 v110(x + 1, y + 1, z);
- Vector3 v111(x + 1, y + 1, z + 1);
- if(World::isAir(x, y - 1, z)) {
- addTriangle(buffer, v000, v100, v001);
- addTriangle(buffer, v100, v101, v001);
- }
- if(World::isAir(x, y + 1, z)) {
- addTriangle(buffer, v010, v011, v110);
- addTriangle(buffer, v110, v011, v111);
- }
- if(World::isAir(x - 1, y, z)) {
- addTriangle(buffer, v000, v001, v010);
- addTriangle(buffer, v001, v011, v010);
- }
- if(World::isAir(x + 1, y, z)) {
- addTriangle(buffer, v100, v110, v101);
- addTriangle(buffer, v101, v110, v111);
- }
- if(World::isAir(x, y, z + 1)) {
- addTriangle(buffer, v001, v101, v011);
- addTriangle(buffer, v111, v011, v101);
- }
- if(World::isAir(x, y, z - 1)) {
- addTriangle(buffer, v000, v010, v100);
- addTriangle(buffer, v110, v100, v010);
- }
- }
- static void buildRenderingBuffer() {
- Buffer buffer(100);
- IntVector3 size = World::getSize();
- for(int x = 0; x < size[0]; x++) {
- for(int y = 0; y < size[1]; y++) {
- for(int z = 0; z < size[2]; z++) {
- addCube(buffer, x, y, z);
- }
- }
- }
- vertices = buffer.getLength() / (2 * sizeof(Vector3));
- vertexBuffer.setData(buffer, GL::STATIC_DRAW);
- }
- void World::render(float lag) {
- if(dirtyVertexBuffer) {
- dirtyVertexBuffer = false;
- buildRenderingBuffer();
- LOG_INFO("rebuilt buffer");
- }
- View view;
- view.updateDirections(Player::getLengthAngle(lag),
- Player::getWidthAngle(lag));
- Vector3 center = Player::getPosition(lag) + Vector3(0.0f, 0.8f, 0.0f);
- shader.use();
- GL::setViewport(Window::getSize()[0], Window::getSize()[1]);
- shader.setMatrix("proj", frustum.updateProjection(Window::getSize()));
- shader.setMatrix("view", view.updateMatrix(center));
- shader.setVector("color", Vector3(1.0f, 1.0f, 1.0f));
- shader.setMatrix("model", Matrix());
- vertexBuffer.draw(vertices);
- }
- static List<Box> getBoxes(const Box& box) {
- int minX = floorf(box.getMin()[0]);
- int minY = floorf(box.getMin()[1]);
- int minZ = floorf(box.getMin()[2]);
- int maxX = floorf(box.getMax()[0]);
- int maxY = floorf(box.getMax()[1]);
- int maxZ = floorf(box.getMax()[2]);
- Box base(Vector3(1.0f, 1.0f, 1.0f));
- List<Box> boxes;
- for(int x = minX; x <= maxX; x++) {
- for(int y = minY; y <= maxY; y++) {
- for(int z = minZ; z <= maxZ; z++) {
- if(!World::isAir(x, y, z)) {
- boxes.add(base.offset(Vector3(x, y, z)));
- }
- }
- }
- }
- return boxes;
- }
- Vector3 World::limitMove(const Box& box, Vector3 move) {
- List<Box> boxes = getBoxes(box.expand(move));
- if(boxes.getLength() == 0) {
- return move;
- }
- Vector3 realMove;
- constexpr float step = 0.05f;
- while(move[0] != 0.0f || move[1] != 0.0f || move[2] != 0.0f) {
- for(int i = 0; i < 3; i++) {
- Vector3 old = realMove;
- if(move[i] > step) {
- realMove[i] += step;
- move[i] -= step;
- } else if(move[i] < -step) {
- realMove[i] -= step;
- move[i] += step;
- } else if(move[i] != 0.0f) {
- realMove[i] += move[i];
- move[i] = 0.0f;
- } else {
- continue;
- }
- Box moved = box.offset(realMove);
- for(const Box& box : boxes) {
- if(box.collidesWith(moved)) {
- move[i] = 0.0f;
- realMove = old;
- break;
- }
- }
- }
- }
- return realMove;
- }
- RayTrace World::rayTrace(const Vector3& start, const Vector3& direction,
- float maxDistance) {
- RayTrace result{RayTrace::Type::AIR, IntVector3()};
- Vector3 pos = start;
- Vector3 step = direction;
- // prevent zero division
- for(int i = 0; i < 3; i++) {
- if(step[i] == 0.0f) {
- step[i] = 0.0001f;
- }
- }
- step.normalize();
- while(maxDistance > 0.0f) {
- IntVector3 lower(floorf(pos[0]), floorf(pos[1]), floorf(pos[2]));
- if(!World::isAir(lower[0], lower[1], lower[2])) {
- result.block = lower;
- result.type = RayTrace::Type::BLOCK;
- return result;
- }
- Vector3 goal(step[0] < 0.0f ? lower[0] : lower[0] + 1,
- step[1] < 0.0f ? lower[1] : lower[1] + 1,
- step[2] < 0.0f ? lower[2] : lower[2] + 1);
- Vector3 f = (goal - pos) / step;
- float min = Math::max(Math::min(f[0], f[1], f[2]), 0.001f);
- pos += step * min;
- maxDistance -= min;
- }
- return result;
- }
|