diff --git a/Makefile b/Makefile index 1c283186..a8265ce5 100644 --- a/Makefile +++ b/Makefile @@ -202,12 +202,12 @@ rm-exe64: $(EX_WIN64_DLL): WIN64_FLAGS += -DUF_EXPORTS $(EX_WIN64_DLL): $(OBJS_WIN64_DLL) - $(WIN64_CC) -shared -o $(EX_WIN64_DLL) -Wl,--out-implib=$(IM_WIN64_DLL) $(OBJS_WIN64_DLL) $(WIN64_LIBS) $(WIN64_INCS) $(WIN64_LINKS) + $(WIN64_CC) -shared -o $(EX_WIN64_DLL) -g -Wl,--out-implib=$(IM_WIN64_DLL) $(OBJS_WIN64_DLL) $(WIN64_LIBS) $(WIN64_INCS) $(WIN64_LINKS) cp $(ENGINE_LIB_DIR)/win64/$(BASE_WIN64_DLL).dll.a $(ENGINE_LIB_DIR)/win64/$(BASE_WIN64_DLL).a $(EXT_EX_WIN64_DLL): WIN64_FLAGS += -DEXT_EXPORTS $(EXT_EX_WIN64_DLL): $(OBJS_EXT_WIN64_DLL) - $(WIN64_CC) -shared -o $(EXT_EX_WIN64_DLL) -Wl,--out-implib=$(EXT_IM_WIN64_DLL) $(OBJS_EXT_WIN64_DLL) $(EXT_WIN64_LIBS) $(EXT_WIN64_INCS) $(EXT_WIN64_LINKS) + $(WIN64_CC) -shared -o $(EXT_EX_WIN64_DLL) -g -Wl,--out-implib=$(EXT_IM_WIN64_DLL) $(OBJS_EXT_WIN64_DLL) $(EXT_WIN64_LIBS) $(EXT_WIN64_INCS) $(EXT_WIN64_LINKS) cp $(ENGINE_LIB_DIR)/win64/$(BASE_EXT_WIN64_DLL).dll.a $(ENGINE_LIB_DIR)/win64/$(BASE_EXT_WIN64_DLL).a $(TARGET_WIN64): $(OBJS_WIN64) diff --git a/engine/src/utils/math/collider.cpp b/engine/src/utils/math/collider.cpp index 89b3c0d2..f2410567 100644 --- a/engine/src/utils/math/collider.cpp +++ b/engine/src/utils/math/collider.cpp @@ -198,6 +198,35 @@ uf::Collider::Manifold UF_API uf::AABBox::intersects( const uf::AABBox& b ) cons float b_back = b.m_origin.z - b.m_corner.z; float b_front = b.m_origin.z + b.m_corner.z; + manifold.depth = 9E9; + auto test = [&]( const pod::Vector3& axis, float minA, float maxA, float minB, float maxB )->bool{ + float axisLSqr = uf::vector::dot(axis, axis); + if ( axisLSqr < 1E-8f ) return false; + + float d0 = maxB - minA; + float d1 = maxA - minB; + if ( d0 < 0 || d1 < 0 ) return false; + float overlap = d0 < d1 ? d0 : -d1; + pod::Vector3 sep = axis * ( overlap / axisLSqr ); + float sepLSqr = uf::vector::dot( sep, sep ); + if ( sepLSqr < manifold.depth ) { + manifold.normal = sep; + manifold.depth = sepLSqr; + } + return true; + }; + + if ( !test( {1, 0, 0}, a_left, a_right, b_left, b_right ) ) return manifold; + if ( !test( {0, 1, 0}, a_bottom, a_top, b_bottom, b_top ) ) return manifold; + if ( !test( {0, 0, 1}, a_back, a_front, b_back, b_front ) ) return manifold; + + manifold.normal = uf::vector::normalize( manifold.normal ); + manifold.depth = sqrt( manifold.depth ); // * 1.001; + manifold.colliding = true; + + return manifold; + +/* if ( a_right < b_left ) return manifold; if ( a_left > b_right ) return manifold; @@ -206,17 +235,18 @@ uf::Collider::Manifold UF_API uf::AABBox::intersects( const uf::AABBox& b ) cons if ( a_front < b_back ) return manifold; if ( a_back > b_front ) return manifold; - + pod::Vector3* a_points = a.expand(); pod::Vector3* b_points = b.expand(); float smallest = 9E9; for ( uint b = 0; b < 8; b++ ) { for ( uint a = 0; a < 8; a++ ) { - float distance = uf::vector::distanceSquared(a_points[a], b_points[b]); - if ( smallest > distance ) smallest = distance; + float distance = uf::vector::distanceSquared(b_points[b], a_points[a]); + smallest = fmin( smallest, distance ); } } + delete[] a_points; delete[] b_points; @@ -225,6 +255,7 @@ uf::Collider::Manifold UF_API uf::AABBox::intersects( const uf::AABBox& b ) cons manifold.colliding = true; return manifold; +*/ } /* diff --git a/ext/world/craeture/craeture.cpp b/ext/world/craeture/craeture.cpp index 925fa428..1b5a2de3 100644 --- a/ext/world/craeture/craeture.cpp +++ b/ext/world/craeture/craeture.cpp @@ -36,6 +36,14 @@ void ext::Craeture::initialize() { const std::string& name = it->asString(); if ( this->m_animation.transforms.find(name) == this->m_animation.transforms.end() ) this->m_animation.transforms[name]; } + + /* Gravity */ { + if ( serializer["collision"]["gravity"] != Json::nullValue ) { + physics.linear.acceleration.x = serializer["collision"]["gravity"][0].asFloat(); + physics.linear.acceleration.y = serializer["collision"]["gravity"][1].asFloat(); + physics.linear.acceleration.z = serializer["collision"]["gravity"][2].asFloat(); + } + } } /* Collider */ { uf::CollisionBody& collider = this->getComponent(); @@ -51,12 +59,7 @@ void ext::Craeture::tick() { } this->animate(); - if ( uf::Window::isKeyPressed("P") ) { - pod::Transform<> transform = uf::transform::flatten(this->getComponent().getTransform()); - std::cout << transform.position.x << ", " << transform.position.y << ", " << transform.position.z << std::endl; - } - - /* Collision */ if ( this->m_parent ) { + /* Collision */ if ( this->m_parent && serializer["collision"]["should"].asBool() ) { uf::Entity& parent = this->getParent(); if ( this->hasComponent() && parent.hasComponent() ) { uf::CollisionBody& collider = this->getComponent(); @@ -64,27 +67,35 @@ void ext::Craeture::tick() { pod::Transform<>& transform = this->getComponent>(); { collider.clear(); - uf::Collider* box = new uf::AABBox( uf::vector::add({0, 1.5, 0}, transform.position), {0.5, 1.5, 0.5} ); + uf::Collider* box = new uf::AABBox( uf::vector::add({0, 1.5, 0}, transform.position), {0.7, 1.6, 0.7} ); collider.add(box); } pod::Physics& physics = this->getComponent(); auto result = pCollider.intersects(collider); uf::Collider::Manifold strongest; + strongest.depth = 0.001; + bool useStrongest = true; for ( auto manifold : result ) { if ( manifold.colliding && manifold.depth > 0 ) { if ( strongest.depth < manifold.depth ) strongest = manifold; - pod::Vector3 mag = uf::vector::normalize(manifold.normal * manifold.depth) * pod::Vector3{0.02, 0.0005, 0.02}; - transform.position += mag; - - // transform = physics.previous; + if ( !useStrongest ) { + pod::Vector3 correction = uf::vector::normalize(manifold.normal) * -(manifold.depth * manifold.depth * 1.001); + transform.position += correction; + if ( manifold.normal.x == 1 || manifold.normal.x == -1 ) physics.linear.velocity.x = 0; + if ( manifold.normal.y == 1 || manifold.normal.y == -1 ) physics.linear.velocity.y = 0; + if ( manifold.normal.z == 1 || manifold.normal.z == -1 ) physics.linear.velocity.z = 0; + } } } - if ( strongest.colliding && strongest.depth > 0 ) { - // std::cout << "Collision!\n\tNormal: " << strongest.normal.x << ", " << strongest.normal.y << ", " << strongest.normal.z << "\n\tDepth: " << strongest.depth << std::endl; - physics.linear.velocity = {0,0,0}; - } else { - physics.linear.acceleration = {0,-9.81,0}; + if ( useStrongest && strongest.colliding ) { + pod::Vector3 correction = uf::vector::normalize(strongest.normal) * -(strongest.depth * strongest.depth * 1.001); + transform.position += correction; + + // std::cout << "Collision! " << ( strongest.colliding ? "yes" : "no" ) << " " << strongest.normal.x << ", " << strongest.normal.y << ", " << strongest.normal.z << " / " << strongest.depth << std::endl; + if ( strongest.normal.x == 1 || strongest.normal.x == -1 ) physics.linear.velocity.x = 0; + if ( strongest.normal.y == 1 || strongest.normal.y == -1 ) physics.linear.velocity.y = 0; + if ( strongest.normal.z == 1 || strongest.normal.z == -1 ) physics.linear.velocity.z = 0; } } } diff --git a/ext/world/player/player.cpp b/ext/world/player/player.cpp index 5b41f0c9..8fc064a0 100644 --- a/ext/world/player/player.cpp +++ b/ext/world/player/player.cpp @@ -177,20 +177,84 @@ void ext::Player::tick() { struct { float move = uf::physics::time::delta * 4; float rotate = uf::physics::time::delta * 4; + float limitSquared = 4*4; } speed; uf::Camera& camera = this->getComponent(); pod::Transform<>& transform = this->getComponent>(); + pod::Physics& physics = this->getComponent(); uf::Serializer& serializer = this->getComponent(); + camera.update(true); + + bool floored = physics.linear.velocity.y == 0; if ( ::lockMouse ) { + /* if (uf::Window::isKeyPressed("W")) uf::transform::move( transform, transform.forward, speed.move ), updateCamera = walking = true; if (uf::Window::isKeyPressed("S")) uf::transform::move( transform, transform.forward, -speed.move ), updateCamera = walking = true; if (uf::Window::isKeyPressed("A")) uf::transform::move( transform, transform.right, -speed.move ), updateCamera = walking = true; if (uf::Window::isKeyPressed("D")) uf::transform::move( transform, transform.right, speed.move ), updateCamera = walking = true; - - if (uf::Window::isKeyPressed("Left")) uf::transform::rotate( transform, transform.up, -speed.rotate ), updateCamera = true; - if (uf::Window::isKeyPressed("Right")) uf::transform::rotate( transform, transform.up, speed.rotate ), updateCamera = true; + */ + if ( true || floored ) { + if (uf::Window::isKeyPressed("W")) { + float mag = uf::vector::magnitude(physics.linear.velocity * pod::Vector3{1, 0, 1}); + if ( mag < speed.limitSquared ) { + physics.linear.velocity += transform.forward * speed.move; + mag = uf::vector::magnitude(physics.linear.velocity); + } + pod::Vector3 correction = transform.forward * sqrt(mag); + physics.linear.velocity.x = correction.x; + physics.linear.velocity.z = correction.z; + updateCamera = walking = true; + } + if (uf::Window::isKeyPressed("S")) { + float mag = uf::vector::magnitude(physics.linear.velocity * pod::Vector3{1, 0, 1}); + if ( mag < speed.limitSquared ) { + physics.linear.velocity += transform.forward * -speed.move; + mag = uf::vector::magnitude(physics.linear.velocity); + } + pod::Vector3 correction = transform.forward * -sqrt(mag); + physics.linear.velocity.x = correction.x; + physics.linear.velocity.z = correction.z; + updateCamera = walking = true; + } + if (uf::Window::isKeyPressed("A")) { + float mag = uf::vector::magnitude(physics.linear.velocity * pod::Vector3{1, 0, 1}); + if ( mag < speed.limitSquared ) { + physics.linear.velocity += transform.right * -speed.move; + mag = uf::vector::magnitude(physics.linear.velocity); + } + pod::Vector3 correction = transform.right * -sqrt(mag); + physics.linear.velocity.x = correction.x; + physics.linear.velocity.z = correction.z; + updateCamera = walking = true; + } + if (uf::Window::isKeyPressed("D")) { + float mag = uf::vector::magnitude(physics.linear.velocity * pod::Vector3{1, 0, 1}); + if ( mag < speed.limitSquared ) { + physics.linear.velocity += transform.right * speed.move; + mag = uf::vector::magnitude(physics.linear.velocity); + } + pod::Vector3 correction = transform.right * sqrt(mag); + physics.linear.velocity.x = correction.x; + physics.linear.velocity.z = correction.z; + updateCamera = walking = true; + } + if ( floored && uf::Window::isKeyPressed(" ") ) { + if ( serializer["collision"]["jump"] != Json::nullValue ) { + if ( serializer["collision"]["jump"][0].asFloat() != 0 ) physics.linear.velocity.x = serializer["collision"]["jump"][0].asFloat(); + if ( serializer["collision"]["jump"][1].asFloat() != 0 ) physics.linear.velocity.y = serializer["collision"]["jump"][1].asFloat(); + if ( serializer["collision"]["jump"][2].asFloat() != 0 ) physics.linear.velocity.z = serializer["collision"]["jump"][2].asFloat(); + } + } + } + + if (uf::Window::isKeyPressed("Left")) { + uf::transform::rotate( transform, transform.up, -speed.rotate ), updateCamera = true; + } + if (uf::Window::isKeyPressed("Right")) { + uf::transform::rotate( transform, transform.up, speed.rotate ), updateCamera = true; + } if (uf::Window::isKeyPressed("LControl")) { if ( !::croutching ) deltaCrouch = true; @@ -214,23 +278,33 @@ void ext::Player::tick() { serializer["animation"]["status"]["rest"] = true; } - if ( walking ) { - uf::SoundEmitter& emitter = this->getComponent(); - int cycle = rand() % serializer["audio"]["footsteps"].size(); - std::string filename = serializer["audio"]["footsteps"][cycle].asString(); - uf::Audio& footstep = emitter.add(filename); + if ( floored ) { + if ( walking ) { + uf::SoundEmitter& emitter = this->getComponent(); + int cycle = rand() % serializer["audio"]["footsteps"].size(); + std::string filename = serializer["audio"]["footsteps"][cycle].asString(); + uf::Audio& footstep = emitter.add(filename); - bool playing = false; - for ( uint i = 0; i < serializer["audio"]["footsteps"].size(); ++i ) { - uf::Audio& audio = emitter.add(serializer["audio"]["footsteps"][i].asString()); - if ( audio.playing() ) playing = true; - } - if ( !playing ) { - footstep.play(); - footstep.setPosition( transform.position ); + bool playing = false; + for ( uint i = 0; i < serializer["audio"]["footsteps"].size(); ++i ) { + uf::Audio& audio = emitter.add(serializer["audio"]["footsteps"][i].asString()); + if ( audio.playing() ) playing = true; + } + if ( !playing ) { + footstep.play(); + footstep.setPosition( transform.position ); + } + } else { + physics.linear.velocity.x = 0; + physics.linear.velocity.y = 0; + physics.linear.velocity.z = 0; } } + // std::cout << physics.linear.velocity.x << ", " << physics.linear.velocity.y << ", " << physics.linear.velocity.z << std::endl; + // std::cout << physics.linear.acceleration.x << ", " << physics.linear.acceleration.y << ", " << physics.linear.acceleration.z << std::endl; + + /* Lock Mouse */ { static uf::Timer timer(false); if ( !timer.running() ) timer.start(); diff --git a/ext/world/terrain/generator.cpp b/ext/world/terrain/generator.cpp index bda46c6b..f2c21026 100644 --- a/ext/world/terrain/generator.cpp +++ b/ext/world/terrain/generator.cpp @@ -27,12 +27,12 @@ void ext::TerrainGenerator::destroy(){ if ( !this->m_voxels ) return; this->m_voxels = NULL; } void ext::TerrainGenerator::generate(){ if ( this->m_voxels ) return; +/* struct { ext::TerrainVoxel::uid_t floor = ext::TerrainVoxelFloor().uid(); ext::TerrainVoxel::uid_t wall = ext::TerrainVoxelWall().uid(); ext::TerrainVoxel::uid_t ceiling = ext::TerrainVoxelCeiling().uid(); } atlas; - this->m_voxels = new ext::TerrainVoxel::uid_t**[this->m_size.x]; for ( uint x = 0; x < this->m_size.x; ++x ) { this->m_voxels[x] = new ext::TerrainVoxel::uid_t*[this->m_size.y]; for ( uint y = 0; y < this->m_size.y; ++y ) { this->m_voxels[x][y] = new ext::TerrainVoxel::uid_t[this->m_size.z]; @@ -47,13 +47,145 @@ void ext::TerrainGenerator::generate(){ if ( this->m_voxels ) return; if ( z > 4 && z < this->m_size.z - 4 ) voxel = 0; } - // if ( x > 2 && x < 6 && z > 2 && z < 6 ) voxel = atlas.wall; - // if ( y > 3 ) voxel = 0; - this->m_voxels[x][y][z] = voxel; } } } +*/ + int lim_x = this->m_size.x; + int lim_y = this->m_size.y; + int lim_z = this->m_size.z; + if ( lim_x < 0 || lim_y < 0 || lim_z < 0 ) return; + + int half_x = lim_x / 2; + int half_y = lim_y / 2; + int half_z = lim_z / 2; + + struct { + ext::TerrainVoxel::uid_t floor = ext::TerrainVoxelFloor().uid();// = uf::World::atlas.getFromString("Regular Floor").getUid(); + ext::TerrainVoxel::uid_t wall = ext::TerrainVoxelWall().uid();// = uf::World::atlas.getFromString("Regular Wall").getUid(); + ext::TerrainVoxel::uid_t ceiling = ext::TerrainVoxelCeiling().uid();// = uf::World::atlas.getFromString("Regular Ceiling").getUid(); + ext::TerrainVoxel::uid_t stair = ext::TerrainVoxelStair().uid();// = uf::World::atlas.getFromString("Regular Stair").getUid(); + ext::TerrainVoxel::uid_t pillar = ext::TerrainVoxelPillar().uid();// = uf::World::atlas.getFromString("Regular Pillar").getUid(); + } atlas; + + this->m_voxels = new ext::TerrainVoxel::uid_t**[this->m_size.x]; + for ( uint x = 0; x < this->m_size.x; ++x ) { this->m_voxels[x] = new ext::TerrainVoxel::uid_t*[this->m_size.y]; + for ( uint y = 0; y < this->m_size.y; ++y ) { this->m_voxels[x][y] = new ext::TerrainVoxel::uid_t[this->m_size.z]; + } + } + + int rand_roomType = rand() % 6; + static bool first = false; + if ( !first ) rand_roomType = 0, first = true; + for ( int x = 0; x < lim_x; x++ ) { + for ( int y = 0; y < lim_y; y++ ) { + for ( int z = 0; z < lim_z; z++ ) { + if ( this->m_voxels == NULL || this->m_voxels[x] == NULL || this->m_voxels[x][y] == NULL ) break; + this->m_voxels[x][y][z] = 0; +// Wall +/* + if ((( x == 0 || x == 1 ) && ( z == lim_z - 1 || z == lim_z - 2 )) || + (( z == 0 || z == 1 ) && ( x == lim_x - 1 || x == lim_x - 2 ))) + this->m_voxels[x][y][z] = wall; +*/ +// Floor + if ( y == 0 ) this->m_voxels[x][y][z] = atlas.floor; +// Ceiling + if ( y + 1 == lim_y ) this->m_voxels[x][y][z] = rand() % 10 < 8.0f ? 0 : atlas.ceiling; + } + } + } + + switch ( rand_roomType ) { + // Wall-less room + case 2: break; + // Typical Room + default: + // North + for ( int y = 0; y < lim_y; y++ ) { + for ( int x = 0; x < lim_x; x++ ) { + float randed_wall = rand() % 10; + if ( !(y < 5 && x>half_x-3&&x 4.0f ) + this->m_voxels[x][y][lim_z-1] = atlas.wall; + else + this->m_voxels[x][y][lim_z-1] = 0; + } + } + // South + for ( int y = 0; y < lim_y; y++ ) { + for ( int x = 0; x < lim_x; x++ ) { + float randed_wall = rand() % 10; + if ( !(y < 5 && x>half_x-3&&x 4.0f ) + this->m_voxels[x][y][0] = atlas.wall; + else + this->m_voxels[x][y][0] = 0; + } + } + // East + for ( int y = 0; y < lim_y; y++ ) { + for ( int z = 0; z < lim_x; z++ ) { + float randed_wall = rand() % 10; + if ( !(y < 5 && z>half_z-3&&z 3.0f ) + this->m_voxels[lim_z-1][y][z] = atlas.wall; + else + this->m_voxels[lim_z-1][y][z] = 0; + } + } + for ( int x = 0; x < lim_x; x++ ) { + for ( int z = 0; z < lim_z; z++ ) { + this->m_voxels[x][0][z] = atlas.floor; + } + } + // Default Room + // Hole room + case 0: + for ( int x = half_x - (half_x / 2); x < half_x + (half_x / 2) + 1; x++ ) + for ( int z = half_z - (half_z / 2); z < half_z + (half_z / 2) + 1; z++ ) + if ( rand() % 10 > 1.25f ) this->m_voxels[x][0][z] = 0; + break; + // Pillar + case 3: + for ( int x = half_x - 1; x <= half_x + 1; x++ ) + for ( int z = half_z - 1; z <= half_z + 1; z++ ) + for ( int y = 0; y < lim_y; y++ ) + if ( rand() % 10 > 1.25f ) this->m_voxels[x][y][z] = atlas.pillar; + break; + // Stair room + case 4: + int randed_direction = rand() % 2; + int start_x = std::max(half_x - 3, 0); + int stop_x = std::min(half_x + 5, lim_x); + int start_z = -2; + int stop_z = 2; + int slope_x = lim_y / ( stop_x - start_x ); + int slope_z = lim_y / ( stop_z - start_z ); + + for ( int x = start_x; x <= stop_x; x++ ) + for ( int z = start_z; z <= stop_z; z++ ) + this->m_voxels[x][lim_y - 1][half_z - 1 + z] = 0; + switch ( randed_direction ) { + // Left to Right + case 0: + for ( int x = start_x; x <= stop_x; x++ ) { + int y = (x - start_x) * slope_x + 1; + for ( int z = start_z; z <= stop_z && y < lim_y; z++ ) + for ( int i = y; i >= y - slope_x && i >= 0; i-- ) + if ( rand() % stop_x > 3 ) this->m_voxels[x][i][half_z - 1 + z] = atlas.stair; + } + break; + // Right to Left + case 1: + for ( int x = start_x; x <= stop_x; x++ ) { + int y = (x - start_x) * slope_x + 3; + for ( int z = start_z; z <= stop_z && y < lim_y; z++ ) + for ( int i = y; i >= y - slope_x && i >= 0; i-- ) + if ( rand() % stop_x > 3 ) this->m_voxels[x][lim_y - 1 - i][half_z - 1 + z] = atlas.stair; + } + break; + } + break; + } } ext::TerrainVoxel::uid_t*** ext::TerrainGenerator::getVoxels() { return this->m_voxels; @@ -87,10 +219,10 @@ void ext::TerrainGenerator::rasterize( uf::Mesh& mesh, const ext::Region& region for ( uint y = 0; y < this->m_size.y; ++y ) { for ( uint z = 0; z < this->m_size.z; ++z ) { offset.position.x = x - (this->m_size.x / 2.0f); - offset.position.y = y; // - (this->m_size.y / 2.0f); + offset.position.y = y - (this->m_size.y / 2.0f); offset.position.z = z - (this->m_size.z / 2.0f); - const ext::TerrainVoxel& voxel = ext::TerrainVoxel::atlas(this->m_voxels[x][y][z]); + ext::TerrainVoxel voxel = ext::TerrainVoxel::atlas(this->m_voxels[x][y][z]); const ext::TerrainVoxel::Model& model = voxel.model(); if ( !voxel.opaque() ) continue; @@ -105,7 +237,7 @@ void ext::TerrainGenerator::rasterize( uf::Mesh& mesh, const ext::Region& region bool top = true, bottom = true, left = true, right = true, front = true, back = true; } should; struct { - ext::TerrainVoxel top, bottom, left, right, front, back; + ext::TerrainVoxel top = ext::TerrainVoxel::atlas(0), bottom = ext::TerrainVoxel::atlas(0), left = ext::TerrainVoxel::atlas(0), right = ext::TerrainVoxel::atlas(0), front = ext::TerrainVoxel::atlas(0), back = ext::TerrainVoxel::atlas(0); } neighbor; if ( x > 0 ) neighbor.left = ext::TerrainVoxel::atlas(this->m_voxels[x-1][y][z]); else if ( regions.left != NULL ) { diff --git a/ext/world/terrain/region.cpp b/ext/world/terrain/region.cpp index 24a93b43..49feb0fb 100644 --- a/ext/world/terrain/region.cpp +++ b/ext/world/terrain/region.cpp @@ -31,10 +31,21 @@ void ext::Region::load() { float r = (rand() % 100) / 100.0; bool addLight = r < metadata["region"]["light"]["random"].asFloat(); -// if ( metadata["region"]["location"][0].asInt() == 0 && metadata["region"]["location"][1].asInt() == 0 && metadata["region"]["location"][2].asInt() == 0 ) addLight = true; -// if ( metadata["region"]["location"][0].asInt() % 2 != 0 || metadata["region"]["location"][2].asInt() % 2 != 0 ) addLight = false; -// static bool first = false; if ( !first ) first = addLight = true; else addLight = false; -// addLight = false; + // Guarantee at least one light source (per XZ plane) + if ( !addLight ) { addLight = true; + ext::Terrain& parent = this->getParent(); + for ( const uf::Entity* pRegion : parent.getChildren() ) if ( pRegion->getName() == "Region" ) { + const pod::Transform<>& tRegion = pRegion->getComponent>(); + const pod::Transform<>& transform = this->getComponent>(); + if ( tRegion.position.y != transform.position.y ) continue; + for ( const uf::Entity* pLight : pRegion->getChildren() ) if ( pLight->getName() == "Light" ) { + addLight = false; + break; + } + if ( !addLight ) break; + } + } + if ( addLight ) { // std::cout << metadata["region"]["location"][0] << ", " << metadata["region"]["location"][1] << ", " << metadata["region"]["location"][2] << std::endl; pod::Vector3 color = { 1.0, 1.0, 1.0 }; { @@ -57,7 +68,7 @@ void ext::Region::load() { transform = uf::transform::initialize( transform ); transform.position = parent.position; - transform.position.y += size.y / 2; + // transform.position.y += size.y / 2; uf::transform::rotate( transform, transform.up, (360.0 / radius) * (3.1415926/180.0) * i ); entity->getComponent().update(true); ((ext::Light*)entity)->setColor( color ); @@ -74,7 +85,7 @@ void ext::Region::load() { transform = uf::transform::initialize( transform ); transform.position = parent.position; - transform.position.y += size.y / 2; + // transform.position.y += size.y / 2; uf::transform::rotate( transform, transform.right, 1.5708 * 1 ); entity->getComponent().setFov(120); entity->getComponent().update(true); @@ -92,7 +103,7 @@ void ext::Region::load() { transform = uf::transform::initialize( transform ); transform.position = parent.position; - transform.position.y += size.y / 2; + // transform.position.y += size.y / 2; uf::transform::rotate( transform, transform.right, 1.5708 * 3 ); entity->getComponent().setFov(120); entity->getComponent().update(true); @@ -100,29 +111,24 @@ void ext::Region::load() { } } - /* Collider */ { pod::Transform<>& transform = this->getComponent>(); uf::CollisionBody& collider = this->getComponent(); -/* - uf::Collider* box = new uf::AABBox( uf::vector::add({0, 1, 0}, transform.position), {0.5, 0.5, 0.5} ); - // uf::Collider* box = new uf::SphereCollider( 1, uf::vector::add({0, 1, 0}, transform.position) ); - collider.add(box); -*/ + auto*** voxels = generator.getVoxels(); for ( uint x = 0; x < size.x; ++x ) { for ( uint y = 0; y < size.y; ++y ) { for ( uint z = 0; z < size.z; ++z ) { pod::Vector3 offset = transform.position; offset.x += x - (size.x / 2.0f); - offset.y += y; // - (size.y / 2.0f); + offset.y += y - (size.y / 2.0f); offset.z += z - (size.z / 2.0f); - const ext::TerrainVoxel& voxel = ext::TerrainVoxel::atlas(voxels[x][y][z]); - + ext::TerrainVoxel voxel = ext::TerrainVoxel::atlas(voxels[x][y][z]); if ( !voxel.opaque() ) continue; uf::Collider* box = new uf::AABBox( offset, {0.5, 0.5, 0.5} ); + // uf::Collider* box = new uf::SphereCollider( 0.5f, offset ); collider.add(box); } } diff --git a/ext/world/terrain/terrain.cpp b/ext/world/terrain/terrain.cpp index 32497551..f6b5c999 100644 --- a/ext/world/terrain/terrain.cpp +++ b/ext/world/terrain/terrain.cpp @@ -32,9 +32,11 @@ void ext::Terrain::tick() { if ( !this->inBounds(location) ) { this->degenerate(location); + this->generate(); continue; } - + } + for ( uf::Entity* kv : this->m_children ) { // Defer generation to make sure all neighbors have their voxels generated std::vector queue; for ( uf::Entity* kv : this->m_children ) { if ( !kv ) continue; @@ -52,7 +54,6 @@ void ext::Terrain::tick() { generator.rasterize(mesh, *kv); } } - this->relocatePlayer(); } void ext::Terrain::render() { @@ -243,6 +244,10 @@ void ext::Terrain::degenerate( const pod::Vector3i& position ) { (int) (transform.position.z / size.z), }; if ( uf::vector::equals( location, position ) ) { + for ( uf::Entity* e : kv->getChildren() ) if ( e->getName() == "Player" ) { + return; + } + /* for ( uf::Entity* e : kv->getChildren() ) if ( e->getName() == "Player" ) { this->getRootParent().moveChild(*e); std::cout << "Emergency Provisions" << std::endl; @@ -255,10 +260,10 @@ void ext::Terrain::degenerate( const pod::Vector3i& position ) { }; recurse(&this->getRootParent(), 0); std::cout << "Emergency Provisions" << std::endl; } + */ delete kv; *it = NULL; this->m_children.erase(it); - // uf::iostream << "Degenerating Region @ ( " << position.x << ", " << position.y << ", " << position.z << ")" << "\n"; - this->generate(); + // this->generate(); break; } } diff --git a/ext/world/terrain/voxel.cpp b/ext/world/terrain/voxel.cpp index ebd0cb0d..043ca984 100644 --- a/ext/world/terrain/voxel.cpp +++ b/ext/world/terrain/voxel.cpp @@ -20,370 +20,450 @@ bool ext::TerrainVoxel::opaque() const { return this->m_opaque; } pod::Vector2ui ext::TerrainVoxel::uv() const { return this->m_uv; } ext::TerrainVoxel::uid_t ext::TerrainVoxel::uid() const { return this->m_uid; } ext::TerrainVoxel::Model ext::TerrainVoxel::model() const { -/* - model.position.left = { - -0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, 0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - }; - model.position.right = { - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - }; - model.position.bottom = { - 0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - }; - model.position.top = { - -0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - }; - model.position.back = { - 0.5f, -0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, 0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - 0.0f, 0.0f, 0.0f, - }; - model.position.front = { - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - - 0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - 0.0f, 0.0f, 0.0f, - }; - model.uv.left = { - 0.0f, 0.0f, - 0.0f, 1.0f, - 0.5f, 0.5f, - - 0.0f, 1.0f, - 1.0f, 1.0f, - 0.5f, 0.5f, - - 1.0f, 1.0f, - 1.0f, 0.0f, - 0.5f, 0.5f, - - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.5f, 0.5f, - }; - model.uv.right = { - 0.0f, 0.0f, - 0.0f, 1.0f, - 0.5f, 0.5f, - - 0.0f, 1.0f, - 1.0f, 1.0f, - 0.5f, 0.5f, - - 1.0f, 1.0f, - 1.0f, 0.0f, - 0.5f, 0.5f, - - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.5f, 0.5f, - }; - model.uv.bottom = { - 0.0f, 0.0f, - 0.0f, 1.0f, - 0.5f, 0.5f, - - 0.0f, 1.0f, - 1.0f, 1.0f, - 0.5f, 0.5f, - - 1.0f, 1.0f, - 1.0f, 0.0f, - 0.5f, 0.5f, - - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.5f, 0.5f, - }; - model.uv.top = { - 0.0f, 0.0f, - 0.0f, 1.0f, - 0.5f, 0.5f, - - 0.0f, 1.0f, - 1.0f, 1.0f, - 0.5f, 0.5f, - - 1.0f, 1.0f, - 1.0f, 0.0f, - 0.5f, 0.5f, - - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.5f, 0.5f, - }; - model.uv.back = { - 0.0f, 0.0f, - 0.0f, 1.0f, - 0.5f, 0.5f, - - 0.0f, 1.0f, - 1.0f, 1.0f, - 0.5f, 0.5f, - - 1.0f, 1.0f, - 1.0f, 0.0f, - 0.5f, 0.5f, - - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.5f, 0.5f, - }; - model.uv.front = { - 0.0f, 0.0f, - 0.0f, 1.0f, - 0.5f, 0.5f, - - 0.0f, 1.0f, - 1.0f, 1.0f, - 0.5f, 0.5f, - - 1.0f, 1.0f, - 1.0f, 0.0f, - 0.5f, 0.5f, - - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.5f, 0.5f, - }; -*/ ext::TerrainVoxel::Model model; - model.position.left = { - -0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - }; - model.position.right = { - 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - }; - model.position.top = { - 0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - }; - model.position.bottom = { - -0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, -0.5f, - }; - model.position.back = { - -0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - }; - model.position.front = { - 0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - }; - model.uv.left = { - 0.0f, 1.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, + if ( true ) { + model.position.left = { + -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + }; + model.position.right = { + 0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + }; + model.position.top = { + 0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, -0.5f, + }; + model.position.bottom = { + -0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, -0.5f, + }; + model.position.back = { + -0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + }; + model.position.front = { + 0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + }; + model.uv.left = { + 0.0f, 1.0f, + 1.0f, 1.0f, + 1.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 1.0f, - }; - model.uv.right = { - 0.0f, 1.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, + }; + model.uv.right = { + 0.0f, 1.0f, + 1.0f, 1.0f, + 1.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 1.0f, - }; - model.uv.bottom = { - 0.0f, 1.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, + }; + model.uv.bottom = { + 0.0f, 1.0f, + 1.0f, 1.0f, + 1.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 1.0f, - }; - model.uv.top = { - 0.0f, 1.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, + }; + model.uv.top = { + 0.0f, 1.0f, + 1.0f, 1.0f, + 1.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 1.0f, - }; - model.uv.back = { - 0.0f, 1.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, + }; + model.uv.back = { + 0.0f, 1.0f, + 1.0f, 1.0f, + 1.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 1.0f, - }; - model.uv.front = { - 0.0f, 1.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, + }; + model.uv.front = { + 0.0f, 1.0f, + 1.0f, 1.0f, + 1.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 1.0f, - }; + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, + }; - model.normal.left = { - -1.0f, 0.0f, 0.0f, - -1.0f, 0.0f, 0.0f, - -1.0f, 0.0f, 0.0f, - - -1.0f, 0.0f, 0.0f, - -1.0f, 0.0f, 0.0f, - -1.0f, 0.0f, 0.0f, - }; - model.normal.right = { - 1.0f, 0.0f, 0.0f, - 1.0f, 0.0f, 0.0f, - 1.0f, 0.0f, 0.0f, - - 1.0f, 0.0f, 0.0f, - 1.0f, 0.0f, 0.0f, - 1.0f, 0.0f, 0.0f, - }; - model.normal.top = { - 0.0f, 1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - - 0.0f, 1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - }; - model.normal.bottom = { - 0.0f,-1.0f, 0.0f, - 0.0f,-1.0f, 0.0f, - 0.0f,-1.0f, 0.0f, - - 0.0f,-1.0f, 0.0f, - 0.0f,-1.0f, 0.0f, - 0.0f,-1.0f, 0.0f, - }; - model.normal.back = { - 0.0f, 0.0f,-1.0f, - 0.0f, 0.0f,-1.0f, - 0.0f, 0.0f,-1.0f, - - 0.0f, 0.0f,-1.0f, - 0.0f, 0.0f,-1.0f, - 0.0f, 0.0f,-1.0f, - }; - model.normal.front = { - 0.0f, 0.0f, 1.0f, - 0.0f, 0.0f, 1.0f, - 0.0f, 0.0f, 1.0f, - - 0.0f, 0.0f, 1.0f, - 0.0f, 0.0f, 1.0f, - 0.0f, 0.0f, 1.0f, - }; + model.normal.left = { + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + }; + model.normal.right = { + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + }; + model.normal.top = { + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + }; + model.normal.bottom = { + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + }; + model.normal.back = { + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + }; + model.normal.front = { + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + }; + } else { + model.position.left = { + -0.5f, -0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, 0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + }; + model.position.right = { + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, 0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + }; + model.position.bottom = { + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + }; + model.position.top = { + -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + }; + model.position.back = { + 0.5f, -0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, 0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.0f, 0.0f, 0.0f, + }; + model.position.front = { + -0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + + -0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + + 0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + 0.0f, 0.0f, 0.0f, + }; + model.uv.left = { + 0.0f, 0.0f, + 0.0f, 1.0f, + 0.5f, 0.5f, + + 0.0f, 1.0f, + 1.0f, 1.0f, + 0.5f, 0.5f, + + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.5f, 0.5f, + + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.5f, 0.5f, + }; + model.uv.right = { + 0.0f, 0.0f, + 0.0f, 1.0f, + 0.5f, 0.5f, + + 0.0f, 1.0f, + 1.0f, 1.0f, + 0.5f, 0.5f, + + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.5f, 0.5f, + + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.5f, 0.5f, + }; + model.uv.bottom = { + 0.0f, 0.0f, + 0.0f, 1.0f, + 0.5f, 0.5f, + + 0.0f, 1.0f, + 1.0f, 1.0f, + 0.5f, 0.5f, + + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.5f, 0.5f, + + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.5f, 0.5f, + }; + model.uv.top = { + 0.0f, 0.0f, + 0.0f, 1.0f, + 0.5f, 0.5f, + + 0.0f, 1.0f, + 1.0f, 1.0f, + 0.5f, 0.5f, + + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.5f, 0.5f, + + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.5f, 0.5f, + }; + model.uv.back = { + 0.0f, 0.0f, + 0.0f, 1.0f, + 0.5f, 0.5f, + + 0.0f, 1.0f, + 1.0f, 1.0f, + 0.5f, 0.5f, + + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.5f, 0.5f, + + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.5f, 0.5f, + }; + model.uv.front = { + 0.0f, 0.0f, + 0.0f, 1.0f, + 0.5f, 0.5f, + + 0.0f, 1.0f, + 1.0f, 1.0f, + 0.5f, 0.5f, + + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.5f, 0.5f, + + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.5f, 0.5f, + }; + model.normal.left = { + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + + + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + }; + model.normal.right = { + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + }; + model.normal.top = { + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + }; + model.normal.bottom = { + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + 0.0f,-1.0f, 0.0f, + }; + model.normal.back = { + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + 0.0f, 0.0f,-1.0f, + }; + model.normal.front = { + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, + }; + } return model; } @@ -392,6 +472,8 @@ ext::TerrainVoxel::Model ext::TerrainVoxel::model() const { ext::TerrainVoxelFloor::TerrainVoxelFloor() : ext::TerrainVoxel( 1, true, {0, 1} ) {} ext::TerrainVoxelWall::TerrainVoxelWall() : ext::TerrainVoxel( 2, true, {1, 1} ) {} ext::TerrainVoxelCeiling::TerrainVoxelCeiling() : ext::TerrainVoxel( 3, true, {2, 1} ) {} +ext::TerrainVoxelPillar::TerrainVoxelPillar() : ext::TerrainVoxel( 4, true, {3, 0} ) {} +ext::TerrainVoxelStair::TerrainVoxelStair() : ext::TerrainVoxel( 5, true, {2, 0} ) {} // const std::vector& ext::TerrainVoxel::atlas() { @@ -401,10 +483,12 @@ const std::vector& ext::TerrainVoxel::atlas() { atlas.push_back( ext::TerrainVoxelFloor() ); atlas.push_back( ext::TerrainVoxelWall() ); atlas.push_back( ext::TerrainVoxelCeiling() ); + atlas.push_back( ext::TerrainVoxelPillar() ); + atlas.push_back( ext::TerrainVoxelStair() ); } return atlas; } -const ext::TerrainVoxel& ext::TerrainVoxel::atlas( ext::TerrainVoxel::uid_t uid ) { +ext::TerrainVoxel ext::TerrainVoxel::atlas( ext::TerrainVoxel::uid_t uid ) { std::vector atlas = ext::TerrainVoxel::atlas(); static ext::TerrainVoxel base; for ( std::vector::iterator it = atlas.begin(); it != atlas.end(); ++it ) { diff --git a/ext/world/terrain/voxel.h b/ext/world/terrain/voxel.h index 598e6231..4259c9c6 100644 --- a/ext/world/terrain/voxel.h +++ b/ext/world/terrain/voxel.h @@ -20,9 +20,9 @@ namespace ext { public: typedef uint uid_t; protected: - ext::TerrainVoxel::uid_t m_uid; - bool m_opaque; - pod::Vector2ui m_uv; + ext::TerrainVoxel::uid_t m_uid = 0; + bool m_opaque = false; + pod::Vector2ui m_uv = {0, 0}; public: struct Model { struct { std::vector left, right, top, bottom, front, back; } position; @@ -37,7 +37,7 @@ namespace ext { static pod::Vector2ui size(); static const std::vector& atlas(); - static const ext::TerrainVoxel& atlas( ext::TerrainVoxel::uid_t ); + static ext::TerrainVoxel atlas( ext::TerrainVoxel::uid_t ); virtual bool opaque() const; virtual pod::Vector2ui uv() const; @@ -57,4 +57,12 @@ namespace ext { public: TerrainVoxelCeiling(); }; + class EXT_API TerrainVoxelPillar : public ext::TerrainVoxel { + public: + TerrainVoxelPillar(); + }; + class EXT_API TerrainVoxelStair : public ext::TerrainVoxel { + public: + TerrainVoxelStair(); + }; } \ No newline at end of file