'Assets', some refactor

This commit is contained in:
MihailRis 2022-03-03 18:14:34 +03:00 committed by GitHub
parent b7e8ff5bea
commit 2aba3e91db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 350 additions and 213 deletions

View File

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

View File

@ -1,7 +1,3 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/objects/Player.cpp

View File

@ -4,13 +4,16 @@
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/voxel_engine.cpp
../src/voxel_engine.cpp \
../src/Assets.cpp
OBJS += \
./src/voxel_engine.o
./src/voxel_engine.o \
./src/Assets.o
CPP_DEPS += \
./src/voxel_engine.d
./src/voxel_engine.d \
./src/Assets.d
# Each subdirectory must supply rules for building sources it contributes

View File

@ -30,5 +30,6 @@ src/lighting \
src/loaders \
src/physics \
src/voxels \
src/objects \
src/window \

View File

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

View File

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

View File

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

BIN
res/ascii.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
res/block (copy).png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

31
src/Assets.cpp Normal file
View File

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

22
src/Assets.h Normal file
View File

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

View File

@ -1,8 +1,8 @@
#ifndef DECLARATIONS_CPP
#define DECLARATIONS_CPP
#ifndef DECLARATIONS_H
#define DECLARATIONS_H
#include <iostream>
#include "Assets.h"
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "window/Window.h"
@ -10,49 +10,42 @@
#include "voxels/Block.h"
Shader *shader, *linesShader, *crosshairShader;
Texture *texture;
// 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){
std::cerr << "failed to load shader" << std::endl;
Window::terminate();
return 1;
std::cerr << "failed to load shader '" << name << "'" << std::endl;
return false;
}
crosshairShader = load_shader("res/crosshair.glslv", "res/crosshair.glslf");
if (crosshairShader == nullptr){
std::cerr << "failed to load crosshair shader" << std::endl;
Window::terminate();
return 1;
}
linesShader = load_shader("res/lines.glslv", "res/lines.glslf");
if (linesShader == nullptr){
std::cerr << "failed to load lines shader" << std::endl;
Window::terminate();
return 1;
}
texture = load_texture("res/block.png");
if (texture == nullptr){
std::cerr << "failed to load texture" << std::endl;
delete shader;
Window::terminate();
return 1;
}
return 0;
assets->store(shader, name);
return true;
}
// Deleting GL objects like shaders, textures
void finalize_assets(){
delete shader;
delete texture;
delete crosshairShader;
delete linesShader;
bool _load_texture(Assets* assets, std::string filename, std::string name){
Texture* texture = load_texture(filename);
if (texture == nullptr){
std::cerr << "failed to load texture '" << name << "'" << std::endl;
return false;
}
assets->store(texture, name);
return true;
}
int initialize_assets(Assets* assets) {
#define LOAD_SHADER(VERTEX, FRAGMENT, NAME) \
if (!_load_shader(assets, VERTEX, FRAGMENT, NAME))\
return 1;
#define LOAD_TEXTURE(FILENAME, NAME) \
if (!_load_texture(assets, FILENAME, NAME))\
return 1;
LOAD_SHADER("res/main.glslv", "res/main.glslf", "main");
LOAD_SHADER("res/crosshair.glslv", "res/crosshair.glslf", "crosshair");
LOAD_SHADER("res/lines.glslv", "res/lines.glslf", "lines");
LOAD_TEXTURE("res/block.png", "block");
return 0;
}
@ -115,5 +108,5 @@ void setup_definitions() {
block->obstacle = false;
Block::blocks[block->id] = block;
}
#endif // DECLARATIONS_CPP
#endif // DECLARATIONS_H

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

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

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

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

View File

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

View File

@ -5,6 +5,8 @@
Player::Player(glm::vec3 position, float speed, Camera* camera) :
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));
}

View File

@ -11,6 +11,7 @@ public:
float speed;
Camera* camera;
Hitbox* hitbox;
int choosenBlock;
float camX, camY;
Player(glm::vec3 position, float speed, Camera* camera);
~Player();

View File

@ -1,3 +1,5 @@
// Install dependencies:
// sudo apt install libgl-dev libglew-dev libglfw3-dev libpng-dev libglm-dev
#include <iostream>
#define GLEW_STATIC
@ -37,22 +39,15 @@ using namespace glm;
#include "physics/Hitbox.h"
#include "physics/PhysicsSolver.h"
#include "Assets.h"
#include "objects/Player.h"
#include "declarations.h"
#include "world_render.h"
int WIDTH = 1280;
int HEIGHT = 720;
Chunks* chunks;
WorldFiles* wfile;
bool occlusion = false;
// Save all world data to files
void write_world(){
void write_world(WorldFiles* wfile, Chunks* chunks){
for (unsigned int i = 0; i < chunks->volume; i++){
Chunk* chunk = chunks->chunks[i];
if (chunk == nullptr)
@ -64,78 +59,17 @@ void write_world(){
}
// Deleting world data from memory
void close_world(){
void close_world(WorldFiles* wfile, Chunks* chunks){
delete chunks;
delete wfile;
}
int main() {
setup_definitions();
Window::initialize(WIDTH, HEIGHT, "Window 2.0");
Events::initialize();
int result = initialize_assets();
if (result){
Window::terminate();
return result;
}
Camera* camera = new Camera(vec3(-320,255,32), radians(90.0f));
wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8));
chunks = new Chunks(34,1,34, 0,0,0);
Player* player = new Player(vec3(camera->position), 5.0f, camera);
wfile->readPlayer(player);
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
VoxelRenderer renderer(1024*1024);
PhysicsSolver physics(vec3(0,-16.0f,0));
Lighting lighting(chunks);
init_renderer();
ChunksController chunksController(chunks, &lighting);
float lastTime = glfwGetTime();
float delta = 0.0f;
int choosenBlock = 1;
long frame = 0;
glfwSwapInterval(1);
while (!Window::isShouldClose()){
frame++;
float currentTime = glfwGetTime();
delta = currentTime - lastTime;
lastTime = currentTime;
if (frame % 240 == 0)
std::cout << 1.0/delta << std::endl;
if (Events::jpressed(GLFW_KEY_O)){
occlusion = !occlusion;
}
if (Events::jpressed(GLFW_KEY_ESCAPE)){
Window::setShouldClose(true);
}
if (Events::jpressed(GLFW_KEY_TAB)){
Events::toogleCursor();
}
for (int i = 1; i < 10; i++){
if (Events::jpressed(GLFW_KEY_0+i)){
choosenBlock = i;
}
}
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;
@ -143,7 +77,7 @@ int main() {
float speed = player->speed;
int substeps = (int)(delta * 1000);
substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps));
physics.step(chunks, hitbox, delta, substeps, shift);
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;
@ -185,10 +119,6 @@ int main() {
hitbox->velocity.x = dir.x * speed;
hitbox->velocity.z = dir.z * speed;
chunks->setCenter(wfile, camera->position.x,0,camera->position.z);
chunksController._buildMeshes(&renderer, frame);
chunksController.loadVisible(wfile);
if (Events::_cursor_locked){
player->camY += -Events::deltaY / Window::height * 2;
player->camX += -Events::deltaX / Window::height * 2;
@ -203,7 +133,10 @@ int main() {
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;
@ -217,31 +150,116 @@ int main() {
int y = (int)iend.y;
int z = (int)iend.z;
chunks->set(x,y,z, 0);
lighting.onBlockSet(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);
if (!physics->isBlockInside(x,y,z, player->hitbox)){
chunks->set(x, y, z, player->choosenBlock);
lighting->onBlockSet(x,y,z, player->choosenBlock);
}
}
}
}
draw_world(camera, shader, texture, crosshairShader, linesShader, chunks, occlusion);
}
int WIDTH = 1280;
int HEIGHT = 720;
int main() {
setup_definitions();
Window::initialize(WIDTH, HEIGHT, "Window 2.0");
Events::initialize();
std::cout << "-- loading assets" << std::endl;
Assets* assets = new Assets();
int result = initialize_assets(assets);
if (result){
delete assets;
Window::terminate();
return result;
}
std::cout << "-- loading world" << std::endl;
Camera *camera = new Camera(vec3(-320,255,32), radians(90.0f));
WorldFiles *wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8));
Chunks *chunks = new Chunks(34,1,34, 0,0,0);
Player* player = new Player(vec3(camera->position), 5.0f, camera);
wfile->readPlayer(player);
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
std::cout << "-- preparing systems" << std::endl;
VoxelRenderer renderer(1024*1024);
PhysicsSolver physics(vec3(0,-16.0f,0));
Lighting lighting(chunks);
init_renderer();
ChunksController chunksController(chunks, &lighting);
float lastTime = glfwGetTime();
float delta = 0.0f;
long frame = 0;
bool occlusion = false;
glfwSwapInterval(1);
std::cout << "-- initializing finished" << std::endl;
while (!Window::isShouldClose()){
frame++;
float currentTime = glfwGetTime();
delta = currentTime - lastTime;
lastTime = currentTime;
if (Events::jpressed(GLFW_KEY_O)){
occlusion = !occlusion;
}
if (Events::jpressed(GLFW_KEY_ESCAPE)){
Window::setShouldClose(true);
}
if (Events::jpressed(GLFW_KEY_TAB)){
Events::toogleCursor();
}
for (int i = 1; i < 10; i++){
if (Events::jpressed(GLFW_KEY_0+i)){
player->choosenBlock = i;
}
}
update_controls(&physics, chunks, player, delta);
update_interaction(chunks, &physics, player, &lighting);
chunks->setCenter(wfile, camera->position.x,0,camera->position.z);
chunksController._buildMeshes(&renderer, frame);
chunksController.loadVisible(wfile);
draw_world(camera, assets, chunks, occlusion);
Window::swapBuffers();
Events::pullEvents();
}
std::cout << "-- saving world" << std::endl;
wfile->writePlayer(player);
write_world();
close_world();
write_world(wfile, chunks);
close_world(wfile, chunks);
std::cout << "-- shutting down" << std::endl;
delete assets;
finalize_renderer();
finalize_assets();
Window::terminate();
return 0;
}

View File

@ -96,8 +96,7 @@ bool chunks_comparator(size_t i, size_t j) {
}
void draw_world(Camera* camera, Shader* shader, Texture* texture,
Shader* crosshairShader, Shader* linesShader,
void draw_world(Camera* camera, Assets* assets,
Chunks* chunks, bool occlusion){
glClearColor(0.7f,0.71f,0.73f,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -105,6 +104,10 @@ void draw_world(Camera* camera, Shader* shader, Texture* texture,
_chunks = chunks;
// Draw VAO
Texture* texture = assets->getTexture("block");
Shader* shader = assets->getShader("main");
Shader* crosshairShader = assets->getShader("crosshair");
Shader* linesShader = assets->getShader("lines");
shader->use();
shader->uniformMatrix("u_proj", camera->getProjection());
shader->uniformMatrix("u_view", camera->getView());