Merge pull request #6 from MihailRis/part-11

Part 11
This commit is contained in:
MihailRis 2022-03-12 23:32:25 +03:00 committed by GitHub
commit 76abbf86b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
63 changed files with 4235 additions and 794 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
Debug/src/*.d
Debug/src/*.o
Debug/src/*/*.d
Debug/src/*/*.o
Debug/voxel_engine

View File

@ -11,6 +11,7 @@ RM := rm -rf
-include src/window/subdir.mk
-include src/voxels/subdir.mk
-include src/physics/subdir.mk
-include src/objects/subdir.mk
-include src/loaders/subdir.mk
-include src/lighting/subdir.mk
-include src/graphics/subdir.mk
@ -42,16 +43,30 @@ endif
-include ../makefile.defs
OPTIONAL_TOOL_DEPS := \
$(wildcard ../makefile.defs) \
$(wildcard ../makefile.init) \
$(wildcard ../makefile.targets) \
BUILD_ARTIFACT_NAME := voxel_engine
BUILD_ARTIFACT_EXTENSION :=
BUILD_ARTIFACT_PREFIX :=
BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),)
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: voxel_engine
all: main-build
# Main-build Target
main-build: voxel_engine
# Tool invocations
voxel_engine: $(OBJS) $(USER_OBJS)
voxel_engine: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS)
@echo 'Building target: $@'
@echo 'Invoking: Cross G++ Linker'
g++ -o "voxel_engine" $(OBJS) $(USER_OBJS) $(LIBS)
g++ -pthread -o "voxel_engine" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
@ -60,6 +75,6 @@ clean:
-$(RM) $(CC_DEPS)$(C++_DEPS)$(EXECUTABLES)$(OBJS)$(C_UPPER_DEPS)$(CXX_DEPS)$(C_DEPS)$(CPP_DEPS) voxel_engine
-@echo ' '
.PHONY: all clean dependents
.PHONY: all clean dependents main-build
-include ../makefile.targets

View File

@ -23,12 +23,13 @@ CPP_DEPS :=
# Every subdirectory with source files must be described here
SUBDIRS := \
src \
src/files \
src/graphics \
src/lighting \
src/loaders \
src/objects \
src/physics \
src \
src/voxels \
src/window \

View File

@ -17,10 +17,10 @@ CPP_DEPS += \
# Each subdirectory must supply rules for building sources it contributes
src/files/%.o: ../src/files/%.cpp
src/files/%.o: ../src/files/%.cpp src/files/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -4,6 +4,7 @@
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/graphics/Batch2D.cpp \
../src/graphics/LineBatch.cpp \
../src/graphics/Mesh.cpp \
../src/graphics/Shader.cpp \
@ -11,6 +12,7 @@ CPP_SRCS += \
../src/graphics/VoxelRenderer.cpp
OBJS += \
./src/graphics/Batch2D.o \
./src/graphics/LineBatch.o \
./src/graphics/Mesh.o \
./src/graphics/Shader.o \
@ -18,6 +20,7 @@ OBJS += \
./src/graphics/VoxelRenderer.o
CPP_DEPS += \
./src/graphics/Batch2D.d \
./src/graphics/LineBatch.d \
./src/graphics/Mesh.d \
./src/graphics/Shader.d \
@ -26,10 +29,10 @@ CPP_DEPS += \
# Each subdirectory must supply rules for building sources it contributes
src/graphics/%.o: ../src/graphics/%.cpp
src/graphics/%.o: ../src/graphics/%.cpp src/graphics/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -20,10 +20,10 @@ CPP_DEPS += \
# Each subdirectory must supply rules for building sources it contributes
src/lighting/%.o: ../src/lighting/%.cpp
src/lighting/%.o: ../src/lighting/%.cpp src/lighting/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -14,10 +14,10 @@ CPP_DEPS += \
# Each subdirectory must supply rules for building sources it contributes
src/loaders/%.o: ../src/loaders/%.cpp
src/loaders/%.o: ../src/loaders/%.cpp src/loaders/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -0,0 +1,24 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/objects/Player.cpp
OBJS += \
./src/objects/Player.o
CPP_DEPS += \
./src/objects/Player.d
# Each subdirectory must supply rules for building sources it contributes
src/objects/%.o: ../src/objects/%.cpp src/objects/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -17,10 +17,10 @@ CPP_DEPS += \
# Each subdirectory must supply rules for building sources it contributes
src/physics/%.o: ../src/physics/%.cpp
src/physics/%.o: ../src/physics/%.cpp src/physics/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -4,20 +4,23 @@
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/Assets.cpp \
../src/voxel_engine.cpp
OBJS += \
./src/Assets.o \
./src/voxel_engine.o
CPP_DEPS += \
./src/Assets.d \
./src/voxel_engine.d
# Each subdirectory must supply rules for building sources it contributes
src/%.o: ../src/%.cpp
src/%.o: ../src/%.cpp src/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -7,6 +7,8 @@ CPP_SRCS += \
../src/voxels/Block.cpp \
../src/voxels/Chunk.cpp \
../src/voxels/Chunks.cpp \
../src/voxels/ChunksController.cpp \
../src/voxels/ChunksLoader.cpp \
../src/voxels/WorldGenerator.cpp \
../src/voxels/voxel.cpp
@ -14,6 +16,8 @@ OBJS += \
./src/voxels/Block.o \
./src/voxels/Chunk.o \
./src/voxels/Chunks.o \
./src/voxels/ChunksController.o \
./src/voxels/ChunksLoader.o \
./src/voxels/WorldGenerator.o \
./src/voxels/voxel.o
@ -21,15 +25,17 @@ CPP_DEPS += \
./src/voxels/Block.d \
./src/voxels/Chunk.d \
./src/voxels/Chunks.d \
./src/voxels/ChunksController.d \
./src/voxels/ChunksLoader.d \
./src/voxels/WorldGenerator.d \
./src/voxels/voxel.d
# Each subdirectory must supply rules for building sources it contributes
src/voxels/%.o: ../src/voxels/%.cpp
src/voxels/%.o: ../src/voxels/%.cpp src/voxels/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -20,10 +20,10 @@ CPP_DEPS += \
# Each subdirectory must supply rules for building sources it contributes
src/window/%.o: ../src/window/%.cpp
src/window/%.o: ../src/window/%.cpp src/window/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -11,6 +11,7 @@ RM := rm -rf
-include src/window/subdir.mk
-include src/voxels/subdir.mk
-include src/physics/subdir.mk
-include src/objects/subdir.mk
-include src/loaders/subdir.mk
-include src/lighting/subdir.mk
-include src/graphics/subdir.mk
@ -58,14 +59,14 @@ BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ART
# All Target
all: main-build
# Main-build Target
# Main-build Target -std=c++0x
main-build: VOXEL_ENGINE.exe
# Tool invocations
VOXEL_ENGINE.exe: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS)
@echo 'Building target: $@'
@echo 'Invoking: MinGW C++ Linker'
g++ -o "VOXEL_ENGINE.exe" $(OBJS) $(USER_OBJS) $(LIBS) -static-libgcc -static-libstdc++
g++ -lpthread -o "VOXEL_ENGINE.exe" $(OBJS) $(USER_OBJS) $(LIBS) -static-libgcc -static-libstdc++
@echo 'Finished building target: $@'
@echo ' '

View File

@ -22,13 +22,15 @@ CPP_DEPS :=
C_DEPS :=
# Every subdirectory with source files must be described here
SUBDIRS := \
src \
src/files \
src/graphics \
src/lighting \
src/loaders \
src/physics \
src/voxels \
src/window \
SUBDIRS := \
src \
src/files \
src/graphics \
src/lighting \
src/loaders \
src/physics \
src/objects \
src/voxels \
src/objects \
src/window \

View File

@ -1,28 +1,27 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/graphics/LineBatch.cpp \
../src/graphics/Mesh.cpp \
../src/graphics/Shader.cpp \
../src/graphics/Texture.cpp \
../src/graphics/VoxelRenderer.cpp
CPP_SRCS += \
../src/graphics/LineBatch.cpp \
../src/graphics/Mesh.cpp \
../src/graphics/Shader.cpp \
../src/graphics/Texture.cpp \
../src/graphics/VoxelRenderer.cpp \
../src/graphics/Batch2D.cpp
OBJS += \
./src/graphics/LineBatch.o \
./src/graphics/Mesh.o \
./src/graphics/Shader.o \
./src/graphics/Texture.o \
./src/graphics/VoxelRenderer.o
OBJS += \
./src/graphics/LineBatch.o \
./src/graphics/Mesh.o \
./src/graphics/Shader.o \
./src/graphics/Texture.o \
./src/graphics/VoxelRenderer.o \
./src/graphics/Batch2D.o
CPP_DEPS += \
./src/graphics/LineBatch.d \
./src/graphics/Mesh.d \
./src/graphics/Shader.d \
./src/graphics/Texture.d \
./src/graphics/VoxelRenderer.d
CPP_DEPS += \
./src/graphics/LineBatch.d \
./src/graphics/Mesh.d \
./src/graphics/Shader.d \
./src/graphics/Texture.d \
./src/graphics/VoxelRenderer.d \
./src/graphics/Batch2D.d
# Each subdirectory must supply rules for building sources it contributes

View File

@ -0,0 +1,20 @@
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/objects/Player.cpp
OBJS += \
./src/objects/Player.o
CPP_DEPS += \
./src/objects/Player.d
# Each subdirectory must supply rules for building sources it contributes
src/objects/%.o: ../src/objects/%.cpp src/objects/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: GCC C++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,16 +1,15 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/voxel_engine.cpp
../src/voxel_engine.cpp \
../src/Assets.cpp
OBJS += \
./src/voxel_engine.o
./src/voxel_engine.o \
./src/Assets.o
CPP_DEPS += \
./src/voxel_engine.d
./src/voxel_engine.d \
./src/Assets.d
# Each subdirectory must supply rules for building sources it contributes

View File

@ -6,21 +6,27 @@
CPP_SRCS += \
../src/voxels/Block.cpp \
../src/voxels/Chunk.cpp \
../src/voxels/Chunks.cpp \
../src/voxels/Chunks.cpp \
../src/voxels/ChunksController.cpp \
../src/voxels/ChunksLoader.cpp \
../src/voxels/WorldGenerator.cpp \
../src/voxels/voxel.cpp
OBJS += \
./src/voxels/Block.o \
./src/voxels/Chunk.o \
./src/voxels/Chunks.o \
./src/voxels/Chunks.o \
./src/voxels/ChunksController.o \
./src/voxels/ChunksLoader.o \
./src/voxels/WorldGenerator.o \
./src/voxels/voxel.o
CPP_DEPS += \
./src/voxels/Block.d \
./src/voxels/Chunk.d \
./src/voxels/Chunks.d \
./src/voxels/Chunks.d \
./src/voxels/ChunksController.d \
./src/voxels/ChunksLoader.d \
./src/voxels/WorldGenerator.d \
./src/voxels/voxel.d
@ -29,7 +35,7 @@ CPP_DEPS += \
src/voxels/%.o: ../src/voxels/%.cpp src/voxels/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: GCC C++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
g++ -D _WIN32_WINNT=0501 -pthread -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

BIN
res/ascii.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
res/block (copy).png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

@ -2,6 +2,9 @@
layout (location = 0) in vec2 v_position;
uniform float u_ar;
uniform float u_scale;
void main(){
gl_Position = vec4(v_position, 0.0, 1.0);
gl_Position = vec4(v_position.x * u_ar * u_scale, v_position.y * u_scale, 0.0, 1.0);
}

View File

@ -10,8 +10,8 @@ uniform vec3 u_fogColor;
void main(){
vec4 tex_color = texture(u_texture0, a_texCoord);
if (tex_color.a < 0.5)
discard;
//if (tex_color.a < 0.5)
// discard;
float depth = (a_distance/256.0)*(a_distance/256.0)*256.0;
f_color = mix(a_color * tex_color, vec4(u_fogColor,1.0), min(1.0, depth/256.0));
f_color = mix(a_color * tex_color, vec4(u_fogColor,1.0), min(1.0, depth/256.0/1.0f));
}

View File

@ -12,10 +12,12 @@ uniform mat4 u_model;
uniform mat4 u_proj;
uniform mat4 u_view;
uniform vec3 u_skyLightColor;
uniform vec3 u_cameraPos;
uniform float u_gamma;
void main(){
vec4 viewmodelpos = u_view * u_model * vec4(v_position, 1.0);
vec2 pos2d = (u_model * vec4(v_position, 1.0)).xz-u_cameraPos.xz;
vec4 viewmodelpos = u_view * u_model * vec4(v_position+vec3(0,pow(length(pos2d)*0.0, 3.0),0), 1.0);
a_color = vec4(pow(v_light.rgb, vec3(u_gamma)),1.0f);
a_texCoord = v_texCoord;
a_color.rgb += u_skyLightColor * v_light.a*0.5;

31
src/Assets.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "Assets.h"
#include "graphics/Texture.h"
#include "graphics/Shader.h"
Assets::~Assets() {
for (auto& iter : shaders){
delete iter.second;
}
for (auto& iter : textures){
delete iter.second;
}
}
Texture* Assets::getTexture(std::string name){
return textures[name];
}
void Assets::store(Texture* texture, std::string name){
textures[name] = texture;
}
Shader* Assets::getShader(std::string name){
return shaders[name];
}
void Assets::store(Shader* shader, std::string name){
shaders[name] = shader;
}

22
src/Assets.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef SRC_ASSETS_H_
#define SRC_ASSETS_H_
#include <string>
#include <unordered_map>
class Texture;
class Shader;
class Assets {
std::unordered_map<std::string, Texture*> textures;
std::unordered_map<std::string, Shader*> shaders;
public:
~Assets();
Texture* getTexture(std::string name);
void store(Texture* texture, std::string name);
Shader* getShader(std::string name);
void store(Shader* shader, std::string name);
};
#endif /* SRC_ASSETS_H_ */

114
src/declarations.h Normal file
View File

@ -0,0 +1,114 @@
#ifndef DECLARATIONS_H
#define DECLARATIONS_H
#include <iostream>
#include "Assets.h"
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "window/Window.h"
#include "voxels/Block.h"
// Shaders, textures, renderers
bool _load_shader(Assets* assets, std::string vertex_file, std::string fragment_file, std::string name){
Shader* shader = load_shader(vertex_file, fragment_file);
if (shader == nullptr){
std::cerr << "failed to load shader '" << name << "'" << std::endl;
return false;
}
assets->store(shader, name);
return true;
}
bool _load_texture(Assets* assets, std::string filename, std::string name){
Texture* texture = load_texture(filename);
if (texture == nullptr){
std::cerr << "failed to load texture '" << name << "'" << std::endl;
return false;
}
assets->store(texture, name);
return true;
}
int initialize_assets(Assets* assets) {
#define LOAD_SHADER(VERTEX, FRAGMENT, NAME) \
if (!_load_shader(assets, VERTEX, FRAGMENT, NAME))\
return 1;
#define LOAD_TEXTURE(FILENAME, NAME) \
if (!_load_texture(assets, FILENAME, NAME))\
return 1;
LOAD_SHADER("res/main.glslv", "res/main.glslf", "main");
LOAD_SHADER("res/crosshair.glslv", "res/crosshair.glslf", "crosshair");
LOAD_SHADER("res/lines.glslv", "res/lines.glslf", "lines");
LOAD_TEXTURE("res/block.png", "block");
return 0;
}
// All in-game definitions (blocks, items, etc..)
void setup_definitions() {
// AIR
Block* block = new Block(0,0);
block->drawGroup = 1;
block->lightPassing = true;
block->skyLightPassing = true;
block->obstacle = false;
block->selectable = false;
Block::blocks[block->id] = block;
// STONE
block = new Block(1,2);
Block::blocks[block->id] = block;
// GRASS
block = new Block(2,4);
block->textureFaces[2] = 2;
block->textureFaces[3] = 1;
Block::blocks[block->id] = block;
// LAMP
block = new Block(3,3);
block->emission[0] = 15;
block->emission[1] = 14;
block->emission[2] = 13;
Block::blocks[block->id] = block;
// GLASS
block = new Block(4,5);
block->drawGroup = 2;
block->lightPassing = true;
Block::blocks[block->id] = block;
// PLANKS
block = new Block(5,6);
Block::blocks[block->id] = block;
// WOOD
block = new Block(6,7);
block->textureFaces[2] = 8;
block->textureFaces[3] = 8;
Block::blocks[block->id] = block;
// LEAVES
block = new Block(7,9);
Block::blocks[block->id] = block;
// ACTUAL STONE
block = new Block(8,10);
Block::blocks[block->id] = block;
// WATER
block = new Block(9,11);
block->drawGroup = 4;
block->lightPassing = true;
block->skyLightPassing = false;
block->obstacle = false;
block->selectable = false;
Block::blocks[block->id] = block;
}
#endif // DECLARATIONS_H

View File

@ -1,6 +1,9 @@
#include "WorldFiles.h"
#include "files.h"
#include "../window/Camera.h"
#include "../objects/Player.h"
#include "../physics/Hitbox.h"
#include "../voxels/Chunk.h"
union {
@ -14,6 +17,9 @@ union {
#include <fstream>
#include <iostream>
#define SECTION_POSITION 1
#define SECTION_ROTATION 2
unsigned long WorldFiles::totalCompressed = 0;
int bytes2Int(const unsigned char* src, unsigned int offset){
@ -27,6 +33,23 @@ void int2Bytes(int value, char* dest, unsigned int offset){
dest[offset+3] = (char) (value >> 0 & 255);
}
void float2Bytes(float fvalue, char* dest, unsigned int offset){
uint32_t value = *((uint32_t*)&fvalue);
dest[offset] = (char) (value >> 24 & 255);
dest[offset+1] = (char) (value >> 16 & 255);
dest[offset+2] = (char) (value >> 8 & 255);
dest[offset+3] = (char) (value >> 0 & 255);
}
float bytes2Float(char* srcs, unsigned int offset){
unsigned char* src = (unsigned char*) srcs;
uint32_t value = ((src[offset] << 24) |
(src[offset+1] << 16) |
(src[offset+2] << 8) |
(src[offset+3]));
return *(float*)(&value);
}
WorldFiles::WorldFiles(const char* directory, size_t mainBufferCapacity) : directory(directory){
mainBufferIn = new char[CHUNK_VOL*2];
mainBufferOut = new char[mainBufferCapacity];
@ -81,6 +104,10 @@ std::string WorldFiles::getRegionFile(int x, int y) {
return directory + std::to_string(x) + "_" + std::to_string(y) + ".bin";
}
std::string WorldFiles::getPlayerFile() {
return directory + "/player.bin";
}
bool WorldFiles::getChunk(int x, int y, char* out){
assert(out != nullptr);
@ -129,7 +156,7 @@ bool WorldFiles::readChunk(int x, int y, char* out){
input.read((char*)(&offset), 4);
// Ordering bytes from big-endian to machine order (any, just reading)
offset = bytes2Int((const unsigned char*)(&offset), 0);
assert (offset < 1000000);
//assert (offset < 1000000);
if (offset == 0){
input.close();
return false;
@ -161,6 +188,52 @@ void WorldFiles::write(){
}
}
void WorldFiles::writePlayer(Player* player){
char dst[1+3*4 + 1+2*4];
glm::vec3 position = player->hitbox->position;
size_t offset = 0;
dst[offset++] = SECTION_POSITION;
float2Bytes(position.x, dst, offset); offset += 4;
float2Bytes(position.y, dst, offset); offset += 4;
float2Bytes(position.z, dst, offset); offset += 4;
dst[offset++] = SECTION_ROTATION;
float2Bytes(player->camX, dst, offset); offset += 4;
float2Bytes(player->camY, dst, offset); offset += 4;
write_binary_file(getPlayerFile(), (const char*)dst, sizeof(dst));
}
bool WorldFiles::readPlayer(Player* player) {
size_t length = 0;
char* data = read_binary_file(getPlayerFile(), length);
if (data == nullptr){
std::cerr << "could not to read player.bin" << std::endl;
return false;
}
glm::vec3 position = player->hitbox->position;
size_t offset = 0;
while (offset < length){
char section = data[offset++];
switch (section){
case SECTION_POSITION:
position.x = bytes2Float(data, offset); offset += 4;
position.y = bytes2Float(data, offset); offset += 4;
position.z = bytes2Float(data, offset); offset += 4;
break;
case SECTION_ROTATION:
player->camX = bytes2Float(data, offset); offset += 4;
player->camY = bytes2Float(data, offset); offset += 4;
break;
}
}
player->hitbox->position = position;
player->camera->position = position + vec3(0, 1, 0);
return true;
}
unsigned int WorldFiles::writeRegion(char* out, int x, int y, char** region){
unsigned int offset = REGION_VOL * 4;
for (unsigned int i = 0; i < offset; i++)

View File

@ -3,7 +3,9 @@
#include <map>
#include <unordered_map>
#include <string>
class Player;
#define REGION_SIZE_BIT 5
#define REGION_SIZE (1 << (REGION_SIZE_BIT))
#define REGION_VOL ((REGION_SIZE) * (REGION_SIZE))
@ -26,13 +28,16 @@ public:
void put(const char* chunkData, int x, int y);
bool readPlayer(Player* player);
bool readChunk(int x, int y, char* out);
bool getChunk(int x, int y, char* out);
void readRegion(char* fileContent);
unsigned int writeRegion(char* out, int x, int y, char** region);
void writePlayer(Player* player);
void write();
std::string getRegionFile(int x, int y);
std::string getPlayerFile();
};
extern void longToCoords(int& x, int& y, long key);

View File

@ -40,14 +40,18 @@ bool read_binary_file(std::string filename, char* data, size_t size) {
return true;
}
bool read_binary_file(std::string filename, char* data, size_t offset, size_t size) {
char* read_binary_file(std::string filename, size_t& length) {
std::ifstream input(filename, std::ios::binary);
if (!input.is_open())
return false;
input.seekg(offset);
input.read(data, size);
return nullptr;
input.seekg(0, std::ios_base::end);
length = input.tellg();
input.seekg(0, std::ios_base::beg);
char* data = new char[length];
input.read(data, length);
input.close();
return true;
return data;
}
// returns decompressed length

View File

@ -7,6 +7,7 @@ extern bool write_binary_file(std::string filename, const char* data, size_t siz
extern unsigned int append_binary_file(std::string filename, const char* data, size_t size);
extern bool read_binary_file(std::string filename, char* data, size_t size);
extern bool read_binary_file(std::string filename, char* data, size_t offset, size_t size);
extern char* read_binary_file(std::string filename, size_t& length);
extern unsigned int calcRLE(const char* src, unsigned int length);
extern unsigned int compressRLE(const char* src, unsigned int length, char* dst);

18
src/graphics/Batch2D.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "Batch2D.h"
#include "Mesh.h"
Batch2D::Batch2D(size_t capacity) : capacity(capacity),
offset(0),
color(1.0f, 1.0f, 1.0f, 1.0f){
const int attrs[] = {
2, 2, 4, 0 //null terminator
};
buffer = new float[capacity];
mesh = new Mesh(nullptr, 0, attrs);
}
Batch2D::~Batch2D(){
delete buffer;
delete mesh;
}

27
src/graphics/Batch2D.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef SRC_GRAPHICS_BATCH2D_H_
#define SRC_GRAPHICS_BATCH2D_H_
#include <stdlib.h>
#include <glm/glm.hpp>
class Mesh;
class Batch2D {
float* buffer;
size_t capacity;
size_t offset;
glm::vec4 color;
Mesh* mesh;
void vertex(float x, float y,
float u, float v,
float r, float g, float b, float a);
public:
Batch2D(size_t capacity);
~Batch2D();
void rect(float x, float y, float w, float h);
void render();
};
#endif /* SRC_GRAPHICS_BATCH2D_H_ */

View File

@ -67,6 +67,7 @@ void LineBatch::box(float x, float y, float z, float w, float h, float d,
line(x+w, y+h, z-d, x+w, y+h, z+d, r,g,b,a);
}
#include <iostream>
void LineBatch::render(){
if (index == 0)
return;

View File

@ -12,7 +12,12 @@ Mesh::Mesh(const float* buffer, size_t vertices, const int* attrs) : vertices(ve
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, buffer, GL_STATIC_DRAW);
if (buffer){
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, buffer, GL_STATIC_DRAW);
} else {
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STATIC_DRAW);
}
// attributes
int offset = 0;

View File

@ -44,254 +44,282 @@ VoxelRenderer::~VoxelRenderer(){
delete[] buffer;
}
inline void _renderBlock(float* buffer, int x, int y, int z, const Chunk** chunks, voxel vox, size_t& index){
unsigned int id = vox.id;
if (!id){
return;
}
float l;
float uvsize = 1.0f/16.0f;
Block* block = Block::blocks[id];
unsigned char group = block->drawGroup;
if (!IS_BLOCKED(x,y+1,z,group)){
l = 1.0f;
SETUP_UV(block->textureFaces[3]);
float lr = LIGHT(x,y+1,z, 0) / 15.0f;
float lg = LIGHT(x,y+1,z, 1) / 15.0f;
float lb = LIGHT(x,y+1,z, 2) / 15.0f;
float ls = LIGHT(x,y+1,z, 3) / 15.0f;
float lr0 = (LIGHT(x-1,y+1,z,0) + lr*30 + LIGHT(x-1,y+1,z-1,0) + LIGHT(x,y+1,z-1,0)) / 5.0f / 15.0f;
float lr1 = (LIGHT(x-1,y+1,z,0) + lr*30 + LIGHT(x-1,y+1,z+1,0) + LIGHT(x,y+1,z+1,0)) / 5.0f / 15.0f;
float lr2 = (LIGHT(x+1,y+1,z,0) + lr*30 + LIGHT(x+1,y+1,z+1,0) + LIGHT(x,y+1,z+1,0)) / 5.0f / 15.0f;
float lr3 = (LIGHT(x+1,y+1,z,0) + lr*30 + LIGHT(x+1,y+1,z-1,0) + LIGHT(x,y+1,z-1,0)) / 5.0f / 15.0f;
float lg0 = (LIGHT(x-1,y+1,z,1) + lg*30 + LIGHT(x-1,y+1,z-1,1) + LIGHT(x,y+1,z-1,1)) / 5.0f / 15.0f;
float lg1 = (LIGHT(x-1,y+1,z,1) + lg*30 + LIGHT(x-1,y+1,z+1,1) + LIGHT(x,y+1,z+1,1)) / 5.0f / 15.0f;
float lg2 = (LIGHT(x+1,y+1,z,1) + lg*30 + LIGHT(x+1,y+1,z+1,1) + LIGHT(x,y+1,z+1,1)) / 5.0f / 15.0f;
float lg3 = (LIGHT(x+1,y+1,z,1) + lg*30 + LIGHT(x+1,y+1,z-1,1) + LIGHT(x,y+1,z-1,1)) / 5.0f / 15.0f;
float lb0 = (LIGHT(x-1,y+1,z,2) + lb*30 + LIGHT(x-1,y+1,z-1,2) + LIGHT(x,y+1,z-1,2)) / 5.0f / 15.0f;
float lb1 = (LIGHT(x-1,y+1,z,2) + lb*30 + LIGHT(x-1,y+1,z+1,2) + LIGHT(x,y+1,z+1,2)) / 5.0f / 15.0f;
float lb2 = (LIGHT(x+1,y+1,z,2) + lb*30 + LIGHT(x+1,y+1,z+1,2) + LIGHT(x,y+1,z+1,2)) / 5.0f / 15.0f;
float lb3 = (LIGHT(x+1,y+1,z,2) + lb*30 + LIGHT(x+1,y+1,z-1,2) + LIGHT(x,y+1,z-1,2)) / 5.0f / 15.0f;
float ls0 = (LIGHT(x-1,y+1,z,3) + ls*30 + LIGHT(x-1,y+1,z-1,3) + LIGHT(x,y+1,z-1,3)) / 5.0f / 15.0f;
float ls1 = (LIGHT(x-1,y+1,z,3) + ls*30 + LIGHT(x-1,y+1,z+1,3) + LIGHT(x,y+1,z+1,3)) / 5.0f / 15.0f;
float ls2 = (LIGHT(x+1,y+1,z,3) + ls*30 + LIGHT(x+1,y+1,z+1,3) + LIGHT(x,y+1,z+1,3)) / 5.0f / 15.0f;
float ls3 = (LIGHT(x+1,y+1,z,3) + ls*30 + LIGHT(x+1,y+1,z-1,3) + LIGHT(x,y+1,z-1,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0, lg0, lb0, ls0);
VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1, lg1, lb1, ls1);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2, lg2, lb2, ls2);
VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0, lg0, lb0, ls0);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2, lg2, lb2, ls2);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v1, lr3, lg3, lb3, ls3);
}
if (!IS_BLOCKED(x,y-1,z,group)){
l = 0.75f;
SETUP_UV(block->textureFaces[2]);
float lr = LIGHT(x,y-1,z, 0) / 15.0f;
float lg = LIGHT(x,y-1,z, 1) / 15.0f;
float lb = LIGHT(x,y-1,z, 2) / 15.0f;
float ls = LIGHT(x,y-1,z, 3) / 15.0f;
float lr0 = (LIGHT(x-1,y-1,z-1,0) + lr*30 + LIGHT(x-1,y-1,z,0) + LIGHT(x,y-1,z-1,0)) / 5.0f / 15.0f;
float lr1 = (LIGHT(x+1,y-1,z+1,0) + lr*30 + LIGHT(x+1,y-1,z,0) + LIGHT(x,y-1,z+1,0)) / 5.0f / 15.0f;
float lr2 = (LIGHT(x-1,y-1,z+1,0) + lr*30 + LIGHT(x-1,y-1,z,0) + LIGHT(x,y-1,z+1,0)) / 5.0f / 15.0f;
float lr3 = (LIGHT(x+1,y-1,z-1,0) + lr*30 + LIGHT(x+1,y-1,z,0) + LIGHT(x,y-1,z-1,0)) / 5.0f / 15.0f;
float lg0 = (LIGHT(x-1,y-1,z-1,1) + lg*30 + LIGHT(x-1,y-1,z,1) + LIGHT(x,y-1,z-1,1)) / 5.0f / 15.0f;
float lg1 = (LIGHT(x+1,y-1,z+1,1) + lg*30 + LIGHT(x+1,y-1,z,1) + LIGHT(x,y-1,z+1,1)) / 5.0f / 15.0f;
float lg2 = (LIGHT(x-1,y-1,z+1,1) + lg*30 + LIGHT(x-1,y-1,z,1) + LIGHT(x,y-1,z+1,1)) / 5.0f / 15.0f;
float lg3 = (LIGHT(x+1,y-1,z-1,1) + lg*30 + LIGHT(x+1,y-1,z,1) + LIGHT(x,y-1,z-1,1)) / 5.0f / 15.0f;
float lb0 = (LIGHT(x-1,y-1,z-1,2) + lb*30 + LIGHT(x-1,y-1,z,2) + LIGHT(x,y-1,z-1,2)) / 5.0f / 15.0f;
float lb1 = (LIGHT(x+1,y-1,z+1,2) + lb*30 + LIGHT(x+1,y-1,z,2) + LIGHT(x,y-1,z+1,2)) / 5.0f / 15.0f;
float lb2 = (LIGHT(x-1,y-1,z+1,2) + lb*30 + LIGHT(x-1,y-1,z,2) + LIGHT(x,y-1,z+1,2)) / 5.0f / 15.0f;
float lb3 = (LIGHT(x+1,y-1,z-1,2) + lb*30 + LIGHT(x+1,y-1,z,2) + LIGHT(x,y-1,z-1,2)) / 5.0f / 15.0f;
float ls0 = (LIGHT(x-1,y-1,z-1,3) + ls*30 + LIGHT(x-1,y-1,z,3) + LIGHT(x,y-1,z-1,3)) / 5.0f / 15.0f;
float ls1 = (LIGHT(x+1,y-1,z+1,3) + ls*30 + LIGHT(x+1,y-1,z,3) + LIGHT(x,y-1,z+1,3)) / 5.0f / 15.0f;
float ls2 = (LIGHT(x-1,y-1,z+1,3) + ls*30 + LIGHT(x-1,y-1,z,3) + LIGHT(x,y-1,z+1,3)) / 5.0f / 15.0f;
float ls3 = (LIGHT(x+1,y-1,z-1,3) + ls*30 + LIGHT(x+1,y-1,z,3) + LIGHT(x,y-1,z-1,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr3,lg3,lb3,ls3);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
}
if (!IS_BLOCKED(x+1,y,z,group)){
l = 0.95f;
SETUP_UV(block->textureFaces[1]);
float lr = LIGHT(x+1,y,z, 0) / 15.0f;
float lg = LIGHT(x+1,y,z, 1) / 15.0f;
float lb = LIGHT(x+1,y,z, 2) / 15.0f;
float ls = LIGHT(x+1,y,z, 3) / 15.0f;
float lr0 = (LIGHT(x+1,y-1,z-1,0) + lr*30 + LIGHT(x+1,y,z-1,0) + LIGHT(x+1,y-1,z,0)) / 5.0f / 15.0f;
float lr1 = (LIGHT(x+1,y+1,z-1,0) + lr*30 + LIGHT(x+1,y,z-1,0) + LIGHT(x+1,y+1,z,0)) / 5.0f / 15.0f;
float lr2 = (LIGHT(x+1,y+1,z+1,0) + lr*30 + LIGHT(x+1,y,z+1,0) + LIGHT(x+1,y+1,z,0)) / 5.0f / 15.0f;
float lr3 = (LIGHT(x+1,y-1,z+1,0) + lr*30 + LIGHT(x+1,y,z+1,0) + LIGHT(x+1,y-1,z,0)) / 5.0f / 15.0f;
float lg0 = (LIGHT(x+1,y-1,z-1,1) + lg*30 + LIGHT(x+1,y,z-1,1) + LIGHT(x+1,y-1,z,1)) / 5.0f / 15.0f;
float lg1 = (LIGHT(x+1,y+1,z-1,1) + lg*30 + LIGHT(x+1,y,z-1,1) + LIGHT(x+1,y+1,z,1)) / 5.0f / 15.0f;
float lg2 = (LIGHT(x+1,y+1,z+1,1) + lg*30 + LIGHT(x+1,y,z+1,1) + LIGHT(x+1,y+1,z,1)) / 5.0f / 15.0f;
float lg3 = (LIGHT(x+1,y-1,z+1,1) + lg*30 + LIGHT(x+1,y,z+1,1) + LIGHT(x+1,y-1,z,1)) / 5.0f / 15.0f;
float lb0 = (LIGHT(x+1,y-1,z-1,2) + lb*30 + LIGHT(x+1,y,z-1,2) + LIGHT(x+1,y-1,z,2)) / 5.0f / 15.0f;
float lb1 = (LIGHT(x+1,y+1,z-1,2) + lb*30 + LIGHT(x+1,y,z-1,2) + LIGHT(x+1,y+1,z,2)) / 5.0f / 15.0f;
float lb2 = (LIGHT(x+1,y+1,z+1,2) + lb*30 + LIGHT(x+1,y,z+1,2) + LIGHT(x+1,y+1,z,2)) / 5.0f / 15.0f;
float lb3 = (LIGHT(x+1,y-1,z+1,2) + lb*30 + LIGHT(x+1,y,z+1,2) + LIGHT(x+1,y-1,z,2)) / 5.0f / 15.0f;
float ls0 = (LIGHT(x+1,y-1,z-1,3) + ls*30 + LIGHT(x+1,y,z-1,3) + LIGHT(x+1,y-1,z,3)) / 5.0f / 15.0f;
float ls1 = (LIGHT(x+1,y+1,z-1,3) + ls*30 + LIGHT(x+1,y,z-1,3) + LIGHT(x+1,y+1,z,3)) / 5.0f / 15.0f;
float ls2 = (LIGHT(x+1,y+1,z+1,3) + ls*30 + LIGHT(x+1,y,z+1,3) + LIGHT(x+1,y+1,z,3)) / 5.0f / 15.0f;
float ls3 = (LIGHT(x+1,y-1,z+1,3) + ls*30 + LIGHT(x+1,y,z+1,3) + LIGHT(x+1,y-1,z,3)) / 5.0f / 15.0f;
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u1,v1, lr3,lg3,lb3,ls3);
}
if (!IS_BLOCKED(x-1,y,z,group)){
l = 0.85f;
SETUP_UV(block->textureFaces[0]);
float lr = LIGHT(x-1,y,z, 0) / 15.0f;
float lg = LIGHT(x-1,y,z, 1) / 15.0f;
float lb = LIGHT(x-1,y,z, 2) / 15.0f;
float ls = LIGHT(x-1,y,z, 3) / 15.0f;
float lr0 = (LIGHT(x-1,y-1,z-1,0) + lr*30 + LIGHT(x-1,y,z-1,0) + LIGHT(x-1,y-1,z,0)) / 5.0f / 15.0f;
float lr1 = (LIGHT(x-1,y+1,z+1,0) + lr*30 + LIGHT(x-1,y,z+1,0) + LIGHT(x-1,y+1,z,0)) / 5.0f / 15.0f;
float lr2 = (LIGHT(x-1,y+1,z-1,0) + lr*30 + LIGHT(x-1,y,z-1,0) + LIGHT(x-1,y+1,z,0)) / 5.0f / 15.0f;
float lr3 = (LIGHT(x-1,y-1,z+1,0) + lr*30 + LIGHT(x-1,y,z+1,0) + LIGHT(x-1,y-1,z,0)) / 5.0f / 15.0f;
float lg0 = (LIGHT(x-1,y-1,z-1,1) + lg*30 + LIGHT(x-1,y,z-1,1) + LIGHT(x-1,y-1,z,1)) / 5.0f / 15.0f;
float lg1 = (LIGHT(x-1,y+1,z+1,1) + lg*30 + LIGHT(x-1,y,z+1,1) + LIGHT(x-1,y+1,z,1)) / 5.0f / 15.0f;
float lg2 = (LIGHT(x-1,y+1,z-1,1) + lg*30 + LIGHT(x-1,y,z-1,1) + LIGHT(x-1,y+1,z,1)) / 5.0f / 15.0f;
float lg3 = (LIGHT(x-1,y-1,z+1,1) + lg*30 + LIGHT(x-1,y,z+1,1) + LIGHT(x-1,y-1,z,1)) / 5.0f / 15.0f;
float lb0 = (LIGHT(x-1,y-1,z-1,2) + lb*30 + LIGHT(x-1,y,z-1,2) + LIGHT(x-1,y-1,z,2)) / 5.0f / 15.0f;
float lb1 = (LIGHT(x-1,y+1,z+1,2) + lb*30 + LIGHT(x-1,y,z+1,2) + LIGHT(x-1,y+1,z,2)) / 5.0f / 15.0f;
float lb2 = (LIGHT(x-1,y+1,z-1,2) + lb*30 + LIGHT(x-1,y,z-1,2) + LIGHT(x-1,y+1,z,2)) / 5.0f / 15.0f;
float lb3 = (LIGHT(x-1,y-1,z+1,2) + lb*30 + LIGHT(x-1,y,z+1,2) + LIGHT(x-1,y-1,z,2)) / 5.0f / 15.0f;
float ls0 = (LIGHT(x-1,y-1,z-1,3) + ls*30 + LIGHT(x-1,y,z-1,3) + LIGHT(x-1,y-1,z,3)) / 5.0f / 15.0f;
float ls1 = (LIGHT(x-1,y+1,z+1,3) + ls*30 + LIGHT(x-1,y,z+1,3) + LIGHT(x-1,y+1,z,3)) / 5.0f / 15.0f;
float ls2 = (LIGHT(x-1,y+1,z-1,3) + ls*30 + LIGHT(x-1,y,z-1,3) + LIGHT(x-1,y+1,z,3)) / 5.0f / 15.0f;
float ls3 = (LIGHT(x-1,y-1,z+1,3) + ls*30 + LIGHT(x-1,y,z+1,3) + LIGHT(x-1,y-1,z,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3);
VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
}
if (!IS_BLOCKED(x,y,z+1,group)){
l = 0.9f;
SETUP_UV(block->textureFaces[5]);
float lr = LIGHT(x,y,z+1, 0) / 15.0f;
float lg = LIGHT(x,y,z+1, 1) / 15.0f;
float lb = LIGHT(x,y,z+1, 2) / 15.0f;
float ls = LIGHT(x,y,z+1, 3) / 15.0f;
float lr0 = l*(LIGHT(x-1,y-1,z+1,0) + lr*30 + LIGHT(x,y-1,z+1,0) + LIGHT(x-1,y,z+1,0)) / 5.0f / 15.0f;
float lr1 = l*(LIGHT(x+1,y+1,z+1,0) + lr*30 + LIGHT(x,y+1,z+1,0) + LIGHT(x+1,y,z+1,0)) / 5.0f / 15.0f;
float lr2 = l*(LIGHT(x-1,y+1,z+1,0) + lr*30 + LIGHT(x,y+1,z+1,0) + LIGHT(x-1,y,z+1,0)) / 5.0f / 15.0f;
float lr3 = l*(LIGHT(x+1,y-1,z+1,0) + lr*30 + LIGHT(x,y-1,z+1,0) + LIGHT(x+1,y,z+1,0)) / 5.0f / 15.0f;
float lg0 = l*(LIGHT(x-1,y-1,z+1,1) + lg*30 + LIGHT(x,y-1,z+1,1) + LIGHT(x-1,y,z+1,1)) / 5.0f / 15.0f;
float lg1 = l*(LIGHT(x+1,y+1,z+1,1) + lg*30 + LIGHT(x,y+1,z+1,1) + LIGHT(x+1,y,z+1,1)) / 5.0f / 15.0f;
float lg2 = l*(LIGHT(x-1,y+1,z+1,1) + lg*30 + LIGHT(x,y+1,z+1,1) + LIGHT(x-1,y,z+1,1)) / 5.0f / 15.0f;
float lg3 = l*(LIGHT(x+1,y-1,z+1,1) + lg*30 + LIGHT(x,y-1,z+1,1) + LIGHT(x+1,y,z+1,1)) / 5.0f / 15.0f;
float lb0 = l*(LIGHT(x-1,y-1,z+1,2) + lb*30 + LIGHT(x,y-1,z+1,2) + LIGHT(x-1,y,z+1,2)) / 5.0f / 15.0f;
float lb1 = l*(LIGHT(x+1,y+1,z+1,2) + lb*30 + LIGHT(x,y+1,z+1,2) + LIGHT(x+1,y,z+1,2)) / 5.0f / 15.0f;
float lb2 = l*(LIGHT(x-1,y+1,z+1,2) + lb*30 + LIGHT(x,y+1,z+1,2) + LIGHT(x-1,y,z+1,2)) / 5.0f / 15.0f;
float lb3 = l*(LIGHT(x+1,y-1,z+1,2) + lb*30 + LIGHT(x,y-1,z+1,2) + LIGHT(x+1,y,z+1,2)) / 5.0f / 15.0f;
float ls0 = l*(LIGHT(x-1,y-1,z+1,3) + ls*30 + LIGHT(x,y-1,z+1,3) + LIGHT(x-1,y,z+1,3)) / 5.0f / 15.0f;
float ls1 = l*(LIGHT(x+1,y+1,z+1,3) + ls*30 + LIGHT(x,y+1,z+1,3) + LIGHT(x+1,y,z+1,3)) / 5.0f / 15.0f;
float ls2 = l*(LIGHT(x-1,y+1,z+1,3) + ls*30 + LIGHT(x,y+1,z+1,3) + LIGHT(x-1,y,z+1,3)) / 5.0f / 15.0f;
float ls3 = l*(LIGHT(x+1,y-1,z+1,3) + ls*30 + LIGHT(x,y-1,z+1,3) + LIGHT(x+1,y,z+1,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
}
if (!IS_BLOCKED(x,y,z-1,group)){
l = 0.8f;
SETUP_UV(block->textureFaces[4]);
float lr = LIGHT(x,y,z-1, 0) / 15.0f;
float lg = LIGHT(x,y,z-1, 1) / 15.0f;
float lb = LIGHT(x,y,z-1, 2) / 15.0f;
float ls = LIGHT(x,y,z-1, 3) / 15.0f;
float lr0 = l*(LIGHT(x-1,y-1,z-1,0) + lr*30 + LIGHT(x,y-1,z-1,0) + LIGHT(x-1,y,z-1,0)) / 5.0f / 15.0f;
float lr1 = l*(LIGHT(x-1,y+1,z-1,0) + lr*30 + LIGHT(x,y+1,z-1,0) + LIGHT(x-1,y,z-1,0)) / 5.0f / 15.0f;
float lr2 = l*(LIGHT(x+1,y+1,z-1,0) + lr*30 + LIGHT(x,y+1,z-1,0) + LIGHT(x+1,y,z-1,0)) / 5.0f / 15.0f;
float lr3 = l*(LIGHT(x+1,y-1,z-1,0) + lr*30 + LIGHT(x,y-1,z-1,0) + LIGHT(x+1,y,z-1,0)) / 5.0f / 15.0f;
float lg0 = l*(LIGHT(x-1,y-1,z-1,1) + lg*30 + LIGHT(x,y-1,z-1,1) + LIGHT(x-1,y,z-1,1)) / 5.0f / 15.0f;
float lg1 = l*(LIGHT(x-1,y+1,z-1,1) + lg*30 + LIGHT(x,y+1,z-1,1) + LIGHT(x-1,y,z-1,1)) / 5.0f / 15.0f;
float lg2 = l*(LIGHT(x+1,y+1,z-1,1) + lg*30 + LIGHT(x,y+1,z-1,1) + LIGHT(x+1,y,z-1,1)) / 5.0f / 15.0f;
float lg3 = l*(LIGHT(x+1,y-1,z-1,1) + lg*30 + LIGHT(x,y-1,z-1,1) + LIGHT(x+1,y,z-1,1)) / 5.0f / 15.0f;
float lb0 = l*(LIGHT(x-1,y-1,z-1,2) + lb*30 + LIGHT(x,y-1,z-1,2) + LIGHT(x-1,y,z-1,2)) / 5.0f / 15.0f;
float lb1 = l*(LIGHT(x-1,y+1,z-1,2) + lb*30 + LIGHT(x,y+1,z-1,2) + LIGHT(x-1,y,z-1,2)) / 5.0f / 15.0f;
float lb2 = l*(LIGHT(x+1,y+1,z-1,2) + lb*30 + LIGHT(x,y+1,z-1,2) + LIGHT(x+1,y,z-1,2)) / 5.0f / 15.0f;
float lb3 = l*(LIGHT(x+1,y-1,z-1,2) + lb*30 + LIGHT(x,y-1,z-1,2) + LIGHT(x+1,y,z-1,2)) / 5.0f / 15.0f;
float ls0 = l*(LIGHT(x-1,y-1,z-1,3) + ls*30 + LIGHT(x,y-1,z-1,3) + LIGHT(x-1,y,z-1,3)) / 5.0f / 15.0f;
float ls1 = l*(LIGHT(x-1,y+1,z-1,3) + ls*30 + LIGHT(x,y+1,z-1,3) + LIGHT(x-1,y,z-1,3)) / 5.0f / 15.0f;
float ls2 = l*(LIGHT(x+1,y+1,z-1,3) + ls*30 + LIGHT(x,y+1,z-1,3) + LIGHT(x+1,y,z-1,3)) / 5.0f / 15.0f;
float ls3 = l*(LIGHT(x+1,y-1,z-1,3) + ls*30 + LIGHT(x,y-1,z-1,3) + LIGHT(x+1,y,z-1,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u1,v1, lr3,lg3,lb3,ls3);
}
}
Mesh* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks){
size_t index = 0;
for (int y = 0; y < CHUNK_H; y++){
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
voxel vox = chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x];
unsigned int id = vox.id;
if (!id){
if (vox.id == 9 || vox.id == 4)
continue;
}
_renderBlock(buffer, x, y, z, chunks, vox, index);
}
}
}
float l;
float uvsize = 1.0f/16.0f;
for (int y = 0; y < CHUNK_H; y++){
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
voxel vox = chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x];
if (vox.id != 9)
continue;
_renderBlock(buffer, x, y, z, chunks, vox, index);
}
}
}
Block* block = Block::blocks[id];
unsigned char group = block->drawGroup;
if (!IS_BLOCKED(x,y+1,z,group)){
l = 1.0f;
SETUP_UV(block->textureFaces[3]);
float lr = LIGHT(x,y+1,z, 0) / 15.0f;
float lg = LIGHT(x,y+1,z, 1) / 15.0f;
float lb = LIGHT(x,y+1,z, 2) / 15.0f;
float ls = LIGHT(x,y+1,z, 3) / 15.0f;
float lr0 = (LIGHT(x-1,y+1,z,0) + lr*30 + LIGHT(x-1,y+1,z-1,0) + LIGHT(x,y+1,z-1,0)) / 5.0f / 15.0f;
float lr1 = (LIGHT(x-1,y+1,z,0) + lr*30 + LIGHT(x-1,y+1,z+1,0) + LIGHT(x,y+1,z+1,0)) / 5.0f / 15.0f;
float lr2 = (LIGHT(x+1,y+1,z,0) + lr*30 + LIGHT(x+1,y+1,z+1,0) + LIGHT(x,y+1,z+1,0)) / 5.0f / 15.0f;
float lr3 = (LIGHT(x+1,y+1,z,0) + lr*30 + LIGHT(x+1,y+1,z-1,0) + LIGHT(x,y+1,z-1,0)) / 5.0f / 15.0f;
float lg0 = (LIGHT(x-1,y+1,z,1) + lg*30 + LIGHT(x-1,y+1,z-1,1) + LIGHT(x,y+1,z-1,1)) / 5.0f / 15.0f;
float lg1 = (LIGHT(x-1,y+1,z,1) + lg*30 + LIGHT(x-1,y+1,z+1,1) + LIGHT(x,y+1,z+1,1)) / 5.0f / 15.0f;
float lg2 = (LIGHT(x+1,y+1,z,1) + lg*30 + LIGHT(x+1,y+1,z+1,1) + LIGHT(x,y+1,z+1,1)) / 5.0f / 15.0f;
float lg3 = (LIGHT(x+1,y+1,z,1) + lg*30 + LIGHT(x+1,y+1,z-1,1) + LIGHT(x,y+1,z-1,1)) / 5.0f / 15.0f;
float lb0 = (LIGHT(x-1,y+1,z,2) + lb*30 + LIGHT(x-1,y+1,z-1,2) + LIGHT(x,y+1,z-1,2)) / 5.0f / 15.0f;
float lb1 = (LIGHT(x-1,y+1,z,2) + lb*30 + LIGHT(x-1,y+1,z+1,2) + LIGHT(x,y+1,z+1,2)) / 5.0f / 15.0f;
float lb2 = (LIGHT(x+1,y+1,z,2) + lb*30 + LIGHT(x+1,y+1,z+1,2) + LIGHT(x,y+1,z+1,2)) / 5.0f / 15.0f;
float lb3 = (LIGHT(x+1,y+1,z,2) + lb*30 + LIGHT(x+1,y+1,z-1,2) + LIGHT(x,y+1,z-1,2)) / 5.0f / 15.0f;
float ls0 = (LIGHT(x-1,y+1,z,3) + ls*30 + LIGHT(x-1,y+1,z-1,3) + LIGHT(x,y+1,z-1,3)) / 5.0f / 15.0f;
float ls1 = (LIGHT(x-1,y+1,z,3) + ls*30 + LIGHT(x-1,y+1,z+1,3) + LIGHT(x,y+1,z+1,3)) / 5.0f / 15.0f;
float ls2 = (LIGHT(x+1,y+1,z,3) + ls*30 + LIGHT(x+1,y+1,z+1,3) + LIGHT(x,y+1,z+1,3)) / 5.0f / 15.0f;
float ls3 = (LIGHT(x+1,y+1,z,3) + ls*30 + LIGHT(x+1,y+1,z-1,3) + LIGHT(x,y+1,z-1,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0, lg0, lb0, ls0);
VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1, lg1, lb1, ls1);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2, lg2, lb2, ls2);
VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0, lg0, lb0, ls0);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2, lg2, lb2, ls2);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v1, lr3, lg3, lb3, ls3);
}
if (!IS_BLOCKED(x,y-1,z,group)){
l = 0.75f;
SETUP_UV(block->textureFaces[2]);
float lr = LIGHT(x,y-1,z, 0) / 15.0f;
float lg = LIGHT(x,y-1,z, 1) / 15.0f;
float lb = LIGHT(x,y-1,z, 2) / 15.0f;
float ls = LIGHT(x,y-1,z, 3) / 15.0f;
float lr0 = (LIGHT(x-1,y-1,z-1,0) + lr*30 + LIGHT(x-1,y-1,z,0) + LIGHT(x,y-1,z-1,0)) / 5.0f / 15.0f;
float lr1 = (LIGHT(x+1,y-1,z+1,0) + lr*30 + LIGHT(x+1,y-1,z,0) + LIGHT(x,y-1,z+1,0)) / 5.0f / 15.0f;
float lr2 = (LIGHT(x-1,y-1,z+1,0) + lr*30 + LIGHT(x-1,y-1,z,0) + LIGHT(x,y-1,z+1,0)) / 5.0f / 15.0f;
float lr3 = (LIGHT(x+1,y-1,z-1,0) + lr*30 + LIGHT(x+1,y-1,z,0) + LIGHT(x,y-1,z-1,0)) / 5.0f / 15.0f;
float lg0 = (LIGHT(x-1,y-1,z-1,1) + lg*30 + LIGHT(x-1,y-1,z,1) + LIGHT(x,y-1,z-1,1)) / 5.0f / 15.0f;
float lg1 = (LIGHT(x+1,y-1,z+1,1) + lg*30 + LIGHT(x+1,y-1,z,1) + LIGHT(x,y-1,z+1,1)) / 5.0f / 15.0f;
float lg2 = (LIGHT(x-1,y-1,z+1,1) + lg*30 + LIGHT(x-1,y-1,z,1) + LIGHT(x,y-1,z+1,1)) / 5.0f / 15.0f;
float lg3 = (LIGHT(x+1,y-1,z-1,1) + lg*30 + LIGHT(x+1,y-1,z,1) + LIGHT(x,y-1,z-1,1)) / 5.0f / 15.0f;
float lb0 = (LIGHT(x-1,y-1,z-1,2) + lb*30 + LIGHT(x-1,y-1,z,2) + LIGHT(x,y-1,z-1,2)) / 5.0f / 15.0f;
float lb1 = (LIGHT(x+1,y-1,z+1,2) + lb*30 + LIGHT(x+1,y-1,z,2) + LIGHT(x,y-1,z+1,2)) / 5.0f / 15.0f;
float lb2 = (LIGHT(x-1,y-1,z+1,2) + lb*30 + LIGHT(x-1,y-1,z,2) + LIGHT(x,y-1,z+1,2)) / 5.0f / 15.0f;
float lb3 = (LIGHT(x+1,y-1,z-1,2) + lb*30 + LIGHT(x+1,y-1,z,2) + LIGHT(x,y-1,z-1,2)) / 5.0f / 15.0f;
float ls0 = (LIGHT(x-1,y-1,z-1,3) + ls*30 + LIGHT(x-1,y-1,z,3) + LIGHT(x,y-1,z-1,3)) / 5.0f / 15.0f;
float ls1 = (LIGHT(x+1,y-1,z+1,3) + ls*30 + LIGHT(x+1,y-1,z,3) + LIGHT(x,y-1,z+1,3)) / 5.0f / 15.0f;
float ls2 = (LIGHT(x-1,y-1,z+1,3) + ls*30 + LIGHT(x-1,y-1,z,3) + LIGHT(x,y-1,z+1,3)) / 5.0f / 15.0f;
float ls3 = (LIGHT(x+1,y-1,z-1,3) + ls*30 + LIGHT(x+1,y-1,z,3) + LIGHT(x,y-1,z-1,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr3,lg3,lb3,ls3);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
}
if (!IS_BLOCKED(x+1,y,z,group)){
l = 0.95f;
SETUP_UV(block->textureFaces[1]);
float lr = LIGHT(x+1,y,z, 0) / 15.0f;
float lg = LIGHT(x+1,y,z, 1) / 15.0f;
float lb = LIGHT(x+1,y,z, 2) / 15.0f;
float ls = LIGHT(x+1,y,z, 3) / 15.0f;
float lr0 = (LIGHT(x+1,y-1,z-1,0) + lr*30 + LIGHT(x+1,y,z-1,0) + LIGHT(x+1,y-1,z,0)) / 5.0f / 15.0f;
float lr1 = (LIGHT(x+1,y+1,z-1,0) + lr*30 + LIGHT(x+1,y,z-1,0) + LIGHT(x+1,y+1,z,0)) / 5.0f / 15.0f;
float lr2 = (LIGHT(x+1,y+1,z+1,0) + lr*30 + LIGHT(x+1,y,z+1,0) + LIGHT(x+1,y+1,z,0)) / 5.0f / 15.0f;
float lr3 = (LIGHT(x+1,y-1,z+1,0) + lr*30 + LIGHT(x+1,y,z+1,0) + LIGHT(x+1,y-1,z,0)) / 5.0f / 15.0f;
float lg0 = (LIGHT(x+1,y-1,z-1,1) + lg*30 + LIGHT(x+1,y,z-1,1) + LIGHT(x+1,y-1,z,1)) / 5.0f / 15.0f;
float lg1 = (LIGHT(x+1,y+1,z-1,1) + lg*30 + LIGHT(x+1,y,z-1,1) + LIGHT(x+1,y+1,z,1)) / 5.0f / 15.0f;
float lg2 = (LIGHT(x+1,y+1,z+1,1) + lg*30 + LIGHT(x+1,y,z+1,1) + LIGHT(x+1,y+1,z,1)) / 5.0f / 15.0f;
float lg3 = (LIGHT(x+1,y-1,z+1,1) + lg*30 + LIGHT(x+1,y,z+1,1) + LIGHT(x+1,y-1,z,1)) / 5.0f / 15.0f;
float lb0 = (LIGHT(x+1,y-1,z-1,2) + lb*30 + LIGHT(x+1,y,z-1,2) + LIGHT(x+1,y-1,z,2)) / 5.0f / 15.0f;
float lb1 = (LIGHT(x+1,y+1,z-1,2) + lb*30 + LIGHT(x+1,y,z-1,2) + LIGHT(x+1,y+1,z,2)) / 5.0f / 15.0f;
float lb2 = (LIGHT(x+1,y+1,z+1,2) + lb*30 + LIGHT(x+1,y,z+1,2) + LIGHT(x+1,y+1,z,2)) / 5.0f / 15.0f;
float lb3 = (LIGHT(x+1,y-1,z+1,2) + lb*30 + LIGHT(x+1,y,z+1,2) + LIGHT(x+1,y-1,z,2)) / 5.0f / 15.0f;
float ls0 = (LIGHT(x+1,y-1,z-1,3) + ls*30 + LIGHT(x+1,y,z-1,3) + LIGHT(x+1,y-1,z,3)) / 5.0f / 15.0f;
float ls1 = (LIGHT(x+1,y+1,z-1,3) + ls*30 + LIGHT(x+1,y,z-1,3) + LIGHT(x+1,y+1,z,3)) / 5.0f / 15.0f;
float ls2 = (LIGHT(x+1,y+1,z+1,3) + ls*30 + LIGHT(x+1,y,z+1,3) + LIGHT(x+1,y+1,z,3)) / 5.0f / 15.0f;
float ls3 = (LIGHT(x+1,y-1,z+1,3) + ls*30 + LIGHT(x+1,y,z+1,3) + LIGHT(x+1,y-1,z,3)) / 5.0f / 15.0f;
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u1,v1, lr3,lg3,lb3,ls3);
}
if (!IS_BLOCKED(x-1,y,z,group)){
l = 0.85f;
SETUP_UV(block->textureFaces[0]);
float lr = LIGHT(x-1,y,z, 0) / 15.0f;
float lg = LIGHT(x-1,y,z, 1) / 15.0f;
float lb = LIGHT(x-1,y,z, 2) / 15.0f;
float ls = LIGHT(x-1,y,z, 3) / 15.0f;
float lr0 = (LIGHT(x-1,y-1,z-1,0) + lr*30 + LIGHT(x-1,y,z-1,0) + LIGHT(x-1,y-1,z,0)) / 5.0f / 15.0f;
float lr1 = (LIGHT(x-1,y+1,z+1,0) + lr*30 + LIGHT(x-1,y,z+1,0) + LIGHT(x-1,y+1,z,0)) / 5.0f / 15.0f;
float lr2 = (LIGHT(x-1,y+1,z-1,0) + lr*30 + LIGHT(x-1,y,z-1,0) + LIGHT(x-1,y+1,z,0)) / 5.0f / 15.0f;
float lr3 = (LIGHT(x-1,y-1,z+1,0) + lr*30 + LIGHT(x-1,y,z+1,0) + LIGHT(x-1,y-1,z,0)) / 5.0f / 15.0f;
float lg0 = (LIGHT(x-1,y-1,z-1,1) + lg*30 + LIGHT(x-1,y,z-1,1) + LIGHT(x-1,y-1,z,1)) / 5.0f / 15.0f;
float lg1 = (LIGHT(x-1,y+1,z+1,1) + lg*30 + LIGHT(x-1,y,z+1,1) + LIGHT(x-1,y+1,z,1)) / 5.0f / 15.0f;
float lg2 = (LIGHT(x-1,y+1,z-1,1) + lg*30 + LIGHT(x-1,y,z-1,1) + LIGHT(x-1,y+1,z,1)) / 5.0f / 15.0f;
float lg3 = (LIGHT(x-1,y-1,z+1,1) + lg*30 + LIGHT(x-1,y,z+1,1) + LIGHT(x-1,y-1,z,1)) / 5.0f / 15.0f;
float lb0 = (LIGHT(x-1,y-1,z-1,2) + lb*30 + LIGHT(x-1,y,z-1,2) + LIGHT(x-1,y-1,z,2)) / 5.0f / 15.0f;
float lb1 = (LIGHT(x-1,y+1,z+1,2) + lb*30 + LIGHT(x-1,y,z+1,2) + LIGHT(x-1,y+1,z,2)) / 5.0f / 15.0f;
float lb2 = (LIGHT(x-1,y+1,z-1,2) + lb*30 + LIGHT(x-1,y,z-1,2) + LIGHT(x-1,y+1,z,2)) / 5.0f / 15.0f;
float lb3 = (LIGHT(x-1,y-1,z+1,2) + lb*30 + LIGHT(x-1,y,z+1,2) + LIGHT(x-1,y-1,z,2)) / 5.0f / 15.0f;
float ls0 = (LIGHT(x-1,y-1,z-1,3) + ls*30 + LIGHT(x-1,y,z-1,3) + LIGHT(x-1,y-1,z,3)) / 5.0f / 15.0f;
float ls1 = (LIGHT(x-1,y+1,z+1,3) + ls*30 + LIGHT(x-1,y,z+1,3) + LIGHT(x-1,y+1,z,3)) / 5.0f / 15.0f;
float ls2 = (LIGHT(x-1,y+1,z-1,3) + ls*30 + LIGHT(x-1,y,z-1,3) + LIGHT(x-1,y+1,z,3)) / 5.0f / 15.0f;
float ls3 = (LIGHT(x-1,y-1,z+1,3) + ls*30 + LIGHT(x-1,y,z+1,3) + LIGHT(x-1,y-1,z,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3);
VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
}
if (!IS_BLOCKED(x,y,z+1,group)){
l = 0.9f;
SETUP_UV(block->textureFaces[5]);
float lr = LIGHT(x,y,z+1, 0) / 15.0f;
float lg = LIGHT(x,y,z+1, 1) / 15.0f;
float lb = LIGHT(x,y,z+1, 2) / 15.0f;
float ls = LIGHT(x,y,z+1, 3) / 15.0f;
float lr0 = l*(LIGHT(x-1,y-1,z+1,0) + lr*30 + LIGHT(x,y-1,z+1,0) + LIGHT(x-1,y,z+1,0)) / 5.0f / 15.0f;
float lr1 = l*(LIGHT(x+1,y+1,z+1,0) + lr*30 + LIGHT(x,y+1,z+1,0) + LIGHT(x+1,y,z+1,0)) / 5.0f / 15.0f;
float lr2 = l*(LIGHT(x-1,y+1,z+1,0) + lr*30 + LIGHT(x,y+1,z+1,0) + LIGHT(x-1,y,z+1,0)) / 5.0f / 15.0f;
float lr3 = l*(LIGHT(x+1,y-1,z+1,0) + lr*30 + LIGHT(x,y-1,z+1,0) + LIGHT(x+1,y,z+1,0)) / 5.0f / 15.0f;
float lg0 = l*(LIGHT(x-1,y-1,z+1,1) + lg*30 + LIGHT(x,y-1,z+1,1) + LIGHT(x-1,y,z+1,1)) / 5.0f / 15.0f;
float lg1 = l*(LIGHT(x+1,y+1,z+1,1) + lg*30 + LIGHT(x,y+1,z+1,1) + LIGHT(x+1,y,z+1,1)) / 5.0f / 15.0f;
float lg2 = l*(LIGHT(x-1,y+1,z+1,1) + lg*30 + LIGHT(x,y+1,z+1,1) + LIGHT(x-1,y,z+1,1)) / 5.0f / 15.0f;
float lg3 = l*(LIGHT(x+1,y-1,z+1,1) + lg*30 + LIGHT(x,y-1,z+1,1) + LIGHT(x+1,y,z+1,1)) / 5.0f / 15.0f;
float lb0 = l*(LIGHT(x-1,y-1,z+1,2) + lb*30 + LIGHT(x,y-1,z+1,2) + LIGHT(x-1,y,z+1,2)) / 5.0f / 15.0f;
float lb1 = l*(LIGHT(x+1,y+1,z+1,2) + lb*30 + LIGHT(x,y+1,z+1,2) + LIGHT(x+1,y,z+1,2)) / 5.0f / 15.0f;
float lb2 = l*(LIGHT(x-1,y+1,z+1,2) + lb*30 + LIGHT(x,y+1,z+1,2) + LIGHT(x-1,y,z+1,2)) / 5.0f / 15.0f;
float lb3 = l*(LIGHT(x+1,y-1,z+1,2) + lb*30 + LIGHT(x,y-1,z+1,2) + LIGHT(x+1,y,z+1,2)) / 5.0f / 15.0f;
float ls0 = l*(LIGHT(x-1,y-1,z+1,3) + ls*30 + LIGHT(x,y-1,z+1,3) + LIGHT(x-1,y,z+1,3)) / 5.0f / 15.0f;
float ls1 = l*(LIGHT(x+1,y+1,z+1,3) + ls*30 + LIGHT(x,y+1,z+1,3) + LIGHT(x+1,y,z+1,3)) / 5.0f / 15.0f;
float ls2 = l*(LIGHT(x-1,y+1,z+1,3) + ls*30 + LIGHT(x,y+1,z+1,3) + LIGHT(x-1,y,z+1,3)) / 5.0f / 15.0f;
float ls3 = l*(LIGHT(x+1,y-1,z+1,3) + ls*30 + LIGHT(x,y-1,z+1,3) + LIGHT(x+1,y,z+1,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
}
if (!IS_BLOCKED(x,y,z-1,group)){
l = 0.8f;
SETUP_UV(block->textureFaces[4]);
float lr = LIGHT(x,y,z-1, 0) / 15.0f;
float lg = LIGHT(x,y,z-1, 1) / 15.0f;
float lb = LIGHT(x,y,z-1, 2) / 15.0f;
float ls = LIGHT(x,y,z-1, 3) / 15.0f;
float lr0 = l*(LIGHT(x-1,y-1,z-1,0) + lr*30 + LIGHT(x,y-1,z-1,0) + LIGHT(x-1,y,z-1,0)) / 5.0f / 15.0f;
float lr1 = l*(LIGHT(x-1,y+1,z-1,0) + lr*30 + LIGHT(x,y+1,z-1,0) + LIGHT(x-1,y,z-1,0)) / 5.0f / 15.0f;
float lr2 = l*(LIGHT(x+1,y+1,z-1,0) + lr*30 + LIGHT(x,y+1,z-1,0) + LIGHT(x+1,y,z-1,0)) / 5.0f / 15.0f;
float lr3 = l*(LIGHT(x+1,y-1,z-1,0) + lr*30 + LIGHT(x,y-1,z-1,0) + LIGHT(x+1,y,z-1,0)) / 5.0f / 15.0f;
float lg0 = l*(LIGHT(x-1,y-1,z-1,1) + lg*30 + LIGHT(x,y-1,z-1,1) + LIGHT(x-1,y,z-1,1)) / 5.0f / 15.0f;
float lg1 = l*(LIGHT(x-1,y+1,z-1,1) + lg*30 + LIGHT(x,y+1,z-1,1) + LIGHT(x-1,y,z-1,1)) / 5.0f / 15.0f;
float lg2 = l*(LIGHT(x+1,y+1,z-1,1) + lg*30 + LIGHT(x,y+1,z-1,1) + LIGHT(x+1,y,z-1,1)) / 5.0f / 15.0f;
float lg3 = l*(LIGHT(x+1,y-1,z-1,1) + lg*30 + LIGHT(x,y-1,z-1,1) + LIGHT(x+1,y,z-1,1)) / 5.0f / 15.0f;
float lb0 = l*(LIGHT(x-1,y-1,z-1,2) + lb*30 + LIGHT(x,y-1,z-1,2) + LIGHT(x-1,y,z-1,2)) / 5.0f / 15.0f;
float lb1 = l*(LIGHT(x-1,y+1,z-1,2) + lb*30 + LIGHT(x,y+1,z-1,2) + LIGHT(x-1,y,z-1,2)) / 5.0f / 15.0f;
float lb2 = l*(LIGHT(x+1,y+1,z-1,2) + lb*30 + LIGHT(x,y+1,z-1,2) + LIGHT(x+1,y,z-1,2)) / 5.0f / 15.0f;
float lb3 = l*(LIGHT(x+1,y-1,z-1,2) + lb*30 + LIGHT(x,y-1,z-1,2) + LIGHT(x+1,y,z-1,2)) / 5.0f / 15.0f;
float ls0 = l*(LIGHT(x-1,y-1,z-1,3) + ls*30 + LIGHT(x,y-1,z-1,3) + LIGHT(x-1,y,z-1,3)) / 5.0f / 15.0f;
float ls1 = l*(LIGHT(x-1,y+1,z-1,3) + ls*30 + LIGHT(x,y+1,z-1,3) + LIGHT(x-1,y,z-1,3)) / 5.0f / 15.0f;
float ls2 = l*(LIGHT(x+1,y+1,z-1,3) + ls*30 + LIGHT(x,y+1,z-1,3) + LIGHT(x+1,y,z-1,3)) / 5.0f / 15.0f;
float ls3 = l*(LIGHT(x+1,y-1,z-1,3) + ls*30 + LIGHT(x,y-1,z-1,3) + LIGHT(x+1,y,z-1,3)) / 5.0f / 15.0f;
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u1,v1, lr3,lg3,lb3,ls3);
}
for (int y = 0; y < CHUNK_H; y++){
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
voxel vox = chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x];
if (vox.id != 4)
continue;
_renderBlock(buffer, x, y, z, chunks, vox, index);
}
}
}

View File

@ -1,3 +1,4 @@
#include <iostream>
#include <assert.h>
#include "LightSolver.h"
#include "Lightmap.h"
@ -60,7 +61,7 @@ void LightSolver::solve(){
};
while (!remqueue.empty()){
lightentry entry = remqueue.front();
const lightentry entry = remqueue.front();
remqueue.pop();
for (size_t i = 0; i < 6; i++) {

View File

@ -6,23 +6,19 @@
#include "../voxels/voxel.h"
#include "../voxels/Block.h"
Chunks* Lighting::chunks = nullptr;
LightSolver* Lighting::solverR = nullptr;
LightSolver* Lighting::solverG = nullptr;
LightSolver* Lighting::solverB = nullptr;
LightSolver* Lighting::solverS = nullptr;
int Lighting::initialize(Chunks* chunks){
Lighting::chunks = chunks;
Lighting::Lighting(Chunks* chunks){
this->chunks = chunks;
solverR = new LightSolver(chunks, 0);
solverG = new LightSolver(chunks, 1);
solverB = new LightSolver(chunks, 2);
solverS = new LightSolver(chunks, 3);
return 0;
}
void Lighting::finalize(){
delete solverR, solverG, solverB, solverS;
Lighting::~Lighting(){
delete solverR;
delete solverG;
delete solverB;
delete solverS;
}
void Lighting::clear(){
@ -37,15 +33,14 @@ void Lighting::clear(){
}
}
void Lighting::onChunkLoaded(int cx, int cy, int cz){
void Lighting::onChunkLoaded(int cx, int cy, int cz, bool sky){
Chunk* chunk = chunks->getChunk(cx, cy, cz);
Chunk* chunkUpper = chunks->getChunk(cx, cy+1, cz);
Chunk* chunkLower = chunks->getChunk(cx, cy-1, cz);
if (chunkLower){
if (chunkLower && sky){
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
int gx = x + cx * CHUNK_W;
int gy = cy * CHUNK_H;
int gz = z + cz * CHUNK_D;
int light = chunk->lightmap->getS(x,0,z);
@ -65,7 +60,7 @@ void Lighting::onChunkLoaded(int cx, int cy, int cz){
break;
voxel* vox = &(current->voxels[(y * CHUNK_D + z) * CHUNK_W + x]);//chunks->get(gx,gy+y,gz);
Block* block = Block::blocks[vox->id];
if (!block->lightPassing)
if (!block->skyLightPassing)
break;
//current->lightmap->setS(x,y,z, 0);
current->modified = true;
@ -76,7 +71,7 @@ void Lighting::onChunkLoaded(int cx, int cy, int cz){
}
}
}
if (chunkUpper){
if (chunkUpper && sky){
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
int gx = x + cx * CHUNK_W;
@ -99,7 +94,7 @@ void Lighting::onChunkLoaded(int cx, int cy, int cz){
break;
voxel* vox = &(current->voxels[(y * CHUNK_D + z) * CHUNK_W + x]);//chunks->get(gx,gy+y,gz);
Block* block = Block::blocks[vox->id];
if (!block->lightPassing)
if (!block->skyLightPassing)
break;
current->lightmap->setS(x,y,z, 15);
current->modified = true;
@ -110,7 +105,7 @@ void Lighting::onChunkLoaded(int cx, int cy, int cz){
}
}
}
} else {
} else if (sky){
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
int gx = x + cx * CHUNK_W;
@ -129,11 +124,11 @@ void Lighting::onChunkLoaded(int cx, int cy, int cz){
break;
voxel* vox = &(current->voxels[(y * CHUNK_D + z) * CHUNK_W + x]);//chunks->get(gx,gy+y,gz);
Block* block = Block::blocks[vox->id];
if (!block->lightPassing)
if (!block->skyLightPassing)
break;
current->lightmap->setS(x,y,z, 15);
current->modified = true;
//solverS->add(gx,y+ncy*CHUNK_H,gz);
solverS->add(gx,y+ncy*CHUNK_H,gz);
}
}
}
@ -163,10 +158,13 @@ void Lighting::onChunkLoaded(int cx, int cy, int cz){
int gx = x + cx * CHUNK_W;
int gy = y + cy * CHUNK_H;
int gz = z + cz * CHUNK_D;
solverR->add(gx,gy,gz);
solverG->add(gx,gy,gz);
solverB->add(gx,gy,gz);
solverS->add(gx,gy,gz);
if (chunks->getLight(x,y,z)){
solverR->add(gx,gy,gz);
solverG->add(gx,gy,gz);
solverB->add(gx,gy,gz);
if (sky)
solverS->add(gx,gy,gz);
}
}
}
}
@ -199,7 +197,7 @@ void Lighting::onBlockSet(int x, int y, int z, int id){
if (chunks->getLight(x,y+1,z, 3) == 0xF){
for (int i = y; i >= 0; i--){
voxel* vox = chunks->get(x,i,z);
if (vox == nullptr || vox->id != 0)
if ((vox == nullptr || vox->id != 0) && Block::blocks[id]->skyLightPassing)
break;
solverS->add(x,i,z, 0xF);
}
@ -220,7 +218,7 @@ void Lighting::onBlockSet(int x, int y, int z, int id){
solverR->remove(x,y,z);
solverG->remove(x,y,z);
solverB->remove(x,y,z);
if (!block->lightPassing){
if (!block->skyLightPassing){
solverS->remove(x,y,z);
for (int i = y-1; i >= 0; i--){
solverS->remove(x,i,z);

View File

@ -5,18 +5,18 @@ class Chunks;
class LightSolver;
class Lighting {
static Chunks* chunks;
static LightSolver* solverR;
static LightSolver* solverG;
static LightSolver* solverB;
static LightSolver* solverS;
Chunks* chunks = nullptr;
LightSolver* solverR = nullptr;
LightSolver* solverG = nullptr;
LightSolver* solverB = nullptr;
LightSolver* solverS = nullptr;
public:
static int initialize(Chunks* chunks);
static void finalize();
Lighting(Chunks* chunks);
~Lighting();
static void clear();
static void onChunkLoaded(int cx, int cy, int cz);
static void onBlockSet(int x, int y, int z, int id);
void clear();
void onChunkLoaded(int cx, int cy, int cz, bool sky);
void onBlockSet(int x, int y, int z, int id);
};
#endif /* LIGHTING_LIGHTING_H_ */

View File

@ -10,3 +10,9 @@ Lightmap::Lightmap(){
Lightmap::~Lightmap(){
delete[] map;
}
void Lightmap::set(const Lightmap* lightmap) {
for (unsigned int i = 0; i < CHUNK_VOL; i++){
map[i] = lightmap->map[i];
}
}

View File

@ -9,6 +9,12 @@ public:
Lightmap();
~Lightmap();
void set(const Lightmap* lightmap);
inline unsigned short get(int x, int y, int z){
return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x]);
}
inline unsigned char get(int x, int y, int z, int channel){
return (map[y*CHUNK_D*CHUNK_W+z*CHUNK_W+x] >> (channel << 2)) & 0xF;
}

View File

@ -4,8 +4,9 @@
#include <GL/glew.h>
#include "../graphics/Texture.h"
// comment line below for use spng instead of libpng
#ifndef _WIN32
#define LIBPNG
#endif
#ifdef LIBPNG
#include <png.h>
@ -101,7 +102,7 @@ int _png_load(const char* file, int* width, int* height){
alpha, GL_UNSIGNED_BYTE, (GLvoid *) image_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);

2433
src/maths/FastNoiseLite.h Normal file

File diff suppressed because it is too large Load Diff

16
src/objects/Player.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "Player.h"
#include "../physics/Hitbox.h"
#include <glm/glm.hpp>
Player::Player(glm::vec3 position, float speed, Camera* camera) :
speed(speed),
camera(camera),
choosenBlock(1),
camX(0.0f), camY(0.0f){
hitbox = new Hitbox(position, vec3(0.2f,0.9f,0.2f));
}
Player::~Player(){
delete hitbox;
}

24
src/objects/Player.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef SRC_OBJECTS_PLAYER_H_
#define SRC_OBJECTS_PLAYER_H_
#include <glm/glm.hpp>
class Camera;
class Hitbox;
class Player {
public:
float speed;
Camera* camera;
Hitbox* hitbox;
bool flight = false;
int choosenBlock;
float camX, camY;
float cameraShaking = 0.0f;
float cameraShakingTimer = 0.0f;
glm::vec3 interpVel {0.0f, 0.0f, 0.0f};
Player(glm::vec3 position, float speed, Camera* camera);
~Player();
};
#endif /* SRC_OBJECTS_PLAYER_H_ */

View File

@ -1,4 +1,4 @@
#include "Hitbox.h"
Hitbox::Hitbox(vec3 position, vec3 halfsize) : position(position), halfsize(halfsize), velocity(0.0f,0.0f,0.0f) {
Hitbox::Hitbox(vec3 position, vec3 halfsize) : position(position), halfsize(halfsize), velocity(0.0f,0.0f,0.0f), linear_damping(0.1f) {
}

View File

@ -12,6 +12,7 @@ public:
vec3 position;
vec3 halfsize;
vec3 velocity;
float linear_damping;
bool grounded = false;
Hitbox(vec3 position, vec3 halfsize);

View File

@ -4,20 +4,23 @@
#include <iostream>
#define E 0.01
#define E 0.03
#define DEFAULT_FRICTION 10.0
PhysicsSolver::PhysicsSolver(vec3 gravity) : gravity(gravity) {
}
void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting) {
void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting, float gravityScale) {
hitbox->grounded = false;
for (unsigned i = 0; i < substeps; i++){
float dt = delta / (float)substeps;
float linear_damping = hitbox->linear_damping;
vec3& pos = hitbox->position;
vec3& half = hitbox->halfsize;
vec3& vel = hitbox->velocity;
vel.x += gravity.x*dt;
vel.y += gravity.y*dt;
vel.z += gravity.z*dt;
vel.x += gravity.x*dt * gravityScale;
vel.y += gravity.y*dt * gravityScale;
vel.z += gravity.z*dt * gravityScale;
float px = pos.x;
float pz = pos.z;
@ -73,21 +76,24 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
}
}
hitbox->grounded = false;
if (vel.y < 0.0){
for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){
bool broken = false;
for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){
int y = floor(pos.y-half.y-E);
if (chunks->isObstacle(x,y,z)){
vel.y *= 0.0;
pos.y = y + 1 + half.y;
int f = 18.0;
int f = DEFAULT_FRICTION;
vel.x *= max(0.0, 1.0 - dt * f);
vel.z *= max(0.0, 1.0 - dt * f);
hitbox->grounded = true;
broken = true;
break;
}
}
if (broken)
break;
}
}
if (vel.y > 0.0){
@ -103,6 +109,9 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
}
}
vel.x *= max(0.0, 1.0 - dt * linear_damping);
vel.z *= max(0.0, 1.0 - dt * linear_damping);
pos.x += vel.x * dt;
pos.y += vel.y * dt;
pos.z += vel.z * dt;

View File

@ -14,7 +14,7 @@ class PhysicsSolver {
vec3 gravity;
public:
PhysicsSolver(vec3 gravity);
void step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting);
void step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting, float gravityScale);
bool isBlockInside(int x, int y, int z, Hitbox* hitbox);
};

View File

@ -1,9 +1,13 @@
// Install dependencies:
// sudo apt install libgl-dev libglew-dev libglfw3-dev libpng-dev libglm-dev
#include <iostream>
#include <cmath>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <vector>
#include <ctime>
// GLM
@ -27,6 +31,7 @@ using namespace glm;
#include "voxels/Chunks.h"
#include "voxels/Block.h"
#include "voxels/WorldGenerator.h"
#include "voxels/ChunksController.h"
#include "files/files.h"
#include "files/WorldFiles.h"
#include "lighting/LightSolver.h"
@ -35,176 +40,15 @@ using namespace glm;
#include "physics/Hitbox.h"
#include "physics/PhysicsSolver.h"
int WIDTH = 1280;
int HEIGHT = 720;
#include "Assets.h"
#include "objects/Player.h"
float vertices[] = {
// x y
-0.01f,-0.01f,
0.01f, 0.01f,
#include "declarations.h"
#include "world_render.h"
-0.01f, 0.01f,
0.01f,-0.01f,
};
int attrs[] = {
2, 0 //null terminator
};
Mesh *crosshair;
Shader *shader, *linesShader, *crosshairShader;
Texture *texture;
LineBatch *lineBatch;
Chunks* chunks;
WorldFiles* wfile;
bool occlusion = false;
// All in-game definitions (blocks, items, etc..)
void setup_definitions() {
// AIR
Block* block = new Block(0,0);
block->drawGroup = 1;
block->lightPassing = true;
block->obstacle = false;
Block::blocks[block->id] = block;
// STONE
block = new Block(1,2);
Block::blocks[block->id] = block;
// GRASS
block = new Block(2,4);
block->textureFaces[2] = 2;
block->textureFaces[3] = 1;
Block::blocks[block->id] = block;
// LAMP
block = new Block(3,3);
block->emission[0] = 15;
block->emission[1] = 14;
block->emission[2] = 13;
Block::blocks[block->id] = block;
// GLASS
block = new Block(4,5);
block->drawGroup = 2;
block->lightPassing = true;
Block::blocks[block->id] = block;
// PLANKS
block = new Block(5,6);
Block::blocks[block->id] = block;
}
// Shaders, textures, renderers
int initialize_assets() {
shader = load_shader("res/main.glslv", "res/main.glslf");
if (shader == nullptr){
std::cerr << "failed to load shader" << std::endl;
Window::terminate();
return 1;
}
crosshairShader = load_shader("res/crosshair.glslv", "res/crosshair.glslf");
if (crosshairShader == nullptr){
std::cerr << "failed to load crosshair shader" << std::endl;
Window::terminate();
return 1;
}
linesShader = load_shader("res/lines.glslv", "res/lines.glslf");
if (linesShader == nullptr){
std::cerr << "failed to load lines shader" << std::endl;
Window::terminate();
return 1;
}
texture = load_texture("res/block.png");
if (texture == nullptr){
std::cerr << "failed to load texture" << std::endl;
delete shader;
Window::terminate();
return 1;
}
return 0;
}
void draw_world(Camera* camera){
glClearColor(0.7f,0.85f,1.0f,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw VAO
shader->use();
shader->uniformMatrix("u_proj", camera->getProjection());
shader->uniformMatrix("u_view", camera->getView());
shader->uniform1f("u_gamma", 1.6f);
shader->uniform3f("u_skyLightColor", 1.8f,1.8f,1.8f);
shader->uniform3f("u_fogColor", 0.7f,0.85f,1.0f);
texture->bind();
mat4 model(1.0f);
const float cameraX = camera->position.x;
const float cameraZ = camera->position.z;
const float camDirX = camera->dir.x;
const float camDirZ = camera->dir.z;
for (size_t i = 0; i < chunks->volume; i++){
Chunk* chunk = chunks->chunks[i];
if (chunk == nullptr)
continue;
Mesh* mesh = chunks->meshes[i];
if (mesh == nullptr)
continue;
// Simple frustum culling (culling chunks behind the camera in 2D - XZ)
if (occlusion){
bool unoccluded = false;
do {
if ((chunk->x*CHUNK_W-cameraX)*camDirX + (chunk->z*CHUNK_D-cameraZ)*camDirZ >= 0.0){
unoccluded = true; break;
}
if (((chunk->x+1)*CHUNK_W-cameraX)*camDirX + (chunk->z*CHUNK_D-cameraZ)*camDirZ >= 0.0){
unoccluded = true; break;
}
if (((chunk->x+1)*CHUNK_W-cameraX)*camDirX + ((chunk->z+1)*CHUNK_D-cameraZ)*camDirZ >= 0.0){
unoccluded = true; break;
}
if ((chunk->x*CHUNK_W-cameraX)*camDirX + ((chunk->z+1)*CHUNK_D-cameraZ)*camDirZ >= 0.0){
unoccluded = true; break;
}
} while (false);
if (!unoccluded)
continue;
}
model = glm::translate(mat4(1.0f), vec3(chunk->x*CHUNK_W+0.5f, chunk->y*CHUNK_H+0.5f, chunk->z*CHUNK_D+0.5f));
shader->uniformMatrix("u_model", model);
mesh->draw(GL_TRIANGLES);
}
crosshairShader->use();
crosshair->draw(GL_LINES);
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());
glLineWidth(2.0f);
lineBatch->render();
}
// Deleting GL objects like shaders, textures
void finalize_assets(){
delete shader;
delete texture;
delete crosshair;
delete crosshairShader;
delete linesShader;
delete lineBatch;
}
// Save all world data to files
void write_world(){
void write_world(WorldFiles* wfile, Chunks* chunks){
for (unsigned int i = 0; i < chunks->volume; i++){
Chunk* chunk = chunks->chunks[i];
if (chunk == nullptr)
@ -216,47 +60,242 @@ void write_world(){
}
// Deleting world data from memory
void close_world(){
void close_world(WorldFiles* wfile, Chunks* chunks){
delete chunks;
delete wfile;
}
#define CROUCH_SPEED_MUL 0.25f
#define CROUCH_SHIFT_Y -0.2f
#define RUN_SPEED_MUL 1.5f
#define CROUCH_ZOOM 0.9f
#define RUN_ZOOM 1.1f
#define C_ZOOM 0.1f
#define ZOOM_SPEED 16.0f
#define DEFAULT_AIR_DAMPING 0.1f
#define PLAYER_NOT_ONGROUND_DAMPING 10.0f
#define CAMERA_SHAKING_OFFSET 0.025f
#define CAMERA_SHAKING_OFFSET_Y 0.031f
#define CAMERA_SHAKING_SPEED 1.6f
#define CAMERA_SHAKING_DELTA_K 10.0f
#define FLIGHT_SPEED_MUL 5.0f
#define JUMP_FORCE 7.0f
void update_controls(PhysicsSolver* physics,
Chunks* chunks,
Player* player,
float delta){
if (Events::jpressed(GLFW_KEY_TAB)){
Events::toogleCursor();
}
for (int i = 1; i < 10; i++){
if (Events::jpressed(GLFW_KEY_0+i)){
player->choosenBlock = i;
}
}
// Controls
Camera* camera = player->camera;
Hitbox* hitbox = player->hitbox;
bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL);
bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint;
bool zoom = Events::pressed(GLFW_KEY_C);
float speed = player->speed;
if (player->flight){
speed *= FLIGHT_SPEED_MUL;
}
int substeps = (int)(delta * 1000);
substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps));
physics->step(chunks, hitbox, delta, substeps, shift, player->flight ? 0.0f : 1.0f);
camera->position.x = hitbox->position.x;
camera->position.y = hitbox->position.y + 0.5f;
camera->position.z = hitbox->position.z;
if (player->flight && hitbox->grounded)
player->flight = false;
// Camera shaking
player->interpVel = player->interpVel * (1.0f - delta * 5) + hitbox->velocity * delta * 0.1f;
if (hitbox->grounded && player->interpVel.y < 0.0f){
player->interpVel.y *= -30.0f;
}
float factor = hitbox->grounded ? length(vec2(hitbox->velocity.x, hitbox->velocity.z)) : 0.0f;
player->cameraShakingTimer += delta * factor * CAMERA_SHAKING_SPEED;
float shakeTimer = player->cameraShakingTimer;
player->cameraShaking = player->cameraShaking * (1.0f - delta * CAMERA_SHAKING_DELTA_K) + factor * delta * CAMERA_SHAKING_DELTA_K;
camera->position += camera->right * sin(shakeTimer) * CAMERA_SHAKING_OFFSET * player->cameraShaking;
camera->position += camera->up * abs(cos(shakeTimer)) * CAMERA_SHAKING_OFFSET_Y * player->cameraShaking;
camera->position -= min(player->interpVel * 0.05f, 1.0f);
if (Events::jpressed(GLFW_KEY_F)){
player->flight = !player->flight;
if (player->flight){
hitbox->velocity.y += 1;
hitbox->grounded = false;
}
}
// Field of view manipulations
float dt = min(1.0f, delta * ZOOM_SPEED);
if (dt > 1.0f)
dt = 1.0f;
float zoomValue = 1.0f;
if (shift){
speed *= CROUCH_SPEED_MUL;
camera->position.y += CROUCH_SHIFT_Y;
zoomValue = CROUCH_ZOOM;
} else if (sprint){
speed *= RUN_SPEED_MUL;
zoomValue = RUN_ZOOM;
}
if (zoom)
zoomValue *= C_ZOOM;
camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt);
if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){
hitbox->velocity.y = JUMP_FORCE;
}
vec3 dir(0,0,0);
if (Events::pressed(GLFW_KEY_W)){
dir.x += camera->dir.x;
dir.z += camera->dir.z;
}
if (Events::pressed(GLFW_KEY_S)){
dir.x -= camera->dir.x;
dir.z -= camera->dir.z;
}
if (Events::pressed(GLFW_KEY_D)){
dir.x += camera->right.x;
dir.z += camera->right.z;
}
if (Events::pressed(GLFW_KEY_A)){
dir.x -= camera->right.x;
dir.z -= camera->right.z;
}
hitbox->linear_damping = DEFAULT_AIR_DAMPING;
if (player->flight){
hitbox->linear_damping = PLAYER_NOT_ONGROUND_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9;
if (Events::pressed(GLFW_KEY_SPACE)){
hitbox->velocity.y += speed * delta * 9;
}
if (Events::pressed(GLFW_KEY_LEFT_SHIFT)){
hitbox->velocity.y -= speed * delta * 9;
}
}
if (length(dir) > 0.0f){
dir = normalize(dir);
if (!hitbox->grounded)
hitbox->linear_damping = PLAYER_NOT_ONGROUND_DAMPING;
hitbox->velocity.x += dir.x * speed * delta * 9;
hitbox->velocity.z += dir.z * speed * delta * 9;
}
if (Events::_cursor_locked){
player->camY += -Events::deltaY / Window::height * 2;
player->camX += -Events::deltaX / Window::height * 2;
if (player->camY < -radians(89.0f)){
player->camY = -radians(89.0f);
}
if (player->camY > radians(89.0f)){
player->camY = radians(89.0f);
}
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
}
}
void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting){
Camera* camera = player->camera;
vec3 end;
vec3 norm;
vec3 iend;
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
if (vox != nullptr){
lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
if (Events::jclicked(GLFW_MOUSE_BUTTON_1)){
int x = (int)iend.x;
int y = (int)iend.y;
int z = (int)iend.z;
chunks->set(x,y,z, 0);
lighting->onBlockSet(x,y,z,0);
}
if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){
int x = (int)(iend.x)+(int)(norm.x);
int y = (int)(iend.y)+(int)(norm.y);
int z = (int)(iend.z)+(int)(norm.z);
if (!physics->isBlockInside(x,y,z, player->hitbox)){
chunks->set(x, y, z, player->choosenBlock);
lighting->onBlockSet(x,y,z, player->choosenBlock);
}
}
}
}
int WIDTH = 1280;
int HEIGHT = 720;
#define GRAVITY 19.6f
#define DEFAULT_PLAYER_SPEED 4.0f
vec3 spawnpoint(-320, 255, 32);
int main() {
setup_definitions();
Window::initialize(WIDTH, HEIGHT, "Window 2.0");
Window::initialize(WIDTH, HEIGHT, "VoxelEngine Part-11");
Events::initialize();
int result = initialize_assets();
std::cout << "-- loading assets" << std::endl;
Assets* assets = new Assets();
int result = initialize_assets(assets);
if (result){
delete assets;
Window::terminate();
return result;
}
std::cout << "-- loading world" << std::endl;
Camera *camera = new Camera(spawnpoint, radians(90.0f));
WorldFiles *wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8));
Chunks *chunks = new Chunks(34,1,34, 0,0,0);
Player* player = new Player(vec3(camera->position), DEFAULT_PLAYER_SPEED, camera);
wfile->readPlayer(player);
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
std::cout << "-- preparing systems" << std::endl;
wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8));
chunks = new Chunks(32,1,32, 0,0,0);
VoxelRenderer renderer(1024*1024);
lineBatch = new LineBatch(4096);
PhysicsSolver physics(vec3(0,-16.0f,0));
PhysicsSolver physics(vec3(0,-GRAVITY,0));
Lighting lighting(chunks);
Lighting::initialize(chunks);
init_renderer();
crosshair = new Mesh(vertices, 4, attrs);
Camera* camera = new Camera(vec3(32,32,32), radians(90.0f));
Hitbox* hitbox = new Hitbox(vec3(32,120,32), vec3(0.2f,0.9f,0.2f));
ChunksController chunksController(chunks, &lighting);
float lastTime = glfwGetTime();
float delta = 0.0f;
float camX = 0.0f;
float camY = 0.0f;
float playerSpeed = 4.0f;
int choosenBlock = 1;
long frame = 0;
glfwSwapInterval(0);
bool occlusion = false;
glfwSwapInterval(1);
std::cout << "-- initializing finished" << std::endl;
while (!Window::isShouldClose()){
frame++;
@ -264,131 +303,40 @@ int main() {
delta = currentTime - lastTime;
lastTime = currentTime;
//if (frame % 240 == 0)
// std::cout << delta << std::endl;
if (Events::jpressed(GLFW_KEY_ESCAPE)){
Window::setShouldClose(true);
}
if (Events::jpressed(GLFW_KEY_O)){
occlusion = !occlusion;
}
if (Events::jpressed(GLFW_KEY_ESCAPE)){
Window::setShouldClose(true);
}
if (Events::jpressed(GLFW_KEY_TAB)){
Events::toogleCursor();
}
for (int i = 1; i < 6; i++){
if (Events::jpressed(GLFW_KEY_0+i)){
choosenBlock = i;
}
}
// Controls
bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL);
bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint;
float speed = playerSpeed;
int substeps = (int)(delta * 1000);
substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps));
physics.step(chunks, hitbox, delta, substeps, shift);
camera->position.x = hitbox->position.x;
camera->position.y = hitbox->position.y + 0.5f;
camera->position.z = hitbox->position.z;
float dt = min(1.0f, delta * 16);
if (shift){
speed *= 0.25f;
camera->position.y -= 0.2f;
camera->zoom = 0.9f * dt + camera->zoom * (1.0f - dt);
} else if (sprint){
speed *= 1.5f;
camera->zoom = 1.1f * dt + camera->zoom * (1.0f - dt);
} else {
camera->zoom = dt + camera->zoom * (1.0f - dt);
}
if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){
hitbox->velocity.y = 6.0f;
}
vec3 dir(0,0,0);
if (Events::pressed(GLFW_KEY_W)){
dir.x += camera->dir.x;
dir.z += camera->dir.z;
}
if (Events::pressed(GLFW_KEY_S)){
dir.x -= camera->dir.x;
dir.z -= camera->dir.z;
}
if (Events::pressed(GLFW_KEY_D)){
dir.x += camera->right.x;
dir.z += camera->right.z;
}
if (Events::pressed(GLFW_KEY_A)){
dir.x -= camera->right.x;
dir.z -= camera->right.z;
}
if (length(dir) > 0.0f)
dir = normalize(dir);
hitbox->velocity.x = dir.x * speed;
hitbox->velocity.z = dir.z * speed;
update_controls(&physics, chunks, player, delta);
update_interaction(chunks, &physics, player, &lighting);
chunks->setCenter(wfile, camera->position.x,0,camera->position.z);
chunks->_buildMeshes(&renderer);
chunks->loadVisible(wfile);
chunksController._buildMeshes(&renderer, frame);
if (Events::_cursor_locked){
camY += -Events::deltaY / Window::height * 2;
camX += -Events::deltaX / Window::height * 2;
int freeLoaders = chunksController.countFreeLoaders();
for (int i = 0; i < freeLoaders; i++)
chunksController.loadVisible(wfile);
if (camY < -radians(89.0f)){
camY = -radians(89.0f);
}
if (camY > radians(89.0f)){
camY = radians(89.0f);
}
camera->rotation = mat4(1.0f);
camera->rotate(camY, camX, 0);
}
{
vec3 end;
vec3 norm;
vec3 iend;
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
if (vox != nullptr){
lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
if (Events::jclicked(GLFW_MOUSE_BUTTON_1)){
int x = (int)iend.x;
int y = (int)iend.y;
int z = (int)iend.z;
chunks->set(x,y,z, 0);
Lighting::onBlockSet(x,y,z,0);
}
if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){
int x = (int)(iend.x)+(int)(norm.x);
int y = (int)(iend.y)+(int)(norm.y);
int z = (int)(iend.z)+(int)(norm.z);
if (!physics.isBlockInside(x,y,z, hitbox)){
chunks->set(x, y, z, choosenBlock);
Lighting::onBlockSet(x,y,z, choosenBlock);
}
}
}
}
draw_world(camera);
draw_world(camera, assets, chunks, occlusion);
Window::swapBuffers();
Events::pullEvents();
}
std::cout << "-- saving world" << std::endl;
write_world();
close_world();
wfile->writePlayer(player);
write_world(wfile, chunks);
close_world(wfile, chunks);
Lighting::finalize();
finalize_assets();
std::cout << "-- shutting down" << std::endl;
delete assets;
finalize_renderer();
Events::finalize();
Window::terminate();
return 0;
}

View File

@ -11,7 +11,9 @@ public:
unsigned char emission[3];
unsigned char drawGroup = 0;
bool lightPassing = false;
bool skyLightPassing = false;
bool obstacle = true;
bool selectable = true;
Block(unsigned int id, int texture);
};

View File

@ -27,3 +27,20 @@ bool Chunk::isEmpty(){
}
return true;
}
Chunk* Chunk::clone() const {
Chunk* other = new Chunk(x,y,z);
for (int i = 0; i < CHUNK_VOL; i++)
other->voxels[i] = voxels[i];
other->lightmap->set(lightmap);
return other;
}
void Chunk::incref(){
references++;
}
void Chunk::decref(){
if (--references <= 0)
delete this;
}

View File

@ -15,10 +15,18 @@ public:
voxel* voxels;
Lightmap* lightmap;
bool modified = true;
bool ready = false;
bool loaded = false;
int surrounding = 0;
int references = 1;
Chunk(int x, int y, int z);
~Chunk();
bool isEmpty();
Chunk* clone() const;
void incref();
void decref();
};
#endif /* VOXELS_CHUNK_H_ */

View File

@ -6,14 +6,8 @@
#include "../lighting/Lightmap.h"
#include "../files/WorldFiles.h"
#include "../lighting/Lighting.h"
#include "../graphics/VoxelRenderer.h"
#include "../graphics/Mesh.h"
#include <glm/glm.hpp>
using namespace glm;
#include <math.h>
#include <limits.h>
@ -33,82 +27,12 @@ Chunks::Chunks(int w, int h, int d, int ox, int oy, int oz) : w(w), h(h), d(d),
Chunks::~Chunks(){
for (size_t i = 0; i < volume; i++){
delete chunks[i];
if (chunks[i])
chunks[i]->decref();
}
delete[] chunks;
}
bool Chunks::_buildMeshes(VoxelRenderer* renderer) {
int nearX = 0;
int nearY = 0;
int nearZ = 0;
int minDistance = 1000000000;
for (unsigned int y = 0; y < h; y++){
for (unsigned int z = 1; z < d-1; z++){
for (unsigned int x = 1; x < w-1; x++){
int index = (y * d + z) * w + x;
Chunk* chunk = chunks[index];
if (chunk == nullptr)
continue;
Mesh* mesh = meshes[index];
if (mesh != nullptr && !chunk->modified)
continue;
int lx = x - w / 2;
int ly = y - h / 2;
int lz = z - d / 2;
int distance = (lx * lx + ly * ly + lz * lz);
if (distance < minDistance){
minDistance = distance;
nearX = x;
nearY = y;
nearZ = z;
}
}
}
}
int index = (nearY * d + nearZ) * w + nearX;
Chunk* closes[27];
Chunk* chunk = chunks[index];
if (chunk == nullptr)
return false;
Mesh* mesh = meshes[index];
if (mesh == nullptr || chunk->modified){
if (mesh != nullptr)
delete mesh;
if (chunk->isEmpty()){
meshes[index] = nullptr;
return false;
}
chunk->modified = false;
for (int i = 0; i < 27; i++)
closes[i] = nullptr;
for (size_t j = 0; j < volume; j++){
Chunk* other = chunks[j];
if (other == nullptr)
continue;
int ox = other->x - chunk->x;
int oy = other->y - chunk->y;
int oz = other->z - chunk->z;
if (abs(ox) > 1 || abs(oy) > 1 || abs(oz) > 1)
continue;
ox += 1;
oy += 1;
oz += 1;
closes[(oy * 3 + oz) * 3 + ox] = other;
}
mesh = renderer->render(chunk, (const Chunk**)closes);
meshes[index] = mesh;
return true;
}
return false;
}
voxel* Chunks::get(int x, int y, int z){
x -= ox * CHUNK_W;
y -= oy * CHUNK_H;
@ -158,6 +82,27 @@ unsigned char Chunks::getLight(int x, int y, int z, int channel){
return chunk->lightmap->get(lx,ly,lz, channel);
}
unsigned short Chunks::getLight(int x, int y, int z){
x -= ox * CHUNK_W;
y -= oy * CHUNK_H;
z -= oz * CHUNK_D;
int cx = x / CHUNK_W;
int cy = y / CHUNK_H;
int cz = z / CHUNK_D;
if (x < 0) cx--;
if (y < 0) cy--;
if (z < 0) cz--;
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= h || cz >= d)
return 0;
Chunk* chunk = chunks[(cy * d + cz) * w + cx];
if (chunk == nullptr)
return 0;
int lx = x - cx * CHUNK_W;
int ly = y - cy * CHUNK_H;
int lz = z - cz * CHUNK_D;
return chunk->lightmap->get(lx,ly,lz);
}
Chunk* Chunks::getChunkByVoxel(int x, int y, int z){
x -= ox * CHUNK_W;
y -= oy * CHUNK_H;
@ -248,7 +193,7 @@ voxel* Chunks::rayCast(vec3 a, vec3 dir, float maxDist, vec3& end, vec3& norm, v
while (t <= maxDist){
voxel* voxel = get(ix, iy, iz);
if (voxel == nullptr || voxel->id){
if (voxel == nullptr || Block::blocks[voxel->id]->selectable){
end.x = px + t * dx;
end.y = py + t * dy;
end.z = pz + t * dz;
@ -317,46 +262,6 @@ void Chunks::setCenter(WorldFiles* worldFiles, int x, int y, int z) {
translate(worldFiles, cx,cy,cz);
}
bool Chunks::loadVisible(WorldFiles* worldFiles){
int nearX = 0;
int nearY = 0;
int nearZ = 0;
int minDistance = (w/2)*(w/2);
for (unsigned int y = 0; y < h; y++){
for (unsigned int z = 1; z < d-1; z++){
for (unsigned int x = 1; x < w-1; x++){
int index = (y * d + z) * w + x;
Chunk* chunk = chunks[index];
if (chunk != nullptr)
continue;
int lx = x - w / 2;
int ly = y - h / 2;
int lz = z - d / 2;
int distance = (lx * lx + ly * ly + lz * lz);
if (distance < minDistance){
minDistance = distance;
nearX = x;
nearY = y;
nearZ = z;
}
}
}
}
int index = (nearY * d + nearZ) * w + nearX;
Chunk* chunk = chunks[index];
if (chunk != nullptr)
return false;
chunk = new Chunk(nearX+ox,nearY+oy,nearZ+oz);
if (!worldFiles->getChunk(chunk->x, chunk->z, (char*)chunk->voxels)){
WorldGenerator::generate(chunk->voxels, chunk->x, chunk->y, chunk->z);
}
chunks[index] = chunk;
Lighting::onChunkLoaded(ox+nearX, oy+nearY, oz+nearZ);
return true;
}
void Chunks::translate(WorldFiles* worldFiles, int dx, int dy, int dz){
for (unsigned int i = 0; i < volume; i++){
chunksSecond[i] = nullptr;
@ -374,7 +279,7 @@ void Chunks::translate(WorldFiles* worldFiles, int dx, int dy, int dz){
Mesh* mesh = meshes[(y * d + z) * w + x];
if (nx < 0 || ny < 0 || nz < 0 || nx >= w || ny >= h || nz >= d){
worldFiles->put((const char*)chunk->voxels, chunk->x, chunk->z);
delete chunk;
chunk->decref();
delete mesh;
continue;
}
@ -395,3 +300,33 @@ void Chunks::translate(WorldFiles* worldFiles, int dx, int dy, int dz){
oy += dy;
oz += dz;
}
void Chunks::_setOffset(int x, int y, int z){
ox = x;
oy = y;
oz = z;
}
bool Chunks::putChunk(Chunk* chunk) {
int x = chunk->x;
int y = chunk->y;
int z = chunk->z;
x -= ox;
y -= oy;
z -= oz;
if (x < 0 || y < 0 || z < 0 || x >= w || y >= h || z >= d)
return false;
chunks[(y * d + z) * w + x] = chunk;
return true;
}
void Chunks::clear(bool freeMemory){
for (size_t i = 0; i < volume; i++){
if (freeMemory){
chunks[i]->decref();
delete meshes[i];
}
chunks[i] = nullptr;
meshes[i] = nullptr;
}
}

View File

@ -26,20 +26,25 @@ public:
Chunks(int w, int h, int d, int ox, int oy, int oz);
~Chunks();
bool putChunk(Chunk* chunk);
Chunk* getChunk(int x, int y, int z);
Chunk* getChunkByVoxel(int x, int y, int z);
voxel* get(int x, int y, int z);
unsigned short getLight(int x, int y, int z);
unsigned char getLight(int x, int y, int z, int channel);
void set(int x, int y, int z, int id);
voxel* rayCast(vec3 start, vec3 dir, float maxLength, vec3& end, vec3& norm, vec3& iend);
bool isObstacle(int x, int y, int z);
// does not move chunks inside
void _setOffset(int x, int y, int z);
void setCenter(WorldFiles* worldFiles, int x, int y, int z);
void translate(WorldFiles* worldFiles, int x, int y, int z);
bool loadVisible(WorldFiles* worldFiles);
bool _buildMeshes(VoxelRenderer* renderer);
void clear(bool freeMemory);
};
#endif /* VOXELS_CHUNKS_H_ */

View File

@ -0,0 +1,216 @@
#include "ChunksController.h"
#include "Chunk.h"
#include "Chunks.h"
#include "WorldGenerator.h"
#include "../graphics/Mesh.h"
#include "../graphics/VoxelRenderer.h"
#include "../lighting/Lighting.h"
#include "../files/WorldFiles.h"
#include "ChunksLoader.h"
#include <iostream>
#ifdef _WIN32
#define _WIN32_WINNT 0x0501
#include <mingw.thread.h>
#else
#include <thread>
#endif
#define MIN_SURROUNDING 9
ChunksController::ChunksController(Chunks* chunks, Lighting* lighting) : chunks(chunks), lighting(lighting){
loadersCount = std::thread::hardware_concurrency() - 1;
if (loadersCount <= 0)
loadersCount = 1;
loaders = new ChunksLoader*[loadersCount];
for (int i = 0; i < loadersCount; i++){
loaders[i] = new ChunksLoader();
}
std::cout << "created " << loadersCount << " loaders" << std::endl;
}
ChunksController::~ChunksController(){
for (int i = 0; i < loadersCount; i++)
delete loaders[i];
delete[] loaders;
}
int ChunksController::countFreeLoaders(){
int count = 0;
for (int i = 0; i < loadersCount; i++){
if (!loaders[i]->isBusy())
count++;
}
return count;
}
bool ChunksController::loadVisible(WorldFiles* worldFiles){
const int w = chunks->w;
const int h = chunks->h;
const int d = chunks->d;
const int ox = chunks->ox;
const int oy = chunks->oy;
const int oz = chunks->oz;
int nearX = 0;
int nearY = 0;
int nearZ = 0;
int minDistance = (w/2)*(w/2);
for (int y = 0; y < h; y++){
for (int z = 2; z < d-2; z++){
for (int x = 2; x < w-2; x++){
int index = (y * d + z) * w + x;
Chunk* chunk = chunks->chunks[index];
if (chunk != nullptr){
int surrounding = 0;
for (int oz = -1; oz <= 1; oz++){
for (int ox = -1; ox <= 1; ox++){
Chunk* other = chunks->getChunk(chunk->x+ox, chunk->y, chunk->z+oz);
if (other != nullptr && other->ready) surrounding++;
}
}
chunk->surrounding = surrounding;
continue;
}
int lx = x - w / 2;
int ly = y - h / 2;
int lz = z - d / 2;
int distance = (lx * lx + ly * ly + lz * lz);
if (distance < minDistance){
minDistance = distance;
nearX = x;
nearY = y;
nearZ = z;
}
}
}
}
int index = (nearY * d + nearZ) * w + nearX;
Chunk* chunk = chunks->chunks[index];
if (chunk != nullptr)
return false;
ChunksLoader* freeLoader = nullptr;
for (int i = 0; i < loadersCount; i++){
ChunksLoader* loader = loaders[i];
if (loader->isBusy()){
continue;
}
freeLoader = loader;
break;
}
if (freeLoader == nullptr)
return false;
chunk = new Chunk(nearX+ox,nearY+oy,nearZ+oz);
if (worldFiles->getChunk(chunk->x, chunk->z, (char*)chunk->voxels))
chunk->loaded = true;
chunks->chunks[index] = chunk;
Chunk* closes[27];
for (int i = 0; i < 27; i++)
closes[i] = nullptr;
for (size_t j = 0; j < chunks->volume; j++){
Chunk* other = chunks->chunks[j];
if (other == nullptr)
continue;
if (!other->ready)
continue;
int ox = other->x - chunk->x;
int oy = other->y - chunk->y;
int oz = other->z - chunk->z;
if (abs(ox) > 1 || abs(oy) > 1 || abs(oz) > 1)
continue;
ox += 1;
oy += 1;
oz += 1;
closes[(oy * 3 + oz) * 3 + ox] = other;
}
freeLoader->perform(chunk, (Chunk**)closes);
return true;
}
bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) {
const int w = chunks->w;
const int h = chunks->h;
const int d = chunks->d;
int nearX = 0;
int nearY = 0;
int nearZ = 0;
int minDistance = 1000000000;
for (int y = 0; y < h; y++){
for (int z = 1; z < d-1; z++){
for (int x = 1; x < w-1; x++){
int index = (y * d + z) * w + x;
Chunk* chunk = chunks->chunks[index];
if (chunk == nullptr)
continue;
Mesh* mesh = chunks->meshes[index];
if (mesh != nullptr && !chunk->modified)
continue;
if (!chunk->ready || chunk->surrounding < MIN_SURROUNDING){
continue;
}
int lx = x - w / 2;
int ly = y - h / 2;
int lz = z - d / 2;
int distance = (lx * lx + ly * ly + lz * lz);
if (distance < minDistance){
minDistance = distance;
nearX = x;
nearY = y;
nearZ = z;
}
}
}
}
int index = (nearY * d + nearZ) * w + nearX;
Chunk* chunk = chunks->chunks[index];
if (chunk == nullptr){
return false;
}
Mesh* mesh = chunks->meshes[index];
if (mesh == nullptr || chunk->modified){
Chunk* closes[27];
if (mesh != nullptr)
delete mesh;
if (chunk->isEmpty()){
chunks->meshes[index] = nullptr;
return false;
}
chunk->modified = false;
for (int i = 0; i < 27; i++)
closes[i] = nullptr;
for (size_t j = 0; j < chunks->volume; j++){
Chunk* other = chunks->chunks[j];
if (other == nullptr)
continue;
if (!other->ready)
continue;
int ox = other->x - chunk->x;
int oy = other->y - chunk->y;
int oz = other->z - chunk->z;
if (abs(ox) > 1 || abs(oy) > 1 || abs(oz) > 1)
continue;
ox += 1;
oy += 1;
oz += 1;
closes[(oy * 3 + oz) * 3 + ox] = other;
}
mesh = renderer->render(chunk, (const Chunk**)closes);
chunks->meshes[index] = mesh;
return true;
}
return false;
}

View File

@ -0,0 +1,25 @@
#ifndef VOXELS_CHUNKSCONTROLLER_H_
#define VOXELS_CHUNKSCONTROLLER_H_
class Chunks;
class Lighting;
class WorldFiles;
class VoxelRenderer;
class ChunksLoader;
class ChunksController {
private:
Chunks* chunks;
Lighting* lighting;
ChunksLoader** loaders;
int loadersCount;
public:
ChunksController(Chunks* chunks, Lighting* lighting);
~ChunksController();
int countFreeLoaders();
bool loadVisible(WorldFiles* worldFiles);
bool _buildMeshes(VoxelRenderer* renderer, int tick);
};
#endif /* VOXELS_CHUNKSCONTROLLER_H_ */

View File

@ -0,0 +1,67 @@
#include "ChunksLoader.h"
#include <chrono>
#include "Chunk.h"
#include "Chunks.h"
#include "WorldGenerator.h"
#include "../lighting/Lighting.h"
#include <iostream>
#define CLOSES_C 27
void ChunksLoader::_thread(){
Chunks chunks(3,3,3, -1,-1,-1);
Lighting lighting(&chunks);
while (working){
if (current == nullptr){
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
Chunk* chunk = current;
chunks._setOffset(chunk->x-1, chunk->y-1, chunk->z-1);
for (size_t i = 0; i < CLOSES_C; i++){
Chunk* other = closes[i];
if (other){
chunks.putChunk(other);
}
}
if (!chunk->loaded){
WorldGenerator::generate(chunk->voxels, chunk->x, chunk->y, chunk->z);
}
chunks.putChunk(chunk);
lighting.onChunkLoaded(chunk->x, chunk->y, chunk->z, true);
chunks.clear(false);
for (int i = 0; i < CLOSES_C; i++){
Chunk* other = closes[i];
if (other)
other->decref();
}
chunk->ready = true;
current = nullptr;
chunk->decref();
}
}
void ChunksLoader::perform(Chunk* chunk, Chunk** closes_passed){
if (isBusy()){
std::cerr << "performing while busy" << std::endl;
return;
}
chunk->incref();
if (closes == nullptr){
closes = new Chunk*[CLOSES_C];
}
for (int i = 0; i < CLOSES_C; i++){
Chunk* other = closes_passed[i];
if (other == nullptr)
closes[i] = nullptr;
else {
other->incref();
closes[i] = other;
}
}
current = chunk;
}

42
src/voxels/ChunksLoader.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef VOXELS_CHUNKSLOADER_H_
#define VOXELS_CHUNKSLOADER_H_
#ifdef _WIN32
#define _WIN32_WINNT 0x0501
#include <mingw.thread.h>
#else
#include <thread>
#endif
#include <atomic>
class Chunk;
class ChunksLoader final {
private:
std::thread loaderThread;
void _thread();
std::atomic<Chunk*> current {nullptr};
std::atomic<Chunk**> closes {nullptr};
std::atomic<bool> working {true};
public:
ChunksLoader() : loaderThread{} {
loaderThread = std::thread{&ChunksLoader::_thread, this};
}
~ChunksLoader(){
working = false;
loaderThread.join();
}
bool isBusy(){
return current != nullptr;
}
void perform(Chunk* chunk, Chunk** closes_passed);
void stop(){
working = false;
}
};
#endif /* VOXELS_CHUNKSLOADER_H_ */

View File

@ -5,29 +5,129 @@
#include <math.h>
#include <glm/glm.hpp>
#include <glm/gtc/noise.hpp>
#define FNL_IMPL
#include "../maths/FastNoiseLite.h"
#include <time.h>
class PseudoRandom {
unsigned seed;
public:
PseudoRandom(){
seed = (unsigned)time(0);
}
int rand(){
seed = (8253729 * seed + 2396403);
return seed % 32768;
}
void setSeed(int number){
seed = (unsigned)number+8253729;
rand();
}
};
float calc_height(fnl_state *noise, int real_x, int real_z){
const float s = 0.2f;
float height = fnlGetNoise3D(noise, real_x*0.0125f*s*32,real_z*0.0125f*s*32, 0.0f);
height += fnlGetNoise3D(noise, real_x*0.025f*s*32,real_z*0.025f*s*32, 0.0f)*0.5f;
height += fnlGetNoise3D(noise, real_x*0.05f*s*32,real_z*0.05f*s*32, 0.0f)*0.25f;
height += fnlGetNoise3D(noise, real_x*0.1f*s*32,real_z*0.1f*s*32, 0.0f)*0.225f;
height += fnlGetNoise3D(noise, real_x*0.2f*s*32,real_z*0.2f*s*32, 0.0f)*0.125f;
height += fnlGetNoise3D(noise, real_x*0.4f*s*32,real_z*0.4f*s*32, 0.0f)*0.125f*0.5F;
height = height * 0.5f + 0.5f;
height *= height;
height *= (140.0f)*0.12f/s;
height += (42)*0.12f/s;
return height;
}
float calc_height_faster(fnl_state *noise, int real_x, int real_z){
const float s = 0.2f;
float height = fnlGetNoise3D(noise, real_x*0.0125f*s*32,real_z*0.0125f*s*32, 0.0f);
height += fnlGetNoise3D(noise, real_x*0.025f*s*32,real_z*0.025f*s*32, 0.0f)*0.5f;
height += fnlGetNoise3D(noise, real_x*0.05f*s*32,real_z*0.05f*s*32, 0.0f)*0.25f;
height += fnlGetNoise3D(noise, real_x*0.1f*s*32,real_z*0.1f*s*32, 0.0f)*0.225f;
height += fnlGetNoise3D(noise, real_x*0.2f*s*32,real_z*0.2f*s*32, 0.0f)*0.125f;
//height += fnlGetNoise3D(noise, real_x*0.4f*s*32,real_z*0.4f*s*32, 0.0f)*0.125f*0.5F;
height = height * 0.5f + 0.5f;
height *= height;
height *= (140.0f)*0.12f/s;
height += (42)*0.12f/s;
return height;
}
#include <iostream>
int generate_tree(fnl_state *noise, PseudoRandom* random, const float* heights, int real_x, int real_y, int real_z, int tileSize){
const int tileX = floor((double)real_x/(double)tileSize);
const int tileY = floor((double)real_z/(double)tileSize);
random->setSeed(tileX*4325261+tileY*12160951+tileSize*9431111);
bool gentree = fnlGetNoise3D(noise, tileX*3.0f+633, 0.0, tileY*3.0f) > -0.1f && (random->rand() % 10) < 7;
if (!gentree)
return 0;
const int randomX = (random->rand() % (tileSize/2)) - tileSize/4;
const int randomZ = (random->rand() % (tileSize/2)) - tileSize/4;
int centerX = tileX * tileSize + tileSize/2 + randomX;
int centerY = tileY * tileSize + tileSize/2 + randomZ;
int height = (int)calc_height_faster(noise, centerX, centerY);
if (height < 55)
return 0;
int lx = real_x - centerX;
int radius = random->rand() % 4 + 3;
int ly = real_y - height - 3 * radius;
int lz = real_z - centerY;
if (lx == 0 && lz == 0 && real_y - height < 4*radius)
return 6;
if (lx*lx+ly*ly/2+lz*lz < radius*radius)
return 7;
return 0;
}
void WorldGenerator::generate(voxel* voxels, int cx, int cy, int cz){
const float s = 0.25f;
fnl_state noise = fnlCreateState();
noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
PseudoRandom random;
float heights[CHUNK_VOL];
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
int real_x = x + cx * CHUNK_W;
int real_z = z + cz * CHUNK_D;
float height = glm::perlin(glm::vec3(real_x*0.0125f*s,real_z*0.0125f*s, 0.0f));
height += glm::perlin(glm::vec3(real_x*0.025f*s,real_z*0.025f*s, 0.0f))*0.5f;
height += glm::perlin(glm::vec3(real_x*0.05f*s,real_z*0.05f*s, 0.0f))*0.25f;
height += glm::perlin(glm::vec3(real_x*0.1f*s,real_z*0.1f*s, 0.0f))*0.225f;
height += glm::perlin(glm::vec3(real_x*0.2f*s,real_z*0.2f*s, 0.0f))*0.125f;
height = height * 0.5f + 0.5f;
height *= height;
height *= 140.0f;
height += 48;
float height = calc_height(&noise, real_x, real_z);
heights[z*CHUNK_W+x] = height;
}
}
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
int real_x = x + cx * CHUNK_W;
int real_z = z + cz * CHUNK_D;
float height = heights[z*CHUNK_W+x];
for (int y = 0; y < CHUNK_H; y++){
int real_y = y + cy * CHUNK_H;
int id = 0;
int id = real_y < 55 ? 9 : 0;
if (real_y == (int)height)
id = 2;
else if (real_y < height)
id = 1;
else if (real_y < height){
if (real_y < height-6)
id = 8;
else
id = 1;
} else {
int tree = generate_tree(&noise, &random, heights, real_x, real_y, real_z, 16);
if (tree)
id = tree;
else if ((tree = generate_tree(&noise, &random, heights, real_x, real_y, real_z, 19))){
id = tree;
}else if ((tree = generate_tree(&noise, &random, heights, real_x, real_y, real_z, 23))){
id = tree;
}
}
if (real_y <= 2)
id = 2;
voxels[(y * CHUNK_D + z) * CHUNK_W + x].id = id;

View File

@ -70,6 +70,11 @@ int Events::initialize(){
return 0;
}
void Events::finalize(){
delete[] _keys;
delete[] _frames;
}
bool Events::pressed(int keycode){
if (keycode < 0 || keycode >= _MOUSE_BUTTONS)
return false;

View File

@ -18,6 +18,7 @@ public:
static bool _cursor_started;
static int initialize();
static void finalize();
static void pullEvents();
static bool pressed(int keycode);

156
src/world_render.h Normal file
View File

@ -0,0 +1,156 @@
#ifndef WORLD_RENDERER_CPP
#define WORLD_RENDERER_CPP
#include <vector>
#include <algorithm>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "window/Window.h"
#include "window/Camera.h"
#include "graphics/Mesh.h"
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/LineBatch.h"
#include "voxels/Chunks.h"
#include "voxels/Chunk.h"
float _camera_cx;
float _camera_cz;
Chunks* _chunks;
Mesh *crosshair;
float vertices[] = {
// x y
-0.01f,-0.01f,
0.01f, 0.01f,
-0.01f, 0.01f,
0.01f,-0.01f,
};
int attrs[] = {
2, 0 //null terminator
};
LineBatch *lineBatch;
void init_renderer(){
crosshair = new Mesh(vertices, 4, attrs);
lineBatch = new LineBatch(4096);
}
void finalize_renderer(){
delete crosshair;
delete lineBatch;
}
void draw_chunk(size_t index, Camera* camera, Shader* shader, bool occlusion){
Chunk* chunk = _chunks->chunks[index];
Mesh* mesh = _chunks->meshes[index];
if (mesh == nullptr)
return;
// Simple frustum culling (culling chunks behind the camera in 2D - XZ)
if (occlusion){
const float cameraX = camera->position.x;
const float cameraZ = camera->position.z;
const float camDirX = camera->dir.x;
const float camDirZ = camera->dir.z;
bool unoccluded = false;
do {
if ((chunk->x*CHUNK_W-cameraX)*camDirX + (chunk->z*CHUNK_D-cameraZ)*camDirZ >= 0.0){
unoccluded = true; break;
}
if (((chunk->x+1)*CHUNK_W-cameraX)*camDirX + (chunk->z*CHUNK_D-cameraZ)*camDirZ >= 0.0){
unoccluded = true; break;
}
if (((chunk->x+1)*CHUNK_W-cameraX)*camDirX + ((chunk->z+1)*CHUNK_D-cameraZ)*camDirZ >= 0.0){
unoccluded = true; break;
}
if ((chunk->x*CHUNK_W-cameraX)*camDirX + ((chunk->z+1)*CHUNK_D-cameraZ)*camDirZ >= 0.0){
unoccluded = true; break;
}
} while (false);
if (!unoccluded)
return;
}
mat4 model = glm::translate(mat4(1.0f), vec3(chunk->x*CHUNK_W+0.5f, chunk->y*CHUNK_H+0.5f, chunk->z*CHUNK_D+0.5f));
shader->uniformMatrix("u_model", model);
mesh->draw(GL_TRIANGLES);
}
bool chunks_comparator(size_t i, size_t j) {
Chunk* a = _chunks->chunks[i];
Chunk* b = _chunks->chunks[j];
return ((a->x + 0.5f - _camera_cx)*(a->x + 0.5f - _camera_cx) + (a->z + 0.5f - _camera_cz)*(a->z + 0.5f - _camera_cz)
>
(b->x + 0.5f - _camera_cx)*(b->x + 0.5f - _camera_cx) + (b->z + 0.5f - _camera_cz)*(b->z + 0.5f - _camera_cz));
}
void draw_world(Camera* camera, Assets* assets,
Chunks* chunks, bool occlusion){
glClearColor(0.7f,0.71f,0.73f,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_chunks = chunks;
// Draw VAO
Texture* texture = assets->getTexture("block");
Shader* shader = assets->getShader("main");
Shader* crosshairShader = assets->getShader("crosshair");
Shader* linesShader = assets->getShader("lines");
shader->use();
shader->uniformMatrix("u_proj", camera->getProjection());
shader->uniformMatrix("u_view", camera->getView());
shader->uniform1f("u_gamma", 1.6f);
shader->uniform3f("u_skyLightColor", 1.8f,1.8f,1.8f);
shader->uniform3f("u_fogColor", 0.7f,0.71f,0.73f);
shader->uniform3f("u_cameraPos", camera->position.x,camera->position.y,camera->position.z);
texture->bind();
std::vector<size_t> indices;
for (size_t i = 0; i < chunks->volume; i++){
Chunk* chunk = chunks->chunks[i];
if (chunk == nullptr)
continue;
if (chunks->meshes[i] != nullptr)
indices.push_back(i);
}
float px = camera->position.x / (float)CHUNK_W;
float pz = camera->position.z / (float)CHUNK_D;
_camera_cx = px;
_camera_cz = pz;
std::sort(indices.begin(), indices.end(), chunks_comparator);
for (size_t i = 0; i < indices.size(); i++){
draw_chunk(indices[i], camera, shader, occlusion);
}
crosshairShader->use();
crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);
crosshairShader->uniform1f("u_scale", 1.0f / ((float)Window::height / 1000.0f));
crosshair->draw(GL_LINES);
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());
glLineWidth(2.0f);
lineBatch->line(camera->position.x, camera->position.y-0.5f, camera->position.z, camera->position.x+0.1f, camera->position.y-0.5f, camera->position.z, 1, 0, 0, 1);
lineBatch->line(camera->position.x, camera->position.y-0.5f, camera->position.z, camera->position.x, camera->position.y-0.5f, camera->position.z+0.1f, 0, 0, 1, 1);
lineBatch->render();
}
#endif // WORLD_RENDERER_CPP