Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukitsu committed Jun 4, 2024
1 parent 659949e commit 9128909
Show file tree
Hide file tree
Showing 27 changed files with 258 additions and 251 deletions.
2 changes: 2 additions & 0 deletions Jukcraft/Jukcraft.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<ClInclude Include="src\models\Model.h" />
<ClInclude Include="src\pch.h" />
<ClInclude Include="src\physics\constants.h" />
<ClInclude Include="src\renderer\chunk\RenderRegion.h" />
<ClInclude Include="src\renderer\Renderer.h" />
<ClInclude Include="src\renderer\chunk\Mesh.h" />
<ClInclude Include="src\renderer\gfx\buffers\DynamicBuffer.h" />
Expand Down Expand Up @@ -192,6 +193,7 @@
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="src\renderer\chunk\Mesh.cpp" />
<ClCompile Include="src\renderer\chunk\RenderRegion.cpp" />
<ClCompile Include="src\renderer\gfx\buffers\DynamicBuffer.cpp" />
<ClCompile Include="src\vendor\stb\stb_build.cpp" />
<ClCompile Include="src\world\LightEngine.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions Jukcraft/Jukcraft.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<ClInclude Include="src\world\chunk\util.h">
<Filter>world\chunk</Filter>
</ClInclude>
<ClInclude Include="src\renderer\chunk\RenderRegion.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\core\App.cpp">
Expand Down Expand Up @@ -188,5 +189,6 @@
<ClCompile Include="src\world\chunk\ChunkManager.cpp">
<Filter>world\chunk</Filter>
</ClCompile>
<ClCompile Include="src\renderer\chunk\RenderRegion.cpp" />
</ItemGroup>
</Project>
7 changes: 4 additions & 3 deletions Jukcraft/assets/shaders/terrain/vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ void main(void) {
float blocklightMultiplier = pow(0.8, 15.0 - blockLight);
float intermediateSkylightMultiplier = pow(0.8, 15.0 - skyLight * u_Daylight);
float skylightMultiplier = pow(0.8, 15.0 - skyLight);
float skylightBlueTint = pow(0.8, 15.0 - skyLight * (2.0 - pow(u_Daylight, 2)));

float shading = float((a_VertexData & 0x3) + 2) / 5.0f;

gl_Position = u_CameraTransforms * vec4(pos, 1.0f);

vs_Out.v_TexCoords = vec3(c_TexCoords[a_VertexData >> 10 & 0x3], a_VertexData >> 2 & 0xFF);
vs_Out.v_Light = vec3(
clamp(blocklightMultiplier * (1.0 + 0.5 * (1.0 - u_Daylight)), intermediateSkylightMultiplier, 1.0),
clamp(blocklightMultiplier * (1.0 + 0.25 * (1.0 - u_Daylight)), intermediateSkylightMultiplier, 1.0),
clamp(skylightMultiplier * u_Daylight, blocklightMultiplier, 1.0)
clamp(blocklightMultiplier * 1.5, intermediateSkylightMultiplier, 1.0),
clamp(blocklightMultiplier * 1.25, intermediateSkylightMultiplier, 1.0),
clamp(skylightBlueTint, blocklightMultiplier, 1.0)
);
vs_Out.v_Shading = shading * ao;
}
16 changes: 7 additions & 9 deletions Jukcraft/src/core/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,25 @@ namespace Jukcraft {
void App::run() {

float lastTime = static_cast<float>(glfwGetTime()), timer = lastTime;
float deltaTimePerTick = 0.0f, renderDeltaTime = 0.0f, nowTime = 0.0f;
float partialTicks = 0.0f, nowTime = 0.0f;
int frames = 0, updates = 0;

float deltaTime = 0.0f;
while (!window->shouldClose()) {

nowTime = static_cast<float>(glfwGetTime());
renderDeltaTime = (nowTime - lastTime);
deltaTimePerTick += (nowTime - lastTime) * TICK_RATE;
partialTicks += (nowTime - lastTime) * TICK_RATE;
lastTime = nowTime;

// - Only update at 60 tick / s
while (deltaTimePerTick >= 1.0) {
deltaTime = deltaTimePerTick / TICK_RATE;
game->tick(deltaTime); // - Update function
// - Only update at 64 tick / s
while (partialTicks >= 1.0) {
game->tick(); // - Update function
updates++;
deltaTimePerTick--;
partialTicks--;

}

game->renderNewFrame(renderDeltaTime);
game->renderNewFrame(partialTicks);
frames++;

window->endFrame();
Expand Down
15 changes: 5 additions & 10 deletions Jukcraft/src/core/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ namespace Jukcraft {
}


void Camera::update(const float deltaTime) {
glm::vec3 interpolatedPos = glm::mix(player->position, player->oldPosition, player->interpolationStep);
player->interpolationStep -= deltaTime;
void Camera::update(const float partialTicks) {
glm::vec3 interpolatedPos = glm::mix(player->oldPosition, player->position, partialTicks);

const glm::vec2 cursorPos = App::GetWindow().getCursorPos();
const glm::vec2 deltaCursor = cursorPos - lastCursorPos;
Expand All @@ -44,18 +43,14 @@ namespace Jukcraft {
if (App::GetWindow().isKeyPressed(GLFW_KEY_LEFT_SHIFT)) input.y += -1;
if (App::GetWindow().isKeyPressed(GLFW_KEY_SPACE)) input.y += 1;

if (deltaTime * 20.0f > 1.0f)
speed = WALK_SPEED;
else
speed += (WALK_SPEED - speed) * deltaTime * 20;

speed = WALK_SPEED;


if (input.y > 0) {
player->jump();
} if (input.x || input.z) {
const float angle = player->yaw - glm::atan<float>((float)input.z, (float)input.x) + glm::pi<float>() / 2;
player->acceleration.x = glm::cos(angle) * speed;
player->acceleration.z = glm::sin(angle) * speed;
player->setRelativeAccel(glm::vec3((float)input.x * speed, 0.0f, (float)input.z * speed));
}


Expand Down
12 changes: 6 additions & 6 deletions Jukcraft/src/core/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Jukcraft {
}

}
void Game::hitCallback(int button, const glm::vec3& currentBlock, const glm::vec3& nextBlock) {
void Game::hitCallback(int button, const BlockPos& currentBlock, const BlockPos& nextBlock) {
switch (button) {
case GLFW_MOUSE_BUTTON_LEFT:
world->setBlock(nextBlock, 0);
Expand All @@ -56,13 +56,13 @@ namespace Jukcraft {
}


void Game::tick(const float deltaTime) {
world->tick(deltaTime);
player->tick(deltaTime);
void Game::tick() {
world->tick();
player->tick();
}

void Game::renderNewFrame(const float deltaTime) {
camera.update(deltaTime);
void Game::renderNewFrame(const float partialTicks) {
camera.update(partialTicks);

Renderer::Begin(world->getSkyColor());
world->render();
Expand Down
4 changes: 2 additions & 2 deletions Jukcraft/src/core/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Jukcraft {
class Game {
public:
Game();
void hitCallback(int button, const glm::vec3& currentBlock, const glm::vec3& nextBlock);
void tick(const float deltaTime);
void hitCallback(int button, const BlockPos& currentBlock, const BlockPos& nextBlock);
void tick();
void renderNewFrame(const float deltaTime);
void onMousePress(int button);
void speedTime() {
Expand Down
3 changes: 1 addition & 2 deletions Jukcraft/src/core/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ namespace Jukcraft {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
context = glfwCreateWindow(window.getWidth(), window.getHeight(), "", nullptr, window.handle);
context = glfwCreateWindow(window.getWidth(), window.getHeight(), "New Context", nullptr, window.handle);
}
~SharedContext() {
glfwMakeContextCurrent(window.handle);
Expand Down
39 changes: 37 additions & 2 deletions Jukcraft/src/core/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,49 @@ namespace Jukcraft {
uint32_t baseInstance;
};

constexpr uint8_t CHUNK_DIM = 16;
constexpr uint8_t CHUNK_HEIGHT = 128;

constexpr uint8_t WORLD_SIZE = 2;


struct PerChunkData {
glm::vec3 chunkPos;
};

struct BlockPos : public glm::ivec3 {

BlockPos() :glm::ivec3() {}
BlockPos(const glm::ivec3& vec) :glm::ivec3() {}
BlockPos(glm::ivec3&& vec) :glm::ivec3(vec) {}
BlockPos(int x, int y, int z) :glm::ivec3(x, y, z) {}
BlockPos(int scalar) :glm::ivec3(scalar) {}
BlockPos(const glm::ivec2& chunkPos, const glm::ivec3& localPos)
:glm::ivec3((int)CHUNK_DIM * glm::ivec3(chunkPos.x, 0, chunkPos.y) + localPos)
{

}

constexpr glm::ivec2 getChunkPos() const noexcept {
return { glm::floor((float)x / CHUNK_DIM), glm::floor((float)z / CHUNK_DIM) };
}

constexpr glm::ivec3 getLocalPos() const noexcept {
return { glm::mod((float)x, (float)CHUNK_DIM), y, glm::mod((float)z, (float)CHUNK_DIM) };
}

};



constexpr glm::vec3 EAST = glm::vec3(1.0f, 0.0f, 0.0f);
constexpr glm::vec3 WEST = glm::vec3(-1.0f, 0.0f, 0.0f);
constexpr glm::vec3 UP = glm::vec3(0.0f, 1.0f, 0.0f);
constexpr glm::vec3 DOWN = glm::vec3(0.0f, -1.0f, 0.0f);
constexpr glm::vec3 SOUTH = glm::vec3(0.0f, 0.0f, 1.0f);
constexpr glm::vec3 NORTH = glm::vec3(0.0f, 0.0f, -1.0f);

constexpr float TICK_RATE = 60.0f;
constexpr float TICK_RATE = 64.0f;

constexpr uint8_t EAST_INDEX = 0;
constexpr uint8_t WEST_INDEX = 1;
Expand Down Expand Up @@ -91,7 +126,7 @@ namespace Jukcraft {
}

template<typename T>
int sign(T x) {
constexpr int sign(T x) {
return (x > 0) - (x <= 0);
}
}
21 changes: 16 additions & 5 deletions Jukcraft/src/entity/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,34 @@ namespace Jukcraft {
}
Entity(const glm::vec3& initialPos, const glm::vec3& initialVelocity,
const glm::vec3& initialAcceleration, float initialYaw, float initialPitch)
:position(initialPos), velocity(initialVelocity), acceleration(initialAcceleration),
:position(initialPos), velocity(initialVelocity), relativeAccel(initialAcceleration),
yaw(initialYaw), pitch(initialPitch) {

}
virtual ~Entity() {}

virtual void tick(float delta_time) = 0;
virtual void tick() = 0;
virtual void applyPhysics() = 0;
virtual void aiStep() = 0;
virtual void move(const glm::vec3& motion) = 0;
virtual void push(const glm::vec3& motion) = 0;
virtual void render(float deltaTime) {}

constexpr const glm::vec3& getPos() const { return position; }
constexpr const glm::vec3& getVelocity() const { return position; }
constexpr const glm::vec3& getAccel() const { return position; }
constexpr const glm::vec3& getVelocity() const { return velocity; }
constexpr const glm::vec3& getRelativeAccel() const { return relativeAccel; }
constexpr float getYaw() const { return yaw; }
constexpr float getPitch() const { return pitch; }

void setPos(const glm::vec3& pos) { position = pos; }
void setVelocity(const glm::vec3& v) { velocity = v; }
void setRelativeAccel(const glm::vec3& a) { relativeAccel = a; }
void setYaw(float theta) { yaw = theta; }
void setPitch(float phi) { pitch = phi; }
protected:
glm::vec3 position;
glm::vec3 velocity;
glm::vec3 acceleration;
glm::vec3 relativeAccel;
float yaw, pitch;
};
}
23 changes: 8 additions & 15 deletions Jukcraft/src/entity/HitRay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Jukcraft {
}

bool HitRay::check(int button, HitCallback callback,
float distance, const glm::vec3& currentBlock, const glm::vec3& nextBlock) {
float distance, const BlockPos& currentBlock, const BlockPos& nextBlock) {

if (world.getBlock(nextBlock)) {
callback(button, currentBlock, nextBlock);
Expand All @@ -26,20 +26,13 @@ namespace Jukcraft {
return false;
}
}
bool HitRay::step(int button, HitCallback callback) {
glm::ivec3 sign = glm::ivec3(1, 1, 1);
glm::vec3 localPos = position - (glm::vec3)block;

glm::vec3 absoluteVector = vector;
bool HitRay::step(int button, HitCallback callback) {
glm::ivec3 sign = glm::sign(vector);
glm::vec3 localPos = (glm::vec3)sign * (position - (glm::vec3)block);

for (uint8_t component = 0; component < 3; component++) {
if (vector[component] < 0) {
sign[component] = -1;
glm::vec3 absoluteVector = glm::abs(vector);

absoluteVector[component] = -absoluteVector[component];
localPos[component] = -localPos[component];
}
}

float vx = absoluteVector.x, vy = absoluteVector.y, vz = absoluteVector.z;
float lx = localPos.x, ly = localPos.y, lz = localPos.z;
Expand All @@ -52,7 +45,7 @@ namespace Jukcraft {

if (y >= -0.5f && y <= 0.5f && z >= -0.5f && z <= 0.5f) {
float distance = glm::distance(glm::vec3(x, y, z), localPos);
return check(button, callback, distance, block, glm::vec3(block.x + sign.x, block.y, block.z));
return check(button, callback, distance, block, glm::ivec3(block.x + sign.x, block.y, block.z));
}
}

Expand All @@ -63,7 +56,7 @@ namespace Jukcraft {

if (x >= -0.5f && x <= 0.5f && z >= -0.5f && z <= 0.5f) {
float distance = glm::distance(glm::vec3(x, y, z), localPos);
return check(button, callback, distance, block, glm::vec3(block.x, block.y + sign.y, block.z));
return check(button, callback, distance, block, glm::ivec3(block.x, block.y + sign.y, block.z));
}
}

Expand All @@ -74,7 +67,7 @@ namespace Jukcraft {

if (x >= -0.5f && x <= 0.5f && y >= -0.5f && y <= 0.5f) {
float distance = glm::distance(glm::vec3(x, y, z), localPos);
return check(button, callback, distance, block, glm::vec3(block.x, block.y, block.z + sign.z));
return check(button, callback, distance, block, glm::ivec3(block.x, block.y, block.z + sign.z));
}
}

Expand Down
6 changes: 3 additions & 3 deletions Jukcraft/src/entity/HitRay.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@


namespace Jukcraft {
using HitCallback = std::function<void(int, const glm::vec3&, const glm::vec3&)>;
using HitCallback = std::function<void(int, const BlockPos&, const BlockPos&)>;

struct HitRay {
HitRay(World& world, LivingEntity& entity);
bool check(int button, HitCallback callback,
float distance, const glm::vec3& currentBlock, const glm::vec3& nextBlock);
float distance, const BlockPos& currentBlock, const BlockPos& nextBlock);
bool step(int button, HitCallback callback);

World& world;
glm::vec3 vector;
glm::vec3 position;
glm::ivec3 block;
BlockPos block;
float distance;
};

Expand Down
Loading

0 comments on commit 9128909

Please sign in to comment.