i forgor what i did

This commit is contained in:
Tunacan 2021-08-31 16:26:46 +03:00
parent afc246141c
commit c2e3d555bb
7 changed files with 83 additions and 33 deletions

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
out/ out/
.vscode/ .vscode/
voksel voksel
voksel.exe

31
WindowsMakefile Normal file
View file

@ -0,0 +1,31 @@
CC = x86_64-w64-mingw32-gcc
CPP = x86_64-w64-mingw32-g++
STRIP = x86_64-w64-mingw32-strip
CFLAGS = -O2 -g -Wstrict-aliasing -Iinclude
LDFLAGS = -static -lglfw3 -lopengl32 -lgdi32 -luser32 -lkernel32 -lssp -lnoise
OBJ = out/glad.o out/stb_image.o out/world.o out/main.o
BIN = voksel.exe
.PHONY: all clean
all: dirs $(BIN)
dirs:
mkdir -p out
run: all
./$(BIN)
$(BIN): $(OBJ)
$(CPP) -o $(BIN) $^ $(LDFLAGS)
$(STRIP) $(BIN)
out/%.o: src/%.c
$(CC) -o $@ -c $< $(CFLAGS)
out/%.o: src/%.cpp
$(CPP) -o $@ -c $< $(CFLAGS)
clean:
rm -rf $(BIN) $(OBJ)

View file

@ -17,11 +17,11 @@ enum CameraMovement {
}; };
// Default camera values // Default camera values
const float YAW = -90.0f; const f32 YAW = -90.0f;
const float PITCH = 0.0f; const f32 PITCH = 0.0f;
const float SPEED = 2.5f; const f32 SPEED = 2.5f;
const float SENSITIVITY = 0.1f; const f32 SENSITIVITY = 0.1f;
const float ZOOM = 90.0f; const f32 ZOOM = 90.0f;
// An abstract camera class that processes input and calculates the corresponding Euler Angles, Vectors and Matrices for use in OpenGL // An abstract camera class that processes input and calculates the corresponding Euler Angles, Vectors and Matrices for use in OpenGL
@ -35,16 +35,17 @@ public:
glm::vec3 right; glm::vec3 right;
glm::vec3 worldUp; glm::vec3 worldUp;
// euler Angles // euler Angles
float yaw; f32 yaw;
float pitch; f32 pitch;
// camera options // camera options
float movementSpeed; f32 movementSpeed;
float mouseSensitivity; f32 mouseSensitivity;
float zoom; f32 zoom;
u32 renderDistance = 4;
// constructor with vectors // 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), 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), f32 yaw = YAW, f32 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) front(glm::vec3(0.0f, 0.0f, -1.0f)), movementSpeed(SPEED), mouseSensitivity(SENSITIVITY), zoom(ZOOM)
{ {
updateCameraVectors(); updateCameraVectors();
@ -58,7 +59,7 @@ public:
// 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) // 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) void processKeyboard(CameraMovement direction, double deltaTime)
{ {
float velocity = movementSpeed * deltaTime; f32 velocity = movementSpeed * deltaTime;
if (direction == FORWARD) if (direction == FORWARD)
position += front * velocity; position += front * velocity;
if (direction == BACKWARD) if (direction == BACKWARD)
@ -70,7 +71,7 @@ public:
} }
// processes input received from a mouse input system. Expects the offset value in both the x and y direction. // 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) void processMouseMovement(f32 xoffset, f32 yoffset, GLboolean constrainPitch = true)
{ {
xoffset *= mouseSensitivity; xoffset *= mouseSensitivity;
yoffset *= mouseSensitivity; yoffset *= mouseSensitivity;

View file

@ -16,7 +16,7 @@ u32 windowWidth = 800;
u32 windowHeight = 600; u32 windowHeight = 600;
// camera // camera
Camera camera(glm::vec3(0.0f, 2.0f, -3.0f)); Camera camera(glm::vec3(0.0f, 2.0f, 0.0f));
f32 lastX = windowWidth / 2.0f; f32 lastX = windowWidth / 2.0f;
f32 lastY = windowHeight / 2.0f; f32 lastY = windowHeight / 2.0f;
bool firstMouse = true; bool firstMouse = true;
@ -59,7 +59,7 @@ int main(int argc, char *argv[]) {
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
stbi_set_flip_vertically_on_load(true); stbi_set_flip_vertically_on_load(true);
Shader shader("../res/shaders/chunk.vs.glsl", "../res/shaders/chunk.fs.glsl"); Shader shader("res/shaders/chunk.vs.glsl", "res/shaders/chunk.fs.glsl");
TextureAtlas textureAtlas("res/textureatlas.png", GL_RGBA, 16); TextureAtlas textureAtlas("res/textureatlas.png", GL_RGBA, 16);
@ -89,7 +89,7 @@ int main(int argc, char *argv[]) {
glm::mat4 view = camera.getViewMatrix(); glm::mat4 view = camera.getViewMatrix();
shader.setMat4("view", view); shader.setMat4("view", view);
world->render(&shader, &textureAtlas); world->render(&camera, &shader, &textureAtlas);
{ {
i64 timeTaken = 0; i64 timeTaken = 0;
@ -99,9 +99,25 @@ int main(int argc, char *argv[]) {
auto p = c->first; auto p = c->first;
timeTaken += c->second->buildMesh(world, &textureAtlas, p.x, p.y, p.z); timeTaken += c->second->buildMesh(world, &textureAtlas, p.x, p.y, p.z);
totalTimeTaken += timeTaken; totalTimeTaken += timeTaken;
world->chunksWithMeshes[p] = c->second;
world->chunksToBuild.erase(p); world->chunksToBuild.erase(p);
} else break; } else break;
} while (timeTaken < 1500); } while (timeTaken < 2000);
}
if (world->chunksWithMeshes.size() > 0) {
auto c = world->chunksWithMeshes.begin();
auto p = c->first;
if (p.x < floor(camera.position.x / 3.2f) - camera.renderDistance ||
p.y < floor(camera.position.y / 3.2f) - camera.renderDistance ||
p.z < floor(camera.position.z / 3.2f) - camera.renderDistance ||
p.x > floor(camera.position.x / 3.2f) + camera.renderDistance ||
p.y > floor(camera.position.y / 3.2f) + camera.renderDistance ||
p.z > floor(camera.position.z / 3.2f) + camera.renderDistance)
{
if (c->second->mesh != nullptr) delete c->second->mesh;
world->chunksWithMeshes.erase(p);
}
} }
glfwSwapBuffers(window); glfwSwapBuffers(window);

View file

@ -11,7 +11,7 @@ public:
i32 height; i32 height;
i32 nrChannels; i32 nrChannels;
Texture(char *texturePath, int format) { Texture(const char *texturePath, int format) {
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -27,7 +27,7 @@ public:
stbi_image_free(data); stbi_image_free(data);
} }
void bind(Shader *shader, char *name, u32 unit) { void bind(Shader *shader, const char *name, u32 unit) {
glActiveTexture(GL_TEXTURE0 + unit); glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
shader->setInt(name, unit); shader->setInt(name, unit);
@ -38,7 +38,7 @@ class TextureAtlas : public Texture {
public: public:
f32 stride; f32 stride;
TextureAtlas(char *texturePath, int format, u32 tileSize) : Texture(texturePath, format) { TextureAtlas(const char *texturePath, int format, u32 tileSize) : Texture(texturePath, format) {
stride = static_cast<f32>(tileSize) / static_cast<f32>(width); stride = static_cast<f32>(tileSize) / static_cast<f32>(width);
} }

View file

@ -5,13 +5,8 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
World::World() { World::World() {}
Chunk::Chunk() {}
}
Chunk::Chunk() {
}
// returns the time it took in microseconds // returns the time it took in microseconds
i64 Chunk::buildMesh(World *world, TextureAtlas *textureAtlas, i32 chunkX, i32 chunkY, i32 chunkZ) { i64 Chunk::buildMesh(World *world, TextureAtlas *textureAtlas, i32 chunkX, i32 chunkY, i32 chunkZ) {
@ -102,6 +97,10 @@ i64 Chunk::buildMesh(World *world, TextureAtlas *textureAtlas, i32 chunkX, i32 c
uv.push_back(sideTexCoordsInverse[i]); uv.push_back(sideTexCoordsInverse[i]);
} }
} }
delete[] topTexCoords;
delete[] sideTexCoords;
delete[] sideTexCoordsInverse;
delete[] bottomTexCoords;
} }
} }
} }
@ -222,12 +221,12 @@ void World::setBlock(u32 block, i32 x, i32 y, i32 z, TextureAtlas *textureAtlas)
} }
} }
void World::render(Shader *shader, TextureAtlas *textureAtlas) { void World::render(Camera *camera, Shader *shader, TextureAtlas *textureAtlas) {
shader->use(); shader->use();
textureAtlas->bind(shader, "a_texture", 0); textureAtlas->bind(shader, "a_texture", 0);
for (i32 x = -8; x < 9; x++) for (i32 x = floor(camera->position.x / 3.2f) - camera->renderDistance; x < floor(camera->position.x / 3.2f) + camera->renderDistance; x++)
for (i32 y = -4; y < 5; y++) for (i32 y = floor(camera->position.y / 3.2f) - camera->renderDistance; y < floor(camera->position.y / 3.2f) + camera->renderDistance; y++)
for (i32 z = -8; z < 9; z++) { for (i32 z = floor(camera->position.z / 3.2f) - camera->renderDistance; z < floor(camera->position.z / 3.2f) + camera->renderDistance; z++) {
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(CHUNK_SIZE * 0.2f * x, CHUNK_SIZE * 0.2f * y, CHUNK_SIZE * 0.2f * z)); model = glm::translate(model, glm::vec3(CHUNK_SIZE * 0.2f * x, CHUNK_SIZE * 0.2f * y, CHUNK_SIZE * 0.2f * z));
model = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.1f)); model = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.1f));

View file

@ -7,6 +7,7 @@
#include "mesh.h" #include "mesh.h"
#include "shader.h" #include "shader.h"
#include "texture.h" #include "texture.h"
#include "camera.h"
#include "util.h" #include "util.h"
#define CHUNK_SIZE 16 #define CHUNK_SIZE 16
@ -28,6 +29,7 @@ public:
noise::module::Perlin noise; noise::module::Perlin noise;
std::unordered_map<ivec3, Chunk*> chunks; std::unordered_map<ivec3, Chunk*> chunks;
std::unordered_map<ivec3, Chunk*> chunksToBuild; std::unordered_map<ivec3, Chunk*> chunksToBuild;
std::unordered_map<ivec3, Chunk*> chunksWithMeshes;
World(); World();
@ -37,5 +39,5 @@ public:
inline u32 getBlockForMesh(Chunk *chunk, i32 chunkX, i32 chunkY, i32 chunkZ, i32 offsetX, i32 offsetY, i32 offsetZ); inline u32 getBlockForMesh(Chunk *chunk, i32 chunkX, i32 chunkY, i32 chunkZ, i32 offsetX, i32 offsetY, i32 offsetZ);
void setBlock(u32 block, i32 x, i32 y, i32 z, TextureAtlas *textureAtlas); void setBlock(u32 block, i32 x, i32 y, i32 z, TextureAtlas *textureAtlas);
void render(Shader *shader, TextureAtlas *textureAtlas); void render(Camera *camera, Shader *shader, TextureAtlas *textureAtlas);
}; };