Les vertices entre les Chunks ne sont plus dessinées!

This commit is contained in:
MarcEricMartel 2021-11-18 19:27:16 -05:00
parent 9745e8f60b
commit 592cb5c4f8
8 changed files with 38 additions and 29 deletions

View File

@ -68,13 +68,13 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>ClangCL</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>ClangCL</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>

View File

@ -16,7 +16,7 @@ void Chunk::SetBlock(int x, int y, int z, BlockType type) {
BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); }
void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST]) {
void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST], World* world) {
float u, v, s;
// Update mesh
if (m_isDirty) {
@ -28,10 +28,12 @@ void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST]) {
for (int y = 0; y < CHUNK_SIZE_Y; ++y) {
if (count > USHRT_MAX)
break;
BlockType bt = GetBlock(x, y, z);
if (bt != BTYPE_AIR) {
blockinfo[bt]->GetTexture(u, v, s);
AddBlockToMesh(vd, count, bt, x, y, z, u, v, s);
AddBlockToMesh(vd, count, bt, x, y, z, u, v, s, world);
}
}
}
@ -47,21 +49,9 @@ void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST]) {
}
void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt,
int x, int y, int z, float u, float v, float s) {
if (x == CHUNK_SIZE_X - 1 || GetBlock(x + 1, y, z) == BTYPE_AIR) { // x
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z, .9f, .9f, .9f, u, v);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, .9f, .9f, .9f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .9f, .9f, .9f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .9f, .9f, .9f, u + s, v);
}
int x, int y, int z, float u, float v, float s, World* world) {
if (x == 0 || GetBlock(x - 1, y, z) == BTYPE_AIR) { // -x
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .9f, .9f, .9f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, .9f, .9f, .9f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x, y, z, .9f, .9f, .9f, u + s, v);
vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .9f, .9f, .9f, u, v);
}
int cx = x + m_posX * CHUNK_SIZE_X, cy = z + m_posY * CHUNK_SIZE_Z;
if (y == CHUNK_SIZE_Y - 1 || GetBlock(x, y + 1, z) == BTYPE_AIR) { // y
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, .8f, .8f, .8f, u, v);
@ -77,14 +67,28 @@ void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType b
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .8f, .8f, .8f, u + s, v);
}
if (z == CHUNK_SIZE_Z - 1 || GetBlock(x, y, z + 1) == BTYPE_AIR) { // z
if (cx == INT16_MAX - 1 || world->BlockAt(cx + 1, y, cy) == BTYPE_AIR) { // x
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z, .9f, .9f, .9f, u, v);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, .9f, .9f, .9f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .9f, .9f, .9f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .9f, .9f, .9f, u + s, v);
}
if (cx == 0 || world->BlockAt(cx - 1, y, cy) == BTYPE_AIR) { // -x
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .9f, .9f, .9f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, .9f, .9f, .9f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x, y, z, .9f, .9f, .9f, u + s, v);
vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .9f, .9f, .9f, u, v);
}
if (cy == INT16_MAX - 1 || world->BlockAt(cx, y, cy + 1) == BTYPE_AIR) { // z
vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, 1.f, 1.f, 1.f, u, v);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, 1.f, 1.f, 1.f, u + s, v);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, 1.f, 1.f, 1.f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, 1.f, 1.f, 1.f, u, v + s);
}
if (z == 0 || GetBlock(x, y, z - 1) == BTYPE_AIR) { // -z
if (cy == 0 || world->BlockAt(cx, y, cy - 1) == BTYPE_AIR) { // -z
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, 1.f, 1.f, 1.f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, 1.f, 1.f, 1.f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z, 1.f, 1.f, 1.f, u + s, v);

View File

@ -5,6 +5,9 @@
#include "array2d.h"
#include "vertexbuffer.h"
#include "blockinfo.h"
#include "world.h"
class World;
class Chunk {
private:
@ -15,7 +18,7 @@ class Chunk {
int m_posX; // Position du chunk dans l'array constituant le monde.
int m_posY;
void AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt, int x, int y, int z, float u, float v, float s);
void AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt, int x, int y, int z, float u, float v, float s, World* world);
public:
Chunk(int x, int y);
@ -25,7 +28,7 @@ class Chunk {
void SetBlock(int x, int y, int z, BlockType type);
BlockType GetBlock(int x, int y, int z);
void Update(BlockInfo* blockinfo[BTYPE_LAST]);
void Update(BlockInfo* blockinfo[BTYPE_LAST], World* world);
void Render() const;
bool IsDirty() const;

View File

@ -12,7 +12,7 @@
#include <gl/GLU.h>
#endif
#define VIEW_DISTANCE 64
#define VIEW_DISTANCE 128
#define CHUNK_SIZE_X 16
#define CHUNK_SIZE_Y 128
#define CHUNK_SIZE_Z 16

View File

@ -30,7 +30,7 @@ void Engine::Init()
glEnable(GL_CULL_FACE);
// Light
GLfloat light0Pos[4] = { 0.0f, CHUNK_SIZE_Y, 0.0f, 1.0f };
/*GLfloat light0Pos[4] = { 0.0f, CHUNK_SIZE_Y, 0.0f, 1.0f };
GLfloat light0Amb[4] = { 0.2f, 0.2f, 0.2f, 1.f };
GLfloat light0Diff[4] = { 1.f, 1.f, 1.f, 1.f };
GLfloat light0Spec[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
@ -39,7 +39,7 @@ void Engine::Init()
glLightfv(GL_LIGHT0, GL_POSITION, light0Pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0Amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0Diff);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0Spec);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0Spec);*/
// Objet de skybox avec sa propre texture et son propre shader!
m_skybox.Init(0.00013f);
@ -248,7 +248,7 @@ void Engine::Render(float elapsedTime) {
all.ApplyTranslation(chx * CHUNK_SIZE_X, 0, chy * CHUNK_SIZE_Z);
all.Use();
if (m_world.GetChunks().Get(chx, chy)->IsDirty())
m_world.GetChunks().Get(chx, chy)->Update(m_blockinfo);
m_world.GetChunks().Get(chx, chy)->Update(m_blockinfo, &m_world);
m_world.GetChunks().Get(chx, chy)->Render();
all.ApplyTranslation(-chx * CHUNK_SIZE_X, 0, -chy * CHUNK_SIZE_Z);
}
@ -262,7 +262,7 @@ void Engine::Render(float elapsedTime) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (m_player.GetPosition().y < -10.f) m_player = Player(Vector3f(64, 34, 64)); // Respawn si le bonho- joueur tombe en bas du monde.
if (m_player.GetPosition().y < -10.f) m_player = Player(Vector3f(VIEW_DISTANCE * CHUNK_SIZE_X / 2, CHUNK_SIZE_Y / 2, VIEW_DISTANCE * CHUNK_SIZE_Z / 2)); // Respawn si le bonho- joueur tombe en bas du monde.
}
void Engine::KeyPressEvent(unsigned char key)

View File

@ -52,7 +52,7 @@ private:
Shader m_shader01;
Audio m_audio = Audio(AUDIO_PATH "music01.wav");
Player m_player = Player(Vector3f(64, 34, 64));
Player m_player = Player(Vector3f(VIEW_DISTANCE * CHUNK_SIZE_X / 2, CHUNK_SIZE_Y / 2, VIEW_DISTANCE * CHUNK_SIZE_Z / 2));
bool m_keyW = false;
bool m_keyA = false;

View File

@ -152,7 +152,7 @@ void OpenglContext::ShowCrossCursor() const
void OpenglContext::InitWindow(int width, int height)
{
m_app.create((m_fullscreen ? sf::VideoMode::getFullscreenModes()[0] : sf::VideoMode(width, height, 32)), m_title.c_str(), m_fullscreen ? sf::Style::Fullscreen : (sf::Style::Resize | sf::Style::Close), sf::ContextSettings(32, 8, 0));
m_app.create((m_fullscreen ? sf::VideoMode::getFullscreenModes()[0] : sf::VideoMode(width, height, 32)), m_title.c_str(), m_fullscreen ? sf::Style::Fullscreen : (sf::Style::Resize | sf::Style::Close), sf::ContextSettings(32, 8, 8));
}
OpenglContext::MOUSE_BUTTON OpenglContext::ConvertMouseButton(sf::Mouse::Button button) const

View File

@ -6,6 +6,8 @@
#include "vector3.h"
#include "transformation.h"
class Chunk;
class World {
public:
World();