HALLELUJAH!

This commit is contained in:
MarcEricMartel 2021-11-27 13:35:39 -05:00
parent 1520ade4f0
commit 8e222f39a9
5 changed files with 76 additions and 172 deletions

View File

@ -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 + 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);
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);
}
if (y == 0 || GetBlock(x, y - 1, z) == BTYPE_AIR) { // -y
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);
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);
}
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);
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, 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);
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, 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);
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 + 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);
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);
}
}

View File

@ -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;
}
}

View File

@ -72,18 +72,14 @@ private:
bool m_mouseC = false;
bool m_mouseWU = false;
bool m_mouseWD = false;
bool m_block = false;
};
template <class T>
static bool EqualWithEpsilon(const T& v1, const T& v2, T epsilon = T(0.0001))
{
return (fabs(v2 - v1) < epsilon);
}
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);
}
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__

View File

@ -13,8 +13,8 @@ void Player::TurnLeftRight(float value) {
void Player::TurnTopBottom(float value) {
m_rotX += value;
if (m_rotX > 45) m_rotX = 45;
else if (m_rotX < -45) m_rotX = -45;
if (m_rotX > 80) m_rotX = 80;
else if (m_rotX < -80) m_rotX = -80;
}
@ -25,17 +25,19 @@ 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(sin(yrotrad),
-sin(xrotrad),
-cos(yrotrad));
m_direction = Vector3f(cos(xrotrad) * sin(yrotrad),
-sin(xrotrad),
cos(xrotrad) * -cos(yrotrad));
m_direction.Normalize();
if (front) {
delta.x += float(sin(yrotrad)) * elapsedTime * 10.f;
delta.z += float(-cos(yrotrad)) * elapsedTime * 10.f;
}
else if (back) {
delta.x -= float(-sin(yrotrad)) * elapsedTime * -10.f;
delta.z -= float(cos(yrotrad)) * elapsedTime * -10.f;
delta.x += float(-sin(yrotrad)) * elapsedTime * 10.f;
delta.z += float(cos(yrotrad)) * elapsedTime * 10.f;
}
if (left) {
@ -43,8 +45,8 @@ Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jum
delta.z += float(-sin(yrotrad)) * elapsedTime * 10.f;
}
else if (right) {
delta.x -= float(cos(yrotrad)) * elapsedTime * -10.f;
delta.z -= float(sin(yrotrad)) * elapsedTime * -10.f;
delta.x += float(cos(yrotrad)) * elapsedTime * 10.f;
delta.z += float(sin(yrotrad)) * elapsedTime * 10.f;
}
delta.Normalize();

View File

@ -23,8 +23,9 @@ class World {
private:
Array2d<Chunk*> m_chunks = Array2d<Chunk*>(WORLD_SIZE_X, WORLD_SIZE_Y);
Array2d<Chunk*> m_buffer = Array2d<Chunk*>(WORLD_SIZE_X, WORLD_SIZE_Y);
int m_center[2] = {0, 0};
unsigned int m_center[2] = {UINT16_MAX / 2, UINT16_MAX / 2};
};
#endif // WORLD_H__