Bientôt un monde infini?
This commit is contained in:
parent
6446be1ceb
commit
fe624fdafc
@ -28,6 +28,7 @@
|
||||
<ClInclude Include="engine.h" />
|
||||
<ClInclude Include="matrix4.h" />
|
||||
<ClInclude Include="openglcontext.h" />
|
||||
<ClInclude Include="perlin.h" />
|
||||
<ClInclude Include="player.h" />
|
||||
<ClInclude Include="shader.h" />
|
||||
<ClInclude Include="skybox.h" />
|
||||
@ -46,6 +47,7 @@
|
||||
<ClCompile Include="engine.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="openglcontext.cpp" />
|
||||
<ClCompile Include="perlin.cpp" />
|
||||
<ClCompile Include="player.cpp" />
|
||||
<ClCompile Include="shader.cpp" />
|
||||
<ClCompile Include="skybox.cpp" />
|
||||
@ -59,7 +61,7 @@
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{A21FD938-1FEA-4687-AB86-0EABAC30877B}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>mcclone</RootNamespace>
|
||||
<RootNamespace>SQCSim2021</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<ProjectName>SQCSim2021</ProjectName>
|
||||
</PropertyGroup>
|
||||
|
@ -68,6 +68,9 @@
|
||||
<ClInclude Include="world.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="perlin.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="blockinfo.cpp">
|
||||
@ -115,5 +118,8 @@
|
||||
<ClCompile Include="world.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="perlin.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -16,7 +16,7 @@ void Chunk::RemoveBlock(int x, int y, int z, World* world) {
|
||||
void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
|
||||
m_blocks.Set(x, y, z, type);
|
||||
|
||||
CheckNeighbors(x, y, world);
|
||||
CheckNeighbors(x, z, world);
|
||||
|
||||
m_isDirty = true;
|
||||
}
|
||||
@ -24,25 +24,25 @@ void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
|
||||
BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); }
|
||||
|
||||
void Chunk::CheckNeighbors(int x, int z, World* world) {
|
||||
if (x == 0 && m_posX > 0 &&
|
||||
world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z) &&
|
||||
!world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->IsDirty())
|
||||
world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->MakeDirty();
|
||||
else if (x == CHUNK_SIZE_X - 1 && m_posX < VIEW_DISTANCE &&
|
||||
world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z) &&
|
||||
!world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->IsDirty())
|
||||
world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->MakeDirty();
|
||||
if (x == 0 && m_posX >= 0 &&
|
||||
world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z))
|
||||
world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->MakeDirty();
|
||||
else if (x == CHUNK_SIZE_X - 1 && m_posX < WORLD_SIZE_X &&
|
||||
world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X - 1, 1, m_posY * CHUNK_SIZE_Z))
|
||||
world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X - 1, 1, m_posY * CHUNK_SIZE_Z)->MakeDirty();
|
||||
|
||||
if (z == 0 && m_posY > 0 &&
|
||||
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY - 1) * CHUNK_SIZE_Z) &&
|
||||
!world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY - 1) * CHUNK_SIZE_Z)->IsDirty())
|
||||
if (z == 0 && m_posY >= 0 &&
|
||||
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY - 1) * CHUNK_SIZE_Z))
|
||||
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY - 1) * CHUNK_SIZE_Z)->MakeDirty();
|
||||
else if (z == CHUNK_SIZE_X - 1 && m_posY < VIEW_DISTANCE &&
|
||||
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z) &&
|
||||
!world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z)->IsDirty())
|
||||
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z)->MakeDirty();
|
||||
else if (z == CHUNK_SIZE_X - 1 && m_posY < WORLD_SIZE_Y &&
|
||||
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z - 1))
|
||||
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z - 1)->MakeDirty();
|
||||
}
|
||||
|
||||
int Chunk::GetPosX() const { return m_posX; }
|
||||
|
||||
int Chunk::GetPosY() const { return m_posY; }
|
||||
|
||||
void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST], World* world) {
|
||||
float u, v, s;
|
||||
// Update mesh
|
||||
@ -81,45 +81,45 @@ void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType b
|
||||
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);
|
||||
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .8f, .8f, .8f, u, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .8f, .8f, .8f, u + s, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, .8f, .8f, .8f, u + s, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z, .8f, .8f, .8f, u, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, .8f, .8f, .8f, u, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, .8f, .8f, .8f, u + s, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z, .8f, .8f, .8f, u + s, v);
|
||||
}
|
||||
|
||||
if (y == 0 || GetBlock(x, y - 1, z) == BTYPE_AIR) { // -y
|
||||
vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .8f, .8f, .8f, u, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x, y, z, .8f, .8f, .8f, u, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z, .8f, .8f, .8f, u + s, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .8f, .8f, .8f, u + s, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z + 1.f, .8f, .8f, .8f, u, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z, .8f, .8f, .8f, u, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z, .8f, .8f, .8f, u + s, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z + 1.f, .8f, .8f, .8f, u + s, v);
|
||||
}
|
||||
|
||||
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 (world->BlockAt(cx + 1, y, cy) == BTYPE_AIR) { // x
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z, .9f, .9f, .9f, u, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z, .9f, .9f, .9f, u, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, .9f, .9f, .9f, u + s, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_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 (world->BlockAt(cx - 1, y, cy) == BTYPE_AIR) { // -x
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, .9f, .9f, .9f, u, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z, .9f, .9f, .9f, u + s, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z, .9f, .9f, .9f, u + s, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_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 (world->BlockAt(cx, y, cy + 1) == BTYPE_AIR) { // z
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z + 1.f, 1.f, 1.f, 1.f, u, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z + 1.f, 1.f, 1.f, 1.f, u + s, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, 1.f, 1.f, 1.f, u + s, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, 1.f, 1.f, 1.f, u, v + s);
|
||||
}
|
||||
|
||||
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);
|
||||
vd[count++] = VertexBuffer::VertexData(x, y, z, 1.f, 1.f, 1.f, u, v);
|
||||
if (world->BlockAt(cx, y, cy - 1) == BTYPE_AIR) { // -z
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z, 1.f, 1.f, 1.f, u, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z, 1.f, 1.f, 1.f, u + s, v + s);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z, 1.f, 1.f, 1.f, u + s, v);
|
||||
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z, 1.f, 1.f, 1.f, u, v);
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,3 +129,5 @@ bool Chunk::IsDirty() const { return m_isDirty; }
|
||||
|
||||
void Chunk::MakeDirty() { m_isDirty = true; }
|
||||
|
||||
void Chunk::MakeModified() { m_isModified = true; }
|
||||
|
||||
|
@ -13,6 +13,7 @@ class Chunk {
|
||||
Array3d<BlockType> m_blocks = Array3d<BlockType>(CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z);
|
||||
VertexBuffer m_vertexBuffer;
|
||||
bool m_isDirty = true;
|
||||
bool m_isModified = false;
|
||||
|
||||
int m_posX; // Position du chunk dans l'array constituant le monde.
|
||||
int m_posY;
|
||||
@ -27,12 +28,15 @@ class Chunk {
|
||||
void SetBlock(int x, int y, int z, BlockType type, World* world);
|
||||
BlockType GetBlock(int x, int y, int z);
|
||||
void CheckNeighbors(int x, int z, World* world);
|
||||
int GetPosX() const;
|
||||
int GetPosY() const;
|
||||
|
||||
void Update(BlockInfo* blockinfo[BTYPE_LAST], World* world);
|
||||
|
||||
void Render() const;
|
||||
bool IsDirty() const;
|
||||
void MakeDirty();
|
||||
void MakeModified();
|
||||
};
|
||||
|
||||
#endif // CHUNK_H__
|
||||
|
@ -12,12 +12,17 @@
|
||||
#include <gl/GLU.h>
|
||||
#endif
|
||||
|
||||
#define VIEW_DISTANCE 128
|
||||
#define CHUNK_SIZE_X 16
|
||||
#define CHUNK_SIZE_Y 128
|
||||
#define CHUNK_SIZE_Z 16
|
||||
#define MAX_RENDER_CHUNKS 2
|
||||
#define MAX_UPDATE_CHUNKS 2
|
||||
|
||||
#define WORLD_SIZE_X 8
|
||||
#define WORLD_SIZE_Y 8
|
||||
|
||||
#define VIEW_DISTANCE 128
|
||||
#define FRAMES_RENDER_CHUNKS 2
|
||||
#define FRAMES_UPDATE_CHUNKS 2
|
||||
#define MAX_SELECTION_DISTANCE 5
|
||||
|
||||
typedef uint8_t BlockType;
|
||||
enum BLOCK_TYPE { BTYPE_AIR, BTYPE_DIRT, BTYPE_GRASS, BTYPE_METAL, BTYPE_ICE, BTYPE_LAST };
|
||||
|
@ -67,7 +67,7 @@ void Engine::LoadResource() {
|
||||
TextureAtlas::TextureIndex texGrassIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "grass.png");
|
||||
TextureAtlas::TextureIndex texMetalIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal.png");
|
||||
|
||||
if (!m_textureAtlas.Generate(512, false)) {
|
||||
if (!m_textureAtlas.Generate(128, false)) {
|
||||
std::cout << " Unable to generate texture atlas ..." << std::endl;
|
||||
abort();
|
||||
}
|
||||
@ -116,6 +116,9 @@ void Engine::DrawHud(float elapsedTime) {
|
||||
ss << " Fps : " << GetFps(elapsedTime);
|
||||
PrintText(10, Height() - 25, ss.str());
|
||||
ss.str("");
|
||||
ss << " Rendered Chunks : " << m_renderCount;
|
||||
PrintText(10, Height() - 35, ss.str());
|
||||
ss.str("");
|
||||
ss << " Velocity : " << m_player.GetVelocity(); // IMPORTANT : on utilise l ’ operateur << pour afficher la position
|
||||
PrintText(10, 10, ss.str());
|
||||
ss.str("");
|
||||
@ -198,110 +201,109 @@ void Engine::Render(float elapsedTime) {
|
||||
glDisable(GL_LIGHT0);
|
||||
m_skybox.Render(skybox);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Chunks
|
||||
all.Use();
|
||||
// Génération/Update de Chunks.
|
||||
glEnable(GL_LIGHT0);
|
||||
m_shader01.Use();
|
||||
m_textureAtlas.Bind();
|
||||
|
||||
int cx = m_player.GetPosition().x;
|
||||
int cy = m_player.GetPosition().z;
|
||||
int accRender = 0;
|
||||
static int frameGenerate = 0;
|
||||
static int frameUpdate = 0;
|
||||
int side = 0;
|
||||
m_shader01.Use();
|
||||
m_renderCount = 0;
|
||||
|
||||
for (int chx = cx - CHUNK_SIZE_X * 6; chx < cx + CHUNK_SIZE_X * 6; chx+= CHUNK_SIZE_X)
|
||||
for (int chy = cy - CHUNK_SIZE_Z * 6; chy < cy + CHUNK_SIZE_Z * 6; chy += CHUNK_SIZE_Z) {
|
||||
if (chx / CHUNK_SIZE_X < VIEW_DISTANCE && chy / CHUNK_SIZE_Z < VIEW_DISTANCE &&
|
||||
chx >= 0 && chy >= 0)
|
||||
if (!m_world.ChunkAt(chx, 1, chy)) {
|
||||
if (frameGenerate > 0) --frameGenerate;
|
||||
if (frameUpdate > 0) --frameUpdate;
|
||||
|
||||
m_world.GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, new Chunk(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z));
|
||||
if (!frameGenerate || !frameUpdate)
|
||||
while (side * CHUNK_SIZE_X <= VIEW_DISTANCE) {
|
||||
int tx = -side, ty = -side;
|
||||
|
||||
for (int x = 0; x < CHUNK_SIZE_X; ++x)
|
||||
for (int z = 0; z < CHUNK_SIZE_Z; ++z)
|
||||
for (int y = 0; y < 32; ++y)
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(x, y, z, (chx + chy) % (BTYPE_LAST - 1) + 1, &m_world);
|
||||
for (; tx <= side; ++tx)
|
||||
UpdateWorld(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z);
|
||||
for (; ty <= side; ++ty)
|
||||
UpdateWorld(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z);
|
||||
for (; tx >= -side; --tx)
|
||||
UpdateWorld(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z);
|
||||
for (; ty >= -side; --ty)
|
||||
UpdateWorld(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z);
|
||||
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(5, 32, 15, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(5, 33, 15, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(5, 34, 15, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(6, 34, 15, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(7, 34, 15, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(7, 33, 15, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(7, 32, 15, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(8, 32, 3, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(8, 33, 4, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(8, 34, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(8, 35, 6, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(11, 32, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(11, 33, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(11, 34, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(11, 35, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(12, 32, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(12, 33, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(12, 34, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(12, 35, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(13, 32, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(13, 33, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(13, 34, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(13, 35, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(14, 32, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(14, 33, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(14, 34, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(14, 35, 5, BTYPE_GRASS, &m_world);
|
||||
++side;
|
||||
}
|
||||
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 32, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 33, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 34, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 35, 5, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 32, 6, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 33, 6, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 34, 6, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 35, 6, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 32, 7, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 33, 7, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 34, 7, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 35, 7, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 32, 8, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 33, 8, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 34, 8, BTYPE_GRASS, &m_world);
|
||||
m_world.ChunkAt(chx, 1, chy)->SetBlock(3, 35, 8, BTYPE_GRASS, &m_world);
|
||||
// Rendering de Chunks.
|
||||
all.Use();
|
||||
if (m_renderer) { // Choix d'algorithme de rendu pour comparer.
|
||||
Vector3f direct = m_player.GetDirection();
|
||||
Vector3f renderpos = m_player.GetPosition();
|
||||
|
||||
if (++accRender > MAX_RENDER_CHUNKS) {
|
||||
chx = cx + CHUNK_SIZE_X * 6;
|
||||
chy = cy + CHUNK_SIZE_Z * 6;
|
||||
direct.y = 0;
|
||||
direct.Normalize();
|
||||
|
||||
Vector3f viewL = renderpos - direct * CHUNK_SIZE_X * 2,
|
||||
viewR = viewL;
|
||||
|
||||
viewL.Dot(direct);
|
||||
viewR.Dot(direct);
|
||||
|
||||
for (int x = -3; x <= VIEW_DISTANCE / CHUNK_SIZE_X; ++x) {
|
||||
int chunkxL = -1, chunkyL = -1, chunkxR = -1, chunkyR = -1;
|
||||
|
||||
if (m_world.ChunkAt(viewL)) {
|
||||
chunkxL = m_world.ChunkAt(viewL)->GetPosX();
|
||||
chunkyL = m_world.ChunkAt(viewL)->GetPosY();
|
||||
}
|
||||
if (m_world.ChunkAt(viewR)) {
|
||||
chunkxR = m_world.ChunkAt(viewR)->GetPosX();
|
||||
chunkyR = m_world.ChunkAt(viewR)->GetPosY();
|
||||
}
|
||||
|
||||
if (chunkxL == chunkxR) {
|
||||
++chunkxR;
|
||||
--chunkxL;
|
||||
}
|
||||
if (chunkyL == chunkyR) {
|
||||
++chunkyR;
|
||||
--chunkyL;
|
||||
}
|
||||
|
||||
if (chunkxL >= 0 && chunkyL >= 0 && chunkxR >= 0 && chunkyR >= 0)
|
||||
for (int rx = chunkxL; rx != chunkxR; chunkxL < chunkxR ? ++rx : --rx)
|
||||
for (int ry = chunkyL; ry != chunkyR; chunkyL < chunkyR ? ++ry : --ry)
|
||||
if (m_world.GetChunks().Get(rx, ry)) {
|
||||
m_world.GetChunks().Get(rx, ry)->Render();
|
||||
++m_renderCount;
|
||||
}
|
||||
|
||||
viewL.x += (direct.x + direct.z) * CHUNK_SIZE_X / 2;
|
||||
viewL.z += (direct.z - direct.x) * CHUNK_SIZE_X / 2;
|
||||
viewR.x += (direct.x - direct.z) * CHUNK_SIZE_X / 2;
|
||||
viewR.z += (direct.z + direct.x) * CHUNK_SIZE_X / 2;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (int chx = 0; chx < WORLD_SIZE_X; chx++)
|
||||
for (int chy = 0; chy < WORLD_SIZE_Y; chy++)
|
||||
if (m_world.GetChunks().Get(chx, chy)) {
|
||||
m_world.GetChunks().Get(chx, chy)->Render();
|
||||
++m_renderCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int accUpdate = 0;
|
||||
|
||||
for (int chx = 0; chx < VIEW_DISTANCE; chx++)
|
||||
for (int chy = 0; chy < VIEW_DISTANCE; chy++) {
|
||||
if (m_world.GetChunks().Get(chx, chy)) {
|
||||
all.ApplyTranslation(chx * CHUNK_SIZE_X, 0, chy * CHUNK_SIZE_Z);
|
||||
all.Use();
|
||||
if (m_world.GetChunks().Get(chx, chy)->IsDirty())
|
||||
if (++accUpdate < MAX_UPDATE_CHUNKS)
|
||||
m_world.GetChunks().Get(chx, chy)->Update(m_blockinfo, &m_world);
|
||||
/* View fustrum culling va ici */
|
||||
m_world.GetChunks().Get(chx, chy)->Render();
|
||||
all.ApplyTranslation(-chx * CHUNK_SIZE_X, 0, -chy * CHUNK_SIZE_Z);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
m_shader01.Disable();
|
||||
|
||||
if (m_mouseL)
|
||||
GetBlockAtCursor(BTYPE_DIRT);
|
||||
else if (m_mouseR) GetBlockAtCursor(BTYPE_AIR);
|
||||
|
||||
if (m_wireframe)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
DrawHud(elapsedTime);
|
||||
if (m_wireframe)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
if (m_player.GetPosition().y < -20.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.
|
||||
|
||||
if (m_player.GetPosition().y < -20.f)
|
||||
m_player = Player(Vector3f(CHUNK_SIZE_X * WORLD_SIZE_X / 2, CHUNK_SIZE_Y, CHUNK_SIZE_Z * WORLD_SIZE_X / 2)); // Respawn si le bonho- joueur tombe en bas du monde.
|
||||
}
|
||||
|
||||
void Engine::KeyPressEvent(unsigned char key)
|
||||
@ -352,6 +354,7 @@ void Engine::KeyPressEvent(unsigned char key)
|
||||
case 24: // Y - Ignorer
|
||||
case 255: // Fn - Ignorer
|
||||
case 12: // M - Ignorer
|
||||
case 17: // R - Ignorer
|
||||
break;
|
||||
default:
|
||||
std::cout << "Unhandled key: " << (int)key << std::endl;
|
||||
@ -364,6 +367,9 @@ void Engine::KeyReleaseEvent(unsigned char key)
|
||||
case 12:
|
||||
m_audio.ToggleMusicState();
|
||||
break;
|
||||
case 17:
|
||||
m_renderer = !m_renderer;
|
||||
break;
|
||||
case 24: // Y
|
||||
m_wireframe = !m_wireframe;
|
||||
if (m_wireframe)
|
||||
@ -413,12 +419,46 @@ void Engine::MouseMoveEvent(int x, int y)
|
||||
CenterMouse();
|
||||
}
|
||||
|
||||
void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y)
|
||||
{
|
||||
void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) {
|
||||
switch (button) {
|
||||
case MOUSE_BUTTON_LEFT:
|
||||
m_mouseL = true;
|
||||
break;
|
||||
case MOUSE_BUTTON_RIGHT:
|
||||
m_mouseR = true;
|
||||
break;
|
||||
case MOUSE_BUTTON_MIDDLE:
|
||||
m_mouseC = true;
|
||||
break;
|
||||
case MOUSE_BUTTON_WHEEL_UP:
|
||||
m_mouseWU = true;
|
||||
break;
|
||||
case MOUSE_BUTTON_WHEEL_DOWN:
|
||||
m_mouseWD = true;
|
||||
break;
|
||||
case MOUSE_BUTTON_NONE: break;
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::MouseReleaseEvent(const MOUSE_BUTTON& button, int x, int y)
|
||||
{
|
||||
void Engine::MouseReleaseEvent(const MOUSE_BUTTON& button, int x, int y) {
|
||||
switch (button) {
|
||||
case MOUSE_BUTTON_LEFT:
|
||||
m_mouseL = false;
|
||||
break;
|
||||
case MOUSE_BUTTON_RIGHT:
|
||||
m_mouseR = false;
|
||||
break;
|
||||
case MOUSE_BUTTON_MIDDLE:
|
||||
m_mouseC = false;
|
||||
break;
|
||||
case MOUSE_BUTTON_WHEEL_UP:
|
||||
m_mouseWU = false;
|
||||
break;
|
||||
case MOUSE_BUTTON_WHEEL_DOWN:
|
||||
m_mouseWD = false;
|
||||
break;
|
||||
case MOUSE_BUTTON_NONE: break;
|
||||
}
|
||||
}
|
||||
|
||||
bool Engine::LoadTexture(Texture& texture, const std::string& filename, bool stopOnError)
|
||||
@ -435,3 +475,178 @@ bool Engine::LoadTexture(Texture& texture, const std::string& filename, bool sto
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Engine::GenerateChunk(int chx, int chy) {
|
||||
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
|
||||
chx >= 0 && chy >= 0)
|
||||
if (!m_world.ChunkAt(chx, 1, chy)) {
|
||||
m_world.GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, new Chunk(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z));
|
||||
Chunk* chunk = m_world.GetChunks().Get(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z);
|
||||
|
||||
for (int x = 0; x < CHUNK_SIZE_X; ++x)
|
||||
for (int z = 0; z < CHUNK_SIZE_Z; ++z)
|
||||
for (int y = 0; y < 32; ++y)
|
||||
chunk->SetBlock(x, y, z, (chx + chy) % (BTYPE_LAST - 1) + 1, &m_world);
|
||||
|
||||
chunk->SetBlock(5, 32, 15, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(5, 33, 15, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(5, 34, 15, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(6, 34, 15, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(7, 34, 15, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(7, 33, 15, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(7, 32, 15, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(8, 32, 3, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(8, 33, 4, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(8, 34, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(8, 35, 6, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(11, 32, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(11, 33, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(11, 34, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(11, 35, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(12, 32, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(12, 33, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(12, 34, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(12, 35, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(13, 32, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(13, 33, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(13, 34, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(13, 35, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(14, 32, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(14, 33, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(14, 34, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(14, 35, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 32, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 33, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 34, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 35, 5, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 32, 6, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 33, 6, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 34, 6, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 35, 6, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 32, 7, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 33, 7, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 34, 7, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 35, 7, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 32, 8, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 33, 8, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 34, 8, BTYPE_GRASS, &m_world);
|
||||
chunk->SetBlock(3, 35, 8, BTYPE_GRASS, &m_world);
|
||||
|
||||
std::cout << "Chunk generated: " << chx / CHUNK_SIZE_X << ", " << chy / CHUNK_SIZE_Z << std::endl;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Engine::UpdateWorld(int& generates, int& updates, int chx, int chy) {
|
||||
if (generates == 0 && GenerateChunk(chx, chy)) generates = FRAMES_RENDER_CHUNKS;
|
||||
if (updates == 0 && m_world.ChunkAt(chx, 1, chy) &&
|
||||
m_world.ChunkAt(chx, 1, chy)->IsDirty()) {
|
||||
m_world.ChunkAt(chx, 1, chy)->Update(m_blockinfo, &m_world);
|
||||
updates = FRAMES_UPDATE_CHUNKS;
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::GetBlockAtCursor(BlockType blockType)
|
||||
{
|
||||
int x = Width() / 2;
|
||||
int y = Height() / 2;
|
||||
|
||||
Vector3f currentBlock;
|
||||
Vector3f currentFaceNormal;
|
||||
|
||||
GLint viewport[4];
|
||||
GLdouble modelview[16];
|
||||
GLdouble projection[16];
|
||||
GLfloat winX, winY, winZ;
|
||||
GLdouble posX, posY, posZ;
|
||||
|
||||
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
|
||||
glGetDoublev(GL_PROJECTION_MATRIX, projection);
|
||||
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||
|
||||
winX = (float)x;
|
||||
winY = (float)viewport[3] - (float)y;
|
||||
glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
|
||||
|
||||
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
|
||||
|
||||
// Le cast vers int marche juste pour les valeurs entiere, utiliser une fonction de la libc si besoin
|
||||
// de valeurs negatives
|
||||
int px = (int)(posX);
|
||||
int py = (int)(posY);
|
||||
int pz = (int)(posZ);
|
||||
|
||||
bool found = false;
|
||||
|
||||
if ((m_player.GetPosition() - Vector3f((float)posX, (float)posY, (float)posZ)).Length() < MAX_SELECTION_DISTANCE)
|
||||
{
|
||||
// Apres avoir determine la position du bloc en utilisant la partie entiere du hit
|
||||
// point retourne par opengl, on doit verifier de chaque cote du bloc trouve pour trouver
|
||||
// le vrai bloc. Le vrai bloc peut etre different a cause d'erreurs de precision de nos
|
||||
// nombres flottants (si z = 14.999 par exemple, et qu'il n'y a pas de blocs a la position
|
||||
// 14 (apres arrondi vers l'entier) on doit trouver et retourner le bloc en position 15 s'il existe
|
||||
// A cause des erreurs de precisions, ils arrive que le cote d'un bloc qui doit pourtant etre a la
|
||||
// position 15 par exemple nous retourne plutot la position 15.0001
|
||||
for (int x = px - 1; !found && x <= px + 1; ++x) {
|
||||
for (int y = py - 1; !found && x >= 0 && y <= py + 1; ++y) {
|
||||
for (int z = pz - 1; !found && y >= 0 && z <= pz + 1; ++z) {
|
||||
if (z >= 0) {
|
||||
BlockType bt = m_world.BlockAt((float)x, (float)y, (float)z);
|
||||
if (bt == BTYPE_AIR)
|
||||
continue;
|
||||
|
||||
// Skip water blocs
|
||||
//if(bloc->Type == BT_WATER)
|
||||
// continue;
|
||||
|
||||
currentBlock.x = x;
|
||||
currentBlock.y = y;
|
||||
currentBlock.z = z;
|
||||
|
||||
if (InRangeWithEpsilon<float>((float)posX, (float)x, (float)x + 1.f, 0.05f) && InRangeWithEpsilon<float>((float)posY, (float)y, (float)y + 1.f, 0.05f) && InRangeWithEpsilon<float>((float)posZ, (float)z, (float)z + 1.f, 0.05f))
|
||||
{
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
currentBlock.x = -1;
|
||||
}
|
||||
else {
|
||||
// Find on which face of the bloc we got an hit
|
||||
currentFaceNormal.Zero();
|
||||
|
||||
const float epsilon = 0.01f;
|
||||
|
||||
// Front et back:
|
||||
if (blockType != BTYPE_AIR)
|
||||
if (EqualWithEpsilon<float>((float)posZ, (float)currentBlock.z, epsilon))
|
||||
currentFaceNormal.z = -1;
|
||||
else if (EqualWithEpsilon<float>((float)posZ, (float)currentBlock.z + 1.f, epsilon))
|
||||
currentFaceNormal.z = 1;
|
||||
else if (EqualWithEpsilon<float>((float)posX, (float)currentBlock.x, epsilon))
|
||||
currentFaceNormal.x = -1;
|
||||
else if (EqualWithEpsilon<float>((float)posX, (float)currentBlock.x + 1.f, epsilon))
|
||||
currentFaceNormal.x = 1;
|
||||
else if (EqualWithEpsilon<float>((float)posY, (float)currentBlock.y, epsilon))
|
||||
currentFaceNormal.y = -1;
|
||||
else if (EqualWithEpsilon<float>((float)posY, (float)currentBlock.y + 1.f, epsilon))
|
||||
currentFaceNormal.y = 1;
|
||||
|
||||
currentBlock += currentFaceNormal;
|
||||
|
||||
int bx = (int)currentBlock.x % CHUNK_SIZE_X;
|
||||
int by = (int)currentBlock.y % CHUNK_SIZE_Y;
|
||||
int bz = (int)currentBlock.z % CHUNK_SIZE_Z;
|
||||
|
||||
m_world.ChunkAt(currentBlock)->SetBlock(bx, by, bz, blockType, &m_world);
|
||||
m_world.ChunkAt(currentBlock)->MakeModified();
|
||||
}
|
||||
}
|
||||
|
@ -34,13 +34,21 @@ private:
|
||||
void DrawHud(float elapsedTime);
|
||||
void PrintText(unsigned int x, unsigned int y, const std::string& t);
|
||||
int GetFps(float elapsedTime) const;
|
||||
bool GenerateChunk(int chx, int chy);
|
||||
void UpdateWorld(int& generates, int& updates, int chx, int chy);
|
||||
|
||||
void GetBlockAtCursor(BlockType blocktype);
|
||||
|
||||
bool m_wireframe = false;
|
||||
bool m_renderer = false;
|
||||
|
||||
int m_renderCount = 0;
|
||||
|
||||
BlockInfo* m_blockinfo[BTYPE_LAST];
|
||||
TextureAtlas m_textureAtlas = TextureAtlas(BTYPE_LAST);
|
||||
World m_world = World();
|
||||
|
||||
|
||||
Texture m_textureFloor;
|
||||
Texture m_textureSkybox;
|
||||
Texture m_textureFont;
|
||||
@ -51,7 +59,7 @@ private:
|
||||
Shader m_shader01;
|
||||
Audio m_audio = Audio(AUDIO_PATH "music01.wav");
|
||||
|
||||
Player m_player = Player(Vector3f(VIEW_DISTANCE * CHUNK_SIZE_X / 2, CHUNK_SIZE_Y / 2, VIEW_DISTANCE * CHUNK_SIZE_Z / 2));
|
||||
Player m_player = Player(Vector3f(CHUNK_SIZE_X * WORLD_SIZE_X / 2, CHUNK_SIZE_Y, CHUNK_SIZE_Z * WORLD_SIZE_X / 2));
|
||||
|
||||
bool m_keyW = false;
|
||||
bool m_keyA = false;
|
||||
@ -59,7 +67,23 @@ private:
|
||||
bool m_keyD = false;
|
||||
bool m_keylshift = false;
|
||||
bool m_keySpace = false;
|
||||
|
||||
bool m_mouseL = false;
|
||||
bool m_mouseR = false;
|
||||
bool m_mouseC = false;
|
||||
bool m_mouseWU = false;
|
||||
bool m_mouseWD = false;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
static bool EqualWithEpsilon(const T& v1, const T& v2, T epsilon = T(0.0001))
|
||||
{
|
||||
return (fabs(v2 - v1) < epsilon);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static bool InRangeWithEpsilon(const T& v, const T& vinf, const T& vsup, T epsilon = T(0.0001))
|
||||
{
|
||||
return (v >= vinf - epsilon && v <= vsup + epsilon);
|
||||
}
|
||||
|
||||
#endif // ENGINE_H__
|
||||
|
90
SQCSim2021/exemple_lecture_ecriture_fichier.cpp
Normal file
90
SQCSim2021/exemple_lecture_ecriture_fichier.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
|
||||
void FichierTexte()
|
||||
{
|
||||
std::ofstream sortie("fichier.txt"); // std::fstream::app
|
||||
if(!sortie.is_open())
|
||||
{
|
||||
std::cerr << "Erreur d'ouverture de fichier" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
sortie << "Premiere ligne de plusieurs mots" << std::endl;
|
||||
sortie << "Age: " << 6 << std::endl;
|
||||
sortie.close();
|
||||
|
||||
|
||||
std::ifstream entree("fichier.txt");
|
||||
if(!entree.is_open())
|
||||
{
|
||||
std::cerr << "Erreur d'ouverture de fichier" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string mot;
|
||||
std::string line;
|
||||
int age;
|
||||
entree >> mot;
|
||||
std::cout << mot << std::endl;
|
||||
|
||||
std::getline(entree, line);
|
||||
std::cout << line << std::endl;
|
||||
|
||||
entree >> mot;
|
||||
entree >> age;
|
||||
std::cout << age << std::endl;
|
||||
|
||||
|
||||
// TODO montrer eof..
|
||||
}
|
||||
|
||||
|
||||
void FichierBinaire()
|
||||
{
|
||||
srand(time(0));
|
||||
char data[1024];
|
||||
for(int i = 0; i < sizeof(data); ++i)
|
||||
data[i] = rand() % 256;
|
||||
|
||||
std::ofstream sortie("fichier.bin", std::fstream::binary);
|
||||
sortie.write(data, sizeof(data));
|
||||
sortie.close();
|
||||
|
||||
|
||||
// Relire le fichier et comparer...
|
||||
std::ifstream entree("fichier.bin", std::fstream::binary);
|
||||
|
||||
// Obtenir la taille du fichier
|
||||
entree.seekg(0, std::ios_base::end);
|
||||
int size = entree.tellg();
|
||||
entree.seekg(0, std::ios_base::beg);
|
||||
|
||||
char* data2 = new char[size];
|
||||
entree.read(data2, size);
|
||||
entree.close();
|
||||
|
||||
// Comparaison
|
||||
bool pareil = true;
|
||||
for(int i = 0; i < size; ++i)
|
||||
{
|
||||
if(data[i] != data2[i])
|
||||
{
|
||||
pareil = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Les donnees sont " << (pareil ? "pareilles" : "differentes") << std::endl;
|
||||
|
||||
|
||||
delete [] data2;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
FichierTexte();
|
||||
FichierBinaire();
|
||||
|
||||
}
|
@ -153,7 +153,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, 8));
|
||||
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));
|
||||
}
|
||||
|
||||
OpenglContext::MOUSE_BUTTON OpenglContext::ConvertMouseButton(sf::Mouse::Button button) const
|
||||
|
262
SQCSim2021/perlin.cpp
Normal file
262
SQCSim2021/perlin.cpp
Normal file
@ -0,0 +1,262 @@
|
||||
/* coherent noise function over 1, 2 or 3 dimensions */
|
||||
/* (copyright Ken Perlin) */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "perlin.h"
|
||||
|
||||
#define B SAMPLE_SIZE
|
||||
#define BM (SAMPLE_SIZE-1)
|
||||
|
||||
#define N 0x1000
|
||||
#define NP 12 /* 2^N */
|
||||
#define NM 0xfff
|
||||
|
||||
#define s_curve(t) ( t * t * (3.0f - 2.0f * t) )
|
||||
#define lerp(t, a, b) ( a + t * (b - a) )
|
||||
|
||||
#define setup(i,b0,b1,r0,r1)\
|
||||
t = vec[i] + N;\
|
||||
b0 = ((int)t) & BM;\
|
||||
b1 = (b0+1) & BM;\
|
||||
r0 = t - (int)t;\
|
||||
r1 = r0 - 1.0f;
|
||||
|
||||
float Perlin::noise1(float arg)
|
||||
{
|
||||
int bx0, bx1;
|
||||
float rx0, rx1, sx, t, u, v, vec[1];
|
||||
|
||||
vec[0] = arg;
|
||||
|
||||
if (mStart)
|
||||
{
|
||||
srand(mSeed);
|
||||
mStart = false;
|
||||
init();
|
||||
}
|
||||
|
||||
setup(0, bx0,bx1, rx0,rx1);
|
||||
|
||||
sx = s_curve(rx0);
|
||||
|
||||
u = rx0 * g1[ p[ bx0 ] ];
|
||||
v = rx1 * g1[ p[ bx1 ] ];
|
||||
|
||||
return lerp(sx, u, v);
|
||||
}
|
||||
|
||||
float Perlin::noise2(float vec[2])
|
||||
{
|
||||
int bx0, bx1, by0, by1, b00, b10, b01, b11;
|
||||
float rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
|
||||
int i, j;
|
||||
|
||||
if (mStart)
|
||||
{
|
||||
srand(mSeed);
|
||||
mStart = false;
|
||||
init();
|
||||
}
|
||||
|
||||
setup(0,bx0,bx1,rx0,rx1);
|
||||
setup(1,by0,by1,ry0,ry1);
|
||||
|
||||
i = p[bx0];
|
||||
j = p[bx1];
|
||||
|
||||
b00 = p[i + by0];
|
||||
b10 = p[j + by0];
|
||||
b01 = p[i + by1];
|
||||
b11 = p[j + by1];
|
||||
|
||||
sx = s_curve(rx0);
|
||||
sy = s_curve(ry0);
|
||||
|
||||
#define at2(rx,ry) ( rx * q[0] + ry * q[1] )
|
||||
|
||||
q = g2[b00];
|
||||
u = at2(rx0,ry0);
|
||||
q = g2[b10];
|
||||
v = at2(rx1,ry0);
|
||||
a = lerp(sx, u, v);
|
||||
|
||||
q = g2[b01];
|
||||
u = at2(rx0,ry1);
|
||||
q = g2[b11];
|
||||
v = at2(rx1,ry1);
|
||||
b = lerp(sx, u, v);
|
||||
|
||||
return lerp(sy, a, b);
|
||||
}
|
||||
|
||||
float Perlin::noise3(float vec[3])
|
||||
{
|
||||
int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
|
||||
float rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
|
||||
int i, j;
|
||||
|
||||
if (mStart)
|
||||
{
|
||||
srand(mSeed);
|
||||
mStart = false;
|
||||
init();
|
||||
}
|
||||
|
||||
setup(0, bx0,bx1, rx0,rx1);
|
||||
setup(1, by0,by1, ry0,ry1);
|
||||
setup(2, bz0,bz1, rz0,rz1);
|
||||
|
||||
i = p[ bx0 ];
|
||||
j = p[ bx1 ];
|
||||
|
||||
b00 = p[ i + by0 ];
|
||||
b10 = p[ j + by0 ];
|
||||
b01 = p[ i + by1 ];
|
||||
b11 = p[ j + by1 ];
|
||||
|
||||
t = s_curve(rx0);
|
||||
sy = s_curve(ry0);
|
||||
sz = s_curve(rz0);
|
||||
|
||||
#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
|
||||
|
||||
q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
|
||||
q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
|
||||
a = lerp(t, u, v);
|
||||
|
||||
q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
|
||||
q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
|
||||
b = lerp(t, u, v);
|
||||
|
||||
c = lerp(sy, a, b);
|
||||
|
||||
q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
|
||||
q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
|
||||
a = lerp(t, u, v);
|
||||
|
||||
q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
|
||||
q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
|
||||
b = lerp(t, u, v);
|
||||
|
||||
d = lerp(sy, a, b);
|
||||
|
||||
return lerp(sz, c, d);
|
||||
}
|
||||
|
||||
void Perlin::normalize2(float v[2])
|
||||
{
|
||||
float s;
|
||||
|
||||
s = (float)sqrt(v[0] * v[0] + v[1] * v[1]);
|
||||
s = 1.0f/s;
|
||||
v[0] = v[0] * s;
|
||||
v[1] = v[1] * s;
|
||||
}
|
||||
|
||||
void Perlin::normalize3(float v[3])
|
||||
{
|
||||
float s;
|
||||
|
||||
s = (float)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
||||
s = 1.0f/s;
|
||||
|
||||
v[0] = v[0] * s;
|
||||
v[1] = v[1] * s;
|
||||
v[2] = v[2] * s;
|
||||
}
|
||||
|
||||
void Perlin::init(void)
|
||||
{
|
||||
int i, j, k;
|
||||
|
||||
for (i = 0 ; i < B ; i++)
|
||||
{
|
||||
p[i] = i;
|
||||
g1[i] = (float)((rand() % (B + B)) - B) / B;
|
||||
for (j = 0 ; j < 2 ; j++)
|
||||
g2[i][j] = (float)((rand() % (B + B)) - B) / B;
|
||||
normalize2(g2[i]);
|
||||
for (j = 0 ; j < 3 ; j++)
|
||||
g3[i][j] = (float)((rand() % (B + B)) - B) / B;
|
||||
normalize3(g3[i]);
|
||||
}
|
||||
|
||||
while (--i)
|
||||
{
|
||||
k = p[i];
|
||||
p[i] = p[j = rand() % B];
|
||||
p[j] = k;
|
||||
}
|
||||
|
||||
for (i = 0 ; i < B + 2 ; i++)
|
||||
{
|
||||
p[B + i] = p[i];
|
||||
g1[B + i] = g1[i];
|
||||
for (j = 0 ; j < 2 ; j++)
|
||||
g2[B + i][j] = g2[i][j];
|
||||
for (j = 0 ; j < 3 ; j++)
|
||||
g3[B + i][j] = g3[i][j];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
float Perlin::perlin_noise_2D(float vec[2])
|
||||
{
|
||||
int terms = mOctaves;
|
||||
//float freq = mFrequency;
|
||||
float result = 0.0f;
|
||||
float amp = mAmplitude;
|
||||
|
||||
vec[0]*=mFrequency;
|
||||
vec[1]*=mFrequency;
|
||||
|
||||
for( int i=0; i<terms; i++ )
|
||||
{
|
||||
result += noise2(vec)*amp;
|
||||
vec[0] *= 2.0f;
|
||||
vec[1] *= 2.0f;
|
||||
amp*=0.5f;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
float Perlin::perlin_noise_3D(float vec[3])
|
||||
{
|
||||
int terms = mOctaves;
|
||||
//float freq = mFrequency;
|
||||
float result = 0.0f;
|
||||
float amp = mAmplitude;
|
||||
|
||||
vec[0]*=mFrequency;
|
||||
vec[1]*=mFrequency;
|
||||
vec[2]*=mFrequency;
|
||||
|
||||
for( int i=0; i<terms; i++ )
|
||||
{
|
||||
result += noise3(vec)*amp;
|
||||
vec[0] *= 2.0f;
|
||||
vec[1] *= 2.0f;
|
||||
vec[2] *= 2.0f;
|
||||
amp*=0.5f;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Perlin::Perlin(int octaves,float freq,float amp,int seed)
|
||||
{
|
||||
mOctaves = octaves;
|
||||
mFrequency = freq;
|
||||
mAmplitude = amp;
|
||||
mSeed = seed;
|
||||
mStart = true;
|
||||
}
|
60
SQCSim2021/perlin.h
Normal file
60
SQCSim2021/perlin.h
Normal file
@ -0,0 +1,60 @@
|
||||
//http://www.flipcode.com/archives/Perlin_Noise_Class.shtml
|
||||
#ifndef PERLIN_H_
|
||||
|
||||
#define PERLIN_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define SAMPLE_SIZE 1024
|
||||
|
||||
class Perlin
|
||||
{
|
||||
public:
|
||||
|
||||
Perlin(int octaves,float freq,float amp,int seed);
|
||||
|
||||
|
||||
float Get(float x,float y)
|
||||
{
|
||||
float vec[2];
|
||||
vec[0] = x;
|
||||
vec[1] = y;
|
||||
return perlin_noise_2D(vec);
|
||||
};
|
||||
|
||||
float Get(float x,float y, float z)
|
||||
{
|
||||
float vec[3];
|
||||
vec[0] = x;
|
||||
vec[1] = y;
|
||||
vec[2] = z;
|
||||
return perlin_noise_3D(vec);
|
||||
};
|
||||
|
||||
private:
|
||||
void init_perlin(int n,float p);
|
||||
float perlin_noise_2D(float vec[2]);
|
||||
float perlin_noise_3D(float vec[3]);
|
||||
|
||||
float noise1(float arg);
|
||||
float noise2(float vec[2]);
|
||||
float noise3(float vec[3]);
|
||||
void normalize2(float v[2]);
|
||||
void normalize3(float v[3]);
|
||||
void init(void);
|
||||
|
||||
int mOctaves;
|
||||
float mFrequency;
|
||||
float mAmplitude;
|
||||
int mSeed;
|
||||
|
||||
int p[SAMPLE_SIZE + SAMPLE_SIZE + 2];
|
||||
float g3[SAMPLE_SIZE + SAMPLE_SIZE + 2][3];
|
||||
float g2[SAMPLE_SIZE + SAMPLE_SIZE + 2][2];
|
||||
float g1[SAMPLE_SIZE + SAMPLE_SIZE + 2];
|
||||
bool mStart;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
@ -25,9 +25,9 @@ Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jum
|
||||
float yrotrad = (m_rotY / 57.2957795056f); // 180/Pi = 57.295...
|
||||
float xrotrad = (m_rotX / 57.2957795056f);
|
||||
|
||||
m_direction = Vector3f(cos(yrotrad) * cos(xrotrad),
|
||||
m_direction = Vector3f(sin(yrotrad),
|
||||
-sin(xrotrad),
|
||||
sin(yrotrad) * cos(xrotrad));
|
||||
-cos(yrotrad));
|
||||
|
||||
if (front) {
|
||||
delta.x += float(sin(yrotrad)) * elapsedTime * 10.f;
|
||||
|
@ -11,7 +11,7 @@ Chunk* World::ChunkAt(float x, float y, float z) const {
|
||||
int cz = (int)z / CHUNK_SIZE_Z;
|
||||
|
||||
if (x < 0 || y < 0 || z < 0 ||
|
||||
cx >= VIEW_DISTANCE || cz >= VIEW_DISTANCE || y >= CHUNK_SIZE_Y)
|
||||
x >= WORLD_SIZE_X * CHUNK_SIZE_X || z >= CHUNK_SIZE_Z * WORLD_SIZE_Y || y > CHUNK_SIZE_Y)
|
||||
return 0;
|
||||
|
||||
return m_chunks.Get(cx, cz);
|
||||
|
@ -17,13 +17,12 @@ class World {
|
||||
|
||||
Chunk* ChunkAt(float x, float y, float z) const;
|
||||
Chunk* ChunkAt(const Vector3f& pos) const;
|
||||
|
||||
|
||||
BlockType BlockAt(float x, float y, float z, BlockType defaultBlockType = BTYPE_AIR) const;
|
||||
BlockType BlockAt(const Vector3f& pos, BlockType defaultBlockType = BTYPE_AIR) const;
|
||||
|
||||
private:
|
||||
Array2d<Chunk*> m_chunks = Array2d<Chunk*>(VIEW_DISTANCE, VIEW_DISTANCE);
|
||||
Array2d<Chunk*> m_chunks = Array2d<Chunk*>(WORLD_SIZE_X, WORLD_SIZE_Y);
|
||||
|
||||
int m_center[2] = {0, 0};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user