Compare commits
3 Commits
a516f29be1
...
core
Author | SHA1 | Date | |
---|---|---|---|
584332788d | |||
f5641d5220 | |||
4a8bf7d05b |
18
Makefile
18
Makefile
@ -1,17 +1,18 @@
|
||||
CXX = g++
|
||||
CXXFLAGS = -g -Isrc
|
||||
CXXFLAGS = -O3 -Isrc
|
||||
LD = ld
|
||||
|
||||
LINKFLAGS = -lGL -lglfw
|
||||
|
||||
CC = gcc
|
||||
CCFLAGS = -g -Isrc
|
||||
CCFLAGS = -O3 -Isrc
|
||||
|
||||
XNOECRAFT_CPP_SRCS = $(shell find src/ -name '*.cpp')
|
||||
XNOECRAFT_C_SRCS = $(shell find src/ -name '*.c')
|
||||
XNOECRAFT_RES = $(shell find res/ -type f)
|
||||
|
||||
XNOECRAFT_OBJS = $(patsubst src/%.cpp,build/%.o,$(XNOECRAFT_CPP_SRCS)) $(patsubst src/%.c,build/%.o,$(XNOECRAFT_C_SRCS)) $(patsubst res/%.dat,build/%.o,$(XNOECRAFT_RES))
|
||||
XNOECRAFT_OBJS_WIN = $(patsubst src/%.cpp,build/%.wo,$(XNOECRAFT_CPP_SRCS)) $(patsubst src/%.c,build/%.wo,$(XNOECRAFT_C_SRCS)) $(patsubst res/%.dat,build/%.o,$(XNOECRAFT_RES))
|
||||
|
||||
XNOECRAFT_SRC_DIRS = $(shell find src/* -type d)
|
||||
XNOECRAFT_RES_DIRS = $(shell find res/* -type d)
|
||||
@ -19,9 +20,11 @@ XNOECRAFT_OBJ_DIRS = $(patsubst src/%,build/%,$(XNOECRAFT_SRC_DIRS)) $(patsubst
|
||||
|
||||
.PHONY = all cleanbuild clean prepare
|
||||
|
||||
all: prepare build/xnoecraft
|
||||
run: all
|
||||
./build/xnoecraft
|
||||
|
||||
all: prepare build/xnoecraft build/xnoecraft.exe
|
||||
|
||||
cleanbuild: clean prepare build/xnoecraft
|
||||
|
||||
clean:
|
||||
@ -32,12 +35,21 @@ prepare: $(XNOECRAFT_OBJ_DIRS)
|
||||
build/xnoecraft: $(XNOECRAFT_OBJS)
|
||||
$(CXX) $(LINKFLAGS) -o $@ $^
|
||||
|
||||
build/xnoecraft.exe: $(XNOECRAFT_OBJS_WIN)
|
||||
x86_64-w64-mingw32-g++ -static -static-libgcc -static-libstdc++ $^ -lglfw3 -lgdi32 -lssp -o $@
|
||||
|
||||
build/%.o: src/%.cpp
|
||||
$(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||
|
||||
build/%.o: src/%.c
|
||||
$(CC) $(CCFLAGS) -o $@ -c $<
|
||||
|
||||
build/%.wo: src/%.cpp
|
||||
x86_64-w64-mingw32-g++ $(CXXFLAGS) -o $@ -c $<
|
||||
|
||||
build/%.wo: src/%.c
|
||||
x86_64-w64-mingw32-gcc $(CCFLAGS) -o $@ -c $<
|
||||
|
||||
build/%.o: res/%.dat
|
||||
$(LD) -r -b binary $< -o $@
|
||||
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
Instructions on how to build:
|
||||
|
||||
- Install GLFW via your Package manager
|
||||
- Install GLM via your Package manager
|
||||
- Install GLFW
|
||||
- Install GLM
|
||||
- Install MinGW cross compiler
|
||||
- Install GLFW MinGW
|
||||
- `make`
|
@ -22,16 +22,4 @@ void main() {
|
||||
|
||||
float intensity = clamp(dot(to_sun_vector, normal_pass), 0.25, 1.0);
|
||||
color.xyz *= intensity;
|
||||
|
||||
//vec3 to_camera_vector = normalize(camera_position_pass - position_pass);
|
||||
//vec3 reflection = reflect(-to_sun_vector, normal_pass);
|
||||
|
||||
//float specular_intensity = max(dot(to_camera_vector, reflection), 0.0);
|
||||
|
||||
//specular_intensity = pow(specular_intensity, shine_dampener);
|
||||
|
||||
//specular_intensity *= reflectivity;
|
||||
|
||||
//if (dot(normal_pass, to_camera_vector) >= 0.3)
|
||||
//color.xyz += vec3(1.0, 1.0, 1.0) * specular_intensity;
|
||||
}
|
@ -10,14 +10,14 @@ private:
|
||||
glm::mat4 projection_matrix;
|
||||
float field_of_view;
|
||||
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
int width;
|
||||
int height;
|
||||
|
||||
void update_projection_matrix(int width, int height);
|
||||
public:
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
Camera();
|
||||
Camera(int width, int height);
|
||||
|
||||
|
187
src/chunk.cpp
187
src/chunk.cpp
@ -31,12 +31,37 @@ void Chunk::set_block(int x, int y, int z, int block) {
|
||||
}
|
||||
|
||||
int Chunk::get_block(int x, int y, int z) {
|
||||
if (x < 0 || x >= Chunk::horiz_size)
|
||||
return 0;
|
||||
if (z < 0 || z >= Chunk::horiz_size)
|
||||
return 0;
|
||||
if (y < 0 || y >= Chunk::vert_size)
|
||||
return 0;
|
||||
|
||||
if (x < 0) {
|
||||
Chunk* c;
|
||||
if (c = World::get_chunk(this->x-1, this->z))
|
||||
return c->get_block(Chunk::horiz_size-1, y, z);
|
||||
else
|
||||
return 0;
|
||||
} else if (x >= Chunk::horiz_size) {
|
||||
Chunk* c;
|
||||
if (c = World::get_chunk(this->x+1, this->z))
|
||||
return c->get_block(0, y, z);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (z < 0) {
|
||||
Chunk* c;
|
||||
if (c = World::get_chunk(this->x, this->z-1))
|
||||
return c->get_block(x, y, Chunk::horiz_size-1);
|
||||
else
|
||||
return 0;
|
||||
} else if (z >= Chunk::horiz_size) {
|
||||
Chunk* c;
|
||||
if (c = World::get_chunk(this->x, this->z+1))
|
||||
return c->get_block(x, y, 0);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
return this->blocks[y * Chunk::horiz_size * Chunk::horiz_size + z * Chunk::horiz_size + x];
|
||||
}
|
||||
|
||||
@ -77,100 +102,98 @@ Chunk::~Chunk() {
|
||||
|
||||
void Chunk::generate() {
|
||||
std::chrono::high_resolution_clock clock;
|
||||
if (mesh_version < current_version) {
|
||||
auto t1 = clock.now();
|
||||
mesh_version++;
|
||||
auto t1 = clock.now();
|
||||
mesh_version++;
|
||||
|
||||
std::vector<glm::vec3> vertex_positions;
|
||||
std::vector<glm::vec3> vertex_normals;
|
||||
std::vector<glm::vec2> vertex_tex_coords;
|
||||
std::vector<int> vertex_indexes;
|
||||
std::vector<glm::vec3> vertex_positions;
|
||||
std::vector<glm::vec3> vertex_normals;
|
||||
std::vector<glm::vec2> vertex_tex_coords;
|
||||
std::vector<int> vertex_indexes;
|
||||
|
||||
int index=0;
|
||||
int index=0;
|
||||
|
||||
for (int x=0; x<Chunk::horiz_size; x++) {
|
||||
for (int z=0; z<Chunk::horiz_size; z++) {
|
||||
for (int y=0; y<Chunk::vert_size; y++) {
|
||||
int block;
|
||||
if (block = get_block(x, y, z)) {
|
||||
bool faces[6] = {
|
||||
!get_block(x-1, y, z), // Left
|
||||
!get_block(x+1, y, z), // Right
|
||||
!get_block(x, y, z-1), // Back
|
||||
!get_block(x, y, z+1), // Front
|
||||
!get_block(x, y-1, z), // Bottom
|
||||
!get_block(x, y+1, z), // Top
|
||||
};
|
||||
for (int x=0; x<Chunk::horiz_size; x++) {
|
||||
for (int z=0; z<Chunk::horiz_size; z++) {
|
||||
for (int y=0; y<Chunk::vert_size; y++) {
|
||||
int block;
|
||||
if (block = get_block(x, y, z)) {
|
||||
bool faces[6] = {
|
||||
!get_block(x-1, y, z), // Left
|
||||
!get_block(x+1, y, z), // Right
|
||||
!get_block(x, y, z-1), // Back
|
||||
!get_block(x, y, z+1), // Front
|
||||
!get_block(x, y-1, z), // Bottom
|
||||
!get_block(x, y+1, z), // Top
|
||||
};
|
||||
|
||||
for (int i=0; i<6; i++) {
|
||||
if (faces[i]) {
|
||||
vertex_positions.emplace_back(cube_vertexes[i][0].x + x, cube_vertexes[i][0].y + y, cube_vertexes[i][0].z + z);
|
||||
vertex_positions.emplace_back(cube_vertexes[i][1].x + x, cube_vertexes[i][1].y + y, cube_vertexes[i][1].z + z);
|
||||
vertex_positions.emplace_back(cube_vertexes[i][2].x + x, cube_vertexes[i][2].y + y, cube_vertexes[i][2].z + z);
|
||||
vertex_positions.emplace_back(cube_vertexes[i][3].x + x, cube_vertexes[i][3].y + y, cube_vertexes[i][3].z + z);
|
||||
for (int i=0; i<6; i++) {
|
||||
if (faces[i]) {
|
||||
vertex_positions.emplace_back(cube_vertexes[i][0].x + x, cube_vertexes[i][0].y + y, cube_vertexes[i][0].z + z);
|
||||
vertex_positions.emplace_back(cube_vertexes[i][1].x + x, cube_vertexes[i][1].y + y, cube_vertexes[i][1].z + z);
|
||||
vertex_positions.emplace_back(cube_vertexes[i][2].x + x, cube_vertexes[i][2].y + y, cube_vertexes[i][2].z + z);
|
||||
vertex_positions.emplace_back(cube_vertexes[i][3].x + x, cube_vertexes[i][3].y + y, cube_vertexes[i][3].z + z);
|
||||
|
||||
vertex_normals.push_back(cube_normals[i]);
|
||||
vertex_normals.push_back(cube_normals[i]);
|
||||
vertex_normals.push_back(cube_normals[i]);
|
||||
vertex_normals.push_back(cube_normals[i]);
|
||||
vertex_normals.push_back(cube_normals[i]);
|
||||
vertex_normals.push_back(cube_normals[i]);
|
||||
vertex_normals.push_back(cube_normals[i]);
|
||||
vertex_normals.push_back(cube_normals[i]);
|
||||
|
||||
int u = block % 16;
|
||||
int v = block / 16;
|
||||
int u = block % 16;
|
||||
int v = block / 16;
|
||||
|
||||
float uv_step = 1.0f / 16.0f;
|
||||
float uv_step = 1.0f / 16.0f;
|
||||
|
||||
vertex_tex_coords.emplace_back(uv_step * (u), uv_step * (v));
|
||||
vertex_tex_coords.emplace_back(uv_step * (u), uv_step * (v+1));
|
||||
vertex_tex_coords.emplace_back(uv_step * (u+1), uv_step * (v+1));
|
||||
vertex_tex_coords.emplace_back(uv_step * (u+1), uv_step * (v));
|
||||
vertex_tex_coords.emplace_back(uv_step * (u), uv_step * (v));
|
||||
vertex_tex_coords.emplace_back(uv_step * (u), uv_step * (v+1));
|
||||
vertex_tex_coords.emplace_back(uv_step * (u+1), uv_step * (v+1));
|
||||
vertex_tex_coords.emplace_back(uv_step * (u+1), uv_step * (v));
|
||||
|
||||
vertex_indexes.push_back(index + 0);
|
||||
vertex_indexes.push_back(index + 1);
|
||||
vertex_indexes.push_back(index + 2);
|
||||
vertex_indexes.push_back(index + 2);
|
||||
vertex_indexes.push_back(index + 3);
|
||||
vertex_indexes.push_back(index + 0);
|
||||
vertex_indexes.push_back(index + 0);
|
||||
vertex_indexes.push_back(index + 1);
|
||||
vertex_indexes.push_back(index + 2);
|
||||
vertex_indexes.push_back(index + 2);
|
||||
vertex_indexes.push_back(index + 3);
|
||||
vertex_indexes.push_back(index + 0);
|
||||
|
||||
index += 4;
|
||||
}
|
||||
index += 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glGenVertexArrays(1, &this->VAO);
|
||||
glBindVertexArray(this->VAO);
|
||||
glGenBuffers(4, this->VBOs);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->VBOs[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_positions.size() * sizeof(glm::vec3), &vertex_positions[0].x, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->VBOs[1]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_normals.size() * sizeof(glm::vec3), &vertex_normals[0].x, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, 0, (void*)0);
|
||||
|
||||
glEnableVertexAttribArray(2);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->VBOs[2]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_tex_coords.size() * sizeof(glm::vec2), &vertex_tex_coords[0].x, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
glEnableVertexAttribArray(3);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->VBOs[3]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertex_indexes.size() * sizeof(int), &vertex_indexes[0], GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(3, 1, GL_INT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
this->vertex_count = vertex_indexes.size();
|
||||
|
||||
auto t2 = clock.now();
|
||||
|
||||
std::chrono::duration<double> d = (t2 - t1);
|
||||
|
||||
std::cerr << "Generated chunk in " << d.count() << " seconds.\n";
|
||||
}
|
||||
|
||||
glGenVertexArrays(1, &this->VAO);
|
||||
glBindVertexArray(this->VAO);
|
||||
glGenBuffers(4, this->VBOs);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->VBOs[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_positions.size() * sizeof(glm::vec3), &vertex_positions[0].x, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->VBOs[1]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_normals.size() * sizeof(glm::vec3), &vertex_normals[0].x, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, 0, (void*)0);
|
||||
|
||||
glEnableVertexAttribArray(2);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->VBOs[2]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_tex_coords.size() * sizeof(glm::vec2), &vertex_tex_coords[0].x, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
glEnableVertexAttribArray(3);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->VBOs[3]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertex_indexes.size() * sizeof(int), &vertex_indexes[0], GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(3, 1, GL_INT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
this->vertex_count = vertex_indexes.size();
|
||||
|
||||
auto t2 = clock.now();
|
||||
|
||||
std::chrono::duration<double> d = (t2 - t1);
|
||||
|
||||
std::cerr << "Generated chunk in " << d.count() << " seconds.\n";
|
||||
}
|
||||
|
||||
bool Chunk::outdated() {
|
||||
|
13
src/chunk.h
13
src/chunk.h
@ -9,6 +9,9 @@
|
||||
#include <iostream>
|
||||
|
||||
class Chunk {
|
||||
public:
|
||||
static constexpr int horiz_size = 16;
|
||||
static constexpr int vert_size = 256;
|
||||
private:
|
||||
GLuint VAO = 0;
|
||||
GLuint VBOs[4];
|
||||
@ -16,14 +19,7 @@ private:
|
||||
int mesh_version=0;
|
||||
int current_version=1;
|
||||
|
||||
static constexpr int horiz_size = 16;
|
||||
static constexpr int vert_size = 256;
|
||||
|
||||
int blocks[horiz_size * horiz_size * vert_size];
|
||||
|
||||
inline void set_block(int x, int y, int z, int block);
|
||||
inline int get_block(int x, int y, int z);
|
||||
|
||||
public:
|
||||
int x;
|
||||
int z;
|
||||
@ -40,6 +36,9 @@ public:
|
||||
glm::mat4 get_transformation_matrix();
|
||||
|
||||
void select();
|
||||
|
||||
void set_block(int x, int y, int z, int block);
|
||||
int get_block(int x, int y, int z);
|
||||
};
|
||||
|
||||
#endif
|
64
src/main.cpp
64
src/main.cpp
@ -34,25 +34,69 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
|
||||
key_states[key] = 0;
|
||||
}
|
||||
|
||||
float vel_forward, vel_y, vel_right;
|
||||
void loop(float deltatime, GLFWwindow* w) {
|
||||
float speed = 5.0f;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClearColor(0.2f, 0.6f, 0.7f, 1.0f);
|
||||
std::cout << "Deltatime: " << deltatime << "\n";
|
||||
|
||||
float speed = 20.0f;
|
||||
glm::vec3 old_position = c->get_position();
|
||||
glm::vec3 new_position = old_position;
|
||||
|
||||
glm::vec3 forward(
|
||||
glm::sin(c->rotation.y),
|
||||
0,
|
||||
glm::cos(c->rotation.y)
|
||||
);
|
||||
|
||||
glm::vec3 right(
|
||||
glm::sin(c->rotation.y + 1.57f),
|
||||
0,
|
||||
glm::cos(c->rotation.y + 1.57f)
|
||||
);
|
||||
|
||||
new_position.y += vel_y * deltatime;
|
||||
if (world->collide(new_position.x, new_position.y-0.5f, new_position.z) || world->collide(new_position.x, new_position.y-1.5f, new_position.z)) {
|
||||
vel_y = 0.0f;
|
||||
new_position.y = old_position.y;
|
||||
}
|
||||
|
||||
vel_forward = 0.0f;
|
||||
vel_right = 0.0f;
|
||||
if (key_states[GLFW_KEY_W])
|
||||
c->move_by(glm::vec3(0.0f, 0.0f, -speed) * deltatime);
|
||||
if (key_states[GLFW_KEY_A])
|
||||
c->move_by(glm::vec3(-speed, 0.0f, 0.0f) * deltatime);
|
||||
vel_forward = -speed;
|
||||
if (key_states[GLFW_KEY_S])
|
||||
c->move_by(glm::vec3(0.0f, 0.0f, speed) * deltatime);
|
||||
vel_forward = speed;
|
||||
if (key_states[GLFW_KEY_A])
|
||||
vel_right = -speed;
|
||||
if (key_states[GLFW_KEY_D])
|
||||
c->move_by(glm::vec3(speed, 0.0f, 0.0f) * deltatime);
|
||||
if (key_states[GLFW_KEY_SPACE])
|
||||
c->change_position_by(glm::vec3(0.0f, speed, 0.0f) * deltatime);
|
||||
if (key_states[GLFW_KEY_LEFT_SHIFT])
|
||||
c->change_position_by(glm::vec3(0.0f, -speed, 0.0f) * deltatime);
|
||||
vel_right = speed;
|
||||
if (key_states[GLFW_KEY_SPACE] && vel_y == 0.0f)
|
||||
vel_y = 6.0f;
|
||||
|
||||
forward *= vel_forward;
|
||||
forward *= deltatime;
|
||||
|
||||
right *= vel_right;
|
||||
right *= deltatime;
|
||||
|
||||
new_position += forward;
|
||||
if (world->collide(new_position.x, new_position.y-0.5f, new_position.z) || world->collide(new_position.x, new_position.y-1.5f, new_position.z)) {
|
||||
new_position.x = old_position.x;
|
||||
new_position.z = old_position.z;
|
||||
}
|
||||
|
||||
new_position += right;
|
||||
if (world->collide(new_position.x, new_position.y-0.5f, new_position.z) || world->collide(new_position.x, new_position.y-1.5f, new_position.z)) {
|
||||
new_position.x = old_position.x;
|
||||
new_position.z = old_position.z;
|
||||
}
|
||||
|
||||
c->set_position(new_position);
|
||||
|
||||
if (vel_y >= -50.0f)
|
||||
vel_y -= 9.81f * deltatime;
|
||||
|
||||
double current_x_pos, current_y_pos;
|
||||
glfwGetCursorPos(w, ¤t_x_pos, ¤t_y_pos);
|
||||
|
@ -6,10 +6,11 @@ FastNoise World::noise;
|
||||
World::World() {
|
||||
World::noise = FastNoise();
|
||||
World::noise.SetNoiseType(FastNoise::Perlin);
|
||||
World::world = this;
|
||||
}
|
||||
|
||||
void World::generate_around(int x, int z) {
|
||||
int world_size_half = this->world_size / 2;
|
||||
int world_size_half = this->world_size;
|
||||
|
||||
this->world_center = glm::vec2(x, z);
|
||||
|
||||
@ -23,8 +24,6 @@ void World::generate_around(int x, int z) {
|
||||
|
||||
for (Chunk* c : this->chunks)
|
||||
c->generate();
|
||||
|
||||
World::world = this;
|
||||
}
|
||||
|
||||
void World::process(int x, int z) {
|
||||
@ -34,12 +33,13 @@ void World::process(int x, int z) {
|
||||
return;
|
||||
|
||||
std::vector<Chunk*> new_chunks;
|
||||
std::vector<Chunk*> to_generate;
|
||||
|
||||
// Player has moved from the world center.
|
||||
|
||||
// Determine world bounds.
|
||||
|
||||
int world_size_half = this->world_size / 2;
|
||||
int world_size_half = this->world_size;
|
||||
|
||||
int x_low = x - world_size_half;
|
||||
int x_high = x + world_size_half;
|
||||
@ -68,21 +68,37 @@ void World::process(int x, int z) {
|
||||
|
||||
Chunk* c = new Chunk(x + rel_x, z + rel_z);
|
||||
new_chunks.push_back(c);
|
||||
to_generate.push_back(c);
|
||||
this->location_map[std::tuple<int, int>(x + rel_x, z + rel_z)] = c;
|
||||
|
||||
// Remove and delete it.
|
||||
this->location_map.erase(std::tuple<int, int>(chunk->x, chunk->z));
|
||||
delete chunk;
|
||||
} else {
|
||||
// If it's in bounds, keep it.
|
||||
// If it's in bounds, keep it
|
||||
new_chunks.push_back(chunk);
|
||||
|
||||
// Determine if its mesh needs regeneration
|
||||
if (x < this->world_center[0]) {
|
||||
if (chunk->x == x_low+2 || chunk->x == x_high)
|
||||
to_generate.push_back(chunk);
|
||||
} else if (x > this->world_center[0]) {
|
||||
if (chunk->x == x_low || chunk->x == x_high-2)
|
||||
to_generate.push_back(chunk);
|
||||
}
|
||||
if (z < this->world_center[1]) {
|
||||
if (chunk->z == z_low+2 || chunk->z == z_high)
|
||||
to_generate.push_back(chunk);
|
||||
} else if (z > this->world_center[1]) {
|
||||
if (chunk->z == z_low || chunk->z == z_high-2)
|
||||
to_generate.push_back(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Go through all the chunks and regenerate them if they are outdated
|
||||
for (Chunk* chunk : new_chunks)
|
||||
if (chunk->outdated())
|
||||
chunk->generate();
|
||||
for (Chunk* chunk : to_generate)
|
||||
chunk->generate();
|
||||
|
||||
//new_chunks.swap(this->chunks);
|
||||
this->chunks = new_chunks;
|
||||
@ -95,8 +111,8 @@ float World::get_height_at(int x, int z) {
|
||||
}
|
||||
|
||||
Chunk* World::get_chunk(int x, int z) {
|
||||
auto search = this->location_map.find(std::tuple<int, int>(x, z));
|
||||
if (search == this->location_map.end())
|
||||
auto search = World::world->location_map.find(std::tuple<int, int>(x, z));
|
||||
if (search == World::world->location_map.end())
|
||||
return 0;
|
||||
return std::get<1>(*search);
|
||||
}
|
||||
@ -113,3 +129,26 @@ void World::render(Camera* c, Shader* s) {
|
||||
glDrawElements(GL_TRIANGLES, chunk->vertex_count, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
}
|
||||
|
||||
bool World::collide(float x, float y, float z) {
|
||||
int chunk_x = x / 16;
|
||||
int chunk_z = z / 16;
|
||||
|
||||
int chunk_rel_x = (int)x % 16;
|
||||
int chunk_rel_z = (int)z % 16;
|
||||
|
||||
if (chunk_rel_x < 0) {
|
||||
chunk_rel_x += (Chunk::horiz_size-1);
|
||||
chunk_x--;
|
||||
}
|
||||
if (chunk_rel_z < 0) {
|
||||
chunk_rel_z += (Chunk::horiz_size-1);
|
||||
chunk_z--;
|
||||
}
|
||||
|
||||
Chunk* c;
|
||||
if (!(c = get_chunk(chunk_x, chunk_z)))
|
||||
return false;
|
||||
|
||||
return c->get_block(chunk_rel_x, y, chunk_rel_z);
|
||||
}
|
@ -30,7 +30,7 @@ private:
|
||||
|
||||
glm::ivec2 world_center;
|
||||
|
||||
int world_size = 32;
|
||||
int world_size = 16;
|
||||
|
||||
static World* world;
|
||||
|
||||
@ -41,9 +41,11 @@ public:
|
||||
void process(int x, int z);
|
||||
|
||||
static float get_height_at(int x, int z);
|
||||
Chunk* get_chunk(int x, int z);
|
||||
static Chunk* get_chunk(int x, int z);
|
||||
|
||||
void render(Camera* c, Shader* s);
|
||||
|
||||
bool collide(float x, float y, float z);
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user