Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions MinecraftYoutube/include/challenges/Cubes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
namespace MinecraftClone
{
struct Window;
struct Camera;

namespace Cubes
{
void init(const Window& window);
void init();
void destroy();

void update(float dt);
void update(const Camera& camera);
}
}

Expand Down
2 changes: 2 additions & 0 deletions MinecraftYoutube/include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/matrix_access.hpp>

// Use this for hash map and hash sets instead of the crappy std lib
#include <robin_hood.h>
Expand Down
20 changes: 20 additions & 0 deletions MinecraftYoutube/include/core/Application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef MINECRAFT_CLONE_APPLICATION_H
#define MINECRAFT_CLONE_APPLICATION_H

namespace MinecraftClone
{
struct Window;

namespace Application
{
bool init();
void run();
void free();

const Window& getWindow();

extern float deltaTime;
}
}

#endif
3 changes: 3 additions & 0 deletions MinecraftYoutube/include/core/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ namespace MinecraftClone
extern float mouseY;
extern float mouseScrollX;
extern float mouseScrollY;
extern float deltaMouseX;
extern float deltaMouseY;

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
void mouseCallback(GLFWwindow* window, double xpos, double ypos);
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
void mouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
void endFrame();

bool isKeyDown(int key);
bool isMouseButtonDown(int mouseButton);
Expand Down
2 changes: 2 additions & 0 deletions MinecraftYoutube/include/core/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace MinecraftClone
void installMainCallbacks();

void close();

inline float getAspectRatio() const { return (float)windowWidth / (float)windowHeight; }

static Window* createWindow(int width, int height, const char* title, bool fullScreenMode=false);
static void freeWindow(Window* window);
Expand Down
28 changes: 28 additions & 0 deletions MinecraftYoutube/include/gameplay/CharacterController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef MINECRAFT_CLONE_CHARACTER_CONTROLLER_H
#define MINECRAFT_CLONE_CHARACTER_CONTROLLER_H
#include "core.h"

namespace MinecraftClone
{
struct CharacterController
{
float controllerBaseSpeed;
float controllerRunSpeed;
float movementSensitivity;

glm::vec3 movementAxis;
glm::vec3 viewAxis;
bool isRunning;
bool lockedToCamera;
};

// TODO: Move this into physics library once it gets created in the physics episode
struct Rigidbody
{
glm::vec3 velocity;
glm::vec3 acceleration;
float friction;
};
}

#endif
16 changes: 16 additions & 0 deletions MinecraftYoutube/include/gameplay/CharacterSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef MINECRAFT_CLONE_CHARACTER_SYSTEM_H
#define MINECRAFT_CLONE_CHARACTER_SYSTEM_H

namespace MinecraftClone
{
struct Camera;
struct CharacterController;
struct Rigidbody;

namespace CharacterSystem
{
void update(CharacterController& controller, Camera& camera, Rigidbody& rb);
}
}

#endif
14 changes: 14 additions & 0 deletions MinecraftYoutube/include/gameplay/PlayerController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef MINECRAFT_CLONE_PLAYER_CONTROLLER_H
#define MINECRAFT_CLONE_PLAYER_CONTROLLER_H

namespace MinecraftClone
{
struct CharacterController;

namespace PlayerController
{
void update(CharacterController& controller);
}
}

#endif
34 changes: 34 additions & 0 deletions MinecraftYoutube/include/renderer/Camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef MINECRAFT_CLONE_CAMERA_H
#define MINECRAFT_CLONE_CAMERA_H
#include "core.h"

namespace MinecraftClone
{
struct Camera
{
// TODO: Replace this with an entity representing the
// camera that holds a Transform containing a position
glm::vec3 position;
glm::quat orientation;
// NOTE: Techinally the forward, right and up vectors
// can be obtained from orientation. However, we can
// cache them to avoid recomputing an entire matrix
// every time we need a directional vector.
glm::vec3 forward;
glm::vec3 right;
glm::vec3 up;
glm::mat4 projection;
glm::mat4 view;
float fov;

// This just calculates the view and then
// returns the member variable view
const glm::mat4& calculateViewMatrix();

// This just calculates projection and then
// returns member variable projection
const glm::mat4& calculateProjectionMatrix();
};
}

#endif
28 changes: 6 additions & 22 deletions MinecraftYoutube/src/challenges/Cubes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "core.h"
#include "core/Input.h"
#include "core/Window.h"
#include "core/Application.h"
#include "renderer/ShaderProgram.h"
#include "renderer/Camera.h"

namespace MinecraftClone
{
Expand Down Expand Up @@ -98,8 +100,6 @@ namespace MinecraftClone
static std::vector<glm::vec3> cubePositions;
static std::vector<int> cubeTextures;

static glm::mat4 projection;

void createDefaultCube()
{
// Define the 8 unique positions for a cube according to the diagram above
Expand Down Expand Up @@ -243,7 +243,7 @@ namespace MinecraftClone
glDeleteTextures(1, &texture.textureId);
}

void init(const Window& window)
void init()
{
if (!texturedCubeShader.compileAndLink("assets/shaders/vertex/cube.glsl", "assets/shaders/fragment/cube.glsl"))
{
Expand All @@ -253,12 +253,6 @@ namespace MinecraftClone

createDefaultCube();

float windowAspect = ((float)window.windowWidth / (float)window.windowHeight);
float fov = 70.0f;
float zNear = 0.1f;
float zFar = 10'000.0f;
projection = glm::perspective(fov, windowAspect, zNear, zFar);

for (auto& filepath : std::filesystem::directory_iterator("assets/images"))
{
if (filepath.path().stem().string() != "normal")
Expand Down Expand Up @@ -299,22 +293,12 @@ namespace MinecraftClone
}
}

void update(float dt)
void update(const Camera& camera)
{
texturedCubeShader.bind();

// Rotate the eye a little bit every frame
static glm::vec3 eye = glm::vec3();
static float eyeRotation = 45.0f;
if (!Input::isKeyDown(GLFW_KEY_SPACE))
eyeRotation += 30.0f * dt;
eye = glm::vec3(glm::sin(glm::radians(eyeRotation)) * 7.0f, 5.0f, glm::cos(glm::radians(eyeRotation)) * 7.0f);

glm::vec3 center = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 view = glm::lookAt(eye, center, up);
texturedCubeShader.uploadMat4("uView", view);
texturedCubeShader.uploadMat4("uProjection", projection);
texturedCubeShader.uploadMat4("uView", camera.view);
texturedCubeShader.uploadMat4("uProjection", camera.projection);

// NOTE: This texture is not required for the challenges
glActiveTexture(GL_TEXTURE0 + 1);
Expand Down
Loading