Add camera pixel perfect.

This commit is contained in:
2024-09-16 21:35:04 -05:00
parent e3a4368d1e
commit 303d0c0a6f
8 changed files with 81 additions and 24 deletions

View File

@ -194,6 +194,25 @@ void SceneItemTransform::lookAt(
this->setWorldTransform(glm::lookAt(position, target, up)); this->setWorldTransform(glm::lookAt(position, target, up));
} }
float_t SceneItemTransform::lookAtPixelPerfect(
const glm::vec3 &position,
const glm::vec3 &look,
const float_t viewportHeight,
const float_t fov,
const float_t scale
) {
float_t z = (
tanf((glm::radians(180.0f) - fov) / 2.0f) *
(viewportHeight/2.0f)
) / scale;
this->lookAt(
glm::vec3(position.x, position.y, position.z + z),
look,
glm::vec3(0, 1, 0)
);
return z;
}
SceneItemTransform::~SceneItemTransform() { SceneItemTransform::~SceneItemTransform() {
std::for_each( std::for_each(
this->children.begin(), this->children.begin(),

View File

@ -171,6 +171,24 @@ namespace Dawn {
const glm::vec3 up const glm::vec3 up
); );
/**
* Shorthand combined for lookAt and perspectivePixelPerfectDistance
* to allow you to create pixel perfect lookAt camera view matricies.
*
* @param position Position of the camera. Z is for an offset.
* @param look Position in world space this transform looks at.
* @param viewportHeight Height of the viewport.
* @param fov Field of view (in radians).
* @return The Z distance that was calculated.
*/
float_t lookAtPixelPerfect(
const glm::vec3 &position,
const glm::vec3 &look,
const float_t viewportHeight,
const float_t fov,
const float_t scale = 1.0f
);
virtual ~SceneItemTransform(); virtual ~SceneItemTransform();
}; };
} }

View File

@ -13,10 +13,12 @@ void Player::updateCameraPosition() {
auto c = camera.lock(); auto c = camera.lock();
if(!c) return; if(!c) return;
glm::vec3 pos = this->getItem()->getLocalPosition(); glm::vec3 pos = this->getItem()->getLocalPosition();
c->getItem()->lookAt( c->getItem()->lookAtPixelPerfect(
pos + glm::vec3(0, 8, 1), pos + glm::vec3(0, 3, 1),
pos, pos,
glm::vec3(0, 1, 0) c->getRenderTarget()->getHeight(),
c->fov,
32.0f
); );
} }

View File

@ -29,23 +29,28 @@ void Map::entityNotifyDispose(std::shared_ptr<SceneItem> item) {
} }
void Map::onInit() { void Map::onInit() {
this->setSize(1, 1, 1);
} }
void Map::onDispose() { void Map::onDispose() {
} }
void Map::setSize( void Map::setSize(
const uint8_t width, const uint32_t width,
const uint8_t height, const uint32_t height,
const uint8_t depth const uint32_t depth,
const uint8_t chunkWidth,
const uint8_t chunkHeight,
const uint8_t chunkDepth
) { ) {
this->chunkOrder.clear(); this->chunkOrder.clear();
this->mapChunkWidth = width; this->mapWidth = width;
this->mapChunkHeight = height; this->mapHeight = height;
this->mapChunkDepth = depth; this->mapDepth = depth;
this->mapChunkCount = width * height * depth; this->mapChunkWidth = chunkWidth;
this->mapChunkHeight = chunkHeight;
this->mapChunkDepth = chunkDepth;
this->mapChunkCount = chunkWidth * chunkHeight * chunkDepth;
this->chunks = std::make_unique<std::shared_ptr<Chunk>[]>( this->chunks = std::make_unique<std::shared_ptr<Chunk>[]>(
this->mapChunkCount this->mapChunkCount
); );
@ -55,14 +60,16 @@ void Map::setSize(
this->chunkTopLeft.z = 0; this->chunkTopLeft.z = 0;
// Load initial chunks // Load initial chunks
for(auto z = 0; z < depth; z++) { for(auto z = 0; z < chunkDepth; z++) {
for(auto y = 0; y < height; y++) { for(auto y = 0; y < chunkHeight; y++) {
for(auto x = 0; x < width; x++) { for(auto x = 0; x < chunkWidth; x++) {
auto chunk = std::make_shared<Chunk>(); auto chunk = std::make_shared<Chunk>();
memset(chunk->tiles, 0, sizeof(chunk->tiles));
chunk->position.x = x; chunk->position.x = x;
chunk->position.y = y; chunk->position.y = y;
chunk->position.z = z; chunk->position.z = z;
this->chunks[x + y * width + z * width * height] = chunk; this->chunks[x + y * chunkWidth + z * chunkWidth * chunkHeight] = chunk;
this->chunkOrder.push_back(chunk); this->chunkOrder.push_back(chunk);
} }
} }

View File

@ -14,8 +14,9 @@ namespace Dawn {
private: private:
std::unordered_map<enum EntityID, std::weak_ptr<Entity>> entities; std::unordered_map<enum EntityID, std::weak_ptr<Entity>> entities;
uint32_t mapWidth, mapHeight, mapDepth;
uint8_t mapChunkWidth, mapChunkHeight, mapChunkDepth; uint8_t mapChunkWidth, mapChunkHeight, mapChunkDepth;
int32_t mapChunkCount; uint32_t mapChunkCount;
std::unique_ptr<std::shared_ptr<Chunk>[]> chunks; std::unique_ptr<std::shared_ptr<Chunk>[]> chunks;
std::vector<std::shared_ptr<Chunk>> chunkOrder; std::vector<std::shared_ptr<Chunk>> chunkOrder;
struct ChunkPosition chunkTopLeft; struct ChunkPosition chunkTopLeft;
@ -46,11 +47,17 @@ namespace Dawn {
* @param width The width of the map. * @param width The width of the map.
* @param height The height of the map. * @param height The height of the map.
* @param depth The depth of the map. * @param depth The depth of the map.
* @param chunkWidth The width of the maps chunks.
* @param chunkHeight The height of the maps chunks.
* @param chunkDepth The depth of the maps chunks.
*/ */
void setSize( void setSize(
const uint8_t width, const uint32_t width,
const uint8_t height, const uint32_t height,
const uint8_t depth const uint32_t depth,
const uint8_t chunkWidth,
const uint8_t chunkHeight,
const uint8_t chunkDepth
); );
/** /**

View File

@ -8,5 +8,6 @@
using namespace Dawn; using namespace Dawn;
bool_t Tile::isSolid() { bool_t Tile::isSolid() {
if(this->id == TileID::Null) return true;
return false; return false;
} }

View File

@ -12,6 +12,10 @@ namespace Dawn {
Test = 1 Test = 1
}; };
struct TilePosition {
uint8_t x, y, z;
};
union TileData { union TileData {
int64_t raw; int64_t raw;
}; };
@ -27,8 +31,4 @@ namespace Dawn {
*/ */
bool_t isSolid(); bool_t isSolid();
}; };
struct TilePosition {
uint8_t x, y, z;
};
} }

View File

@ -44,7 +44,10 @@ void Dawn::worldScene(Scene &s) {
auto world = createWorldPrefab(s); auto world = createWorldPrefab(s);
auto map = createMapPrefab(world.world); auto map = createMapPrefab(world.world);
map.map->setSize(3, 3, 3); map.map->setSize(
32, 32, 32,
3, 3, 3
);
auto chunk = map.map->getChunkAt({ 0, 0, 0 }); auto chunk = map.map->getChunkAt({ 0, 0, 0 });
chunk = map.map->getChunkAt({ 1, 0, 0 }); chunk = map.map->getChunkAt({ 1, 0, 0 });