'Assets', some refactor
This commit is contained in:
parent
b7e8ff5bea
commit
2aba3e91db
@ -1,28 +1,27 @@
|
|||||||
################################################################################
|
|
||||||
# Automatically-generated file. Do not edit!
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# Add inputs and outputs from these tool invocations to the build variables
|
# Add inputs and outputs from these tool invocations to the build variables
|
||||||
CPP_SRCS += \
|
CPP_SRCS += \
|
||||||
../src/graphics/LineBatch.cpp \
|
../src/graphics/LineBatch.cpp \
|
||||||
../src/graphics/Mesh.cpp \
|
../src/graphics/Mesh.cpp \
|
||||||
../src/graphics/Shader.cpp \
|
../src/graphics/Shader.cpp \
|
||||||
../src/graphics/Texture.cpp \
|
../src/graphics/Texture.cpp \
|
||||||
../src/graphics/VoxelRenderer.cpp
|
../src/graphics/VoxelRenderer.cpp \
|
||||||
|
../src/graphics/Batch2D.cpp
|
||||||
|
|
||||||
OBJS += \
|
OBJS += \
|
||||||
./src/graphics/LineBatch.o \
|
./src/graphics/LineBatch.o \
|
||||||
./src/graphics/Mesh.o \
|
./src/graphics/Mesh.o \
|
||||||
./src/graphics/Shader.o \
|
./src/graphics/Shader.o \
|
||||||
./src/graphics/Texture.o \
|
./src/graphics/Texture.o \
|
||||||
./src/graphics/VoxelRenderer.o
|
./src/graphics/VoxelRenderer.o \
|
||||||
|
./src/graphics/Batch2D.o
|
||||||
|
|
||||||
CPP_DEPS += \
|
CPP_DEPS += \
|
||||||
./src/graphics/LineBatch.d \
|
./src/graphics/LineBatch.d \
|
||||||
./src/graphics/Mesh.d \
|
./src/graphics/Mesh.d \
|
||||||
./src/graphics/Shader.d \
|
./src/graphics/Shader.d \
|
||||||
./src/graphics/Texture.d \
|
./src/graphics/Texture.d \
|
||||||
./src/graphics/VoxelRenderer.d
|
./src/graphics/VoxelRenderer.d \
|
||||||
|
./src/graphics/Batch2D.d
|
||||||
|
|
||||||
|
|
||||||
# Each subdirectory must supply rules for building sources it contributes
|
# Each subdirectory must supply rules for building sources it contributes
|
||||||
|
|||||||
@ -1,7 +1,3 @@
|
|||||||
################################################################################
|
|
||||||
# Automatically-generated file. Do not edit!
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# Add inputs and outputs from these tool invocations to the build variables
|
# Add inputs and outputs from these tool invocations to the build variables
|
||||||
CPP_SRCS += \
|
CPP_SRCS += \
|
||||||
../src/objects/Player.cpp
|
../src/objects/Player.cpp
|
||||||
|
|||||||
@ -4,13 +4,16 @@
|
|||||||
|
|
||||||
# Add inputs and outputs from these tool invocations to the build variables
|
# Add inputs and outputs from these tool invocations to the build variables
|
||||||
CPP_SRCS += \
|
CPP_SRCS += \
|
||||||
../src/voxel_engine.cpp
|
../src/voxel_engine.cpp \
|
||||||
|
../src/Assets.cpp
|
||||||
|
|
||||||
OBJS += \
|
OBJS += \
|
||||||
./src/voxel_engine.o
|
./src/voxel_engine.o \
|
||||||
|
./src/Assets.o
|
||||||
|
|
||||||
CPP_DEPS += \
|
CPP_DEPS += \
|
||||||
./src/voxel_engine.d
|
./src/voxel_engine.d \
|
||||||
|
./src/Assets.d
|
||||||
|
|
||||||
|
|
||||||
# Each subdirectory must supply rules for building sources it contributes
|
# Each subdirectory must supply rules for building sources it contributes
|
||||||
|
|||||||
@ -22,13 +22,14 @@ CPP_DEPS :=
|
|||||||
C_DEPS :=
|
C_DEPS :=
|
||||||
|
|
||||||
# Every subdirectory with source files must be described here
|
# Every subdirectory with source files must be described here
|
||||||
SUBDIRS := \
|
SUBDIRS := \
|
||||||
src \
|
src \
|
||||||
src/files \
|
src/files \
|
||||||
src/graphics \
|
src/graphics \
|
||||||
src/lighting \
|
src/lighting \
|
||||||
src/loaders \
|
src/loaders \
|
||||||
src/physics \
|
src/physics \
|
||||||
src/voxels \
|
src/voxels \
|
||||||
src/window \
|
src/objects \
|
||||||
|
src/window \
|
||||||
|
|
||||||
|
|||||||
@ -1,28 +1,27 @@
|
|||||||
################################################################################
|
|
||||||
# Automatically-generated file. Do not edit!
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# Add inputs and outputs from these tool invocations to the build variables
|
# Add inputs and outputs from these tool invocations to the build variables
|
||||||
CPP_SRCS += \
|
CPP_SRCS += \
|
||||||
../src/graphics/LineBatch.cpp \
|
../src/graphics/LineBatch.cpp \
|
||||||
../src/graphics/Mesh.cpp \
|
../src/graphics/Mesh.cpp \
|
||||||
../src/graphics/Shader.cpp \
|
../src/graphics/Shader.cpp \
|
||||||
../src/graphics/Texture.cpp \
|
../src/graphics/Texture.cpp \
|
||||||
../src/graphics/VoxelRenderer.cpp
|
../src/graphics/VoxelRenderer.cpp \
|
||||||
|
../src/graphics/Batch2D.cpp
|
||||||
|
|
||||||
OBJS += \
|
OBJS += \
|
||||||
./src/graphics/LineBatch.o \
|
./src/graphics/LineBatch.o \
|
||||||
./src/graphics/Mesh.o \
|
./src/graphics/Mesh.o \
|
||||||
./src/graphics/Shader.o \
|
./src/graphics/Shader.o \
|
||||||
./src/graphics/Texture.o \
|
./src/graphics/Texture.o \
|
||||||
./src/graphics/VoxelRenderer.o
|
./src/graphics/VoxelRenderer.o \
|
||||||
|
./src/graphics/Batch2D.o
|
||||||
|
|
||||||
CPP_DEPS += \
|
CPP_DEPS += \
|
||||||
./src/graphics/LineBatch.d \
|
./src/graphics/LineBatch.d \
|
||||||
./src/graphics/Mesh.d \
|
./src/graphics/Mesh.d \
|
||||||
./src/graphics/Shader.d \
|
./src/graphics/Shader.d \
|
||||||
./src/graphics/Texture.d \
|
./src/graphics/Texture.d \
|
||||||
./src/graphics/VoxelRenderer.d
|
./src/graphics/VoxelRenderer.d \
|
||||||
|
./src/graphics/Batch2D.d
|
||||||
|
|
||||||
|
|
||||||
# Each subdirectory must supply rules for building sources it contributes
|
# Each subdirectory must supply rules for building sources it contributes
|
||||||
|
|||||||
20
Debug_win/src/objects/subdir.mk
Normal file
20
Debug_win/src/objects/subdir.mk
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Add inputs and outputs from these tool invocations to the build variables
|
||||||
|
CPP_SRCS += \
|
||||||
|
../src/objects/Player.cpp
|
||||||
|
|
||||||
|
OBJS += \
|
||||||
|
./src/objects/Player.o
|
||||||
|
|
||||||
|
CPP_DEPS += \
|
||||||
|
./src/objects/Player.d
|
||||||
|
|
||||||
|
|
||||||
|
# Each subdirectory must supply rules for building sources it contributes
|
||||||
|
src/objects/%.o: ../src/objects/%.cpp src/objects/subdir.mk
|
||||||
|
@echo 'Building file: $<'
|
||||||
|
@echo 'Invoking: GCC C++ Compiler'
|
||||||
|
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<"
|
||||||
|
@echo 'Finished building: $<'
|
||||||
|
@echo ' '
|
||||||
|
|
||||||
|
|
||||||
@ -1,16 +1,15 @@
|
|||||||
################################################################################
|
|
||||||
# Automatically-generated file. Do not edit!
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# Add inputs and outputs from these tool invocations to the build variables
|
# Add inputs and outputs from these tool invocations to the build variables
|
||||||
CPP_SRCS += \
|
CPP_SRCS += \
|
||||||
../src/voxel_engine.cpp
|
../src/voxel_engine.cpp \
|
||||||
|
../src/Assets.cpp
|
||||||
|
|
||||||
OBJS += \
|
OBJS += \
|
||||||
./src/voxel_engine.o
|
./src/voxel_engine.o \
|
||||||
|
./src/Assets.o
|
||||||
|
|
||||||
CPP_DEPS += \
|
CPP_DEPS += \
|
||||||
./src/voxel_engine.d
|
./src/voxel_engine.d \
|
||||||
|
./src/Assets.d
|
||||||
|
|
||||||
|
|
||||||
# Each subdirectory must supply rules for building sources it contributes
|
# Each subdirectory must supply rules for building sources it contributes
|
||||||
|
|||||||
BIN
res/ascii.png
Normal file
BIN
res/ascii.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.0 KiB |
BIN
res/block (copy).png
Normal file
BIN
res/block (copy).png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.1 KiB |
31
src/Assets.cpp
Normal file
31
src/Assets.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include "Assets.h"
|
||||||
|
|
||||||
|
#include "graphics/Texture.h"
|
||||||
|
#include "graphics/Shader.h"
|
||||||
|
|
||||||
|
Assets::~Assets() {
|
||||||
|
for (auto& iter : shaders){
|
||||||
|
delete iter.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& iter : textures){
|
||||||
|
delete iter.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture* Assets::getTexture(std::string name){
|
||||||
|
return textures[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
void Assets::store(Texture* texture, std::string name){
|
||||||
|
textures[name] = texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Shader* Assets::getShader(std::string name){
|
||||||
|
return shaders[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
void Assets::store(Shader* shader, std::string name){
|
||||||
|
shaders[name] = shader;
|
||||||
|
}
|
||||||
22
src/Assets.h
Normal file
22
src/Assets.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef SRC_ASSETS_H_
|
||||||
|
#define SRC_ASSETS_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
class Texture;
|
||||||
|
class Shader;
|
||||||
|
|
||||||
|
class Assets {
|
||||||
|
std::unordered_map<std::string, Texture*> textures;
|
||||||
|
std::unordered_map<std::string, Shader*> shaders;
|
||||||
|
public:
|
||||||
|
~Assets();
|
||||||
|
Texture* getTexture(std::string name);
|
||||||
|
void store(Texture* texture, std::string name);
|
||||||
|
|
||||||
|
Shader* getShader(std::string name);
|
||||||
|
void store(Shader* shader, std::string name);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* SRC_ASSETS_H_ */
|
||||||
@ -1,8 +1,8 @@
|
|||||||
#ifndef DECLARATIONS_CPP
|
#ifndef DECLARATIONS_H
|
||||||
#define DECLARATIONS_CPP
|
#define DECLARATIONS_H
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "Assets.h"
|
||||||
#include "graphics/Shader.h"
|
#include "graphics/Shader.h"
|
||||||
#include "graphics/Texture.h"
|
#include "graphics/Texture.h"
|
||||||
#include "window/Window.h"
|
#include "window/Window.h"
|
||||||
@ -10,49 +10,42 @@
|
|||||||
#include "voxels/Block.h"
|
#include "voxels/Block.h"
|
||||||
|
|
||||||
|
|
||||||
Shader *shader, *linesShader, *crosshairShader;
|
|
||||||
Texture *texture;
|
|
||||||
|
|
||||||
|
|
||||||
// Shaders, textures, renderers
|
// Shaders, textures, renderers
|
||||||
int initialize_assets() {
|
|
||||||
shader = load_shader("res/main.glslv", "res/main.glslf");
|
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){
|
if (shader == nullptr){
|
||||||
std::cerr << "failed to load shader" << std::endl;
|
std::cerr << "failed to load shader '" << name << "'" << std::endl;
|
||||||
Window::terminate();
|
return false;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
assets->store(shader, name);
|
||||||
crosshairShader = load_shader("res/crosshair.glslv", "res/crosshair.glslf");
|
return true;
|
||||||
if (crosshairShader == nullptr){
|
|
||||||
std::cerr << "failed to load crosshair shader" << std::endl;
|
|
||||||
Window::terminate();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
linesShader = load_shader("res/lines.glslv", "res/lines.glslf");
|
|
||||||
if (linesShader == nullptr){
|
|
||||||
std::cerr << "failed to load lines shader" << std::endl;
|
|
||||||
Window::terminate();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
texture = load_texture("res/block.png");
|
|
||||||
if (texture == nullptr){
|
|
||||||
std::cerr << "failed to load texture" << std::endl;
|
|
||||||
delete shader;
|
|
||||||
Window::terminate();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deleting GL objects like shaders, textures
|
bool _load_texture(Assets* assets, std::string filename, std::string name){
|
||||||
void finalize_assets(){
|
Texture* texture = load_texture(filename);
|
||||||
delete shader;
|
if (texture == nullptr){
|
||||||
delete texture;
|
std::cerr << "failed to load texture '" << name << "'" << std::endl;
|
||||||
delete crosshairShader;
|
return false;
|
||||||
delete linesShader;
|
}
|
||||||
|
assets->store(texture, name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int initialize_assets(Assets* assets) {
|
||||||
|
#define LOAD_SHADER(VERTEX, FRAGMENT, NAME) \
|
||||||
|
if (!_load_shader(assets, VERTEX, FRAGMENT, NAME))\
|
||||||
|
return 1;
|
||||||
|
#define LOAD_TEXTURE(FILENAME, NAME) \
|
||||||
|
if (!_load_texture(assets, FILENAME, NAME))\
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
LOAD_SHADER("res/main.glslv", "res/main.glslf", "main");
|
||||||
|
LOAD_SHADER("res/crosshair.glslv", "res/crosshair.glslf", "crosshair");
|
||||||
|
LOAD_SHADER("res/lines.glslv", "res/lines.glslf", "lines");
|
||||||
|
|
||||||
|
LOAD_TEXTURE("res/block.png", "block");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -115,5 +108,5 @@ void setup_definitions() {
|
|||||||
block->obstacle = false;
|
block->obstacle = false;
|
||||||
Block::blocks[block->id] = block;
|
Block::blocks[block->id] = block;
|
||||||
}
|
}
|
||||||
#endif // DECLARATIONS_CPP
|
#endif // DECLARATIONS_H
|
||||||
|
|
||||||
|
|||||||
18
src/graphics/Batch2D.cpp
Normal file
18
src/graphics/Batch2D.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include "Batch2D.h"
|
||||||
|
#include "Mesh.h"
|
||||||
|
|
||||||
|
Batch2D::Batch2D(size_t capacity) : capacity(capacity),
|
||||||
|
offset(0),
|
||||||
|
color(1.0f, 1.0f, 1.0f, 1.0f){
|
||||||
|
const int attrs[] = {
|
||||||
|
2, 2, 4, 0 //null terminator
|
||||||
|
};
|
||||||
|
|
||||||
|
buffer = new float[capacity];
|
||||||
|
mesh = new Mesh(nullptr, 0, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
Batch2D::~Batch2D(){
|
||||||
|
delete buffer;
|
||||||
|
delete mesh;
|
||||||
|
}
|
||||||
27
src/graphics/Batch2D.h
Normal file
27
src/graphics/Batch2D.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef SRC_GRAPHICS_BATCH2D_H_
|
||||||
|
#define SRC_GRAPHICS_BATCH2D_H_
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
class Mesh;
|
||||||
|
|
||||||
|
class Batch2D {
|
||||||
|
float* buffer;
|
||||||
|
size_t capacity;
|
||||||
|
size_t offset;
|
||||||
|
glm::vec4 color;
|
||||||
|
Mesh* mesh;
|
||||||
|
|
||||||
|
void vertex(float x, float y,
|
||||||
|
float u, float v,
|
||||||
|
float r, float g, float b, float a);
|
||||||
|
public:
|
||||||
|
Batch2D(size_t capacity);
|
||||||
|
~Batch2D();
|
||||||
|
|
||||||
|
void rect(float x, float y, float w, float h);
|
||||||
|
void render();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* SRC_GRAPHICS_BATCH2D_H_ */
|
||||||
@ -12,7 +12,12 @@ Mesh::Mesh(const float* buffer, size_t vertices, const int* attrs) : vertices(ve
|
|||||||
|
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, buffer, GL_STATIC_DRAW);
|
if (buffer){
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, buffer, GL_STATIC_DRAW);
|
||||||
|
} else {
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// attributes
|
// attributes
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
Player::Player(glm::vec3 position, float speed, Camera* camera) :
|
Player::Player(glm::vec3 position, float speed, Camera* camera) :
|
||||||
speed(speed),
|
speed(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.2f,0.9f,0.2f));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ public:
|
|||||||
float speed;
|
float speed;
|
||||||
Camera* camera;
|
Camera* camera;
|
||||||
Hitbox* hitbox;
|
Hitbox* hitbox;
|
||||||
|
int choosenBlock;
|
||||||
float camX, camY;
|
float camX, camY;
|
||||||
Player(glm::vec3 position, float speed, Camera* camera);
|
Player(glm::vec3 position, float speed, Camera* camera);
|
||||||
~Player();
|
~Player();
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
// Install dependencies:
|
||||||
|
// sudo apt install libgl-dev libglew-dev libglfw3-dev libpng-dev libglm-dev
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define GLEW_STATIC
|
#define GLEW_STATIC
|
||||||
@ -37,22 +39,15 @@ using namespace glm;
|
|||||||
#include "physics/Hitbox.h"
|
#include "physics/Hitbox.h"
|
||||||
#include "physics/PhysicsSolver.h"
|
#include "physics/PhysicsSolver.h"
|
||||||
|
|
||||||
|
#include "Assets.h"
|
||||||
#include "objects/Player.h"
|
#include "objects/Player.h"
|
||||||
|
|
||||||
#include "declarations.h"
|
#include "declarations.h"
|
||||||
#include "world_render.h"
|
#include "world_render.h"
|
||||||
|
|
||||||
int WIDTH = 1280;
|
|
||||||
int HEIGHT = 720;
|
|
||||||
|
|
||||||
Chunks* chunks;
|
|
||||||
WorldFiles* wfile;
|
|
||||||
|
|
||||||
bool occlusion = false;
|
|
||||||
|
|
||||||
|
|
||||||
// Save all world data to files
|
// Save all world data to files
|
||||||
void write_world(){
|
void write_world(WorldFiles* wfile, Chunks* chunks){
|
||||||
for (unsigned int i = 0; i < chunks->volume; i++){
|
for (unsigned int i = 0; i < chunks->volume; i++){
|
||||||
Chunk* chunk = chunks->chunks[i];
|
Chunk* chunk = chunks->chunks[i];
|
||||||
if (chunk == nullptr)
|
if (chunk == nullptr)
|
||||||
@ -64,27 +59,134 @@ void write_world(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Deleting world data from memory
|
// Deleting world data from memory
|
||||||
void close_world(){
|
void close_world(WorldFiles* wfile, Chunks* chunks){
|
||||||
delete chunks;
|
delete chunks;
|
||||||
delete wfile;
|
delete wfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void update_controls(PhysicsSolver* physics,
|
||||||
|
Chunks* chunks,
|
||||||
|
Player* player,
|
||||||
|
float delta){
|
||||||
|
// Controls
|
||||||
|
Camera* camera = player->camera;
|
||||||
|
Hitbox* hitbox = player->hitbox;
|
||||||
|
bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL);
|
||||||
|
bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint;
|
||||||
|
|
||||||
|
float speed = player->speed;
|
||||||
|
int substeps = (int)(delta * 1000);
|
||||||
|
substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps));
|
||||||
|
physics->step(chunks, hitbox, delta, substeps, shift);
|
||||||
|
camera->position.x = hitbox->position.x;
|
||||||
|
camera->position.y = hitbox->position.y + 0.5f;
|
||||||
|
camera->position.z = hitbox->position.z;
|
||||||
|
|
||||||
|
float dt = min(1.0f, delta * 16);
|
||||||
|
if (shift){
|
||||||
|
speed *= 0.25f;
|
||||||
|
camera->position.y -= 0.2f;
|
||||||
|
camera->zoom = 0.9f * dt + camera->zoom * (1.0f - dt);
|
||||||
|
} else if (sprint){
|
||||||
|
speed *= 1.5f;
|
||||||
|
camera->zoom = 1.1f * dt + camera->zoom * (1.0f - dt);
|
||||||
|
} else {
|
||||||
|
camera->zoom = dt + camera->zoom * (1.0f - dt);
|
||||||
|
}
|
||||||
|
if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){
|
||||||
|
hitbox->velocity.y = 6.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 dir(0,0,0);
|
||||||
|
if (Events::pressed(GLFW_KEY_W)){
|
||||||
|
dir.x += camera->dir.x;
|
||||||
|
dir.z += camera->dir.z;
|
||||||
|
}
|
||||||
|
if (Events::pressed(GLFW_KEY_S)){
|
||||||
|
dir.x -= camera->dir.x;
|
||||||
|
dir.z -= camera->dir.z;
|
||||||
|
}
|
||||||
|
if (Events::pressed(GLFW_KEY_D)){
|
||||||
|
dir.x += camera->right.x;
|
||||||
|
dir.z += camera->right.z;
|
||||||
|
}
|
||||||
|
if (Events::pressed(GLFW_KEY_A)){
|
||||||
|
dir.x -= camera->right.x;
|
||||||
|
dir.z -= camera->right.z;
|
||||||
|
}
|
||||||
|
if (length(dir) > 0.0f)
|
||||||
|
dir = normalize(dir);
|
||||||
|
hitbox->velocity.x = dir.x * speed;
|
||||||
|
hitbox->velocity.z = dir.z * speed;
|
||||||
|
|
||||||
|
if (Events::_cursor_locked){
|
||||||
|
player->camY += -Events::deltaY / Window::height * 2;
|
||||||
|
player->camX += -Events::deltaX / Window::height * 2;
|
||||||
|
|
||||||
|
if (player->camY < -radians(89.0f)){
|
||||||
|
player->camY = -radians(89.0f);
|
||||||
|
}
|
||||||
|
if (player->camY > radians(89.0f)){
|
||||||
|
player->camY = radians(89.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
camera->rotation = mat4(1.0f);
|
||||||
|
camera->rotate(player->camY, player->camX, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting){
|
||||||
|
Camera* camera = player->camera;
|
||||||
|
{
|
||||||
|
vec3 end;
|
||||||
|
vec3 norm;
|
||||||
|
vec3 iend;
|
||||||
|
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
|
||||||
|
if (vox != nullptr){
|
||||||
|
lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
|
||||||
|
|
||||||
|
if (Events::jclicked(GLFW_MOUSE_BUTTON_1)){
|
||||||
|
int x = (int)iend.x;
|
||||||
|
int y = (int)iend.y;
|
||||||
|
int z = (int)iend.z;
|
||||||
|
chunks->set(x,y,z, 0);
|
||||||
|
lighting->onBlockSet(x,y,z,0);
|
||||||
|
}
|
||||||
|
if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){
|
||||||
|
int x = (int)(iend.x)+(int)(norm.x);
|
||||||
|
int y = (int)(iend.y)+(int)(norm.y);
|
||||||
|
int z = (int)(iend.z)+(int)(norm.z);
|
||||||
|
if (!physics->isBlockInside(x,y,z, player->hitbox)){
|
||||||
|
chunks->set(x, y, z, player->choosenBlock);
|
||||||
|
lighting->onBlockSet(x,y,z, player->choosenBlock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int WIDTH = 1280;
|
||||||
|
int HEIGHT = 720;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
setup_definitions();
|
setup_definitions();
|
||||||
|
|
||||||
Window::initialize(WIDTH, HEIGHT, "Window 2.0");
|
Window::initialize(WIDTH, HEIGHT, "Window 2.0");
|
||||||
Events::initialize();
|
Events::initialize();
|
||||||
|
|
||||||
int result = initialize_assets();
|
std::cout << "-- loading assets" << std::endl;
|
||||||
|
Assets* assets = new Assets();
|
||||||
|
int result = initialize_assets(assets);
|
||||||
if (result){
|
if (result){
|
||||||
|
delete assets;
|
||||||
Window::terminate();
|
Window::terminate();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
std::cout << "-- loading world" << std::endl;
|
||||||
|
|
||||||
Camera* camera = new Camera(vec3(-320,255,32), radians(90.0f));
|
Camera *camera = new Camera(vec3(-320,255,32), radians(90.0f));
|
||||||
|
WorldFiles *wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8));
|
||||||
wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8));
|
Chunks *chunks = new Chunks(34,1,34, 0,0,0);
|
||||||
chunks = new Chunks(34,1,34, 0,0,0);
|
|
||||||
|
|
||||||
|
|
||||||
Player* player = new Player(vec3(camera->position), 5.0f, camera);
|
Player* player = new Player(vec3(camera->position), 5.0f, camera);
|
||||||
@ -92,9 +194,10 @@ int main() {
|
|||||||
camera->rotation = mat4(1.0f);
|
camera->rotation = mat4(1.0f);
|
||||||
camera->rotate(player->camY, player->camX, 0);
|
camera->rotate(player->camY, player->camX, 0);
|
||||||
|
|
||||||
|
std::cout << "-- preparing systems" << std::endl;
|
||||||
|
|
||||||
VoxelRenderer renderer(1024*1024);
|
VoxelRenderer renderer(1024*1024);
|
||||||
PhysicsSolver physics(vec3(0,-16.0f,0));
|
PhysicsSolver physics(vec3(0,-16.0f,0));
|
||||||
|
|
||||||
Lighting lighting(chunks);
|
Lighting lighting(chunks);
|
||||||
|
|
||||||
init_renderer();
|
init_renderer();
|
||||||
@ -104,20 +207,20 @@ int main() {
|
|||||||
float lastTime = glfwGetTime();
|
float lastTime = glfwGetTime();
|
||||||
float delta = 0.0f;
|
float delta = 0.0f;
|
||||||
|
|
||||||
int choosenBlock = 1;
|
|
||||||
long frame = 0;
|
long frame = 0;
|
||||||
|
|
||||||
|
bool occlusion = false;
|
||||||
|
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
|
|
||||||
|
std::cout << "-- initializing finished" << std::endl;
|
||||||
|
|
||||||
while (!Window::isShouldClose()){
|
while (!Window::isShouldClose()){
|
||||||
frame++;
|
frame++;
|
||||||
float currentTime = glfwGetTime();
|
float currentTime = glfwGetTime();
|
||||||
delta = currentTime - lastTime;
|
delta = currentTime - lastTime;
|
||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
|
|
||||||
if (frame % 240 == 0)
|
|
||||||
std::cout << 1.0/delta << std::endl;
|
|
||||||
|
|
||||||
if (Events::jpressed(GLFW_KEY_O)){
|
if (Events::jpressed(GLFW_KEY_O)){
|
||||||
occlusion = !occlusion;
|
occlusion = !occlusion;
|
||||||
}
|
}
|
||||||
@ -131,117 +234,32 @@ int main() {
|
|||||||
|
|
||||||
for (int i = 1; i < 10; i++){
|
for (int i = 1; i < 10; i++){
|
||||||
if (Events::jpressed(GLFW_KEY_0+i)){
|
if (Events::jpressed(GLFW_KEY_0+i)){
|
||||||
choosenBlock = i;
|
player->choosenBlock = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Controls
|
update_controls(&physics, chunks, player, delta);
|
||||||
Hitbox* hitbox = player->hitbox;
|
update_interaction(chunks, &physics, player, &lighting);
|
||||||
bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL);
|
|
||||||
bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint;
|
|
||||||
|
|
||||||
float speed = player->speed;
|
|
||||||
int substeps = (int)(delta * 1000);
|
|
||||||
substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps));
|
|
||||||
physics.step(chunks, hitbox, delta, substeps, shift);
|
|
||||||
camera->position.x = hitbox->position.x;
|
|
||||||
camera->position.y = hitbox->position.y + 0.5f;
|
|
||||||
camera->position.z = hitbox->position.z;
|
|
||||||
|
|
||||||
float dt = min(1.0f, delta * 16);
|
|
||||||
if (shift){
|
|
||||||
speed *= 0.25f;
|
|
||||||
camera->position.y -= 0.2f;
|
|
||||||
camera->zoom = 0.9f * dt + camera->zoom * (1.0f - dt);
|
|
||||||
} else if (sprint){
|
|
||||||
speed *= 1.5f;
|
|
||||||
camera->zoom = 1.1f * dt + camera->zoom * (1.0f - dt);
|
|
||||||
} else {
|
|
||||||
camera->zoom = dt + camera->zoom * (1.0f - dt);
|
|
||||||
}
|
|
||||||
if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){
|
|
||||||
hitbox->velocity.y = 6.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 dir(0,0,0);
|
|
||||||
if (Events::pressed(GLFW_KEY_W)){
|
|
||||||
dir.x += camera->dir.x;
|
|
||||||
dir.z += camera->dir.z;
|
|
||||||
}
|
|
||||||
if (Events::pressed(GLFW_KEY_S)){
|
|
||||||
dir.x -= camera->dir.x;
|
|
||||||
dir.z -= camera->dir.z;
|
|
||||||
}
|
|
||||||
if (Events::pressed(GLFW_KEY_D)){
|
|
||||||
dir.x += camera->right.x;
|
|
||||||
dir.z += camera->right.z;
|
|
||||||
}
|
|
||||||
if (Events::pressed(GLFW_KEY_A)){
|
|
||||||
dir.x -= camera->right.x;
|
|
||||||
dir.z -= camera->right.z;
|
|
||||||
}
|
|
||||||
if (length(dir) > 0.0f)
|
|
||||||
dir = normalize(dir);
|
|
||||||
hitbox->velocity.x = dir.x * speed;
|
|
||||||
hitbox->velocity.z = dir.z * speed;
|
|
||||||
|
|
||||||
chunks->setCenter(wfile, camera->position.x,0,camera->position.z);
|
chunks->setCenter(wfile, camera->position.x,0,camera->position.z);
|
||||||
chunksController._buildMeshes(&renderer, frame);
|
chunksController._buildMeshes(&renderer, frame);
|
||||||
chunksController.loadVisible(wfile);
|
chunksController.loadVisible(wfile);
|
||||||
|
|
||||||
if (Events::_cursor_locked){
|
draw_world(camera, assets, chunks, occlusion);
|
||||||
player->camY += -Events::deltaY / Window::height * 2;
|
|
||||||
player->camX += -Events::deltaX / Window::height * 2;
|
|
||||||
|
|
||||||
if (player->camY < -radians(89.0f)){
|
|
||||||
player->camY = -radians(89.0f);
|
|
||||||
}
|
|
||||||
if (player->camY > radians(89.0f)){
|
|
||||||
player->camY = radians(89.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
camera->rotation = mat4(1.0f);
|
|
||||||
camera->rotate(player->camY, player->camX, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
vec3 end;
|
|
||||||
vec3 norm;
|
|
||||||
vec3 iend;
|
|
||||||
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
|
|
||||||
if (vox != nullptr){
|
|
||||||
lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
|
|
||||||
|
|
||||||
if (Events::jclicked(GLFW_MOUSE_BUTTON_1)){
|
|
||||||
int x = (int)iend.x;
|
|
||||||
int y = (int)iend.y;
|
|
||||||
int z = (int)iend.z;
|
|
||||||
chunks->set(x,y,z, 0);
|
|
||||||
lighting.onBlockSet(x,y,z,0);
|
|
||||||
}
|
|
||||||
if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){
|
|
||||||
int x = (int)(iend.x)+(int)(norm.x);
|
|
||||||
int y = (int)(iend.y)+(int)(norm.y);
|
|
||||||
int z = (int)(iend.z)+(int)(norm.z);
|
|
||||||
if (!physics.isBlockInside(x,y,z, hitbox)){
|
|
||||||
chunks->set(x, y, z, choosenBlock);
|
|
||||||
lighting.onBlockSet(x,y,z, choosenBlock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
draw_world(camera, shader, texture, crosshairShader, linesShader, chunks, occlusion);
|
|
||||||
|
|
||||||
Window::swapBuffers();
|
Window::swapBuffers();
|
||||||
Events::pullEvents();
|
Events::pullEvents();
|
||||||
}
|
}
|
||||||
|
std::cout << "-- saving world" << std::endl;
|
||||||
|
|
||||||
wfile->writePlayer(player);
|
wfile->writePlayer(player);
|
||||||
write_world();
|
write_world(wfile, chunks);
|
||||||
close_world();
|
close_world(wfile, chunks);
|
||||||
|
|
||||||
|
std::cout << "-- shutting down" << std::endl;
|
||||||
|
|
||||||
|
delete assets;
|
||||||
finalize_renderer();
|
finalize_renderer();
|
||||||
finalize_assets();
|
|
||||||
Window::terminate();
|
Window::terminate();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -96,8 +96,7 @@ bool chunks_comparator(size_t i, size_t j) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void draw_world(Camera* camera, Shader* shader, Texture* texture,
|
void draw_world(Camera* camera, Assets* assets,
|
||||||
Shader* crosshairShader, Shader* linesShader,
|
|
||||||
Chunks* chunks, bool occlusion){
|
Chunks* chunks, bool occlusion){
|
||||||
glClearColor(0.7f,0.71f,0.73f,1);
|
glClearColor(0.7f,0.71f,0.73f,1);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
@ -105,6 +104,10 @@ void draw_world(Camera* camera, Shader* shader, Texture* texture,
|
|||||||
_chunks = chunks;
|
_chunks = chunks;
|
||||||
|
|
||||||
// Draw VAO
|
// Draw VAO
|
||||||
|
Texture* texture = assets->getTexture("block");
|
||||||
|
Shader* shader = assets->getShader("main");
|
||||||
|
Shader* crosshairShader = assets->getShader("crosshair");
|
||||||
|
Shader* linesShader = assets->getShader("lines");
|
||||||
shader->use();
|
shader->use();
|
||||||
shader->uniformMatrix("u_proj", camera->getProjection());
|
shader->uniformMatrix("u_proj", camera->getProjection());
|
||||||
shader->uniformMatrix("u_view", camera->getView());
|
shader->uniformMatrix("u_view", camera->getView());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user