WorldRenderer and HudRenderer classes
This commit is contained in:
parent
5955c74208
commit
d3f29f8ace
1
.gitignore
vendored
1
.gitignore
vendored
@ -11,3 +11,4 @@ world
|
|||||||
.cproject
|
.cproject
|
||||||
.project
|
.project
|
||||||
.git
|
.git
|
||||||
|
/Default/
|
||||||
|
|||||||
@ -6,16 +6,22 @@
|
|||||||
CPP_SRCS += \
|
CPP_SRCS += \
|
||||||
../src/Assets.cpp \
|
../src/Assets.cpp \
|
||||||
../src/player_control.cpp \
|
../src/player_control.cpp \
|
||||||
|
../src/hud_render.cpp \
|
||||||
|
../src/world_render.cpp \
|
||||||
../src/voxel_engine.cpp
|
../src/voxel_engine.cpp
|
||||||
|
|
||||||
OBJS += \
|
OBJS += \
|
||||||
./src/Assets.o \
|
./src/Assets.o \
|
||||||
./src/player_control.o \
|
./src/player_control.o \
|
||||||
|
../src/hud_render.o \
|
||||||
|
../src/world_render.o \
|
||||||
./src/voxel_engine.o
|
./src/voxel_engine.o
|
||||||
|
|
||||||
CPP_DEPS += \
|
CPP_DEPS += \
|
||||||
./src/Assets.d \
|
./src/Assets.d \
|
||||||
./src/player_control.d \
|
./src/player_control.d \
|
||||||
|
../src/hud_render.d \
|
||||||
|
../src/world_render.d \
|
||||||
./src/voxel_engine.d
|
./src/voxel_engine.d
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -83,7 +83,7 @@ void setup_definitions() {
|
|||||||
block->skyLightPassing = true;
|
block->skyLightPassing = true;
|
||||||
block->obstacle = false;
|
block->obstacle = false;
|
||||||
block->selectable = false;
|
block->selectable = false;
|
||||||
block->type = 0;
|
block->model = 0;
|
||||||
Block::blocks[block->id] = block;
|
Block::blocks[block->id] = block;
|
||||||
|
|
||||||
// DIRT 1
|
// DIRT 1
|
||||||
@ -150,7 +150,7 @@ void setup_definitions() {
|
|||||||
block->drawGroup = 5;
|
block->drawGroup = 5;
|
||||||
block->lightPassing = true;
|
block->lightPassing = true;
|
||||||
block->obstacle = false;
|
block->obstacle = false;
|
||||||
block->type = 2;
|
block->model = 2;
|
||||||
Block::blocks[block->id] = block;
|
Block::blocks[block->id] = block;
|
||||||
|
|
||||||
// FLOWER 13
|
// FLOWER 13
|
||||||
@ -158,7 +158,7 @@ void setup_definitions() {
|
|||||||
block->drawGroup = 5;
|
block->drawGroup = 5;
|
||||||
block->lightPassing = true;
|
block->lightPassing = true;
|
||||||
block->obstacle = false;
|
block->obstacle = false;
|
||||||
block->type = 2;
|
block->model = 2;
|
||||||
Block::blocks[block->id] = block;
|
Block::blocks[block->id] = block;
|
||||||
}
|
}
|
||||||
#endif // DECLARATIONS_H
|
#endif // DECLARATIONS_H
|
||||||
|
|||||||
@ -300,7 +300,7 @@ inline void _renderXBlock(std::vector<float>& buffer, int x, int y, int z, const
|
|||||||
float xs = (float)(char)rand / 512;
|
float xs = (float)(char)rand / 512;
|
||||||
float zs = (float)(char)(rand >> 8) / 512;
|
float zs = (float)(char)(rand >> 8) / 512;
|
||||||
|
|
||||||
if (block->type != 2){
|
if (block->model != 2){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -179,9 +179,9 @@ void update_interaction(Level* level, LineBatch* lineBatch){
|
|||||||
vec3 iend;
|
vec3 iend;
|
||||||
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
|
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
|
||||||
if (vox != nullptr){
|
if (vox != nullptr){
|
||||||
if (Block::blocks[vox->id]->type == 1){
|
if (Block::blocks[vox->id]->model == 1){
|
||||||
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);
|
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);
|
||||||
} else if (Block::blocks[vox->id]->type == 2){
|
} else if (Block::blocks[vox->id]->model == 2){
|
||||||
lineBatch->box(iend.x+0.4f, iend.y+0.3f, iend.z+0.4f, 0.805f,0.805f,0.805f, 0,0,0,0.5f);
|
lineBatch->box(iend.x+0.4f, iend.y+0.3f, iend.z+0.4f, 0.805f,0.805f,0.805f, 0,0,0,0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -51,6 +51,7 @@ using namespace glm;
|
|||||||
|
|
||||||
#include "declarations.h"
|
#include "declarations.h"
|
||||||
#include "world_render.h"
|
#include "world_render.h"
|
||||||
|
#include "hud_render.h"
|
||||||
#include "player_control.h"
|
#include "player_control.h"
|
||||||
|
|
||||||
|
|
||||||
@ -77,7 +78,7 @@ void write_world(World* world, Level* level){
|
|||||||
world->wfile->writePlayer(level->player);
|
world->wfile->writePlayer(level->player);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_level(World* world, Level* level, vec3 position, float delta, long frame){
|
void update_level(World* world, Level* level, vec3 position, float delta, long frame, VoxelRenderer* renderer, LineBatch* lineBatch){
|
||||||
update_controls(level->physics, level->chunks, level->player, delta);
|
update_controls(level->physics, level->chunks, level->player, delta);
|
||||||
update_interaction(level, lineBatch);
|
update_interaction(level, lineBatch);
|
||||||
|
|
||||||
@ -134,7 +135,8 @@ int main() {
|
|||||||
Level* level = load_level(world, player);
|
Level* level = load_level(world, player);
|
||||||
|
|
||||||
std::cout << "-- preparing systems" << std::endl;
|
std::cout << "-- preparing systems" << std::endl;
|
||||||
init_renderer();
|
HudRenderer hud;
|
||||||
|
WorldRenderer worldRenderer(level);
|
||||||
|
|
||||||
float lastTime = glfwGetTime();
|
float lastTime = glfwGetTime();
|
||||||
float delta = 0.0f;
|
float delta = 0.0f;
|
||||||
@ -159,9 +161,9 @@ int main() {
|
|||||||
devdata = !devdata;
|
devdata = !devdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
update_level(world, level, camera->position, delta, frame);
|
update_level(world, level, camera->position, delta, frame, worldRenderer.renderer, worldRenderer.lineBatch);
|
||||||
draw_world(world, level, camera, assets, occlusion);
|
worldRenderer.draw(world, camera, assets, occlusion);
|
||||||
draw_hud(world, level, assets, devdata, fps);
|
hud.draw(world, level, assets, devdata, fps);
|
||||||
|
|
||||||
Window::swapBuffers();
|
Window::swapBuffers();
|
||||||
Events::pullEvents();
|
Events::pullEvents();
|
||||||
@ -172,7 +174,6 @@ int main() {
|
|||||||
|
|
||||||
std::cout << "-- shutting down" << std::endl;
|
std::cout << "-- shutting down" << std::endl;
|
||||||
delete assets;
|
delete assets;
|
||||||
finalize_renderer();
|
|
||||||
Audio::finalize();
|
Audio::finalize();
|
||||||
Events::finalize();
|
Events::finalize();
|
||||||
Window::terminate();
|
Window::terminate();
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
#ifndef VOXELS_BLOCK_H_
|
#ifndef VOXELS_BLOCK_H_
|
||||||
#define VOXELS_BLOCK_H_
|
#define VOXELS_BLOCK_H_
|
||||||
|
|
||||||
|
#define BLOCK_MODEL_CUBE 1
|
||||||
|
#define BLOCK_MODEL_GRASS 2
|
||||||
|
|
||||||
class Block {
|
class Block {
|
||||||
public:
|
public:
|
||||||
static Block* blocks[256];
|
static Block* blocks[256];
|
||||||
@ -15,7 +18,7 @@ public:
|
|||||||
bool obstacle = true;
|
bool obstacle = true;
|
||||||
bool selectable = true;
|
bool selectable = true;
|
||||||
bool breakable = true;
|
bool breakable = true;
|
||||||
unsigned char type = 1;
|
unsigned char model = 1;
|
||||||
|
|
||||||
Block(unsigned int id, int texture);
|
Block(unsigned int id, int texture);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -13,214 +13,30 @@
|
|||||||
#include <glm/ext.hpp>
|
#include <glm/ext.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
#include "window/Window.h"
|
class World;
|
||||||
#include "window/Camera.h"
|
class Level;
|
||||||
#include "graphics/Mesh.h"
|
class Camera;
|
||||||
#include "graphics/Shader.h"
|
class Assets;
|
||||||
#include "graphics/Texture.h"
|
class LineBatch;
|
||||||
#include "graphics/LineBatch.h"
|
class Batch3D;
|
||||||
#include "graphics/Batch3D.h"
|
class VoxelRenderer;
|
||||||
#include "voxels/Chunks.h"
|
class Shader;
|
||||||
#include "voxels/Chunk.h"
|
|
||||||
|
|
||||||
float _camera_cx;
|
|
||||||
float _camera_cz;
|
|
||||||
Chunks* _chunks;
|
|
||||||
|
|
||||||
Mesh *crosshair;
|
class WorldRenderer {
|
||||||
|
Batch3D *batch3d;
|
||||||
|
Level* level;
|
||||||
|
|
||||||
float vertices[] = {
|
void drawChunk(size_t index, Camera* camera, Shader* shader, bool occlusion);
|
||||||
// x y
|
public:
|
||||||
-0.01f,-0.01f,
|
VoxelRenderer *renderer;
|
||||||
0.01f, 0.01f,
|
LineBatch *lineBatch;
|
||||||
|
|
||||||
-0.01f, 0.01f,
|
WorldRenderer(Level* level);
|
||||||
0.01f,-0.01f,
|
~WorldRenderer();
|
||||||
|
|
||||||
|
void draw(World* world, Camera* camera, Assets* assets, bool occlusion);
|
||||||
};
|
};
|
||||||
|
|
||||||
int attrs[] = {
|
|
||||||
2, 0 //null terminator
|
|
||||||
};
|
|
||||||
|
|
||||||
int uiscale = 1;
|
|
||||||
|
|
||||||
LineBatch *lineBatch;
|
|
||||||
Batch2D *batch;
|
|
||||||
Batch3D *batch3d;
|
|
||||||
Camera *uicamera;
|
|
||||||
VoxelRenderer *renderer;
|
|
||||||
|
|
||||||
void init_renderer(){
|
|
||||||
crosshair = new Mesh(vertices, 4, attrs);
|
|
||||||
lineBatch = new LineBatch(4096);
|
|
||||||
|
|
||||||
batch = new Batch2D(1024);
|
|
||||||
batch3d = new Batch3D(1024);
|
|
||||||
uicamera = new Camera(glm::vec3(), Window::height / uiscale);
|
|
||||||
uicamera->perspective = false;
|
|
||||||
uicamera->flipped = true;
|
|
||||||
|
|
||||||
renderer = new VoxelRenderer();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void finalize_renderer(){
|
|
||||||
delete crosshair;
|
|
||||||
delete lineBatch;
|
|
||||||
delete batch;
|
|
||||||
delete renderer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw_chunk(size_t index, Camera* camera, Shader* shader, bool occlusion){
|
|
||||||
Chunk* chunk = _chunks->chunks[index];
|
|
||||||
Mesh* mesh = _chunks->meshes[index];
|
|
||||||
if (mesh == nullptr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Simple frustum culling (culling chunks behind the camera in 2D - XZ)
|
|
||||||
if (occlusion){
|
|
||||||
const float cameraX = camera->position.x;
|
|
||||||
const float cameraZ = camera->position.z;
|
|
||||||
const float camDirX = camera->dir.x;
|
|
||||||
const float camDirZ = camera->dir.z;
|
|
||||||
|
|
||||||
bool unoccluded = false;
|
|
||||||
do {
|
|
||||||
if ((chunk->x*CHUNK_W-cameraX)*camDirX + (chunk->z*CHUNK_D-cameraZ)*camDirZ >= 0.0){
|
|
||||||
unoccluded = true; break;
|
|
||||||
}
|
|
||||||
if (((chunk->x+1)*CHUNK_W-cameraX)*camDirX + (chunk->z*CHUNK_D-cameraZ)*camDirZ >= 0.0){
|
|
||||||
unoccluded = true; break;
|
|
||||||
}
|
|
||||||
if (((chunk->x+1)*CHUNK_W-cameraX)*camDirX + ((chunk->z+1)*CHUNK_D-cameraZ)*camDirZ >= 0.0){
|
|
||||||
unoccluded = true; break;
|
|
||||||
}
|
|
||||||
if ((chunk->x*CHUNK_W-cameraX)*camDirX + ((chunk->z+1)*CHUNK_D-cameraZ)*camDirZ >= 0.0){
|
|
||||||
unoccluded = true; break;
|
|
||||||
}
|
|
||||||
} while (false);
|
|
||||||
if (!unoccluded)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
mat4 model = glm::translate(mat4(1.0f), vec3(chunk->x*CHUNK_W+0.5f, chunk->y*CHUNK_H+0.5f, chunk->z*CHUNK_D+0.5f));
|
|
||||||
shader->uniformMatrix("u_model", model);
|
|
||||||
mesh->draw(GL_TRIANGLES);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool chunks_comparator(size_t i, size_t j) {
|
|
||||||
Chunk* a = _chunks->chunks[i];
|
|
||||||
Chunk* b = _chunks->chunks[j];
|
|
||||||
return ((a->x + 0.5f - _camera_cx)*(a->x + 0.5f - _camera_cx) + (a->z + 0.5f - _camera_cz)*(a->z + 0.5f - _camera_cz)
|
|
||||||
>
|
|
||||||
(b->x + 0.5f - _camera_cx)*(b->x + 0.5f - _camera_cx) + (b->z + 0.5f - _camera_cz)*(b->z + 0.5f - _camera_cz));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void draw_hud(World* world, Level* level, Assets* assets, bool devdata, int fps){
|
|
||||||
Chunks* chunks = level->chunks;
|
|
||||||
Player* player = level->player;
|
|
||||||
|
|
||||||
glDisable(GL_DEPTH_TEST);
|
|
||||||
glDisable(GL_CULL_FACE);
|
|
||||||
Shader* uishader = assets->getShader("ui");
|
|
||||||
uishader->use();
|
|
||||||
uishader->uniformMatrix("u_projview", uicamera->getProjection()*uicamera->getView());
|
|
||||||
|
|
||||||
// draw debug info
|
|
||||||
Font* font = assets->getFont("normal");
|
|
||||||
batch->begin();
|
|
||||||
if (devdata){
|
|
||||||
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, L"fps:", 16, 42, STYLE_OUTLINE);
|
|
||||||
font->draw(batch, std::to_wstring(fps), 40, 42, STYLE_OUTLINE);
|
|
||||||
}
|
|
||||||
batch->render();
|
|
||||||
|
|
||||||
// choosen block preview
|
|
||||||
Texture* blocks = assets->getTexture("block");
|
|
||||||
Texture* sprite = assets->getTexture("sprite");
|
|
||||||
|
|
||||||
batch->texture(sprite);
|
|
||||||
batch->sprite(16, 640, 64, 64, 16, 0, vec4(1.0f));
|
|
||||||
|
|
||||||
batch->texture(blocks);
|
|
||||||
Block* cblock = Block::blocks[player->choosenBlock];
|
|
||||||
if (cblock->type == 1){
|
|
||||||
batch->blockSprite(24, 648, 48, 48, 16, cblock->textureFaces, vec4(1.0f));
|
|
||||||
} else if (cblock->type == 2){
|
|
||||||
batch->sprite(24, 648, 48, 48, 16, cblock->textureFaces[3], vec4(1.0f));
|
|
||||||
}
|
|
||||||
|
|
||||||
batch->render();
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw_world(World* world, Level* level, Camera* camera, Assets* assets, bool occlusion){
|
|
||||||
Chunks* chunks = level->chunks;
|
|
||||||
|
|
||||||
glClearColor(0.7f,0.81f,1.0f,1);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
|
||||||
glEnable(GL_CULL_FACE);
|
|
||||||
|
|
||||||
_chunks = chunks;
|
|
||||||
|
|
||||||
Texture* texture = assets->getTexture("block");
|
|
||||||
Shader* shader = assets->getShader("main");
|
|
||||||
Shader* crosshairShader = assets->getShader("crosshair");
|
|
||||||
Shader* linesShader = assets->getShader("lines");
|
|
||||||
shader->use();
|
|
||||||
shader->uniformMatrix("u_proj", camera->getProjection());
|
|
||||||
shader->uniformMatrix("u_view", camera->getView());
|
|
||||||
shader->uniform1f("u_gamma", 1.6f);
|
|
||||||
shader->uniform3f("u_skyLightColor", 2.2f,2.2f,2.2f);
|
|
||||||
shader->uniform3f("u_fogColor", 0.7f,0.71f,0.73f);
|
|
||||||
shader->uniform3f("u_cameraPos", camera->position.x,camera->position.y,camera->position.z);
|
|
||||||
texture->bind();
|
|
||||||
|
|
||||||
std::vector<size_t> indices;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < chunks->volume; i++){
|
|
||||||
Chunk* chunk = chunks->chunks[i];
|
|
||||||
if (chunk == nullptr)
|
|
||||||
continue;
|
|
||||||
if (chunks->meshes[i] != nullptr)
|
|
||||||
indices.push_back(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
float px = camera->position.x / (float)CHUNK_W;
|
|
||||||
float pz = camera->position.z / (float)CHUNK_D;
|
|
||||||
|
|
||||||
_camera_cx = px;
|
|
||||||
_camera_cz = pz;
|
|
||||||
|
|
||||||
std::sort(indices.begin(), indices.end(), chunks_comparator);
|
|
||||||
|
|
||||||
|
|
||||||
for (size_t i = 0; i < indices.size(); i++){
|
|
||||||
draw_chunk(indices[i], camera, shader, occlusion);
|
|
||||||
}
|
|
||||||
|
|
||||||
shader->uniformMatrix("u_model", mat4(1.0f));
|
|
||||||
batch3d->begin();
|
|
||||||
// draw 3D stuff here
|
|
||||||
batch3d->render();
|
|
||||||
|
|
||||||
crosshairShader->use();
|
|
||||||
crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);
|
|
||||||
crosshairShader->uniform1f("u_scale", 1.0f / ((float)Window::height / 1000.0f));
|
|
||||||
crosshair->draw(GL_LINES);
|
|
||||||
|
|
||||||
linesShader->use();
|
|
||||||
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());
|
|
||||||
glLineWidth(2.0f);
|
|
||||||
lineBatch->line(camera->position.x, camera->position.y-0.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();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // WORLD_RENDERER_CPP
|
#endif // WORLD_RENDERER_CPP
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user