Merge pull request #14 from MihailRis/v13

V13
This commit is contained in:
MihailRis 2023-11-01 04:26:27 +03:00 committed by GitHub
commit 18fcf3f141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
76 changed files with 1238 additions and 1541 deletions

View File

@ -7,29 +7,40 @@ file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES})
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
option(VE_USE_SYSTEM_LIBS "Use system installed libraries" ON)
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4)
else()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra
# additional warnings
-Wformat-nonliteral -Wcast-align
-Wpointer-arith -Winline -Wundef
-Wwrite-strings -Wno-unused-parameter)
endif()
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(OpenAL REQUIRED)
find_package(PNG REQUIRED)
if (WIN32)
set(PNGLIB spng)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libs/glfw)
else()
find_package(PNG REQUIRED)
set(PNGLIB PNG::PNG)
endif(WIN32)
set(LIBS "")
if(NOT VE_USE_SYSTEM_LIBS)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libs/glfw)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libs/glm)
set(LIBS glm)
else()
find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)
endif(NOT VE_USE_SYSTEM_LIBS)
if(UNIX)
find_package(Threads REQUIRED)
set(LIBS ${LIBS} Threads::Threads)
endif(UNIX)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie")
endif()
target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW PNG::PNG)
target_link_libraries(${PROJECT_NAME} ${LIBS} glfw OpenGL::GL ${OPENAL_LIBRARY} GLEW::GLEW ${PNGLIB})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/res DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/build)

View File

@ -1,82 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include src/world/subdir.mk
-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
-include src/files/subdir.mk
-include src/audio/subdir.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
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: main-build
# Main-build Target
main-build: voxel_engine
# Tool invocations
voxel_engine: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS)
@echo 'Building target: $@'
@echo 'Invoking: Cross G++ Linker'
g++ -pthread -o "voxel_engine" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(CC_DEPS)$(C++_DEPS)$(EXECUTABLES)$(OBJS)$(C_UPPER_DEPS)$(CXX_DEPS)$(C_DEPS)$(CPP_DEPS) voxel_engine
-@echo ' '
.PHONY: all clean dependents main-build
-include ../makefile.targets

View File

@ -1,8 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
USER_OBJS :=
LIBS := -lglfw -lpng -lGL -lGLEW -lstdc++fs -lopenal

View File

@ -1 +0,0 @@
../res

View File

@ -1,37 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
C_UPPER_SRCS :=
CXX_SRCS :=
C++_SRCS :=
OBJ_SRCS :=
CC_SRCS :=
ASM_SRCS :=
C_SRCS :=
CPP_SRCS :=
O_SRCS :=
S_UPPER_SRCS :=
CC_DEPS :=
C++_DEPS :=
EXECUTABLES :=
OBJS :=
C_UPPER_DEPS :=
CXX_DEPS :=
C_DEPS :=
CPP_DEPS :=
# Every subdirectory with source files must be described here
SUBDIRS := \
src \
src/audio \
src/files \
src/graphics \
src/lighting \
src/loaders \
src/objects \
src/physics \
src/voxels \
src/window \
src/world \

View File

@ -1,27 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/audio/Audio.cpp \
../src/audio/audioutil.cpp
OBJS += \
./src/audio/Audio.o \
./src/audio/audioutil.o
CPP_DEPS += \
./src/audio/Audio.d \
./src/audio/audioutil.d
# Each subdirectory must supply rules for building sources it contributes
src/audio/%.o: ../src/audio/%.cpp src/audio/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

@ -1,27 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/files/WorldFiles.cpp \
../src/files/files.cpp
OBJS += \
./src/files/WorldFiles.o \
./src/files/files.o
CPP_DEPS += \
./src/files/WorldFiles.d \
./src/files/files.d
# Each subdirectory must supply rules for building sources it contributes
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 "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,54 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/graphics/Batch2D.cpp \
../src/graphics/Batch3D.cpp \
../src/graphics/Font.cpp \
../src/graphics/LineBatch.cpp \
../src/graphics/Mesh.cpp \
../src/graphics/Shader.cpp \
../src/graphics/Texture.cpp \
../src/graphics/Sprite.cpp \
../src/graphics/UVRegion.cpp \
../src/graphics/Framebuffer.cpp \
../src/graphics/VoxelRenderer.cpp
OBJS += \
./src/graphics/Batch2D.o \
./src/graphics/Batch3D.o \
./src/graphics/Font.o \
./src/graphics/LineBatch.o \
./src/graphics/Mesh.o \
./src/graphics/Shader.o \
./src/graphics/Texture.o \
./src/graphics/Sprite.o \
./src/graphics/UVRegion.o \
./src/graphics/Framebuffer.o \
./src/graphics/VoxelRenderer.o
CPP_DEPS += \
./src/graphics/Batch2D.d \
./src/graphics/Batch3D.d \
./src/graphics/Font.d \
./src/graphics/LineBatch.d \
./src/graphics/Mesh.d \
./src/graphics/Shader.d \
./src/graphics/Texture.d \
./src/graphics/Sprite.d \
./src/graphics/UVRegion.d \
./src/graphics/Framebuffer.d \
./src/graphics/VoxelRenderer.d
# Each subdirectory must supply rules for building sources it contributes
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 "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,30 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/lighting/LightSolver.cpp \
../src/lighting/Lighting.cpp \
../src/lighting/Lightmap.cpp
OBJS += \
./src/lighting/LightSolver.o \
./src/lighting/Lighting.o \
./src/lighting/Lightmap.o
CPP_DEPS += \
./src/lighting/LightSolver.d \
./src/lighting/Lighting.d \
./src/lighting/Lightmap.d
# Each subdirectory must supply rules for building sources it contributes
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 "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,24 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/loaders/png_loading.cpp
OBJS += \
./src/loaders/png_loading.o
CPP_DEPS += \
./src/loaders/png_loading.d
# Each subdirectory must supply rules for building sources it contributes
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 "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,24 +0,0 @@
################################################################################
# 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

@ -1,27 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/physics/Hitbox.cpp \
../src/physics/PhysicsSolver.cpp
OBJS += \
./src/physics/Hitbox.o \
./src/physics/PhysicsSolver.o
CPP_DEPS += \
./src/physics/Hitbox.d \
./src/physics/PhysicsSolver.d
# Each subdirectory must supply rules for building sources it contributes
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 "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,39 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/Assets.cpp \
../src/player_control.cpp \
../src/hud_render.cpp \
../src/world_render.cpp \
../src/declarations.cpp \
../src/voxel_engine.cpp
OBJS += \
./src/Assets.o \
./src/player_control.o \
./src/hud_render.o \
./src/world_render.o \
./src/declarations.o \
./src/voxel_engine.o
CPP_DEPS += \
./src/Assets.d \
./src/player_control.d \
./src/hud_render.d \
./src/world_render.d \
./src/declarations.d \
./src/voxel_engine.d
# Each subdirectory must supply rules for building sources it contributes
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 "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,42 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
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
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
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/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

@ -1,30 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/window/Camera.cpp \
../src/window/Events.cpp \
../src/window/Window.cpp
OBJS += \
./src/window/Camera.o \
./src/window/Events.o \
./src/window/Window.o
CPP_DEPS += \
./src/window/Camera.d \
./src/window/Events.d \
./src/window/Window.d
# Each subdirectory must supply rules for building sources it contributes
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 "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,27 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/world/Level.cpp \
../src/world/World.cpp
OBJS += \
./src/world/Level.o \
./src/world/World.o
CPP_DEPS += \
./src/world/Level.d \
./src/world/World.d
# Each subdirectory must supply rules for building sources it contributes
src/world/%.o: ../src/world/%.cpp src/world/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

@ -1,80 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-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
-include src/files/subdir.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
OPTIONAL_TOOL_DEPS := \
$(wildcard ../makefile.defs) \
$(wildcard ../makefile.init) \
$(wildcard ../makefile.targets) \
BUILD_ARTIFACT_NAME := VOXEL_ENGINE
BUILD_ARTIFACT_EXTENSION := exe
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: main-build
# 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++ -lpthread -o "VOXEL_ENGINE.exe" $(OBJS) $(USER_OBJS) $(LIBS) -static-libgcc -static-libstdc++
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(CC_DEPS)$(C++_DEPS)$(EXECUTABLES)$(OBJS)$(C_UPPER_DEPS)$(CXX_DEPS)$(CPP_DEPS)$(C_DEPS) VOXEL_ENGINE.exe
-@echo ' '
.PHONY: all clean dependents
-include ../makefile.targets

View File

@ -1,8 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
USER_OBJS :=
LIBS := -lglfw3 -lglew32 -lopengl32 -lspng -lgdi32 -Wl,-Bstatic -lz

View File

@ -1,36 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
C_UPPER_SRCS :=
CXX_SRCS :=
C++_SRCS :=
OBJ_SRCS :=
CC_SRCS :=
ASM_SRCS :=
CPP_SRCS :=
C_SRCS :=
S_UPPER_SRCS :=
O_SRCS :=
CC_DEPS :=
C++_DEPS :=
EXECUTABLES :=
OBJS :=
C_UPPER_DEPS :=
CXX_DEPS :=
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/objects \
src/voxels \
src/objects \
src/window \

View File

@ -1,27 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/files/WorldFiles.cpp \
../src/files/files.cpp
OBJS += \
./src/files/WorldFiles.o \
./src/files/files.o
CPP_DEPS += \
./src/files/WorldFiles.d \
./src/files/files.d
# Each subdirectory must supply rules for building sources it contributes
src/files/%.o: ../src/files/%.cpp src/files/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,35 +0,0 @@
# 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 \
../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 \
./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 \
./src/graphics/Batch2D.d
# Each subdirectory must supply rules for building sources it contributes
src/graphics/%.o: ../src/graphics/%.cpp src/graphics/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,30 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/lighting/LightSolver.cpp \
../src/lighting/Lighting.cpp \
../src/lighting/Lightmap.cpp
OBJS += \
./src/lighting/LightSolver.o \
./src/lighting/Lighting.o \
./src/lighting/Lightmap.o
CPP_DEPS += \
./src/lighting/LightSolver.d \
./src/lighting/Lighting.d \
./src/lighting/Lightmap.d
# Each subdirectory must supply rules for building sources it contributes
src/lighting/%.o: ../src/lighting/%.cpp src/lighting/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,24 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/loaders/png_loading.cpp
OBJS += \
./src/loaders/png_loading.o
CPP_DEPS += \
./src/loaders/png_loading.d
# Each subdirectory must supply rules for building sources it contributes
src/loaders/%.o: ../src/loaders/%.cpp src/loaders/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,20 +0,0 @@
# 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,27 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/physics/Hitbox.cpp \
../src/physics/PhysicsSolver.cpp
OBJS += \
./src/physics/Hitbox.o \
./src/physics/PhysicsSolver.o
CPP_DEPS += \
./src/physics/Hitbox.d \
./src/physics/PhysicsSolver.d
# Each subdirectory must supply rules for building sources it contributes
src/physics/%.o: ../src/physics/%.cpp src/physics/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,23 +0,0 @@
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/voxel_engine.cpp \
../src/Assets.cpp
OBJS += \
./src/voxel_engine.o \
./src/Assets.o
CPP_DEPS += \
./src/voxel_engine.d \
./src/Assets.d
# Each subdirectory must supply rules for building sources it contributes
src/%.o: ../src/%.cpp src/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,42 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
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
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
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/subdir.mk
@echo 'Building file: $<'
@echo 'Invoking: GCC C++ Compiler'
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 ' '

View File

@ -1,30 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/window/Camera.cpp \
../src/window/Events.cpp \
../src/window/Window.cpp
OBJS += \
./src/window/Camera.o \
./src/window/Events.o \
./src/window/Window.o
CPP_DEPS += \
./src/window/Camera.d \
./src/window/Events.d \
./src/window/Window.d
# Each subdirectory must supply rules for building sources it contributes
src/window/%.o: ../src/window/%.cpp src/window/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

@ -12,15 +12,6 @@
- **F** - toggle flight mode
- **Esc** - exit
# Run in linux:
`$ git clone https://github.com/MihailRis/VoxelEngine-Cpp.git`
`$ cd VoxelEngine-Cpp/Debug`
`$ make`
`$ ./voxel_engine`
#### Build with CMake
```sh
git clone --recursive https://github.com/MihailRis/VoxelEngine-Cpp.git

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 16 KiB

BIN
res/block.png.old.old Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

6
run.sh Normal file
View File

@ -0,0 +1,6 @@
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ../
make
cd ..
build/VoxelEngine

79
src/AssetsLoader.cpp Normal file
View File

@ -0,0 +1,79 @@
#include "AssetsLoader.h"
#include "Assets.h"
#include <iostream>
AssetsLoader::AssetsLoader(Assets* assets) : assets(assets) {
}
void AssetsLoader::addLoader(int tag, aloader_func func) {
loaders[tag] = func;
}
void AssetsLoader::add(int tag, const std::string filename, const std::string alias) {
entries.push(aloader_entry{ tag, filename, alias });
}
bool AssetsLoader::hasNext() const {
return !entries.empty();
}
bool AssetsLoader::loadNext() {
const aloader_entry& entry = entries.front();
std::cout << " loading " << entry.filename << " as " << entry.alias << std::endl;
std::cout.flush();
auto found = loaders.find(entry.tag);
if (found == loaders.end()) {
std::cerr << "unknown asset tag " << entry.tag << std::endl;
return false;
}
aloader_func loader = found->second;
bool status = loader(assets, entry.filename, entry.alias);
entries.pop();
return status;
}
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/Font.h"
bool _load_shader(Assets* assets, const std::string& filename, const std::string& name) {
Shader* shader = load_shader(filename + ".glslv", filename + ".glslf");
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, const std::string& filename, const 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;
}
bool _load_font(Assets* assets, const std::string& filename, const std::string& name) {
std::vector<Texture*> pages;
for (size_t i = 0; i <= 4; i++) {
Texture* texture = load_texture(filename + "_" + std::to_string(i) + ".png");
if (texture == nullptr) {
std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl;
return false;
}
pages.push_back(texture);
}
Font* font = new Font(pages);
assets->store(font, name);
return true;
}
void AssetsLoader::createDefaults(AssetsLoader& loader) {
loader.addLoader(ASSET_SHADER, _load_shader);
loader.addLoader(ASSET_TEXTURE, _load_texture);
loader.addLoader(ASSET_FONT, _load_font);
}

38
src/AssetsLoader.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef SRC_ASSETS_LOADER_H
#define SRC_ASSETS_LOADER_H
#include <string>
#include <functional>
#include <map>
#include <queue>
#define ASSET_TEXTURE 1
#define ASSET_SHADER 2
#define ASSET_FONT 3
class Assets;
typedef std::function<bool(Assets*, const std::string&, const std::string&)> aloader_func;
struct aloader_entry {
int tag;
const std::string filename;
const std::string alias;
};
class AssetsLoader {
Assets* assets;
std::map<int, aloader_func> loaders;
std::queue<aloader_entry> entries;
public:
AssetsLoader(Assets* assets);
void addLoader(int tag, aloader_func func);
void add(int tag, const std::string filename, const std::string alias);
bool hasNext() const;
bool loadNext();
static void createDefaults(AssetsLoader& loader);
};
#endif // SRC_ASSETS_LOADER_H

View File

@ -1,70 +1,21 @@
#include "declarations.h"
#include "Assets.h"
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/Font.h"
#include "AssetsLoader.h"
#include "window/Window.h"
#include "voxels/Block.h"
// Shaders, textures
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;
}
void initialize_assets(AssetsLoader* loader) {
loader->add(ASSET_SHADER, "res/main", "main");
loader->add(ASSET_SHADER, "res/crosshair", "crosshair");
loader->add(ASSET_SHADER, "res/lines", "lines");
loader->add(ASSET_SHADER, "res/ui", "ui");
bool _load_font(Assets* assets, std::string filename, std::string name){
std::vector<Texture*> pages;
for (size_t i = 0; i <= 4; i++){
Texture* texture = load_texture(filename+"_"+std::to_string(i)+".png");
if (texture == nullptr){
std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl;
return false;
}
pages.push_back(texture);
}
Font* font = new Font(pages);
assets->store(font, name);
return true;
}
loader->add(ASSET_TEXTURE, "res/block.png", "block");
loader->add(ASSET_TEXTURE, "res/slot.png", "slot");
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;
#define LOAD_FONT(FILENAME, NAME) \
if (!_load_font(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_SHADER("res/ui.glslv", "res/ui.glslf", "ui");
LOAD_TEXTURE("res/block.png", "block");
LOAD_TEXTURE("res/slot.png", "slot");
LOAD_FONT("res/font", "normal");
return 0;
loader->add(ASSET_FONT, "res/font", "normal");
}
// All in-game definitions (blocks, items, etc..)
@ -106,6 +57,7 @@ void setup_definitions() {
block = new Block(BLOCK_WOOD, 7);
block->textureFaces[2] = 8;
block->textureFaces[3] = 8;
block->rotatable = true;
Block::blocks[block->id] = block;
block = new Block(BLOCK_LEAVES, 9);

View File

@ -21,9 +21,9 @@
#define BLOCK_METAL 15
#define BLOCK_RUST 16
class Assets;
class AssetsLoader;
int initialize_assets(Assets* assets);
void initialize_assets(AssetsLoader* loader);
void setup_definitions();
#endif // DECLARATIONS_H

View File

@ -171,7 +171,7 @@ bool WorldFiles::readChunk(int x, int y, char* out){
input.read(mainBufferIn, compressedSize);
input.close();
decompressRLE(mainBufferIn, compressedSize, out, CHUNK_VOL);
decompressRLE((unsigned char*)mainBufferIn, compressedSize, (unsigned char*)out, CHUNK_VOL);
return true;
}
@ -274,7 +274,7 @@ unsigned int WorldFiles::writeRegion(char* out, int x, int y, char** region){
} else {
int2Bytes(offset, out, i*4);
unsigned int compressedSize = compressRLE(chunk, CHUNK_VOL, compressed);
unsigned int compressedSize = compressRLE((unsigned char*)chunk, CHUNK_VOL, (unsigned char*)compressed);
int2Bytes(compressedSize, out, offset);
offset += 4;

View File

@ -3,9 +3,7 @@
#include <map>
#include <unordered_map>
#ifndef std::string
#include <string>
#endif
class Player;
#define REGION_SIZE_BIT 5

View File

@ -2,6 +2,8 @@
#include <fstream>
#include <iostream>
#include <memory>
#include <stdint.h>
bool write_binary_file_part(std::string filename, const char* data, size_t offset, size_t size){
std::ofstream output(filename, std::ios::out | std::ios::binary | std::ios::in);
@ -48,18 +50,18 @@ char* read_binary_file(std::string filename, size_t& length) {
length = input.tellg();
input.seekg(0, std::ios_base::beg);
char* data = new char[length];
input.read(data, length);
std::unique_ptr<char> data {new char[length]};
input.read(data.get(), length);
input.close();
return data;
return data.release();
}
// returns decompressed length
unsigned int decompressRLE(const char* src, unsigned int length, char* dst, unsigned int targetLength){
unsigned int offset = 0;
for (unsigned int i = 0; i < length;){
size_t decompressRLE(const ubyte* src, size_t length, ubyte* dst, size_t targetLength){
size_t offset = 0;
for (size_t i = 0; i < length;){
unsigned char counter = src[i++];
char c = src[i++];
unsigned char c = src[i++];
for (unsigned int j = 0; j <= counter; j++){
dst[offset++] = c;
}
@ -67,12 +69,12 @@ unsigned int decompressRLE(const char* src, unsigned int length, char* dst, unsi
return offset;
}
unsigned int calcRLE(const char* src, unsigned int length) {
unsigned int offset = 0;
unsigned int counter = 0;
char c = src[0];
for (unsigned int i = 0; i < length; i++){
char cnext = src[i];
size_t calcRLE(const ubyte* src, size_t length) {
size_t offset = 0;
size_t counter = 0;
ubyte c = src[0];
for (size_t i = 0; i < length; i++){
ubyte cnext = src[i];
if (cnext != c || counter == 255){
offset += 2;
c = cnext;
@ -85,12 +87,14 @@ unsigned int calcRLE(const char* src, unsigned int length) {
}
// max result size = length * 2; returns compressed length
unsigned int compressRLE(const char* src, unsigned int length, char* dst) {
unsigned int offset = 0;
unsigned int counter = 0;
char c = src[0];
for (unsigned int i = 1; i < length; i++){
char cnext = src[i];
size_t compressRLE(const ubyte* src, size_t length, ubyte* dst) {
if (length == 0)
return 0;
size_t offset = 0;
uint counter = 0;
ubyte c = src[0];
for (size_t i = 1; i < length; i++){
ubyte cnext = src[i];
if (cnext != c || counter == 255){
dst[offset++] = counter;
dst[offset++] = c;

View File

@ -1,9 +1,8 @@
#ifndef FILES_FILES_H_
#define FILES_FILES_H_
#ifndef std::string
#include <string>
#endif
#include "../typedefs.h"
extern bool write_binary_file(std::string filename, const char* data, size_t size);
extern unsigned int append_binary_file(std::string filename, const char* data, size_t size);
@ -11,8 +10,8 @@ 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);
extern unsigned int decompressRLE(const char* src, unsigned int length, char* dst, unsigned int targetLength);
extern size_t calcRLE(const ubyte* src, size_t length);
extern size_t compressRLE(const ubyte* src, size_t length, ubyte* dst);
extern size_t decompressRLE(const ubyte* src, size_t length, ubyte* dst, size_t targetLength);
#endif /* FILES_FILES_H_ */

View File

@ -62,7 +62,7 @@ void Batch2D::vertex(vec2 point,
void Batch2D::texture(Texture* new_texture){
if (_texture == new_texture)
return;
render();
render(GL_TRIANGLES);
_texture = new_texture;
if (new_texture == nullptr)
blank->bind();
@ -70,21 +70,39 @@ void Batch2D::texture(Texture* new_texture){
new_texture->bind();
}
void Batch2D::point(float x, float y, float r, float g, float b, float a){
// if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES);
vertex(x, y, 0, 0, r,g,b,a);
render(GL_POINTS);
}
void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){
// if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES);
vertex(x1, y1, 0, 0, r,g,b,a);
vertex(x2, y2, 1, 1, r,g,b,a);
render(GL_LINES);
}
void Batch2D::rect(float x, float y, float w, float h){
const float r = color.r;
const float g = color.g;
const float b = color.b;
const float a = color.a;
if (index + 6*VERTEX_SIZE >= capacity)
render();
// if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES);
vertex(x, y, 0, 0, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a);
vertex(x, y+h, 0, 1, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a);
vertex(x, y, 0, 0, r,g,b,a);
vertex(x+w, y, 1, 0, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a);
vertex(x+w, y, 1, 0, r,g,b,a);
render(GL_TRIANGLES);
}
void Batch2D::rect(
@ -96,8 +114,8 @@ void Batch2D::rect(
bool flippedX,
bool flippedY,
vec4 tint) {
if (index + 6*VERTEX_SIZE >= capacity)
render();
// if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES);
float centerX = w*ox;
float centerY = h*oy;
@ -179,6 +197,83 @@ void Batch2D::rect(
vertex(x1, y1, u1, v1, tint.r, tint.g, tint.b, tint.a);
vertex(x3, y3, u3, v3, tint.r, tint.g, tint.b, tint.a);
vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a);
render(GL_TRIANGLES);
}
void Batch2D::rect(float x, float y, float w, float h,
float u, float v, float tx, float ty,
float r, float g, float b, float a){
// if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES);
vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a);
vertex(x, y+h, u, v, r,g,b,a);
vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y, u+tx, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a);
render(GL_TRIANGLES);
}
void Batch2D::rect(float x, float y, float w, float h,
float r0, float g0, float b0,
float r1, float g1, float b1,
float r2, float g2, float b2,
float r3, float g3, float b3,
float r4, float g4, float b4, int sh){
// if (index + 30*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES);
vec2 v0 = vec2(x+h/2,y+h/2);
vec2 v1 = vec2(x+w-sh,y);
vec2 v2 = vec2(x+sh,y);
vec2 v3 = vec2(x,y+sh);
vec2 v4 = vec2(x,y+h-sh);
vec2 v5 = vec2(x+sh,y+h);
vec2 v6 = vec2(x+w-h/2,y+h/2);
vec2 v7 = vec2(x+w-sh,y+h);
vec2 v8 = vec2(x+w,y+h-sh);
vec2 v9 = vec2(x+w,y+sh);
vertex(v0, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v6, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v1, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v0, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v1, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v2, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v0, vec2(0, 0), r0,g0,b0,1.0f);
vertex(v2, vec2(0, 0), r0,g0,b0,1.0f);
vertex(v3, vec2(0, 0), r0,g0,b0,1.0f);
vertex(v0, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v3, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v4, vec2(0, 0), r1,g1,b1,1.0f);
vertex(v0, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v4, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v5, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v0, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v5, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v6, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v6, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v5, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v7, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v6, vec2(0, 0), r4,g4,b4,1.0f);
vertex(v7, vec2(0, 0), r4,g4,b4,1.0f);
vertex(v8, vec2(0, 0), r4,g4,b4,1.0f);
vertex(v6, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v8, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v9, vec2(0, 0), r3,g3,b3,1.0f);
vertex(v6, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v9, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v1, vec2(0, 0), r2,g2,b2,1.0f);
render(GL_TRIANGLES);
}
void Batch2D::sprite(Sprite* sprite) {
@ -210,8 +305,8 @@ void Batch2D::blockSprite(float x, float y, float w, float h, int atlasRes, int
float vu = 1.0f - ((index[3] / atlasRes) * scale) - scale;
float uf = (index[0] % atlasRes) * scale;
float vf = 1.0f - ((index[0] / atlasRes) * scale) - scale;
if (this->index + 18*VERTEX_SIZE >= capacity)
render();
// if (this->index + 18*VERTEX_SIZE >= capacity)
// render();
float ar = 0.88f;
float ox = x + (w * 0.5f);
@ -258,24 +353,14 @@ void Batch2D::blockSprite(float x, float y, float w, float h, int atlasRes, int
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
glDisable(GL_MULTISAMPLE);
render(GL_TRIANGLES);
glEnable(GL_MULTISAMPLE);
}
void Batch2D::rect(float x, float y, float w, float h,
float u, float v, float tx, float ty,
float r, float g, float b, float a){
if (index + 6*VERTEX_SIZE >= capacity)
render();
vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a);
vertex(x, y+h, u, v, r,g,b,a);
vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y, u+tx, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a);
}
void Batch2D::render() {
void Batch2D::render(unsigned int gl_primitive) {
mesh->reload(buffer, index / VERTEX_SIZE);
mesh->draw(GL_TRIANGLES);
mesh->draw(gl_primitive);
index = 0;
}

View File

@ -20,7 +20,7 @@ class Batch2D {
size_t index;
Texture* blank;
Texture* _texture;
Texture* _texture;
void vertex(float x, float y,
float u, float v,
@ -40,6 +40,8 @@ public:
void sprite(float x, float y, float w, float h, int atlasRes, int index, vec4 tint);
void sprite(Sprite* sprite);
void blockSprite(float x, float y, float w, float h, int atlasRes, int index[6], vec4 tint);
void point(float x, float y, float r, float g, float b, float a);
void line(float x1, float y1, float x2, float y2, float r, float g, float b, float a);
void rect(float x, float y,
float w, float h,
float ox, float oy,
@ -49,9 +51,16 @@ public:
void rect(float x, float y, float w, float h);
void rect(float x, float y, float w, float h,
float u, float v, float tx, float ty,
float r, float g, float b, float a);
void render();
float u, float v, float tx, float ty,
float r, float g, float b, float a);
void rect(float x, float y, float w, float h,
float r0, float g0, float b0,
float r1, float g1, float b1,
float r2, float g2, float b2,
float r3, float g3, float b3,
float r4, float g4, float b4, int sh);
void render(unsigned int gl_primitive);
};
#endif /* SRC_GRAPHICS_BATCH2D_H_ */

View File

@ -10,21 +10,21 @@ Font::~Font(){
delete texture;
}
int Font::getGlyphWidth(char c) {
switch (c){
case 'l':
case 'i':
case 'j':
case '|':
case '.':
case ',':
case ':':
case ';': return 7;
case 't': return 8;
case ' ': return 7;
}
return 7;
}
// int Font::getGlyphWidth(char c) {
// switch (c){
// case 'l':
// case 'i':
// case 'j':
// case '|':
// case '.':
// case ',':
// case ':':
// case ';': return 7;
// case 't': return 8;
// case ' ': return 7;
// }
// return 7;
// }
bool Font::isPrintableChar(int c) {
@ -81,7 +81,7 @@ void Font::draw(Batch2D* batch, std::wstring text, int x, int y, int style) {
next = charpage;
}
}
x += getGlyphWidth(c);
x += 8;//getGlyphWidth(c);
}
page = next;
next = 10000;

View File

@ -17,7 +17,7 @@ public:
Font(std::vector<Texture*> pages);
~Font();
int getGlyphWidth(char c);
// int getGlyphWidth(char c);
bool isPrintableChar(int c);
void draw(Batch2D* batch, std::wstring text, int x, int y);
void draw(Batch2D* batch, std::wstring text, int x, int y, int style);

View File

@ -4,23 +4,45 @@
#include "Texture.h"
Framebuffer::Framebuffer(int width, int height) : width(width), height(height) {
glGenFramebuffers(1, &fbo);
bind();
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
texture = new Texture(tex, width, height);
glGenRenderbuffers(1, &depth);
glBindRenderbuffer(GL_RENDERBUFFER, depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
unbind();
// glGenFramebuffers(1, &fbo);
// bind();
// GLuint tex;
// glGenTextures(1, &tex);
// // glBindTexture(GL_TEXTURE_2D, tex);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, tex);
// // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
// glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, width, height, GL_TRUE);
// glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, tex, 0);
// texture = new Texture(tex, width, height);
// glGenRenderbuffers(1, &depth);
// glBindRenderbuffer(GL_RENDERBUFFER, depth);
// glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT24, width, height);
// // glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
// unbind();
// bind();
// glGenFramebuffers(1, &fbo);
// // create a multisampled color attachment texture
// unsigned int textureColorBufferMultiSampled;
// glGenTextures(1, &textureColorBufferMultiSampled);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureColorBufferMultiSampled);
// glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, width, height, GL_TRUE);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, textureColorBufferMultiSampled, 0);
// // create a (also multisampled) renderbuffer object for depth and stencil attachments
// unsigned int rbo;
// glGenRenderbuffers(1, &rbo);
// glBindRenderbuffer(GL_RENDERBUFFER, rbo);
// glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, width, height);
// glBindRenderbuffer(GL_RENDERBUFFER, 0);
// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
// unbind();
}
Framebuffer::~Framebuffer() {

View File

@ -7,12 +7,17 @@ Texture::Texture(unsigned int id, int width, int height) : id(id), width(width),
Texture::Texture(unsigned char* data, int width, int height) : width(width), height(height) {
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA, width, height, GL_TRUE);
// glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
}
Texture::~Texture() {
@ -21,11 +26,15 @@ Texture::~Texture() {
void Texture::bind(){
glBindTexture(GL_TEXTURE_2D, id);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
}
void Texture::reload(unsigned char* data){
glBindTexture(GL_TEXTURE_2D, id);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
// glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, width, height, GL_TRUE);
glBindTexture(GL_TEXTURE_2D, 0);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
}

View File

@ -30,7 +30,7 @@
INDEX += CHUNK_VERTEX_SIZE;
#define SETUP_UV(INDEX) float u1 = ((INDEX) % 16) * uvsize;\
#define SETUP_UV(INDEX) float u1 = ((INDEX) % 16) * uvsize;\
float v1 = 1-((1 + (INDEX) / 16) * uvsize);\
float u2 = u1 + uvsize;\
float v2 = v1 + uvsize;
@ -43,17 +43,41 @@ VoxelRenderer::VoxelRenderer() {
VoxelRenderer::~VoxelRenderer(){
}
inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const Chunk** chunks, unsigned int id, size_t& index){
inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const Chunk** chunks, voxel vox, size_t& index){
float l;
float uvsize = 1.0f/16.0f;
Block* block = Block::blocks[id];
Block* block = Block::blocks[vox.id];
unsigned char group = block->drawGroup;
int textureCopyFaces[6];
int rot = 0;
for (int i=0; i<6; i++){
textureCopyFaces[i] = block->textureFaces[i];
}
if (block->rotatable){
if (vox.states == 0x31){
rot = 1;
textureCopyFaces[0] = block->textureFaces[2];
textureCopyFaces[1] = block->textureFaces[3];
textureCopyFaces[2] = block->textureFaces[0];
textureCopyFaces[3] = block->textureFaces[1];
} else if (vox.states == 0x32){
rot = 2;
} else if (vox.states == 0x33){
rot = 3;
textureCopyFaces[2] = block->textureFaces[4];
textureCopyFaces[3] = block->textureFaces[5];
textureCopyFaces[4] = block->textureFaces[2];
textureCopyFaces[5] = block->textureFaces[3];
}
}
if (!IS_BLOCKED(x,y+1,z,group)){
l = 1.0f;
SETUP_UV(block->textureFaces[3]);
SETUP_UV(textureCopyFaces[3]);
const float lr = LIGHT(x,y+1,z, 0) / 15.0f;
const float lg = LIGHT(x,y+1,z, 1) / 15.0f;
@ -80,18 +104,36 @@ inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const
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)) / 75.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)) / 75.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);
if ((rot == 0) || (rot == 2)){
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);
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);
} else if (rot == 1){
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, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,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,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr3,lg3,lb3,ls3);
} else if (rot == 3){
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]);
SETUP_UV(textureCopyFaces[2]);
const float lr = LIGHT(x,y-1,z, 0) / 15.0f;
const float lg = LIGHT(x,y-1,z, 1) / 15.0f;
@ -118,19 +160,37 @@ inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const
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)) / 75.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)) / 75.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);
if ((rot == 0) || (rot == 2)){
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);
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);
} else if (rot == 1){
VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v2, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u1,v1, 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,v2, 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, u1,v1, lr1,lg1,lb1,ls1);
} else if (rot == 3){
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]);
SETUP_UV(textureCopyFaces[1]);
const float lr = LIGHT(x+1,y,z, 0) / 15.0f;
const float lg = LIGHT(x+1,y,z, 1) / 15.0f;
@ -157,18 +217,36 @@ inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const
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)) / 75.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)) / 75.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);
if ((rot == 0) || (rot == 2)){
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);
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);
} else if (rot == 1){
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, u1,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,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,v2, lr3,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr1,lg3,lb3,ls3);
} else if (rot == 3){
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v2, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v1, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v2, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v1, lr3,lg2,lb2,ls2);
VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr1,lg3,lb3,ls3);
}
}
if (!IS_BLOCKED(x-1,y,z,group)){
l = 0.85f;
SETUP_UV(block->textureFaces[0]);
SETUP_UV(textureCopyFaces[0]);
const float lr = LIGHT(x-1,y,z, 0) / 15.0f;
const float lg = LIGHT(x-1,y,z, 1) / 15.0f;
@ -195,19 +273,37 @@ inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const
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)) / 75.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)) / 75.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);
if ((rot == 0) || (rot == 2)){
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);
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);
} else if (rot == 1){
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);
} else if (rot == 3){
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, u2,v1, 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, u1,v2, 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]);
SETUP_UV(textureCopyFaces[5]);
const float lr = LIGHT(x,y,z+1, 0) / 15.0f;
const float lg = LIGHT(x,y,z+1, 1) / 15.0f;
@ -234,18 +330,36 @@ inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const
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)) / 75.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)) / 75.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);
if ((rot == 0) || (rot == 2)){
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);
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);
} else if (rot == 1){
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, u2,v1, 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, u1,v2, lr3,lg3,lb3,ls3);
VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1);
} else if (rot == 3){
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]);
SETUP_UV(textureCopyFaces[4]);
const float lr = LIGHT(x,y,z-1, 0) / 15.0f;
const float lg = LIGHT(x,y,z-1, 1) / 15.0f;
@ -272,21 +386,38 @@ inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const
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)) / 75.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)) / 75.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);
if ((rot == 0) || (rot == 2)){
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);
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);
} else if (rot == 1){
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,v1, 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, u2,v2, lr3,lg3,lb3,ls3);
} else if (rot == 3){
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);
}
}
}
inline void _renderBlockShadeless(std::vector<float>& buffer, int x, int y, int z, const Chunk** chunks, unsigned int id, size_t& index){
float l;
inline void _renderBlockShadeless(std::vector<float>& buffer, int x, int y, int z, const Chunk** chunks, voxel vox, size_t& index){
float uvsize = 1.0f/16.0f;
Block* block = Block::blocks[id];
Block* block = Block::blocks[vox.id];
unsigned char group = block->drawGroup;
if (!IS_BLOCKED(x,y+1,z,group)){
@ -363,7 +494,7 @@ inline void _renderBlockShadeless(std::vector<float>& buffer, int x, int y, int
inline void _renderXBlock(std::vector<float>& buffer, int x, int y, int z, const Chunk** chunks, voxel vox, size_t& index){
Block* block = Block::blocks[vox.id];
int rand = ((x * z + y) xor (z * y - x)) * (z + y);
int rand = ((x * z + y) ^ (z * y - x)) * (z + y);
float xs = (float)(char)rand / 512;
float zs = (float)(char)(rand >> 8) / 512;
@ -374,86 +505,62 @@ inline void _renderXBlock(std::vector<float>& buffer, int x, int y, int z, const
float uvsize = 1.0f/16.0f;
float lr = LIGHT(x,y,z, 0) / 15.0f;
float lg = LIGHT(x,y,z, 1) / 15.0f;
float lb = LIGHT(x,y,z, 2) / 15.0f;
float ls = LIGHT(x,y,z, 3) / 15.0f;
float lr = LIGHT(x,y,z, 0);
float lg = LIGHT(x,y,z, 1);
float lb = LIGHT(x,y,z, 2);
float ls = LIGHT(x,y,z, 3);
float lr0 = (LIGHT(x,y-1,z,0) + lr*30) / 45.0f;
float lr1 = (LIGHT(x,y+1,z,0) + lr*30) / 45.0f;
float lr2 = (LIGHT(x,y+1,z,0) + lr*30) / 45.0f;
float lr3 = (LIGHT(x,y-1,z,0) + lr*30) / 45.0f;
float lr4 = (LIGHT(x,y-1,z,0) + lr*30) / 45.0f;
float lr5 = (LIGHT(x,y+1,z,0) + lr*30) / 45.0f;
float lr6 = (LIGHT(x,y+1,z,0) + lr*30) / 45.0f;
float lr7 = (LIGHT(x,y-1,z,0) + lr*30) / 45.0f;
float lr0 = (LIGHT(x,y-1,z,0) + lr*3) / 60.0f;
float lr1 = (LIGHT(x,y+1,z,0) + lr*3) / 60.0f;
float lg0 = (LIGHT(x,y-1,z,1) + lg*30) / 45.0f;
float lg1 = (LIGHT(x,y+1,z,1) + lg*30) / 45.0f;
float lg2 = (LIGHT(x,y+1,z,1) + lg*30) / 45.0f;
float lg3 = (LIGHT(x,y-1,z,1) + lg*30) / 45.0f;
float lg4 = (LIGHT(x,y-1,z,1) + lg*30) / 45.0f;
float lg5 = (LIGHT(x,y+1,z,1) + lg*30) / 45.0f;
float lg6 = (LIGHT(x,y+1,z,1) + lg*30) / 45.0f;
float lg7 = (LIGHT(x,y-1,z,1) + lg*30) / 45.0f;
float lg0 = (LIGHT(x,y-1,z,1) + lg*3) / 60.0f;
float lg1 = (LIGHT(x,y+1,z,1) + lg*3) / 60.0f;
float lb0 = (LIGHT(x,y-1,z,2) + lb*30) / 45.0f;
float lb1 = (LIGHT(x,y+1,z,2) + lb*30) / 45.0f;
float lb2 = (LIGHT(x,y+1,z,2) + lb*30) / 45.0f;
float lb3 = (LIGHT(x,y-1,z,2) + lb*30) / 45.0f;
float lb4 = (LIGHT(x,y-1,z,2) + lb*30) / 45.0f;
float lb5 = (LIGHT(x,y+1,z,2) + lb*30) / 45.0f;
float lb6 = (LIGHT(x,y+1,z,2) + lb*30) / 45.0f;
float lb7 = (LIGHT(x,y-1,z,2) + lb*30) / 45.0f;
float lb0 = (LIGHT(x,y-1,z,2) + lb*3) / 60.0f;
float lb1 = (LIGHT(x,y+1,z,2) + lb*3) / 60.0f;
float ls0 = (LIGHT(x,y-1,z,3) + ls*30) / 45.0f;
float ls1 = (LIGHT(x,y+1,z,3) + ls*30) / 45.0f;
float ls2 = (LIGHT(x,y+1,z,3) + ls*30) / 45.0f;
float ls3 = (LIGHT(x,y-1,z,3) + ls*30) / 45.0f;
float ls4 = (LIGHT(x,y-1,z,3) + ls*30) / 45.0f;
float ls5 = (LIGHT(x,y+1,z,3) + ls*30) / 45.0f;
float ls6 = (LIGHT(x,y+1,z,3) + ls*30) / 45.0f;
float ls7 = (LIGHT(x,y-1,z,3) + ls*30) / 45.0f;
float ls0 = (LIGHT(x,y-1,z,3) + ls*3) / 60.0f;
float ls1 = (LIGHT(x,y+1,z,3) + ls*3) / 60.0f;
{SETUP_UV(block->textureFaces[1]);
VERTEX(index, x-0.5f+xs, y-0.5f, z-0.5f+zs, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.5f+xs, y+0.5f, z-0.5f+zs, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.5f+xs, y+0.5f, z+0.5f+zs, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.3535f+xs, y-0.5f, z-0.3535f+zs, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.3535f+xs, y+0.5f, z-0.3535f+zs, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.3535f+xs, y+0.5f, z+0.3535f+zs, u1,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f+xs, y-0.5f, z-0.5f+zs, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f+xs, y+0.5f, z+0.5f+zs, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x+0.5f+xs, y-0.5f, z+0.5f+zs, u1,v1, lr3,lg3,lb3,ls3);}
VERTEX(index, x-0.3535f+xs, y-0.5f, z-0.3535f+zs, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.3535f+xs, y+0.5f, z+0.3535f+zs, u1,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.3535f+xs, y-0.5f, z+0.3535f+zs, u1,v1, lr0,lg0,lb0,ls0);}
{SETUP_UV(block->textureFaces[0]);
VERTEX(index, x-0.5f+xs, y-0.5f, z-0.5f+zs, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f+xs, y+0.5f, z+0.5f+zs, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f+xs, y+0.5f, z-0.5f+zs, u1,v2, lr2,lg2,lb2,ls2);
VERTEX(index, x-0.3535f+xs, y-0.5f, z-0.3535f+zs, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.3535f+xs, y+0.5f, z+0.3535f+zs, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.3535f+xs, y+0.5f, z-0.3535f+zs, u1,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f+xs, y-0.5f, z-0.5f+zs, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.5f+xs, y-0.5f, z+0.5f+zs, u2,v1, lr3,lg3,lb3,ls3);
VERTEX(index, x+0.5f+xs, y+0.5f, z+0.5f+zs, u2,v2, lr1,lg1,lb1,ls1);}
VERTEX(index, x-0.3535f+xs, y-0.5f, z-0.3535f+zs, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.3535f+xs, y-0.5f, z+0.3535f+zs, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.3535f+xs, y+0.5f, z+0.3535f+zs, u2,v2, lr1,lg1,lb1,ls1);}
{SETUP_UV(block->textureFaces[5]);
VERTEX(index, x-0.5f+xs, y-0.5f, z+0.5f+zs, u1,v1, lr4,lg4,lb4,ls4);
VERTEX(index, x+0.5f+xs, y+0.5f, z-0.5f+zs, u2,v2, lr5,lg5,lb5,ls5);
VERTEX(index, x-0.5f+xs, y+0.5f, z+0.5f+zs, u1,v2, lr6,lg6,lb6,ls6);
VERTEX(index, x-0.3535f+xs, y-0.5f, z+0.3535f+zs, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.3535f+xs, y+0.5f, z-0.3535f+zs, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.3535f+xs, y+0.5f, z+0.3535f+zs, u1,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f+xs, y-0.5f, z+0.5f+zs, u1,v1, lr4,lg4,lb4,ls4);
VERTEX(index, x+0.5f+xs, y-0.5f, z-0.5f+zs, u2,v1, lr7,lg7,lb7,ls7);
VERTEX(index, x+0.5f+xs, y+0.5f, z-0.5f+zs, u2,v2, lr5,lg5,lb5,ls5);}
VERTEX(index, x-0.3535f+xs, y-0.5f, z+0.3535f+zs, u1,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.3535f+xs, y-0.5f, z-0.3535f+zs, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.3535f+xs, y+0.5f, z-0.3535f+zs, u2,v2, lr1,lg1,lb1,ls1);}
{SETUP_UV(block->textureFaces[4]);
VERTEX(index, x-0.5f+xs, y-0.5f, z+0.5f+zs, u2,v1, lr4,lg4,lb4,ls4);
VERTEX(index, x-0.5f+xs, y+0.5f, z+0.5f+zs, u2,v2, lr5,lg5,lb5,ls5);
VERTEX(index, x+0.5f+xs, y+0.5f, z-0.5f+zs, u1,v2, lr6,lg6,lb6,ls6);
VERTEX(index, x-0.3535f+xs, y-0.5f, z+0.3535f+zs, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x-0.3535f+xs, y+0.5f, z+0.3535f+zs, u2,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.3535f+xs, y+0.5f, z-0.3535f+zs, u1,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x-0.5f+xs, y-0.5f, z+0.5f+zs, u2,v1, lr4,lg4,lb4,ls4);
VERTEX(index, x+0.5f+xs, y+0.5f, z-0.5f+zs, u1,v2, lr6,lg6,lb6,ls6);
VERTEX(index, x+0.5f+xs, y-0.5f, z-0.5f+zs, u1,v1, lr7,lg7,lb7,ls7);}
VERTEX(index, x-0.3535f+xs, y-0.5f, z+0.3535f+zs, u2,v1, lr0,lg0,lb0,ls0);
VERTEX(index, x+0.3535f+xs, y+0.5f, z-0.3535f+zs, u1,v2, lr1,lg1,lb1,ls1);
VERTEX(index, x+0.3535f+xs, y-0.5f, z-0.3535f+zs, u1,v1, lr0,lg0,lb0,ls0);}
}
const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& size){
@ -471,7 +578,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s
if (block->emission[0] || block->emission[1] || block->emission[2]){
continue;
}
_renderBlock(buffer, x, y, z, chunks, vox.id, index);
_renderBlock(buffer, x, y, z, chunks, vox, index);
}
}
}
@ -484,7 +591,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s
continue;
Block* block = Block::blocks[vox.id];
if (block->emission[0] || block->emission[1] || block->emission[2]){
_renderBlockShadeless(buffer, x, y, z, chunks, vox.id, index);
_renderBlockShadeless(buffer, x, y, z, chunks, vox, index);
}
}
}
@ -498,7 +605,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s
continue;
if (vox.id != 9)
continue;
_renderBlock(buffer, x, y, z, chunks, vox.id, index);
_renderBlock(buffer, x, y, z, chunks, vox, index);
}
}
}
@ -509,7 +616,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s
voxel vox = chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x];
if (vox.id == 0)
continue;
if (Block::blocks[vox.id]->model != BLOCK_MODEL_GRASS)
if (Block::blocks[vox.id]->model != BLOCK_MODEL_X_SPRITE)
continue;
_renderXBlock(buffer, x, y, z, chunks, vox, index);
}
@ -524,7 +631,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s
continue;
if (vox.id != 4)
continue;
_renderBlock(buffer, x, y, z, chunks, vox.id, index);
_renderBlock(buffer, x, y, z, chunks, vox, index);
}
}
}

View File

@ -2,6 +2,8 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "typedefs.h"
#include "Assets.h"
#include "graphics/Shader.h"
#include "graphics/Batch2D.h"
@ -18,18 +20,18 @@
HudRenderer::HudRenderer() {
float vertices[] = {
// x y
-0.01f,-0.01f,
0.01f, 0.01f,
// 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
};
crosshair = new Mesh(vertices, 4, attrs);
// -0.01f, 0.01f,
// 0.01f,-0.01f,
// };
// int attrs[] = {
// 2, 0 //null terminator
// };
// crosshair = new Mesh(vertices, 4, attrs);
batch = new Batch2D(1024);
uicamera = new Camera(glm::vec3(), Window::height / 1.0f);
@ -38,7 +40,7 @@ HudRenderer::HudRenderer() {
}
HudRenderer::~HudRenderer() {
delete crosshair;
// delete crosshair;
delete batch;
delete uicamera;
}
@ -56,12 +58,12 @@ void HudRenderer::drawDebug(Level* level, Assets* assets, int fps, bool occlusio
batch->begin();
font->draw(batch, L"chunks: "+std::to_wstring(chunks->chunksCount), 16, 16, STYLE_OUTLINE);
font->draw(batch, std::to_wstring((int)player->camera->position.x), 10, 30, STYLE_OUTLINE);
font->draw(batch, std::to_wstring((int)player->camera->position.y), 50, 30, STYLE_OUTLINE);
font->draw(batch, std::to_wstring((int)player->camera->position.z), 90, 30, STYLE_OUTLINE);
font->draw(batch, std::to_wstring((int)player->camera->position.y), 90, 30, STYLE_OUTLINE);
font->draw(batch, std::to_wstring((int)player->camera->position.z), 170, 30, STYLE_OUTLINE);
font->draw(batch, L"fps:", 16, 42, STYLE_OUTLINE);
font->draw(batch, std::to_wstring(fps), 44, 42, STYLE_OUTLINE);
font->draw(batch, L"occlusion: "+std::to_wstring(occlusion), 16, 54, STYLE_OUTLINE);
batch->render();
// batch->render();
}
@ -79,62 +81,148 @@ void HudRenderer::draw(Level* level, Assets* assets){
Texture* blocks = assets->getTexture("block");
Texture* sprite = assets->getTexture("slot");
if (!Events::_cursor_locked) {
batch->texture(nullptr);
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.5f);
batch->rect(0, 0, Window::width, Window::height);
batch->texture(nullptr);
batch->color = vec4(1.0f);
if (Events::_cursor_locked && !level->player->debug) {
glLineWidth(2);
batch->line(Window::width/2, Window::height/2-6, Window::width/2, Window::height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(Window::width/2+6, Window::height/2, Window::width/2-6, Window::height/2, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(Window::width/2-5, Window::height/2-5, Window::width/2+5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
batch->line(Window::width/2+5, Window::height/2-5, Window::width/2-5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
}
batch->color = vec4(1.0f);
batch->texture(sprite);
batch->sprite(16, uicamera->fov - 80, 64, 64, 16, 0, vec4(1.0f));
// batch->texture(sprite);
// batch->sprite(Window::width/2-32, uicamera->fov - 80, 64, 64, 16, 0, vec4(1.0f));
// batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
// 0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f,
// 0.55f, 0.55f, 0.55f,
// 0.45f, 0.45f, 0.45f, 0.7f, 0.7f, 0.7f, 2);
batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
0.7f, 0.7f, 0.7f,
0.55f, 0.55f, 0.55f, 0.45f, 0.45f, 0.45f, 4);
batch->rect(Window::width/2-128, Window::height - 80, 256, 64,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 4);
batch->rect(Window::width/2-32+2, Window::height - 80+2, 60, 60,
0.45f, 0.45f, 0.45f, 0.55f, 0.55f, 0.55f,
0.7f, 0.7f, 0.7f,
0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f, 2);
batch->rect(Window::width/2-32+4, Window::height - 80+4, 56, 56,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 2);
batch->texture(blocks);
Player* player = level->player;
{
Block* cblock = Block::blocks[player->choosenBlock];
if (cblock->model == BLOCK_MODEL_CUBE){
batch->blockSprite(24, uicamera->fov - 72, 48, 48, 16, cblock->textureFaces, vec4(1.0f));
} else if (cblock->model == BLOCK_MODEL_GRASS){
batch->sprite(24, uicamera->fov - 72, 48, 48, 16, cblock->textureFaces[3], vec4(1.0f));
batch->blockSprite(Window::width/2-24, uicamera->fov - 72, 48, 48, 16, cblock->textureFaces, vec4(1.0f));
} else if (cblock->model == BLOCK_MODEL_X_SPRITE){
batch->sprite(Window::width/2-24, uicamera->fov - 72, 48, 48, 16, cblock->textureFaces[3], vec4(1.0f));
}
}
if (!Events::_cursor_locked) {
for (unsigned i = 1; i < 256; i++) {
if (!Events::_cursor_locked) { //inventory
uint size = 48;
uint step = 64;
uint inv_wm = step*10;
uint inv_hm = step*8;
uint inv_w = inv_wm - (step - size);
uint inv_h = inv_hm - (step - size);
int inv_x = (Window::width - (inv_w)) / 2;
int inv_y = (Window::height - (inv_h)) / 2;
int xs = (Window::width - inv_w + step)/2;
int ys = (Window::height - inv_h + step)/2;
if (Window::width > inv_w*3){
inv_x = (Window::width + (inv_w)) / 2;
inv_y = (Window::height - (inv_h)) / 2;
xs = (Window::width + inv_w + step)/2;
ys = (Window::height - inv_h + step)/2;
}
int x = 0;
int y = 0;
vec4 tint = vec4(1.0f);
int mx = Events::x;
int my = Events::y;
uint count = (inv_w / step) * (inv_h / step) + 1;
//back
batch->texture(nullptr);
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.3f);
batch->rect(0, 0, Window::width, Window::height);
batch->rect(inv_x - 4, inv_y - 4, inv_w+8, inv_h+8,
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
0.7f, 0.7f, 0.7f,
0.55f, 0.55f, 0.55f, 0.45f, 0.45f, 0.45f, 4);
batch->rect(inv_x, inv_y, inv_w, inv_h,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 4);
batch->color = vec4(0.35f, 0.35f, 0.35f, 1.0f);
for (uint i = 1; i < count; i++) {
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
// batch->rect(x-2, y-2, size+4, size+4);
batch->rect(x-2, y-2, size+4, size+4,
0.45f, 0.45f, 0.45f, 0.55f, 0.55f, 0.55f,
0.7f, 0.7f, 0.7f,
0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f, 2);
batch->rect(x, y, size, size,
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f,
0.65f, 0.65f, 0.65f,
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 2);
}
// batch->color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
// for (unsigned i = 1; i < count; i++) {
// x = xs + step * ((i-1) % (inv_w / step));
// y = ys + step * ((i-1) / (inv_w / step));
// batch->rect(x, y, size, size);
// }
//front
batch->texture(blocks);
for (uint i = 1; i < count; i++) {
Block* cblock = Block::blocks[i];
if (cblock == nullptr)
break;
int size = 48;
int step = 70;
int x = 24 + (i-1) * step;
int y = uicamera->fov - 72 - 70;
y -= 72 * (x / (Window::width - step));
x %= (Window::width - step);
vec4 tint(1.0f);
int mx = Events::x;
int my = Events::y;
if (mx > x && mx < x + size && my > y && my < y + size) {
tint.r *= 2.0f;
tint.g *= 2.0f;
tint.b *= 2.0f;
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
if (mx > x && mx < x + (int)size && my > y && my < y + (int)size) {
tint.r *= 1.2f;
tint.g *= 1.2f;
tint.b *= 1.2f;
if (Events::jclicked(GLFW_MOUSE_BUTTON_LEFT)) {
player->choosenBlock = i;
}
// size = 50;
} else
{
// size = 48;
tint = vec4(1.0f);
}
if (cblock->model == BLOCK_MODEL_CUBE){
batch->blockSprite(x, y, size, size, 16, cblock->textureFaces, tint);
} else if (cblock->model == BLOCK_MODEL_GRASS){
} else if (cblock->model == BLOCK_MODEL_X_SPRITE){
batch->sprite(x, y, size, size, 16, cblock->textureFaces[3], tint);
}
}
}
batch->render();
// batch->render();
Shader* crosshairShader = assets->getShader("crosshair");
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);
if (Events::_cursor_locked && !level->player->debug){
// Shader* crosshairShader = assets->getShader("crosshair");
// crosshairShader->use();
// crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);
// crosshairShader->uniform1f("u_scale", 1.0f / ((float)Window::height / 1000.0f));
// glLineWidth(2.0f);
// crosshair->draw(GL_LINES);
}
}

View File

@ -10,7 +10,7 @@ class Mesh;
class HudRenderer {
Batch2D* batch;
Camera* uicamera;
Mesh* crosshair;
// Mesh* crosshair;
public:
HudRenderer();
~HudRenderer();

View File

@ -20,6 +20,7 @@ int _png_load(const char* file, int* width, int* height){
png_bytepp row_pointers;
png_structp png_ptr;
GLuint texture;
GLuint texturems;
int alpha;
if ( !( f = fopen(file, "r" ) ) ) {
@ -95,18 +96,53 @@ int _png_load(const char* file, int* width, int* height){
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
return 0;
}
// configure second post-processing framebuffer
unsigned int framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t_width, t_height, 0,
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_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
glGenerateMipmap(GL_TEXTURE_2D);
// glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); // we only need a color buffer
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
unsigned int framebufferms;
glGenFramebuffers(1, &framebufferms);
glBindFramebuffer(GL_FRAMEBUFFER, framebufferms);
// create a multisampled color attachment texture
glGenTextures(1, &texturems);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texturems);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 16, GL_RGBA, t_width, t_height, GL_TRUE);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, texturems, 0);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAX_LEVEL, 3);
// glGenerateMipmap(GL_TEXTURE_2D_MULTISAMPLE);
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebufferms);
glBlitFramebuffer(0, 0, t_width, t_height, 0, 0, t_width, t_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
free( image_data );

View File

@ -11,7 +11,7 @@ Player::Player(glm::vec3 position, float speed, Camera* camera) :
camera(camera),
choosenBlock(1),
camX(0.0f), camY(0.0f){
hitbox = new Hitbox(position, vec3(0.2f,0.9f,0.2f));
hitbox = new Hitbox(position, vec3(0.3f,0.9f,0.3f));
}
Player::~Player(){

View File

@ -15,6 +15,7 @@ public:
Hitbox* hitbox;
bool flight = false;
bool noclip = false;
bool debug = false;
int choosenBlock;
float camX, camY;
float cameraShaking = 0.0f;

View File

@ -5,7 +5,6 @@
#include <iostream>
#define E 0.03
#define DEFAULT_FRICTION 10.0
PhysicsSolver::PhysicsSolver(vec3 gravity) : gravity(gravity) {
}
@ -28,89 +27,7 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
float pz = pos.z;
if (collisions) {
if (vel.x < 0.0){
for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){
for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){
int x = floor(pos.x-half.x-E);
if (chunks->isObstacle(x,y,z)){
vel.x *= 0.0;
pos.x = x + 1 + half.x + E;
break;
}
}
}
}
if (vel.x > 0.0){
for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){
for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){
int x = floor(pos.x+half.x+E);
if (chunks->isObstacle(x,y,z)){
vel.x *= 0.0;
pos.x = x - half.x - E;
break;
}
}
}
}
if (vel.z < 0.0){
for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){
for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){
int z = floor(pos.z-half.z-E);
if (chunks->isObstacle(x,y,z)){
vel.z *= 0.0;
pos.z = z + 1 + half.z + E;
break;
}
}
}
}
if (vel.z > 0.0){
for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){
for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){
int z = floor(pos.z+half.z+E);
if (chunks->isObstacle(x,y,z)){
vel.z *= 0.0;
pos.z = z - half.z - E;
break;
}
}
}
}
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 = 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){
for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){
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 - half.y - E;
break;
}
}
}
}
colisionCalc(chunks, hitbox, &vel, &pos, half);
}
vel.x *= max(0.0, 1.0 - dt * linear_damping);
@ -152,6 +69,85 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
}
}
void PhysicsSolver::colisionCalc(Chunks* chunks, Hitbox* hitbox, vec3* vel, vec3* pos, vec3 half){
if (vel->x < 0.0){
for (int y = floor(pos->y-half.y+E); y <= floor(pos->y+half.y-E); y++){
for (int z = floor(pos->z-half.z+E); z <= floor(pos->z+half.z-E); z++){
int x = floor(pos->x-half.x-E);
if (chunks->isObstacle(x,y,z)){
vel->x *= 0.0;
pos->x = x + 1 + half.x + E;
break;
}
}
}
}
if (vel->x > 0.0){
for (int y = floor(pos->y-half.y+E); y <= floor(pos->y+half.y-E); y++){
for (int z = floor(pos->z-half.z+E); z <= floor(pos->z+half.z-E); z++){
int x = floor(pos->x+half.x+E);
if (chunks->isObstacle(x,y,z)){
vel->x *= 0.0;
pos->x = x - half.x - E;
break;
}
}
}
}
if (vel->z < 0.0){
for (int y = floor(pos->y-half.y+E); y <= floor(pos->y+half.y-E); y++){
for (int x = floor(pos->x-half.x+E); x <= floor(pos->x+half.x-E); x++){
int z = floor(pos->z-half.z-E);
if (chunks->isObstacle(x,y,z)){
vel->z *= 0.0;
pos->z = z + 1 + half.z + E;
break;
}
}
}
}
if (vel->z > 0.0){
for (int y = floor(pos->y-half.y+E); y <= floor(pos->y+half.y-E); y++){
for (int x = floor(pos->x-half.x+E); x <= floor(pos->x+half.x-E); x++){
int z = floor(pos->z+half.z+E);
if (chunks->isObstacle(x,y,z)){
vel->z *= 0.0;
pos->z = z - half.z - E;
break;
}
}
}
}
if (vel->y < 0.0){
for (int x = floor(pos->x-half.x+E); x <= floor(pos->x+half.x-E); x++){
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;
hitbox->grounded = true;
break;
}
}
}
}
if (vel->y > 0.0){
for (int x = floor(pos->x-half.x+E); x <= floor(pos->x+half.x-E); x++){
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 - half.y - E;
break;
}
}
}
}
}
bool PhysicsSolver::isBlockInside(int x, int y, int z, Hitbox* hitbox) {
vec3& pos = hitbox->position;
vec3& half = hitbox->halfsize;

View File

@ -21,6 +21,7 @@ public:
bool shifting,
float gravityScale,
bool collisions);
void colisionCalc(Chunks* chunks, Hitbox* hitbox, vec3* vel, vec3* pos, vec3 half);
bool isBlockInside(int x, int y, int z, Hitbox* hitbox);
};

View File

@ -19,8 +19,8 @@
#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 PLAYER_GROUND_DAMPING 10.0f
#define PLAYER_AIR_DAMPING 7.0f
#define CAMERA_SHAKING_OFFSET 0.025f
#define CAMERA_SHAKING_OFFSET_Y 0.031f
#define CAMERA_SHAKING_SPEED 1.6f
@ -35,13 +35,14 @@ PlayerController::PlayerController(Level* level) : level(level) {
void PlayerController::update_controls(float delta){
Player* player = level->player;
for (int i = 1; i < 10; i++){
if (Events::jpressed(GLFW_KEY_0+i)){
player->choosenBlock = i;
/*block choose*/{
for (int i = 1; i < 10; i++){
if (Events::jpressed(GLFW_KEY_0+i)){
player->choosenBlock = i;
}
}
}
}//end
// Controls
Camera* camera = player->camera;
Hitbox* hitbox = player->hitbox;
bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL);
@ -65,24 +66,25 @@ void PlayerController::update_controls(float delta){
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);
/*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);
}//end
if ((Events::jpressed(GLFW_KEY_F) && !player->noclip) ||
(Events::jpressed(GLFW_KEY_N) && player->flight == player->noclip)){
player->flight = !player->flight;
if (player->flight){
hitbox->velocity.y += 1;
hitbox->grounded = false;
}
}
@ -90,25 +92,25 @@ void PlayerController::update_controls(float delta){
player->noclip = !player->noclip;
}
// Field of view manipulations
float dt = min(1.0f, delta * ZOOM_SPEED);
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);
/*field of view manipulations*/{
float dt = min(1.0f, delta * ZOOM_SPEED);
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);
}//end
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;
@ -127,9 +129,9 @@ void PlayerController::update_controls(float delta){
dir.z -= camera->right.z;
}
hitbox->linear_damping = DEFAULT_AIR_DAMPING;
hitbox->linear_damping = PLAYER_GROUND_DAMPING;
if (player->flight){
hitbox->linear_damping = PLAYER_NOT_ONGROUND_DAMPING;
hitbox->linear_damping = PLAYER_AIR_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9;
if (Events::pressed(GLFW_KEY_SPACE)){
hitbox->velocity.y += speed * delta * 9;
@ -138,30 +140,37 @@ void PlayerController::update_controls(float delta){
hitbox->velocity.y -= speed * delta * 9;
}
}
if (!hitbox->grounded)
hitbox->linear_damping = PLAYER_AIR_DAMPING;
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 rotate*/{
if (Events::_cursor_locked){
float rotX = -Events::deltaX / Window::height * 2;
float rotY = -Events::deltaY / Window::height * 2;
if (zoom){
rotX /= 4;
rotY /= 4;
}
player->camX += rotX;
player->camY += rotY;
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
}
if (player->camY < -radians(89.9f)){
player->camY = -radians(89.9f);
}
if (player->camY > radians(89.9f)){
player->camY = radians(89.9f);
}
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
}
}//end
}
void PlayerController::update_interaction(){
@ -176,33 +185,48 @@ void PlayerController::update_interaction(){
if (vox != nullptr){
selectedBlockId = vox->id;
selectedBlockPosition = iend;
int x = (int)iend.x;
int y = (int)iend.y;
int z = (int)iend.z;
uint8_t states = 0;
if (Block::blocks[player->choosenBlock]->rotatable){
// states = states & 0b11111100;
// if (abs(norm.x) > abs(norm.z)){
// if (abs(norm.x) > abs(norm.y)) states = states | 0b00000001;
// if (abs(norm.x) < abs(norm.y)) states = states | 0b00000010;
// }
// if (abs(norm.x) < abs(norm.z)){
// if (abs(norm.z) > abs(norm.y)) states = states | 0b00000011;
// if (abs(norm.z) < abs(norm.y)) states = states | 0b00000010;
// }
if (abs(norm.x) > abs(norm.z)){
if (abs(norm.x) > abs(norm.y)) states = 0x31;
if (abs(norm.x) < abs(norm.y)) states = 0x32;
}
if (abs(norm.x) < abs(norm.z)){
if (abs(norm.z) > abs(norm.y)) states = 0x33;
if (abs(norm.z) < abs(norm.y)) states = 0x32;
}
}
Block* block = Block::blocks[vox->id];
if (Events::jclicked(GLFW_MOUSE_BUTTON_1) && block->breakable){
int x = (int)iend.x;
int y = (int)iend.y;
int z = (int)iend.z;
chunks->set(x,y,z, 0);
chunks->set(x,y,z, 0, 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 (block->model == BLOCK_MODEL_GRASS){
x = (int)iend.x;
y = (int)iend.y;
z = (int)iend.z;
if (block->model != BLOCK_MODEL_X_SPRITE){
x = (int)(iend.x)+(int)(norm.x);
y = (int)(iend.y)+(int)(norm.y);
z = (int)(iend.z)+(int)(norm.z);
}
if (!level->physics->isBlockInside(x,y,z, player->hitbox)){
chunks->set(x, y, z, player->choosenBlock);
chunks->set(x, y, z, player->choosenBlock, states);
lighting->onBlockSet(x,y,z, player->choosenBlock);
}
}
if (Events::jclicked(GLFW_MOUSE_BUTTON_3)){
int x = (int)iend.x;
int y = (int)iend.y;
int z = (int)iend.z;
player->choosenBlock = chunks->get(x,y,z)->id;
}
} else {

4
src/typedefs.h Normal file
View File

@ -0,0 +1,4 @@
#include <stdint.h>
typedef unsigned int uint;
typedef unsigned char ubyte;

View File

@ -10,6 +10,7 @@
#include <vector>
#include <ctime>
#include <exception>
// GLM
#include <glm/glm.hpp>
@ -18,140 +19,127 @@
using namespace glm;
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/Mesh.h"
#include "graphics/VoxelRenderer.h"
#include "graphics/LineBatch.h"
#include "graphics/Batch2D.h"
#include "graphics/Framebuffer.h"
#include "window/Window.h"
#include "window/Events.h"
#include "window/Camera.h"
#include "loaders/png_loading.h"
#include "voxels/voxel.h"
#include "audio/Audio.h"
#include "voxels/Chunk.h"
#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"
#include "lighting/Lightmap.h"
#include "lighting/Lighting.h"
#include "physics/Hitbox.h"
#include "physics/PhysicsSolver.h"
#include "world/World.h"
#include "world/Level.h"
#include "audio/Audio.h"
#include "audio/audioutil.h"
#include "Assets.h"
#include "voxels/ChunksLoader.h"
#include "objects/Player.h"
#include "world/Level.h"
#include "world/World.h"
#include "declarations.h"
#include "Assets.h"
#include "AssetsLoader.h"
#include "world_render.h"
#include "hud_render.h"
#include "player_control.h"
int WIDTH = 1280;
int HEIGHT = 720;
// Save all world data to files
void write_world(World* world, Level* level){
WorldFiles* wfile = world->wfile;
Chunks* chunks = level->chunks;
class initialize_error : public std::runtime_error {
initialize_error(const std::string& message) : std::runtime_error(message) {}
};
for (unsigned int i = 0; i < chunks->volume; i++){
Chunk* chunk = chunks->chunks[i];
if (chunk == nullptr || !chunk->isUnsaved())
continue;
wfile->put((const char*)chunk->voxels, chunk->x, chunk->z);
}
struct EngineSettings {
int displayWidth;
int displayHeight;
const char* title;
};
wfile->write();
world->wfile->writePlayer(level->player);
}
class Engine {
Assets* assets;
Level* level;
void update_level(World* world, Level* level, float delta, long frame, VoxelRenderer* renderer) {
level->playerController->update_controls(delta);
if (Events::_cursor_locked)
level->playerController->update_interaction();
uint64_t frame = 0;
float lastTime = 0.0f;
float delta = 0.0f;
bool occlusion = true;
public:
Engine(const EngineSettings& settings);
~Engine();
vec3 position = level->player->hitbox->position;
level->chunks->setCenter(world->wfile, position.x, position.z);
}
void updateTimers();
void updateHotkeys();
void mainloop();
};
Level* load_level(World* world, Player* player) {
Level* level = new Level(world, player, new Chunks(56, 56, 0, 0), new PhysicsSolver(vec3(0, -19.6f, 0)));
world->wfile->readPlayer(player);
Camera* camera = player->camera;
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
return level;
}
int initialize(Assets*& assets) {
Window::initialize(WIDTH, HEIGHT, "VoxelEngine-Cpp v12");
Engine::Engine(const EngineSettings& settings) {
Window::initialize(settings.displayWidth, settings.displayHeight, settings.title);
Events::initialize();
assets = new Assets();
std::cout << "-- loading assets" << std::endl;
int result = initialize_assets(assets);
if (result){
delete assets;
Window::terminate();
return result;
AssetsLoader loader(assets);
AssetsLoader::createDefaults(loader);
initialize_assets(&loader);
while (loader.hasNext()) {
if (!loader.loadNext()) {
delete assets;
Window::terminate();
throw std::runtime_error("could not to initialize assets");
}
}
return 0;
std::cout << "-- loading world" << std::endl;
vec3 playerPosition = vec3(0, 64, 0);
Camera* camera = new Camera(playerPosition, radians(90.0f));
World* world = new World("world-1", "world/", 42);
Player* player = new Player(playerPosition, 4.0f, camera);
level = world->loadLevel(player);
std::cout << "-- initializing finished" << std::endl;
Audio::initialize();
}
void mainloop(Level* level, Assets* assets) {
void Engine::updateTimers() {
frame++;
float currentTime = glfwGetTime();
delta = currentTime - lastTime;
lastTime = currentTime;
}
void Engine::updateHotkeys() {
if (Events::jpressed(GLFW_KEY_ESCAPE)) {
Window::setShouldClose(true);
}
if (Events::jpressed(GLFW_KEY_TAB) || Events::jpressed(GLFW_KEY_E)) {
Events::toggleCursor();
}
if (Events::jpressed(GLFW_KEY_O)) {
occlusion = !occlusion;
}
if (Events::jpressed(GLFW_KEY_F3)) {
level->player->debug = !level->player->debug;
}
if (Events::jpressed(GLFW_KEY_F5)) {
for (unsigned i = 0; i < level->chunks->volume; i++) {
Chunk* chunk = level->chunks->chunks[i];
if (chunk != nullptr && chunk->isReady()) {
chunk->setModified(true);
}
}
}
}
void Engine::mainloop() {
Camera* camera = level->player->camera;
std::cout << "-- preparing systems" << std::endl;
World* world = level->world;
WorldRenderer worldRenderer(level, assets);
HudRenderer hud;
long frame = 0;
float lastTime = glfwGetTime();
float delta = 0.0f;
bool occlusion = true;
bool devdata = false;
Window::swapInterval(0);
while (!Window::isShouldClose()){
frame++;
float currentTime = glfwGetTime();
delta = currentTime - lastTime;
lastTime = currentTime;
int fps = 1 / delta;
if (Events::jpressed(GLFW_KEY_ESCAPE)){
Window::setShouldClose(true);
}
if (Events::jpressed(GLFW_KEY_TAB)){
Events::toggleCursor();
}
if (Events::jpressed(GLFW_KEY_O)){
occlusion = !occlusion;
}
if (Events::jpressed(GLFW_KEY_F3)){
devdata = !devdata;
}
if (Events::jpressed(GLFW_KEY_F5)){
for (unsigned i = 0; i < level->chunks->volume; i++) {
Chunk* chunk = level->chunks->chunks[i];
if (chunk != nullptr && chunk->isReady()){
chunk->setModified(true);
}
}
}
lastTime = glfwGetTime();
update_level(world, level, delta, frame, worldRenderer.renderer);
Window::swapInterval(1);
while (!Window::isShouldClose()){
updateTimers();
updateHotkeys();
level->update(delta, Events::_cursor_locked);
int freeLoaders = level->chunksController->countFreeLoaders();
for (int i = 0; i < freeLoaders; i++)
level->chunksController->_buildMeshes(worldRenderer.renderer, frame);
level->chunksController->_buildMeshes();
freeLoaders = level->chunksController->countFreeLoaders();
for (int i = 0; i < freeLoaders; i++)
level->chunksController->calculateLights();
@ -159,10 +147,10 @@ void mainloop(Level* level, Assets* assets) {
for (int i = 0; i < freeLoaders; i++)
level->chunksController->loadVisible(world->wfile);
worldRenderer.draw(world, camera, occlusion);
worldRenderer.draw(camera, occlusion);
hud.draw(level, assets);
if (devdata) {
hud.drawDebug(level, assets, fps, occlusion);
if (level->player->debug) {
hud.drawDebug(level, assets, 1 / delta, occlusion);
}
Window::swapBuffers();
@ -170,28 +158,12 @@ void mainloop(Level* level, Assets* assets) {
}
}
int main() {
setup_definitions();
Assets* assets;
int status = initialize(assets);
if (status) return status;
std::cout << "-- loading world" << std::endl;
vec3 playerPosition = vec3(0,150,-10);
Camera* camera = new Camera(playerPosition, radians(90.0f));
World* world = new World("world-1", "world/", 42);
Player* player = new Player(playerPosition, 4.0f, camera);
Level* level = load_level(world, player);
std::cout << "-- initializing finished" << std::endl;
Audio::initialize();
mainloop(level, assets);
Engine::~Engine() {
Audio::finalize();
World* world = level->world;
std::cout << "-- saving world" << std::endl;
write_world(world, level);
world->write(level);
delete level;
delete world;
@ -200,5 +172,19 @@ int main() {
delete assets;
Events::finalize();
Window::terminate();
}
int main() {
setup_definitions();
try {
Engine engine(EngineSettings{ 1280, 720, "VoxelEngine-Cpp v13" });
engine.mainloop();
}
catch (const initialize_error& err) {
std::cerr << "could not to initialize engine" << std::endl;
std::cerr << err.what() << std::endl;
}
return 0;
}

View File

@ -2,7 +2,7 @@
#define VOXELS_BLOCK_H_
#define BLOCK_MODEL_CUBE 1
#define BLOCK_MODEL_GRASS 2
#define BLOCK_MODEL_X_SPRITE 2
class Block {
public:
@ -13,12 +13,15 @@ public:
int textureFaces[6]; // -x,x, -y,y, -z,z
unsigned char emission[3];
unsigned char drawGroup = 0;
unsigned char model = 1; // 0:None 1:Block 2:XSprite
bool lightPassing = false;
bool skyLightPassing = false;
bool obstacle = true;
bool selectable = true;
bool breakable = true;
unsigned char model = 1;
bool rotatable = false;
float hitboxScale = 1;
float hitboxY = 1;
Block(unsigned int id, int texture);
};

View File

@ -6,7 +6,7 @@
#define CHUNK_W 16
#define CHUNK_H 256
#define CHUNK_D 16
#define CHUNK_VOL (CHUNK_W * CHUNK_H * CHUNK_D)
#define CHUNK_VOL (CHUNK_W * CHUNK_H * CHUNK_D * 2)
#define CHUNK_MODIFIED 0x1
#define CHUNK_READY 0x2

View File

@ -123,7 +123,7 @@ Chunk* Chunks::getChunk(int x, int z){
return chunks[z * w + x];
}
void Chunks::set(int x, int y, int z, int id){
void Chunks::set(int x, int y, int z, int id, uint8_t states){
x -= ox * CHUNK_W;
z -= oz * CHUNK_D;
int cx = x / CHUNK_W;
@ -140,6 +140,7 @@ void Chunks::set(int x, int y, int z, int id){
int lx = x - cx * CHUNK_W;
int lz = z - cz * CHUNK_D;
chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].id = id;
chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].states = states;
chunk->setUnsaved(true);
chunk->setModified(true);

View File

@ -34,7 +34,7 @@ public:
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);
void set(int x, int y, int z, int id, uint8_t states);
voxel* rayCast(vec3 start, vec3 dir, float maxLength, vec3& end, vec3& norm, vec3& iend);
bool isObstacle(int x, int y, int z);

View File

@ -10,7 +10,7 @@
#include <iostream>
#include <limits.h>
#ifdef _WIN32
#if defined(_WIN32) && defined(__MINGW32__)
#define _WIN32_WINNT 0x0501
#include <mingw.thread.h>
#else
@ -185,7 +185,7 @@ void ChunksController::calculateLights() {
freeLoader->lights(chunk, (Chunk**)closes);
}
bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) {
bool ChunksController::_buildMeshes() {
const int w = chunks->w;
const int d = chunks->d;

View File

@ -22,7 +22,7 @@ public:
int countFreeLoaders();
bool loadVisible(WorldFiles* worldFiles);
void calculateLights();
bool _buildMeshes(VoxelRenderer* renderer, int tick);
bool _buildMeshes();
};
#endif /* VOXELS_CHUNKSCONTROLLER_H_ */

View File

@ -1,12 +1,12 @@
#ifndef VOXELS_CHUNKSLOADER_H_
#define VOXELS_CHUNKSLOADER_H_
#ifdef _WIN32
#if defined(_WIN32) && defined(__MINGW32__)
#define _WIN32_WINNT 0x0501
#include <mingw.thread.h>
#else
#include <thread>
#endif
#endif // _WIN32 && __MINGW32__
#include <atomic>

View File

@ -31,22 +31,27 @@ public:
}
void setSeed(int number){
seed = ((unsigned short)number+23729 xor (unsigned short)number+16786);
seed = ((unsigned short)(number*23729) ^ (unsigned short)(number+16786));
rand();
}
void setSeed(int number1,int number2){
seed = (((unsigned short)(number1*23729) | (unsigned short)(number2%16786)) ^ (unsigned short)(number2*number1));
rand();
}
};
float calc_height(fnl_state *noise, int real_x, int real_z){
float height = fnlGetNoise3D(noise, real_x*0.0125f*8,real_z*0.0125f*8, 0.0f);
height += fnlGetNoise3D(noise, real_x*0.025f*8,real_z*0.025f*8, 0.0f)*0.5f;
height += fnlGetNoise3D(noise, real_x*0.05f*8,real_z*0.05f*8, 0.0f)*0.25f;
float height = 0;
height += fnlGetNoise3D(noise, real_x*0.0125f*8-125567,real_z*0.0125f*8+3546, 0.0f);
height += fnlGetNoise3D(noise, real_x*0.025f*8+4647,real_z*0.025f*8-3436, 0.0f)*0.5f;
height += fnlGetNoise3D(noise, real_x*0.05f*8-834176,real_z*0.05f*8+23678, 0.0f)*0.25f;
height += fnlGetNoise3D(noise,
real_x*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8,real_z*0.1f*8, 0.0f)*50,
real_z*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8+4363,real_z*0.1f*8, 0.0f)*50,
real_x*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8-23557,real_z*0.1f*8-6568, 0.0f)*50,
real_z*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8+4363,real_z*0.1f*8+4456, 0.0f)*50,
0.0f)*0.1f;
height += fnlGetNoise3D(noise, real_x*0.1f*8,real_z*0.1f*8, 0.0f)*0.125f;
height += fnlGetNoise3D(noise, real_x*0.4f*8,real_z*0.4f*8, 0.0f)*0.0625f;
height += fnlGetNoise3D(noise, real_x*8,real_z*8, 0.0f)*0.03f*(fnlGetNoise3D(noise, -real_x*0.0125f*8-1000,real_z*0.0125f*8+2000, 0.0f)/2+0.5f);
height += fnlGetNoise3D(noise, real_x*0.1f*8-3465,real_z*0.1f*8+4534, 0.0f)*0.125f;
height += fnlGetNoise3D(noise, real_x*0.4f*8+4565,real_z*0.4f*8+46456, 0.0f)*0.0625f;
// height += fnlGetNoise3D(noise, real_x*8,real_z*8, 0.0f)*0.03f*(fnlGetNoise3D(noise, -real_x*0.0125f*8-1000,real_z*0.0125f*8+2000, 0.0f)/2+0.5f);
height *= fnlGetNoise3D(noise, real_x*0.0125f*8+1000,real_z*0.0125f*8+1000, 0.0f)/2+0.5f;
height += 1.0f;
height *= 64.0f;
@ -54,41 +59,43 @@ float calc_height(fnl_state *noise, int real_x, int real_z){
}
float calc_height_faster(fnl_state *noise, int real_x, int real_z){
float height = fnlGetNoise3D(noise, real_x*0.0125f*8,real_z*0.0125f*8, 0.0f);
height += fnlGetNoise3D(noise, real_x*0.025f*8,real_z*0.025f*8, 0.0f)*0.5f;
height += fnlGetNoise3D(noise, real_x*0.05f*8,real_z*0.05f*8, 0.0f)*0.25f;
float height = 0;
height += fnlGetNoise3D(noise, real_x*0.0125f*8-125567,real_z*0.0125f*8+3546, 0.0f);
height += fnlGetNoise3D(noise, real_x*0.025f*8+4647,real_z*0.025f*8-3436, 0.0f)*0.5f;
height += fnlGetNoise3D(noise, real_x*0.05f*8-834176,real_z*0.05f*8+23678, 0.0f)*0.25f;
height += fnlGetNoise3D(noise,
real_x*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8,real_z*0.1f*8, 0.0f)*50,
real_z*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8+4363,real_z*0.1f*8, 0.0f)*50,
real_x*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8-23557,real_z*0.1f*8-6568, 0.0f)*50,
real_z*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8+4363,real_z*0.1f*8+4456, 0.0f)*50,
0.0f)*0.1f;
height += fnlGetNoise3D(noise, real_x*0.1f*8,real_z*0.1f*8, 0.0f)*0.125f;
height += fnlGetNoise3D(noise, real_x*0.1f*8-3465,real_z*0.1f*8+4534, 0.0f)*0.125f;
height *= fnlGetNoise3D(noise, real_x*0.0125f*8+1000,real_z*0.0125f*8+1000, 0.0f)/2+0.5f;
height += 1.0f;
height *= 64.0f;
return height;
}
int generate_tree(fnl_state *noise, PseudoRandom* random, const float* heights, int real_x, int real_y, int real_z, int tileSize){
int generate_tree(fnl_state *noise, PseudoRandom* random, 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);
const int tileZ = floor((double)real_z/(double)tileSize);
random->setSeed(tileX*4325261+tileZ*12160951+tileSize*9431111);
bool gentree = fnlGetNoise3D(noise, tileX*3.0f+633, 0.0, tileY*3.0f) > -0.1f && (random->rand() % 10) < 7;
bool gentree = fnlGetNoise3D(noise, tileX*3.0f+633, 0.0, tileZ*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 < 57) || (fnlGetNoise3D(noise, real_x*0.025f,real_z*0.025f, 0.0f)*0.5f > 0.5))
int centerZ = tileZ * tileSize + tileSize/2 + randomZ;
// int height = (int)(heights[centerX*CHUNK_W+centerZ]);
int height = (int)calc_height_faster(noise, centerX, centerZ);
if ((height < 57)/* || (fnlGetNoise3D(noise, real_x*0.025f,real_z*0.025f, 0.0f)*0.5f > 0.5)*/)
return 0;
int lx = real_x - centerX;
int radius = random->rand() % 4 + 3;
int radius = random->rand() % 4 + 2;
int ly = real_y - height - 3 * radius;
int lz = real_z - centerY;
if (lx == 0 && lz == 0 && real_y - height < 4*radius)
int lz = real_z - centerZ;
if (lx == 0 && lz == 0 && real_y - height < (3*radius + radius/2))
return 6;
if (lx*lx+ly*ly/2+lz*lz < radius*radius)
return 7;
@ -100,6 +107,7 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
noise.seed = seed * 60617077 % 25896307;
PseudoRandom randomtree;
PseudoRandom randomgrass;
float heights[CHUNK_VOL];
@ -121,6 +129,7 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
for (int y = 0; y < CHUNK_H; y++){
int real_y = y;
int id = real_y < 55 ? BLOCK_WATER : BLOCK_AIR;
int states = 0;
if ((real_y == (int)height) && (54 < real_y)) {
id = BLOCK_GRASS_BLOCK;
} else if (real_y < (height - 6)){
@ -128,13 +137,14 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
} else if (real_y < height){
id = BLOCK_DIRT;
} else {
int tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 16);
int tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 23);
if (tree) {
id = tree;
} else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 19))){
id = tree;
} else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 23))){
id = tree;
states = 0x32;
// } else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 19))){
// id = tree;
// } else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 23))){
// id = tree;
}
}
if ( ((height - (1.5 - 0.2 * pow(height - 54, 4))) < real_y) && (real_y < height)){
@ -142,13 +152,20 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
}
if (real_y <= 2)
id = BLOCK_BEDROCK;
if ((id == 0) && (real_y > 55) && ((int)height + 1 == real_y) && ((unsigned short)random() > 56000)){
randomgrass.setSeed(real_x,real_z);
if ((id == 0) && (height > 55.5) && ((int)(height + 1) == real_y) && ((unsigned short)randomgrass.rand() > 56000)){
id = BLOCK_GRASS;
}
if ((id == 0) && (real_y > 55) && ((int)height + 1 == real_y) && ((unsigned short)random() > 64000)){
if ((id == 0) && (height > 55.5) && ((int)(height + 1) == real_y) && ((unsigned short)randomgrass.rand() > 65000)){
id = BLOCK_FLOWER;
}
if ((height > 56) && ((int)(height + 1) == real_y) && ((unsigned short)randomgrass.rand() > 65533)){
id = BLOCK_WOOD;
states = 0x32;
}
voxels[(y * CHUNK_D + z) * CHUNK_W + x].id = id;
voxels[(y * CHUNK_D + z) * CHUNK_W + x].states = states;
}
}
}

View File

@ -5,6 +5,7 @@
struct voxel {
uint8_t id;
uint8_t states;
};
#endif /* VOXELS_VOXEL_H_ */

View File

@ -15,7 +15,7 @@ bool Events::_cursor_started = false;
#define _MOUSE_BUTTONS 1024
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos){
void cursor_position_callback(GLFWwindow*, double xpos, double ypos){
if (Events::_cursor_started){
Events::deltaX += xpos-Events::x;
Events::deltaY += ypos-Events::y;
@ -27,7 +27,7 @@ void cursor_position_callback(GLFWwindow* window, double xpos, double ypos){
Events::y = ypos;
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mode){
void mouse_button_callback(GLFWwindow*, int button, int action, int){
if (action == GLFW_PRESS){
Events::_keys[_MOUSE_BUTTONS+button] = true;
Events::_frames[_MOUSE_BUTTONS+button] = Events::_current;
@ -38,7 +38,7 @@ void mouse_button_callback(GLFWwindow* window, int button, int action, int mode)
}
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
void key_callback(GLFWwindow*, int key, int /*scancode*/, int action, int /*mode*/) {
if (action == GLFW_PRESS){
Events::_keys[key] = true;
Events::_frames[key] = Events::_current;
@ -49,7 +49,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
}
}
void window_size_callback(GLFWwindow* window, int width, int height){
void window_size_callback(GLFWwindow*, int width, int height){
glViewport(0,0, width, height);
Window::width = width;
Window::height = height;

View File

@ -1,19 +1,19 @@
#include <iostream>
#include "Window.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Window.h"
GLFWwindow* Window::window;
int Window::width = 0;
int Window::height = 0;
GLFWwindow* Window::window = nullptr;
uint Window::width = 0;
uint Window::height = 0;
int Window::initialize(int width, int height, const char* title){
int Window::initialize(uint width, uint height, const char* title){
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
//glfwWindowHint(GLFW_SAMPLES, 2);
glfwWindowHint(GLFW_SAMPLES, 16);
window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (window == nullptr){
@ -34,6 +34,8 @@ int Window::initialize(int width, int height, const char* title){
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glEnable(GL_MULTISAMPLE);
// glDisable(GL_MULTISAMPLE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Window::width = width;

View File

@ -1,14 +1,16 @@
#ifndef WINDOW_WINDOW_H_
#define WINDOW_WINDOW_H_
#include "../typedefs.h"
class GLFWwindow;
class Window {
public:
static int width;
static int height;
static uint width;
static uint height;
static GLFWwindow* window; // не лучшее решение делать window публичным
static int initialize(int width, int height, const char* title);
static int initialize(uint width, uint height, const char* title);
static void terminate();
static void viewport(int x, int y, int width, int height);

View File

@ -1,7 +1,12 @@
#include "Level.h"
#include "World.h"
#include "../lighting/Lighting.h"
#include "../voxels/Chunks.h"
#include "../voxels/ChunksController.h"
#include "../player_control.h"
#include "../physics/Hitbox.h"
#include "../physics/PhysicsSolver.h"
#include "../objects/Player.h"
Level::Level(World* world, Player* player, Chunks* chunks, PhysicsSolver* physics) :
world(world),
@ -21,3 +26,16 @@ Level::~Level(){
delete chunksController;
delete playerController;
}
void Level::update(float delta, bool interactions) {
playerController->update_controls(delta);
if (interactions) {
playerController->update_interaction();
}
else
{
playerController->selectedBlockId = -1;
}
vec3 position = player->hitbox->position;
chunks->setCenter(world->wfile, position.x, position.z);
}

View File

@ -20,6 +20,9 @@ public:
PlayerController* playerController;
Level(World* world, Player* player, Chunks* chunks, PhysicsSolver* physics);
~Level();
void update(float delta, bool interactions);
};
#endif /* WORLD_LEVEL_H_ */

View File

@ -3,6 +3,10 @@
#include "../files/WorldFiles.h"
#include "../voxels/Chunk.h"
#include "../voxels/Chunks.h"
#include "Level.h"
#include "../objects/Player.h"
#include "../physics/PhysicsSolver.h"
#include "../window/Camera.h"
World::World(std::string name, std::string directory, int seed) : name(name), seed(seed) {
wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8));
@ -11,3 +15,27 @@ World::World(std::string name, std::string directory, int seed) : name(name), se
World::~World(){
delete wfile;
}
void World::write(Level* level) {
Chunks* chunks = level->chunks;
for (unsigned int i = 0; i < chunks->volume; i++) {
Chunk* chunk = chunks->chunks[i];
if (chunk == nullptr || !chunk->isUnsaved())
continue;
wfile->put((const char*)chunk->voxels, chunk->x, chunk->z);
}
wfile->write();
wfile->writePlayer(level->player);
}
Level* World::loadLevel(Player* player) {
Level* level = new Level(this, player, new Chunks(56, 56, 0, 0), new PhysicsSolver(vec3(0, -19.6f, 0)));
wfile->readPlayer(player);
Camera* camera = player->camera;
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
return level;
}

View File

@ -5,6 +5,8 @@
class WorldFiles;
class Chunks;
class Level;
class Player;
class World {
public:
@ -14,6 +16,9 @@ public:
World(std::string name, std::string directory, int seed);
~World();
void write(Level* level);
Level* loadLevel(Player* player);
};
#endif /* WORLD_WORLD_H_ */

View File

@ -2,6 +2,7 @@
#include <iostream>
#include "graphics/VoxelRenderer.h"
#include <GL/glew.h>
#include "window/Window.h"
#include "window/Camera.h"
@ -63,15 +64,16 @@ bool WorldRenderer::drawChunk(size_t index, Camera* camera, Shader* shader, bool
}
}
}
mat4 model = glm::translate(mat4(1.0f), vec3(chunk->x*CHUNK_W+0.5f, 0.5f, chunk->z*CHUNK_D+0.5f));
shader->uniformMatrix("u_model", model);
glDisable(GL_MULTISAMPLE);
mesh->draw(GL_TRIANGLES);
glEnable(GL_MULTISAMPLE);
return false;
}
void WorldRenderer::draw(World* world, Camera* camera, bool occlusion){
void WorldRenderer::draw(Camera* camera, bool occlusion){
Chunks* chunks = level->chunks;
vec4 skyColor(0.7f, 0.81f, 1.0f, 1.0f);
@ -133,21 +135,54 @@ void WorldRenderer::draw(World* world, Camera* camera, bool occlusion){
// draw 3D stuff here
batch3d->render();
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());
glLineWidth(2.0f);
lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x+0.01f, camera->position.y-0.1f, camera->position.z, 1, 0, 0, 1);
lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x, camera->position.y-0.1f, camera->position.z+0.01f, 0, 0, 1, 1);
lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x, camera->position.y-0.1f+0.01f, camera->position.z, 0, 1, 0, 1);
lineBatch->render();
if (level->playerController->selectedBlockId != -1){
Block* selectedBlock = Block::blocks[level->playerController->selectedBlockId];
vec3 pos = level->playerController->selectedBlockPosition;
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());
glLineWidth(2.0f);
if (selectedBlock->model == 1){
lineBatch->box(pos.x+0.5f, pos.y+0.5f, pos.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
} else if (selectedBlock->model == 2){
lineBatch->box(pos.x+0.4f, pos.y+0.3f, pos.z+0.4f, 0.805f,0.805f,0.805f, 0,0,0,0.5f);
lineBatch->box(pos.x+0.5f, pos.y+0.35f, pos.z+0.5f, 0.805f,0.705f,0.805f, 0,0,0,0.5f);
}
lineBatch->render();
}
if (level->player->debug) {
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());
vec3 point = vec3(camera->position.x+camera->front.x/1,
camera->position.y+camera->front.y/1,
camera->position.z+camera->front.z/1);
glDisable(GL_DEPTH_TEST);
glLineWidth(4.0f);
lineBatch->line(point.x, point.y, point.z,
point.x+0.1f, point.y, point.z,
0, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y, point.z+0.1f,
0, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y+0.1f, point.z,
0, 0, 0, 1);
lineBatch->render();
glLineWidth(2.0f);
lineBatch->line(point.x, point.y, point.z,
point.x+0.1f, point.y, point.z,
1, 0, 0, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y, point.z+0.1f,
0, 0, 1, 1);
lineBatch->line(point.x, point.y, point.z,
point.x, point.y+0.1f, point.z,
0, 1, 0, 1);
lineBatch->render();
glEnable(GL_DEPTH_TEST);
}
}

View File

@ -4,10 +4,7 @@
#include <vector>
#include <algorithm>
#include <GL/glew.h>
#ifndef std::string
#include <string>
#endif
#include <glm/glm.hpp>
#include <glm/ext.hpp>
@ -37,7 +34,7 @@ public:
WorldRenderer(Level* level, Assets* assets);
~WorldRenderer();
void draw(World* world, Camera* camera, bool occlusion);
void draw(Camera* camera, bool occlusion);
};