mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-09 21:54:09 +08:00
perf: optimize multi-chunk rendering logic
This commit is contained in:
@@ -4,4 +4,60 @@ constexpr int WORLD_SIZE_Z = 32;
|
||||
constexpr int WORLD_SIZE_Y = 16;
|
||||
constexpr int MAX_BLOCK_NUM = 2;
|
||||
constexpr int CHUCK_SIZE = 16;
|
||||
constexpr int DISTANCE = 1;
|
||||
constexpr int DISTANCE = 2;
|
||||
|
||||
|
||||
constexpr float VERTICES_POS[6][6][3] = {
|
||||
// ===== front (z = +1) =====
|
||||
-0.5f, -0.5f, 0.5f, // bottom left
|
||||
-0.5f, 0.5f, 0.5f, // top left
|
||||
0.5f, 0.5f, 0.5f, // top right
|
||||
0.5f, 0.5f, 0.5f, // top right
|
||||
0.5f, -0.5f, 0.5f, // bottom right
|
||||
-0.5f, -0.5f, 0.5f, // bottom left
|
||||
// ===== right (x = +1) =====
|
||||
0.5f, -0.5f, 0.5f, // bottom front
|
||||
0.5f, -0.5f, -0.5f, // bottom back
|
||||
0.5f, 0.5f, -0.5f, // top back
|
||||
0.5f, 0.5f, -0.5f, // top back
|
||||
0.5f, 0.5f, 0.5f, // top front
|
||||
0.5f, -0.5f, 0.5f, // bottom front
|
||||
// ===== back (z = -1) =====
|
||||
-0.5f, -0.5f, -0.5f, // bottom left
|
||||
0.5f, -0.5f, -0.5f, // bottom right
|
||||
0.5f, 0.5f, -0.5f, // top right
|
||||
0.5f, 0.5f, -0.5f, // top right
|
||||
-0.5f, 0.5f, -0.5f, // top left
|
||||
-0.5f, -0.5f, -0.5f, // bottom left
|
||||
// ===== left (x = -1) =====
|
||||
-0.5f, -0.5f, -0.5f, // bottom back
|
||||
-0.5f, -0.5f, 0.5f, // bottom front
|
||||
-0.5f, 0.5f, 0.5f, // top front
|
||||
-0.5f, 0.5f, 0.5f, // top front
|
||||
-0.5f, 0.5f, -0.5f, // top back
|
||||
-0.5f, -0.5f, -0.5f, // bottom back
|
||||
// ===== top (y = +1) =====
|
||||
-0.5f, 0.5f, -0.5f, // back left
|
||||
0.5f, 0.5f, -0.5f, // back right
|
||||
0.5f, 0.5f, 0.5f, // front right
|
||||
0.5f, 0.5f, 0.5f, // front right
|
||||
-0.5f, 0.5f, 0.5f, // front left
|
||||
-0.5f, 0.5f, -0.5f, // back left
|
||||
// ===== bottom (y = -1) =====
|
||||
-0.5f, -0.5f, 0.5f, // front left
|
||||
0.5f, -0.5f, 0.5f, // front right
|
||||
0.5f, -0.5f, -0.5f, // back right
|
||||
0.5f, -0.5f, -0.5f, // back right
|
||||
-0.5f, -0.5f, -0.5f, // back left
|
||||
-0.5f, -0.5f, 0.5f // front left
|
||||
};
|
||||
|
||||
constexpr float TEX_COORDS[6][6][2] = {
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
|
||||
};
|
||||
@@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/gameplay/chuck_status.hpp>
|
||||
#include <Cubed/gameplay/block.hpp>
|
||||
|
||||
|
||||
|
||||
class Chuck {
|
||||
private:
|
||||
|
||||
// the index is a array of block id
|
||||
std::vector<uint8_t> m_blocks;
|
||||
|
||||
public:
|
||||
Chuck();
|
||||
~Chuck();
|
||||
const std::vector<uint8_t>& get_chuck_blocks() const;
|
||||
void init_chuck();
|
||||
static int get_index(int x, int y, int z);
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
struct ChuckPos {
|
||||
int x;
|
||||
int y;
|
||||
bool operator==(const ChuckPos&) const = default;
|
||||
struct Hash {
|
||||
std::size_t operator()(const ChuckPos& pos) const{
|
||||
std::size_t h1 = std::hash<int>{}(pos.x);
|
||||
std::size_t h2 = std::hash<int>{}(pos.y);
|
||||
return h1 ^ (h2 << 1);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
32
include/Cubed/gameplay/chunk.hpp
Normal file
32
include/Cubed/gameplay/chunk.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/gameplay/chunk_status.hpp>
|
||||
#include <Cubed/gameplay/block.hpp>
|
||||
|
||||
class World;
|
||||
|
||||
class Chunk {
|
||||
private:
|
||||
ChunkPos m_chunk_pos;
|
||||
World& m_world;
|
||||
// the index is a array of block id
|
||||
std::vector<uint8_t> m_blocks;
|
||||
GLuint m_vbo;
|
||||
std::vector<Vertex> m_vertexs_data;
|
||||
|
||||
public:
|
||||
Chunk(World& world, ChunkPos chunk_pos);
|
||||
~Chunk();
|
||||
const std::vector<uint8_t>& get_chunk_blocks() const;
|
||||
|
||||
static int get_index(int x, int y, int z);
|
||||
|
||||
void gen_vertex_data();
|
||||
GLuint get_vbo() const;
|
||||
const std::vector<Vertex>& get_vertex_data() const;
|
||||
void init_chunk();
|
||||
|
||||
};
|
||||
20
include/Cubed/gameplay/chunk_status.hpp
Normal file
20
include/Cubed/gameplay/chunk_status.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
struct ChunkPos {
|
||||
int x;
|
||||
int z;
|
||||
bool operator==(const ChunkPos&) const = default;
|
||||
struct Hash {
|
||||
std::size_t operator()(const ChunkPos& pos) const{
|
||||
std::size_t h1 = std::hash<int>{}(pos.x);
|
||||
std::size_t h2 = std::hash<int>{}(pos.z);
|
||||
return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
struct Vertex {
|
||||
float x, y, z;
|
||||
float s, t;
|
||||
float layer;
|
||||
};
|
||||
@@ -1,12 +1,14 @@
|
||||
#pragma once
|
||||
#include <unordered_map>
|
||||
|
||||
#include <Cubed/gameplay/chuck.hpp>
|
||||
#include <Cubed/gameplay/chunk.hpp>
|
||||
|
||||
|
||||
|
||||
class World {
|
||||
private:
|
||||
BlockRenderData m_block_render_data;
|
||||
std::unordered_map<ChuckPos , Chuck, ChuckPos::Hash> m_chucks;
|
||||
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
|
||||
|
||||
public:
|
||||
|
||||
@@ -17,5 +19,6 @@ public:
|
||||
void init_world();
|
||||
void render();
|
||||
|
||||
|
||||
|
||||
};
|
||||
@@ -1,28 +0,0 @@
|
||||
#include <Cubed/gameplay/chuck.hpp>
|
||||
|
||||
Chuck::Chuck() {
|
||||
init_chuck();
|
||||
}
|
||||
|
||||
Chuck::~Chuck() {
|
||||
|
||||
}
|
||||
|
||||
const std::vector<uint8_t>& Chuck::get_chuck_blocks() const{
|
||||
return m_blocks;
|
||||
}
|
||||
|
||||
void Chuck::init_chuck() {
|
||||
m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * CHUCK_SIZE, 0);
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int y = 0; y < 2; y++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
m_blocks[get_index(x, y, z)] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Chuck::get_index(int x, int y, int z) {
|
||||
return x * CHUCK_SIZE * CHUCK_SIZE + y * CHUCK_SIZE + z;
|
||||
}
|
||||
83
src/gameplay/chunk.cpp
Normal file
83
src/gameplay/chunk.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <Cubed/gameplay/chunk.hpp>
|
||||
#include <Cubed/gameplay/world.hpp>
|
||||
Chunk::Chunk(World& world, ChunkPos chunk_pos) :
|
||||
m_world(world),
|
||||
m_chunk_pos(chunk_pos)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Chunk::~Chunk() {
|
||||
|
||||
}
|
||||
|
||||
const std::vector<uint8_t>& Chunk::get_chunk_blocks() const{
|
||||
return m_blocks;
|
||||
}
|
||||
|
||||
|
||||
int Chunk::get_index(int x, int y, int z) {
|
||||
return x * CHUCK_SIZE * CHUCK_SIZE + y * CHUCK_SIZE + z;
|
||||
}
|
||||
|
||||
void Chunk::gen_vertex_data() {
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
for (int y = 0; y < CHUCK_SIZE; y++) {
|
||||
int world_x = x + m_chunk_pos.x * CHUCK_SIZE;
|
||||
int world_z = z + m_chunk_pos.z * CHUCK_SIZE;
|
||||
int world_y = y;
|
||||
const auto& block_render_data = m_world.get_block_render_data(world_x, world_y, world_z);
|
||||
// air
|
||||
if (block_render_data.block_id == 0) {
|
||||
continue;
|
||||
}
|
||||
for (int face = 0; face < 6; face++) {
|
||||
if (!block_render_data.draw_face[face]) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Vertex vex = {
|
||||
VERTICES_POS[face][i][0] + (float)world_x * 1.0f,
|
||||
VERTICES_POS[face][i][1] + (float)world_y * 1.0f,
|
||||
VERTICES_POS[face][i][2] + (float)world_z * 1.0f,
|
||||
TEX_COORDS[face][i][0],
|
||||
TEX_COORDS[face][i][1],
|
||||
static_cast<float>(block_render_data.block_id * 6 + face)
|
||||
|
||||
};
|
||||
m_vertexs_data.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GLuint Chunk::get_vbo() const{
|
||||
return m_vbo;
|
||||
}
|
||||
|
||||
const std::vector<Vertex>& Chunk::get_vertex_data() const{
|
||||
return m_vertexs_data;
|
||||
}
|
||||
|
||||
void Chunk::init_chunk() {
|
||||
m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * CHUCK_SIZE, 0);
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int y = 0; y < 2; y++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
m_blocks[get_index(x, y, z)] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
gen_vertex_data();
|
||||
|
||||
glGenBuffers(1, &m_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,70 +7,161 @@ World::World() {
|
||||
World::~World() {
|
||||
|
||||
}
|
||||
static int chuck_x, chuck_z;
|
||||
static int chunk_x, chunk_z;
|
||||
const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) {
|
||||
|
||||
chuck_x = world_x / CHUCK_SIZE;
|
||||
chuck_z = world_z / CHUCK_SIZE;
|
||||
|
||||
auto it = m_chucks.find((ChuckPos){chuck_x, chuck_z});
|
||||
CUBED_ASSERT_MSG(it != m_chucks.end(), "Chuck not find");
|
||||
chunk_x = world_x / CHUCK_SIZE;
|
||||
chunk_z = world_z / CHUCK_SIZE;
|
||||
//LOG::info("Chunk PosX : {} Chuch PosZ : {}", chunk_x, chunk_z);
|
||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||
CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk not find");
|
||||
|
||||
const auto& chuck_blocks = it->second.get_chuck_blocks();
|
||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - chuck_x * CHUCK_SIZE;
|
||||
z = world_z - chuck_z * CHUCK_SIZE;
|
||||
x = world_x - chunk_x * CHUCK_SIZE;
|
||||
z = world_z - chunk_z * CHUCK_SIZE;
|
||||
// block id
|
||||
m_block_render_data.block_id = chuck_blocks[Chuck::get_index(x, y, z)];
|
||||
m_block_render_data.block_id = chunk_blocks[Chunk::get_index(x, y, z)];
|
||||
// draw_face
|
||||
m_block_render_data.draw_face.assign(6, true);
|
||||
if (x > 0 ) {
|
||||
if (x > 0 && chuck_blocks[Chuck::get_index(x - 1, y, z)]) {
|
||||
if (x > 0 && chunk_blocks[Chunk::get_index(x - 1, y, z)]) {
|
||||
m_block_render_data.draw_face[3] = false;
|
||||
}
|
||||
}
|
||||
if (x < CHUCK_SIZE - 1) {
|
||||
if (x < DISTANCE * CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x + 1, y, z)]) {
|
||||
if (x < DISTANCE * CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x + 1, y, z)]) {
|
||||
m_block_render_data.draw_face[1] = false;
|
||||
}
|
||||
}
|
||||
if (z > 0 ) {
|
||||
if (z > 0 && chuck_blocks[Chuck::get_index(x, y, z - 1)]) {
|
||||
if (z > 0 && chunk_blocks[Chunk::get_index(x, y, z - 1)]) {
|
||||
m_block_render_data.draw_face[2] = false;
|
||||
}
|
||||
}
|
||||
if (z < CHUCK_SIZE - 1) {
|
||||
if (z < DISTANCE * CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x, y, z + 1)]) {
|
||||
if (z < DISTANCE * CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x, y, z + 1)]) {
|
||||
m_block_render_data.draw_face[0] = false;
|
||||
}
|
||||
}
|
||||
if (y > 0 ) {
|
||||
if (y > 0 && chuck_blocks[Chuck::get_index(x, y - 1, z)]) {
|
||||
if (y > 0 && chunk_blocks[Chunk::get_index(x, y - 1, z)]) {
|
||||
m_block_render_data.draw_face[5] = false;
|
||||
}
|
||||
}
|
||||
if (y < CHUCK_SIZE - 1) {
|
||||
if (y < CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x, y + 1, z)]) {
|
||||
if (y < CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x, y + 1, z)]) {
|
||||
m_block_render_data.draw_face[4] = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (x == 0 && world_x - 1 > 0) {
|
||||
int adjacent_chunk_x = (world_x - 1) / CHUCK_SIZE;
|
||||
int adjacet_chunk_z = world_z / CHUCK_SIZE;
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - 1 - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - adjacet_chunk_z * CHUCK_SIZE;
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[3] = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (x == CHUCK_SIZE - 1) {
|
||||
int adjacent_chunk_x = (world_x + 1) / CHUCK_SIZE;
|
||||
int adjacet_chunk_z = world_z / CHUCK_SIZE;
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x + 1 - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - adjacet_chunk_z * CHUCK_SIZE;
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[1] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (z == 0 && world_z - 1 > 0) {
|
||||
int adjacent_chunk_x = world_x / CHUCK_SIZE;
|
||||
int adjacet_chunk_z = (world_z - 1) / CHUCK_SIZE;
|
||||
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - 1 - adjacet_chunk_z * CHUCK_SIZE;
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[2] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (z == CHUCK_SIZE - 1) {
|
||||
int adjacent_chunk_x = world_x / CHUCK_SIZE;
|
||||
int adjacet_chunk_z = (world_z + 1) / CHUCK_SIZE;
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z + 1 - adjacet_chunk_z * CHUCK_SIZE;
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[0] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m_block_render_data;
|
||||
}
|
||||
|
||||
|
||||
void World::init_world() {
|
||||
for (int s = 0; s < DISTANCE; s++) {
|
||||
for (int t = 0; t < DISTANCE; t++) {
|
||||
ChuckPos pos{s, t};
|
||||
Chuck chuck;
|
||||
m_chucks[pos] = chuck;
|
||||
ChunkPos pos{s, t};
|
||||
LOG::info("Chunk Pos in init_world X: {} Z: {}", pos.x, pos.z);
|
||||
m_chunks.emplace(pos, Chunk(*this, pos));
|
||||
}
|
||||
}
|
||||
|
||||
LOG::info("World init finfish");
|
||||
|
||||
for (auto& chunk_map : m_chunks) {
|
||||
auto& [chunk_pos, chunk] = chunk_map;
|
||||
chunk.init_chunk();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void World::render() {
|
||||
|
||||
for (const auto& chunk_map : m_chunks) {
|
||||
const auto& [pos, chunk] = chunk_map;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, chunk.get_vbo());
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, s));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, layer));
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, chunk.get_vertex_data().size() * 3);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
//LOG::info("Chunk {} {} render finished", pos.x, pos.z);
|
||||
}
|
||||
}
|
||||
114
src/main.cpp
114
src/main.cpp
@@ -20,15 +20,10 @@
|
||||
constexpr int NUM_VAO = 1;
|
||||
constexpr int NUM_VBO = 1;
|
||||
|
||||
struct Vertex {
|
||||
float x, y, z;
|
||||
float s, t;
|
||||
float layer;
|
||||
};
|
||||
|
||||
|
||||
GLuint rendering_program;
|
||||
GLuint vao[NUM_VAO];
|
||||
GLuint vbo[NUM_VBO];
|
||||
GLuint mv_loc, proj_loc;
|
||||
int width ,height;
|
||||
float aspect;
|
||||
@@ -42,104 +37,17 @@ Player player;
|
||||
Camera camera;
|
||||
TextureManager texture_manager;
|
||||
World world;
|
||||
std::vector<Vertex> vertex_data;
|
||||
|
||||
|
||||
void setup_vertices(void) {
|
||||
float vertices_pos[6][6][3] = {
|
||||
// ===== front (z = +1) =====
|
||||
-0.5f, -0.5f, 0.5f, // bottom left
|
||||
-0.5f, 0.5f, 0.5f, // top left
|
||||
0.5f, 0.5f, 0.5f, // top right
|
||||
0.5f, 0.5f, 0.5f, // top right
|
||||
0.5f, -0.5f, 0.5f, // bottom right
|
||||
-0.5f, -0.5f, 0.5f, // bottom left
|
||||
// ===== right (x = +1) =====
|
||||
0.5f, -0.5f, 0.5f, // bottom front
|
||||
0.5f, -0.5f, -0.5f, // bottom back
|
||||
0.5f, 0.5f, -0.5f, // top back
|
||||
0.5f, 0.5f, -0.5f, // top back
|
||||
0.5f, 0.5f, 0.5f, // top front
|
||||
0.5f, -0.5f, 0.5f, // bottom front
|
||||
// ===== back (z = -1) =====
|
||||
-0.5f, -0.5f, -0.5f, // bottom left
|
||||
0.5f, -0.5f, -0.5f, // bottom right
|
||||
0.5f, 0.5f, -0.5f, // top right
|
||||
0.5f, 0.5f, -0.5f, // top right
|
||||
-0.5f, 0.5f, -0.5f, // top left
|
||||
-0.5f, -0.5f, -0.5f, // bottom left
|
||||
// ===== left (x = -1) =====
|
||||
-0.5f, -0.5f, -0.5f, // bottom back
|
||||
-0.5f, -0.5f, 0.5f, // bottom front
|
||||
-0.5f, 0.5f, 0.5f, // top front
|
||||
-0.5f, 0.5f, 0.5f, // top front
|
||||
-0.5f, 0.5f, -0.5f, // top back
|
||||
-0.5f, -0.5f, -0.5f, // bottom back
|
||||
// ===== top (y = +1) =====
|
||||
-0.5f, 0.5f, -0.5f, // back left
|
||||
0.5f, 0.5f, -0.5f, // back right
|
||||
0.5f, 0.5f, 0.5f, // front right
|
||||
0.5f, 0.5f, 0.5f, // front right
|
||||
-0.5f, 0.5f, 0.5f, // front left
|
||||
-0.5f, 0.5f, -0.5f, // back left
|
||||
// ===== bottom (y = -1) =====
|
||||
-0.5f, -0.5f, 0.5f, // front left
|
||||
0.5f, -0.5f, 0.5f, // front right
|
||||
0.5f, -0.5f, -0.5f, // back right
|
||||
0.5f, -0.5f, -0.5f, // back right
|
||||
-0.5f, -0.5f, -0.5f, // back left
|
||||
-0.5f, -0.5f, 0.5f // front left
|
||||
};
|
||||
|
||||
float tex_coords[6][6][2] = {
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
|
||||
};
|
||||
|
||||
|
||||
// every block
|
||||
for (int x = 0; x < DISTANCE * CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < DISTANCE * CHUCK_SIZE; z++) {
|
||||
for (int y = 0; y < CHUCK_SIZE; y++) {
|
||||
const auto& block_render_data = world.get_block_render_data(x, y, z);
|
||||
// air
|
||||
if (block_render_data.block_id == 0) {
|
||||
continue;
|
||||
}
|
||||
for (int face = 0; face < 6; face++) {
|
||||
if (!block_render_data.draw_face[face]) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Vertex vex = {
|
||||
vertices_pos[face][i][0] + (float)x * 1.0f,
|
||||
vertices_pos[face][i][1] + (float)y * 1.0f,
|
||||
vertices_pos[face][i][2] + (float)z * 1.0f,
|
||||
tex_coords[face][i][0],
|
||||
tex_coords[face][i][1],
|
||||
static_cast<float>(block_render_data.block_id * 6 + face)
|
||||
|
||||
};
|
||||
vertex_data.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
glGenVertexArrays(NUM_VAO, vao);
|
||||
glBindVertexArray(vao[0]);
|
||||
glGenBuffers(NUM_VBO, vbo);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_data.size() * sizeof(Vertex), vertex_data.data(), GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
@@ -247,26 +155,16 @@ void display(GLFWwindow* window, double current_time) {
|
||||
glUseProgram(rendering_program);
|
||||
glBindVertexArray(vao[0]);
|
||||
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, s));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, layer));
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, texture_array);
|
||||
|
||||
|
||||
m_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
v_mat = camera.get_camera_lookat();
|
||||
mv_mat = v_mat * m_mat;
|
||||
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(mv_mat));
|
||||
glUniformMatrix4fv(proj_loc, 1 ,GL_FALSE, glm::value_ptr(p_mat));
|
||||
glDrawArrays(GL_TRIANGLES, 0, vertex_data.size() * 3);
|
||||
|
||||
world.render();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user