HALLELUJAH!
This commit is contained in:
@@ -269,11 +269,15 @@ void Engine::Render(float elapsedTime) {
|
||||
|
||||
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)
|
||||
for (int ry = chunkyL; ry != chunkyR; chunkyL < chunkyR ? ++ry : --ry) {
|
||||
all.ApplyTranslation(rx * CHUNK_SIZE_X, 0, ry * CHUNK_SIZE_Z);
|
||||
all.Use();
|
||||
if (m_world.GetChunks().Get(rx, ry)) {
|
||||
m_world.GetChunks().Get(rx, ry)->Render();
|
||||
++m_renderCount;
|
||||
}
|
||||
all.ApplyTranslation(-rx * CHUNK_SIZE_X, 0, -ry * CHUNK_SIZE_Z);
|
||||
}
|
||||
|
||||
viewL.x += (direct.x + direct.z) * CHUNK_SIZE_X / 2;
|
||||
viewL.z += (direct.z - direct.x) * CHUNK_SIZE_X / 2;
|
||||
@@ -283,11 +287,15 @@ void Engine::Render(float elapsedTime) {
|
||||
}
|
||||
else {
|
||||
for (int chx = 0; chx < WORLD_SIZE_X; chx++)
|
||||
for (int chy = 0; chy < WORLD_SIZE_Y; chy++)
|
||||
for (int chy = 0; chy < WORLD_SIZE_Y; chy++) {
|
||||
all.ApplyTranslation(chx * CHUNK_SIZE_X, 0, chy * CHUNK_SIZE_Z);
|
||||
all.Use();
|
||||
if (m_world.GetChunks().Get(chx, chy)) {
|
||||
m_world.GetChunks().Get(chx, chy)->Render();
|
||||
++m_renderCount;
|
||||
}
|
||||
all.ApplyTranslation(-chx * CHUNK_SIZE_X, 0, -chy * CHUNK_SIZE_Z);
|
||||
}
|
||||
}
|
||||
m_shader01.Disable();
|
||||
|
||||
@@ -444,9 +452,11 @@ void Engine::MouseReleaseEvent(const MOUSE_BUTTON& button, int x, int y) {
|
||||
switch (button) {
|
||||
case MOUSE_BUTTON_LEFT:
|
||||
m_mouseL = false;
|
||||
m_block = false;
|
||||
break;
|
||||
case MOUSE_BUTTON_RIGHT:
|
||||
m_mouseR = false;
|
||||
m_block = false;
|
||||
break;
|
||||
case MOUSE_BUTTON_MIDDLE:
|
||||
m_mouseC = false;
|
||||
@@ -488,50 +498,6 @@ bool Engine::GenerateChunk(int chx, int chy) {
|
||||
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;
|
||||
|
||||
|
||||
@@ -549,104 +515,43 @@ void Engine::UpdateWorld(int& generates, int& updates, int chx, int chy) {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
void Engine::GetBlockAtCursor(BlockType blockType) {
|
||||
Vector3f currentBlock = m_player.GetPosition();
|
||||
Vector3f ray = m_player.GetDirection();
|
||||
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;
|
||||
if (m_block) return;
|
||||
|
||||
// Skip water blocs
|
||||
//if(bloc->Type == BT_WATER)
|
||||
// continue;
|
||||
while ((m_player.GetPosition() - currentBlock).Length() <= MAX_SELECTION_DISTANCE && !found) {
|
||||
currentBlock += ray / 10.f;
|
||||
|
||||
currentBlock.x = x;
|
||||
currentBlock.y = y;
|
||||
currentBlock.z = z;
|
||||
BlockType bt = m_world.BlockAt(currentBlock);
|
||||
|
||||
if (bt != BTYPE_AIR)
|
||||
found = true;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (blockType != BTYPE_AIR) {
|
||||
found = false;
|
||||
while ((m_player.GetPosition() - currentBlock).Length() >= .7f && !found) {
|
||||
currentBlock -= ray / 10.f;
|
||||
|
||||
BlockType bt = m_world.BlockAt(currentBlock);
|
||||
|
||||
if (bt == BTYPE_AIR)
|
||||
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;
|
||||
|
||||
if (found) {
|
||||
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();
|
||||
m_block = true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user