Initial commit
This commit is contained in:
commit
594276ad72
17 changed files with 12123 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
out/
|
||||
.vscode/
|
29
Makefile
Normal file
29
Makefile
Normal file
|
@ -0,0 +1,29 @@
|
|||
CC = gcc
|
||||
CPP = g++
|
||||
CFLAGS = -O2 -g -Wstrict-aliasing -Iinclude
|
||||
LDFLAGS = -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl -lm
|
||||
|
||||
OBJ = out/glad.o out/stb_image.o out/main.o out/world.o
|
||||
BIN = out/voksel
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: dirs $(BIN)
|
||||
|
||||
dirs:
|
||||
mkdir -p out
|
||||
|
||||
run: all
|
||||
./$(BIN)
|
||||
|
||||
$(BIN): $(OBJ)
|
||||
$(CPP) -o $(BIN) $^ $(LDFLAGS)
|
||||
|
||||
out/%.o: src/%.c
|
||||
$(CC) -o $@ -c $< $(CFLAGS)
|
||||
|
||||
out/%.o: src/%.cpp
|
||||
$(CPP) -o $@ -c $< $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -rf $(BIN) $(OBJ)
|
290
include/KHR/khrplatform.h
Normal file
290
include/KHR/khrplatform.h
Normal file
|
@ -0,0 +1,290 @@
|
|||
#ifndef __khrplatform_h_
|
||||
#define __khrplatform_h_
|
||||
|
||||
/*
|
||||
** Copyright (c) 2008-2018 The Khronos Group Inc.
|
||||
**
|
||||
** Permission is hereby granted, free of charge, to any person obtaining a
|
||||
** copy of this software and/or associated documentation files (the
|
||||
** "Materials"), to deal in the Materials without restriction, including
|
||||
** without limitation the rights to use, copy, modify, merge, publish,
|
||||
** distribute, sublicense, and/or sell copies of the Materials, and to
|
||||
** permit persons to whom the Materials are furnished to do so, subject to
|
||||
** the following conditions:
|
||||
**
|
||||
** The above copyright notice and this permission notice shall be included
|
||||
** in all copies or substantial portions of the Materials.
|
||||
**
|
||||
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||
*/
|
||||
|
||||
/* Khronos platform-specific types and definitions.
|
||||
*
|
||||
* The master copy of khrplatform.h is maintained in the Khronos EGL
|
||||
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
|
||||
* The last semantic modification to khrplatform.h was at commit ID:
|
||||
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
|
||||
*
|
||||
* Adopters may modify this file to suit their platform. Adopters are
|
||||
* encouraged to submit platform specific modifications to the Khronos
|
||||
* group so that they can be included in future versions of this file.
|
||||
* Please submit changes by filing pull requests or issues on
|
||||
* the EGL Registry repository linked above.
|
||||
*
|
||||
*
|
||||
* See the Implementer's Guidelines for information about where this file
|
||||
* should be located on your system and for more details of its use:
|
||||
* http://www.khronos.org/registry/implementers_guide.pdf
|
||||
*
|
||||
* This file should be included as
|
||||
* #include <KHR/khrplatform.h>
|
||||
* by Khronos client API header files that use its types and defines.
|
||||
*
|
||||
* The types in khrplatform.h should only be used to define API-specific types.
|
||||
*
|
||||
* Types defined in khrplatform.h:
|
||||
* khronos_int8_t signed 8 bit
|
||||
* khronos_uint8_t unsigned 8 bit
|
||||
* khronos_int16_t signed 16 bit
|
||||
* khronos_uint16_t unsigned 16 bit
|
||||
* khronos_int32_t signed 32 bit
|
||||
* khronos_uint32_t unsigned 32 bit
|
||||
* khronos_int64_t signed 64 bit
|
||||
* khronos_uint64_t unsigned 64 bit
|
||||
* khronos_intptr_t signed same number of bits as a pointer
|
||||
* khronos_uintptr_t unsigned same number of bits as a pointer
|
||||
* khronos_ssize_t signed size
|
||||
* khronos_usize_t unsigned size
|
||||
* khronos_float_t signed 32 bit floating point
|
||||
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
|
||||
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
|
||||
* nanoseconds
|
||||
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
|
||||
* khronos_boolean_enum_t enumerated boolean type. This should
|
||||
* only be used as a base type when a client API's boolean type is
|
||||
* an enum. Client APIs which use an integer or other type for
|
||||
* booleans cannot use this as the base type for their boolean.
|
||||
*
|
||||
* Tokens defined in khrplatform.h:
|
||||
*
|
||||
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
|
||||
*
|
||||
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
|
||||
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
|
||||
*
|
||||
* Calling convention macros defined in this file:
|
||||
* KHRONOS_APICALL
|
||||
* KHRONOS_APIENTRY
|
||||
* KHRONOS_APIATTRIBUTES
|
||||
*
|
||||
* These may be used in function prototypes as:
|
||||
*
|
||||
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
|
||||
* int arg1,
|
||||
* int arg2) KHRONOS_APIATTRIBUTES;
|
||||
*/
|
||||
|
||||
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
|
||||
# define KHRONOS_STATIC 1
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APICALL
|
||||
*-------------------------------------------------------------------------
|
||||
* This precedes the return type of the function in the function prototype.
|
||||
*/
|
||||
#if defined(KHRONOS_STATIC)
|
||||
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
|
||||
* header compatible with static linking. */
|
||||
# define KHRONOS_APICALL
|
||||
#elif defined(_WIN32)
|
||||
# define KHRONOS_APICALL __declspec(dllimport)
|
||||
#elif defined (__SYMBIAN32__)
|
||||
# define KHRONOS_APICALL IMPORT_C
|
||||
#elif defined(__ANDROID__)
|
||||
# define KHRONOS_APICALL __attribute__((visibility("default")))
|
||||
#else
|
||||
# define KHRONOS_APICALL
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIENTRY
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the return type of the function and precedes the function
|
||||
* name in the function prototype.
|
||||
*/
|
||||
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
|
||||
/* Win32 but not WinCE */
|
||||
# define KHRONOS_APIENTRY __stdcall
|
||||
#else
|
||||
# define KHRONOS_APIENTRY
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIATTRIBUTES
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the closing parenthesis of the function prototype arguments.
|
||||
*/
|
||||
#if defined (__ARMCC_2__)
|
||||
#define KHRONOS_APIATTRIBUTES __softfp
|
||||
#else
|
||||
#define KHRONOS_APIATTRIBUTES
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* basic type definitions
|
||||
*-----------------------------------------------------------------------*/
|
||||
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
|
||||
|
||||
|
||||
/*
|
||||
* Using <stdint.h>
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(__VMS ) || defined(__sgi)
|
||||
|
||||
/*
|
||||
* Using <inttypes.h>
|
||||
*/
|
||||
#include <inttypes.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
|
||||
|
||||
/*
|
||||
* Win32
|
||||
*/
|
||||
typedef __int32 khronos_int32_t;
|
||||
typedef unsigned __int32 khronos_uint32_t;
|
||||
typedef __int64 khronos_int64_t;
|
||||
typedef unsigned __int64 khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(__sun__) || defined(__digital__)
|
||||
|
||||
/*
|
||||
* Sun or Digital
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#if defined(__arch64__) || defined(_LP64)
|
||||
typedef long int khronos_int64_t;
|
||||
typedef unsigned long int khronos_uint64_t;
|
||||
#else
|
||||
typedef long long int khronos_int64_t;
|
||||
typedef unsigned long long int khronos_uint64_t;
|
||||
#endif /* __arch64__ */
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif 0
|
||||
|
||||
/*
|
||||
* Hypothetical platform with no float or int64 support
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#define KHRONOS_SUPPORT_INT64 0
|
||||
#define KHRONOS_SUPPORT_FLOAT 0
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
* Generic fallback
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Types that are (so far) the same on all platforms
|
||||
*/
|
||||
typedef signed char khronos_int8_t;
|
||||
typedef unsigned char khronos_uint8_t;
|
||||
typedef signed short int khronos_int16_t;
|
||||
typedef unsigned short int khronos_uint16_t;
|
||||
|
||||
/*
|
||||
* Types that differ between LLP64 and LP64 architectures - in LLP64,
|
||||
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
|
||||
* to be the only LLP64 architecture in current use.
|
||||
*/
|
||||
#ifdef _WIN64
|
||||
typedef signed long long int khronos_intptr_t;
|
||||
typedef unsigned long long int khronos_uintptr_t;
|
||||
typedef signed long long int khronos_ssize_t;
|
||||
typedef unsigned long long int khronos_usize_t;
|
||||
#else
|
||||
typedef signed long int khronos_intptr_t;
|
||||
typedef unsigned long int khronos_uintptr_t;
|
||||
typedef signed long int khronos_ssize_t;
|
||||
typedef unsigned long int khronos_usize_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_FLOAT
|
||||
/*
|
||||
* Float type
|
||||
*/
|
||||
typedef float khronos_float_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_INT64
|
||||
/* Time types
|
||||
*
|
||||
* These types can be used to represent a time interval in nanoseconds or
|
||||
* an absolute Unadjusted System Time. Unadjusted System Time is the number
|
||||
* of nanoseconds since some arbitrary system event (e.g. since the last
|
||||
* time the system booted). The Unadjusted System Time is an unsigned
|
||||
* 64 bit value that wraps back to 0 every 584 years. Time intervals
|
||||
* may be either signed or unsigned.
|
||||
*/
|
||||
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
|
||||
typedef khronos_int64_t khronos_stime_nanoseconds_t;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Dummy value used to pad enum types to 32 bits.
|
||||
*/
|
||||
#ifndef KHRONOS_MAX_ENUM
|
||||
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Enumerated boolean type
|
||||
*
|
||||
* Values other than zero should be considered to be true. Therefore
|
||||
* comparisons should not be made against KHRONOS_TRUE.
|
||||
*/
|
||||
typedef enum {
|
||||
KHRONOS_FALSE = 0,
|
||||
KHRONOS_TRUE = 1,
|
||||
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
|
||||
} khronos_boolean_enum_t;
|
||||
|
||||
#endif /* __khrplatform_h_ */
|
2129
include/glad/glad.h
Normal file
2129
include/glad/glad.h
Normal file
File diff suppressed because it is too large
Load diff
7762
include/stb_image.h
Normal file
7762
include/stb_image.h
Normal file
File diff suppressed because it is too large
Load diff
15
res/shaders/chunk.fs.glsl
Normal file
15
res/shaders/chunk.fs.glsl
Normal file
|
@ -0,0 +1,15 @@
|
|||
#version 330 core
|
||||
out vec4 fragColor;
|
||||
|
||||
in vec2 passTexCoord;
|
||||
|
||||
uniform sampler2D textureAtlas;
|
||||
|
||||
void main() {
|
||||
/*
|
||||
vec4 texColor = texture(textureAtlas, passTexCoord);
|
||||
if (texColor.a < 0.1) discard;
|
||||
fragColor = texColor;
|
||||
*/
|
||||
fragColor = vec4(0.9, 0.8, 0.7, 1.0);
|
||||
}
|
14
res/shaders/chunk.vs.glsl
Normal file
14
res/shaders/chunk.vs.glsl
Normal file
|
@ -0,0 +1,14 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 pos;
|
||||
layout (location = 1) in vec2 texCoord;
|
||||
|
||||
out vec2 passTexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main() {
|
||||
gl_Position = projection * view * model * vec4(pos, 1.0f);
|
||||
passTexCoord = texCoord;
|
||||
}
|
107
src/camera.h
Normal file
107
src/camera.h
Normal file
|
@ -0,0 +1,107 @@
|
|||
#pragma once
|
||||
|
||||
// stolen from learnopengl.com
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods
|
||||
enum CameraMovement {
|
||||
FORWARD,
|
||||
BACKWARD,
|
||||
LEFT,
|
||||
RIGHT
|
||||
};
|
||||
|
||||
// Default camera values
|
||||
const float YAW = -90.0f;
|
||||
const float PITCH = 0.0f;
|
||||
const float SPEED = 2.5f;
|
||||
const float SENSITIVITY = 0.1f;
|
||||
const float ZOOM = 90.0f;
|
||||
|
||||
|
||||
// An abstract camera class that processes input and calculates the corresponding Euler Angles, Vectors and Matrices for use in OpenGL
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
// camera Attributes
|
||||
glm::vec3 position;
|
||||
glm::vec3 front;
|
||||
glm::vec3 up;
|
||||
glm::vec3 right;
|
||||
glm::vec3 worldUp;
|
||||
// euler Angles
|
||||
float yaw;
|
||||
float pitch;
|
||||
// camera options
|
||||
float movementSpeed;
|
||||
float mouseSensitivity;
|
||||
float zoom;
|
||||
|
||||
// constructor with vectors
|
||||
explicit Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
float yaw = YAW, float pitch = PITCH) : position(position), worldUp(up), yaw(yaw), pitch(pitch),
|
||||
front(glm::vec3(0.0f, 0.0f, -1.0f)), movementSpeed(SPEED), mouseSensitivity(SENSITIVITY), zoom(ZOOM)
|
||||
{
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
// returns the view matrix calculated using Euler Angles and the LookAt Matrix
|
||||
glm::mat4 getViewMatrix() const {
|
||||
return glm::lookAt(position, position + front, up);
|
||||
}
|
||||
|
||||
// processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
|
||||
void processKeyboard(CameraMovement direction, double deltaTime)
|
||||
{
|
||||
float velocity = movementSpeed * deltaTime;
|
||||
if (direction == FORWARD)
|
||||
position += front * velocity;
|
||||
if (direction == BACKWARD)
|
||||
position -= front * velocity;
|
||||
if (direction == LEFT)
|
||||
position -= right * velocity;
|
||||
if (direction == RIGHT)
|
||||
position += right * velocity;
|
||||
}
|
||||
|
||||
// processes input received from a mouse input system. Expects the offset value in both the x and y direction.
|
||||
void processMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
|
||||
{
|
||||
xoffset *= mouseSensitivity;
|
||||
yoffset *= mouseSensitivity;
|
||||
|
||||
yaw += xoffset;
|
||||
pitch += yoffset;
|
||||
|
||||
// make sure that when pitch is out of bounds, screen doesn't get flipped
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (pitch > 89.0f)
|
||||
pitch = 89.0f;
|
||||
if (pitch < -89.0f)
|
||||
pitch = -89.0f;
|
||||
}
|
||||
|
||||
// update front, right and up Vectors using the updated Euler angles
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
private:
|
||||
// calculates the front vector from the Camera's (updated) Euler Angles
|
||||
void updateCameraVectors() {
|
||||
// calculate the new front vector
|
||||
glm::vec3 f;
|
||||
f.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
f.y = sin(glm::radians(pitch));
|
||||
f.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
front = glm::normalize(f);
|
||||
// also re-calculate the right and up vector
|
||||
right = glm::normalize(glm::cross(front, worldUp)); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
|
||||
up = glm::normalize(glm::cross(right, front));
|
||||
}
|
||||
};
|
1140
src/glad.c
Normal file
1140
src/glad.c
Normal file
File diff suppressed because it is too large
Load diff
128
src/main.cpp
Normal file
128
src/main.cpp
Normal file
|
@ -0,0 +1,128 @@
|
|||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <stb_image.h>
|
||||
#include <stdio.h>
|
||||
#include "types.h"
|
||||
#include "camera.h"
|
||||
#include "shader.h"
|
||||
#include "texture.h"
|
||||
#include "mesh.h"
|
||||
#include "world.h"
|
||||
|
||||
u32 windowWidth = 800;
|
||||
u32 windowHeight = 600;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
f32 lastX = windowWidth / 2.0f;
|
||||
f32 lastY = windowHeight / 2.0f;
|
||||
bool firstMouse = true;
|
||||
|
||||
// timing
|
||||
f32 deltaTime = 0.0f;
|
||||
f32 lastFrame = 0.0f;
|
||||
|
||||
void framebufferSizeCallback(GLFWwindow* window, int width, int height);
|
||||
void mouseCallback(GLFWwindow* window, double xPos, double yPos);
|
||||
void processInput(GLFWwindow *window);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
|
||||
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "voksel", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
printf("Failed to create GLFW window\n");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
|
||||
glfwSetCursorPosCallback(window, mouseCallback);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
printf("Failed to initialize GLAD\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
|
||||
Shader shader("../res/shaders/chunk.vs.glsl", "../res/shaders/chunk.fs.glsl");
|
||||
|
||||
World *world = new World();
|
||||
world->generateChunk(1, 1, 1)->buildMesh(world, 1, 1, 1);
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
f32 currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
processInput(window);
|
||||
glClearColor(0.0f, 0.6f, 0.6f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shader.use();
|
||||
shader.setInt("textureAtlas", 0);
|
||||
|
||||
glm::mat4 projection = glm::perspective(glm::radians(camera.zoom), (f32)windowWidth / (f32)windowHeight, 0.1f, 100.0f);
|
||||
shader.setMat4("projection", projection);
|
||||
|
||||
glm::mat4 view = camera.getViewMatrix();
|
||||
shader.setMat4("view", view);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
shader.setMat4("model", model);
|
||||
|
||||
world->render(&shader, nullptr);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
delete world;
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow *window) {
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.processKeyboard(FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
camera.processKeyboard(BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
camera.processKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.processKeyboard(RIGHT, deltaTime);
|
||||
}
|
||||
|
||||
void framebufferSizeCallback(GLFWwindow* window, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
windowWidth = width;
|
||||
windowHeight = height;
|
||||
}
|
||||
|
||||
void mouseCallback(GLFWwindow* window, double xPos, double yPos) {
|
||||
if (firstMouse) {
|
||||
lastX = xPos;
|
||||
lastY = yPos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
f32 xOffset = xPos - lastX;
|
||||
f32 yOffset = lastY - yPos; // reversed since y-coordinates go from bottom to top
|
||||
|
||||
lastX = xPos;
|
||||
lastY = yPos;
|
||||
|
||||
camera.processMouseMovement(xOffset, yOffset);
|
||||
}
|
46
src/mesh.h
Normal file
46
src/mesh.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include "types.h"
|
||||
|
||||
class Mesh {
|
||||
public:
|
||||
u32 vaoId;
|
||||
u32 vboId;
|
||||
u32 vertexCount;
|
||||
|
||||
Mesh(f32 *vertices, int size) {
|
||||
vertexCount = size / sizeof(f32) / 5;
|
||||
glGenVertexArrays(1, &vaoId);
|
||||
glGenBuffers(1, &vboId);
|
||||
|
||||
glBindVertexArray(vaoId);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vboId);
|
||||
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(f32), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(f32), (void*)(3 * sizeof(f32)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
delete[] vertices;
|
||||
}
|
||||
|
||||
void render() {
|
||||
glBindVertexArray(vaoId);
|
||||
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
|
||||
}
|
||||
|
||||
~Mesh() {
|
||||
// Delete the VBO
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDeleteBuffers(1, &vboId);
|
||||
|
||||
// Delete the VAO
|
||||
glBindVertexArray(0);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glDeleteVertexArrays(1, &vaoId);
|
||||
}
|
||||
};
|
191
src/shader.h
Normal file
191
src/shader.h
Normal file
|
@ -0,0 +1,191 @@
|
|||
#pragma once
|
||||
|
||||
// stolen from learnopengl.com
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int ID;
|
||||
// constructor generates the shader on the fly
|
||||
// ------------------------------------------------------------------------
|
||||
Shader(const char* vertexPath, const char* fragmentPath, const char* geometryPath = nullptr)
|
||||
{
|
||||
// 1. retrieve the vertex/fragment source code from filePath
|
||||
std::string vertexCode;
|
||||
std::string fragmentCode;
|
||||
std::string geometryCode;
|
||||
std::ifstream vShaderFile;
|
||||
std::ifstream fShaderFile;
|
||||
std::ifstream gShaderFile;
|
||||
// ensure ifstream objects can throw exceptions:
|
||||
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||
gShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||
try
|
||||
{
|
||||
// open files
|
||||
vShaderFile.open(vertexPath);
|
||||
fShaderFile.open(fragmentPath);
|
||||
std::stringstream vShaderStream, fShaderStream;
|
||||
// read file's buffer contents into streams
|
||||
vShaderStream << vShaderFile.rdbuf();
|
||||
fShaderStream << fShaderFile.rdbuf();
|
||||
// close file handlers
|
||||
vShaderFile.close();
|
||||
fShaderFile.close();
|
||||
// convert stream into string
|
||||
vertexCode = vShaderStream.str();
|
||||
fragmentCode = fShaderStream.str();
|
||||
// if geometry shader path is present, also load a geometry shader
|
||||
if(geometryPath != nullptr)
|
||||
{
|
||||
gShaderFile.open(geometryPath);
|
||||
std::stringstream gShaderStream;
|
||||
gShaderStream << gShaderFile.rdbuf();
|
||||
gShaderFile.close();
|
||||
geometryCode = gShaderStream.str();
|
||||
}
|
||||
}
|
||||
catch (std::ifstream::failure& e)
|
||||
{
|
||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
|
||||
}
|
||||
const char* vShaderCode = vertexCode.c_str();
|
||||
const char * fShaderCode = fragmentCode.c_str();
|
||||
// 2. compile shaders
|
||||
unsigned int vertex, fragment;
|
||||
// vertex shader
|
||||
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||
glCompileShader(vertex);
|
||||
checkCompileErrors(vertex, "VERTEX");
|
||||
// fragment Shader
|
||||
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
||||
glCompileShader(fragment);
|
||||
checkCompileErrors(fragment, "FRAGMENT");
|
||||
// if geometry shader is given, compile geometry shader
|
||||
unsigned int geometry;
|
||||
if(geometryPath != nullptr)
|
||||
{
|
||||
const char * gShaderCode = geometryCode.c_str();
|
||||
geometry = glCreateShader(GL_GEOMETRY_SHADER);
|
||||
glShaderSource(geometry, 1, &gShaderCode, NULL);
|
||||
glCompileShader(geometry);
|
||||
checkCompileErrors(geometry, "GEOMETRY");
|
||||
}
|
||||
// shader Program
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
if(geometryPath != nullptr)
|
||||
glAttachShader(ID, geometry);
|
||||
glLinkProgram(ID);
|
||||
checkCompileErrors(ID, "PROGRAM");
|
||||
// delete the shaders as they're linked into our program now and no longer necessery
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
if(geometryPath != nullptr)
|
||||
glDeleteShader(geometry);
|
||||
|
||||
}
|
||||
// activate the shader
|
||||
// ------------------------------------------------------------------------
|
||||
void use()
|
||||
{
|
||||
glUseProgram(ID);
|
||||
}
|
||||
// utility uniform functions
|
||||
// ------------------------------------------------------------------------
|
||||
void setBool(const std::string &name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setInt(const std::string &name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setFloat(const std::string &name, f32 value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setVec2(const std::string &name, const glm::vec2 &value) const
|
||||
{
|
||||
glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
|
||||
}
|
||||
void setVec2(const std::string &name, f32 x, f32 y) const
|
||||
{
|
||||
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setVec3(const std::string &name, const glm::vec3 &value) const
|
||||
{
|
||||
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
|
||||
}
|
||||
void setVec3(const std::string &name, f32 x, f32 y, f32 z) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setVec4(const std::string &name, const glm::vec4 &value) const
|
||||
{
|
||||
glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
|
||||
}
|
||||
void setVec4(const std::string &name, f32 x, f32 y, f32 z, f32 w)
|
||||
{
|
||||
glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setMat2(const std::string &name, const glm::mat2 &mat) const
|
||||
{
|
||||
glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setMat3(const std::string &name, const glm::mat3 &mat) const
|
||||
{
|
||||
glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
void setMat4(const std::string &name, const glm::mat4 &mat) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
|
||||
}
|
||||
|
||||
private:
|
||||
// utility function for checking shader compilation/linking errors.
|
||||
// ------------------------------------------------------------------------
|
||||
void checkCompileErrors(GLuint shader, std::string type)
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[1024];
|
||||
if(type != "PROGRAM")
|
||||
{
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if(!success)
|
||||
{
|
||||
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||
if(!success)
|
||||
{
|
||||
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
|
||||
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
2
src/stb_image.c
Normal file
2
src/stb_image.c
Normal file
|
@ -0,0 +1,2 @@
|
|||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
35
src/texture.h
Normal file
35
src/texture.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
u32 id;
|
||||
u32 width;
|
||||
u32 height;
|
||||
u32 nrChannels;
|
||||
|
||||
Texture(const char *texturePath, const int format) {
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
int width, height, nrChannels;
|
||||
unsigned char *data = stbi_load(texturePath, &width, &height, &nrChannels, 0);
|
||||
if (data) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
} else {
|
||||
printf("Failed to load texture\n");
|
||||
}
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
void bind(Shader *shader, char *name, u32 unit) {
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
shader->setInt(name, unit);
|
||||
}
|
||||
};
|
19
src/types.h
Normal file
19
src/types.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
typedef size_t usize;
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef int16_t i16;
|
||||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
|
||||
typedef float f32;
|
||||
typedef double f64;
|
163
src/world.cpp
Normal file
163
src/world.cpp
Normal file
|
@ -0,0 +1,163 @@
|
|||
#include "world.h"
|
||||
#include <cmath>
|
||||
|
||||
World::World() {
|
||||
|
||||
}
|
||||
|
||||
Chunk::Chunk() {
|
||||
|
||||
}
|
||||
|
||||
void Chunk::buildMesh(World *world, i32 chunkX, i32 chunkY, i32 chunkZ) {
|
||||
delete mesh;
|
||||
std::vector<glm::vec3> vert;
|
||||
std::vector<glm::vec2> uv;
|
||||
|
||||
for (i32 x = 0; x < CHUNK_SIZE; x++) {
|
||||
for (i32 y = 0; y < CHUNK_SIZE; y++) {
|
||||
for (i32 z = 0; z < CHUNK_SIZE; z++) {
|
||||
u32 b = blocks[x][y][z];
|
||||
if (b == 0) continue;
|
||||
u32 worldX = chunkX * CHUNK_SIZE + x;
|
||||
u32 worldY = chunkY * CHUNK_SIZE + y;
|
||||
u32 worldZ = chunkZ * CHUNK_SIZE + z;
|
||||
bool topVisible = world->getBlock(worldX, worldY + 1, worldZ) == 0;
|
||||
bool bottomVisible = world->getBlock(worldX, worldY - 1, worldZ) == 0;
|
||||
bool northVisible = world->getBlock(worldX + 1, worldY, worldZ) == 0;
|
||||
bool southVisible = world->getBlock(worldX - 1, worldY, worldZ) == 0;
|
||||
bool eastVisible = world->getBlock(worldX, worldY, worldZ + 1) == 0;
|
||||
bool westVisible = world->getBlock(worldX, worldY, worldZ - 1) == 0;
|
||||
auto pos = glm::vec3(x * 2, y * 2, z * 2);
|
||||
/*
|
||||
val topTexCoords = textureAtlas.getTexCoords(block.topTileX, block.topTileY)
|
||||
val sideTexCoords = textureAtlas.getTexCoords(block.sideTileX, block.sideTileY)
|
||||
val sideTexCoordsInverse = textureAtlas.getInverseTexCoords(block.sideTileX, block.sideTileY)
|
||||
val bottomTexCoords = textureAtlas.getInverseTexCoords(block.bottomTileX, block.bottomTileY)
|
||||
*/
|
||||
if (topVisible) {
|
||||
vert.push_back(pos + glm::vec3(-1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3(-1, 1, 1));
|
||||
vert.push_back(pos + glm::vec3( 1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3(-1, 1, 1));
|
||||
vert.push_back(pos + glm::vec3( 1, 1, 1));
|
||||
//uv.addAll(topTexCoords)
|
||||
}
|
||||
if (bottomVisible) {
|
||||
vert.push_back(pos + glm::vec3(-1, -1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, -1, -1));
|
||||
vert.push_back(pos + glm::vec3(-1, -1, 1));
|
||||
vert.push_back(pos + glm::vec3( 1, -1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, -1, 1));
|
||||
vert.push_back(pos + glm::vec3(-1, -1, 1));
|
||||
//uv.addAll(bottomTexCoords)
|
||||
}
|
||||
if (northVisible) {
|
||||
vert.push_back(pos + glm::vec3( 1, -1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, -1, 1));
|
||||
vert.push_back(pos + glm::vec3( 1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, 1, 1));
|
||||
vert.push_back(pos + glm::vec3( 1, -1, 1));
|
||||
//uv.addAll(sideTexCoordsInverse)
|
||||
}
|
||||
if (southVisible) {
|
||||
vert.push_back(pos + glm::vec3(-1, -1, -1));
|
||||
vert.push_back(pos + glm::vec3(-1, -1, 1));
|
||||
vert.push_back(pos + glm::vec3(-1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3(-1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3(-1, -1, 1));
|
||||
vert.push_back(pos + glm::vec3(-1, 1, 1));
|
||||
//uv.addAll(sideTexCoords)
|
||||
}
|
||||
if (eastVisible) {
|
||||
vert.push_back(pos + glm::vec3(-1, -1, 1)); // bottom left
|
||||
vert.push_back(pos + glm::vec3( 1, -1, 1)); // bottom right
|
||||
vert.push_back(pos + glm::vec3(-1, 1, 1)); // top left
|
||||
vert.push_back(pos + glm::vec3(-1, 1, 1)); // top left
|
||||
vert.push_back(pos + glm::vec3( 1, -1, 1)); // bottom right
|
||||
vert.push_back(pos + glm::vec3( 1, 1, 1)); // top right
|
||||
//uv.addAll(sideTexCoords)
|
||||
}
|
||||
if (westVisible) {
|
||||
vert.push_back(pos + glm::vec3(-1, -1, -1));
|
||||
vert.push_back(pos + glm::vec3(-1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, -1, -1));
|
||||
vert.push_back(pos + glm::vec3(-1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, 1, -1));
|
||||
vert.push_back(pos + glm::vec3( 1, -1, -1));
|
||||
//uv.addAll(sideTexCoordsInverse)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
auto buf = new f32[vert.size() * 5];
|
||||
for (int i = 0; i < vert.size(); i++) {
|
||||
buf[i * 5] = vert[i].x;
|
||||
buf[i * 5 + 1] = vert[i].y;
|
||||
buf[i * 5 + 2] = vert[i].z;
|
||||
}
|
||||
for (int i = 0; i < uv.size(); i++) {
|
||||
buf[i * 5 + 3] = uv[i].x;
|
||||
buf[i * 5 + 4] = uv[i].y;
|
||||
}
|
||||
this->mesh = new Mesh(&buf[0], vert.size() * 5 * sizeof(f32));
|
||||
}
|
||||
|
||||
Chunk* World::generateChunk(i32 chunkX, i32 chunkY, i32 chunkZ) {
|
||||
Chunk *c = new Chunk();
|
||||
int b = 0;
|
||||
if (chunkX == 1 && chunkY == 1 && chunkZ == 1) {
|
||||
b = 1;
|
||||
}
|
||||
for (i32 x = 0; x < CHUNK_SIZE; x++) {
|
||||
for (i32 y = 0; y < CHUNK_SIZE; y++) {
|
||||
for (i32 z = 0; z < CHUNK_SIZE; z++) {
|
||||
c->blocks[x][y][z] = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
chunks[(ivec3) { chunkX, chunkY, chunkZ }] = c;
|
||||
return c;
|
||||
}
|
||||
|
||||
Chunk* World::getChunk(i32 chunkX, i32 chunkY, i32 chunkZ) {
|
||||
auto x = chunks.find((ivec3) { chunkX, chunkY, chunkZ });
|
||||
Chunk *c;
|
||||
if (x == chunks.end())
|
||||
c = generateChunk(chunkX, chunkY, chunkZ);
|
||||
else c = x->second;
|
||||
return c;
|
||||
}
|
||||
|
||||
inline u32 World::getBlock(i32 x, i32 y, i32 z) {
|
||||
auto chunk = getChunk(x / CHUNK_SIZE, y / CHUNK_SIZE, z / CHUNK_SIZE);
|
||||
return chunk->blocks[x % CHUNK_SIZE][y % CHUNK_SIZE][z % CHUNK_SIZE];
|
||||
}
|
||||
|
||||
inline void World::setBlock(u32 block, i32 x, i32 y, i32 z, bool updateMeshes) {
|
||||
auto xRem = x % CHUNK_SIZE;
|
||||
auto yRem = y % CHUNK_SIZE;
|
||||
auto zRem = z % CHUNK_SIZE;
|
||||
auto chunkX = x / CHUNK_SIZE;
|
||||
auto chunkY = y / CHUNK_SIZE;
|
||||
auto chunkZ = z / CHUNK_SIZE;
|
||||
auto chunk = getChunk(chunkX, chunkY, chunkZ);
|
||||
chunk->blocks[xRem][yRem][zRem] = block;
|
||||
if (updateMeshes) {
|
||||
chunk->buildMesh(this, chunkX, chunkY, chunkZ);
|
||||
if (xRem == 0) getChunk(chunkX - 1, chunkY, chunkZ)->buildMesh(this, chunkX - 1, chunkY, chunkZ);
|
||||
if (xRem == CHUNK_SIZE - 1) getChunk(chunkX + 1, chunkY, chunkZ)->buildMesh(this, chunkX + 1, chunkY, chunkZ);
|
||||
if (yRem == 0) getChunk(chunkX, chunkY - 1, chunkZ)->buildMesh(this, chunkX, chunkY - 1, chunkZ);
|
||||
if (yRem == CHUNK_SIZE - 1) getChunk(chunkX, chunkY + 1, chunkZ)->buildMesh(this, chunkX, chunkY + 1, chunkZ);
|
||||
if (zRem == 0) getChunk(chunkX, chunkY, chunkZ - 1)->buildMesh(this, chunkX, chunkY, chunkZ - 1);
|
||||
if (zRem == CHUNK_SIZE - 1) getChunk(chunkX, chunkY, chunkZ + 1)->buildMesh(this, chunkX, chunkY, chunkZ + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void World::render(Shader *shader, Texture *textureAtlas) {
|
||||
shader->use();
|
||||
auto c = getChunk(1, 1, 1);
|
||||
if (c->mesh != nullptr) c->mesh->render();
|
||||
}
|
51
src/world.h
Normal file
51
src/world.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <glm/glm.hpp>
|
||||
#include "mesh.h"
|
||||
#include "shader.h"
|
||||
#include "texture.h"
|
||||
|
||||
#define CHUNK_SIZE 16
|
||||
|
||||
class World;
|
||||
|
||||
class Chunk {
|
||||
public:
|
||||
u32 blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
|
||||
Mesh *mesh = nullptr;
|
||||
|
||||
Chunk();
|
||||
|
||||
void buildMesh(World *world, i32 chunkX, i32 chunkY, i32 chunkZ);
|
||||
};
|
||||
|
||||
struct ivec3 {
|
||||
i32 x, y, z;
|
||||
friend bool operator==(const ivec3& a, const ivec3& b){
|
||||
return a.x == b.x && a.y == b.y && a.z == b.z;
|
||||
}
|
||||
};
|
||||
|
||||
namespace std {
|
||||
template<> struct hash<ivec3> {
|
||||
std::size_t operator()(const ivec3& k) const noexcept {
|
||||
return (k.x ^ (k.y << 1) ^ (k.z << 2)) >> 3;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
class World {
|
||||
public:
|
||||
std::unordered_map<ivec3, Chunk*> chunks;
|
||||
|
||||
World();
|
||||
|
||||
Chunk* generateChunk(i32 chunkX, i32 chunkY, i32 chunkZ);
|
||||
Chunk* getChunk(i32 chunkX, i32 chunkY, i32 chunkZ);
|
||||
inline u32 getBlock(i32 x, i32 y, i32 z);
|
||||
inline void setBlock(u32 block, i32 x, i32 y, i32 z, bool updateMeshes);
|
||||
|
||||
void render(Shader *shader, Texture *textureAtlas);
|
||||
};
|
Loading…
Reference in a new issue