Bientôt un monde infini?
This commit is contained in:
@@ -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 <20> 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<EFBFBD>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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user