This commit is contained in:
MarcEricMartel
2021-11-30 19:55:11 -05:00
parent de7945038c
commit 97ffc34e24
26 changed files with 221 additions and 203 deletions

View File

@@ -8,8 +8,7 @@ Engine::Engine() { }
Engine::~Engine() { }
void Engine::Init()
{
void Engine::Init() {
GLenum glewErr = glewInit();
if (glewErr != GLEW_OK) {
std::cerr << " ERREUR GLEW : " << glewGetErrorString(glewErr) << std::endl;
@@ -18,17 +17,21 @@ void Engine::Init()
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)Width() / (float)Height(), 0.0001f, 1000.0f);
glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);
//glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_CULL_FACE);
glDisable(GL_FRAMEBUFFER_SRGB);
glEnable(GL_BLEND);
glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
// Light
GLfloat light0Pos[4] = { 0.0f, CHUNK_SIZE_Y, 0.0f, 1.0f };
GLfloat light0Amb[4] = { 0.2f, 0.2f, 0.2f, 1.f };
@@ -41,11 +44,14 @@ void Engine::Init()
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0Diff);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0Spec);
// Init manifeste de chunks render<65>s.
m_renderManifest.reserve(3000);
// Objet de skybox avec sa propre texture et son propre shader!
m_skybox.Init(0.00013f);
// Objet de musique!
m_audio.ToggleMusicState();
//m_audio.ToggleMusicState();
// Init Chunks
m_world.GetChunks().Reset(nullptr);
@@ -67,7 +73,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(128, false)) {
if (!m_textureAtlas.Generate(512, false)) {
std::cout << " Unable to generate texture atlas ..." << std::endl;
abort();
}
@@ -101,7 +107,7 @@ void Engine::DrawHud(float elapsedTime) {
glDisable(GL_LIGHTING);
glColor4f(1.f, 1.f, 1.f, 1.f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
//glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
@@ -119,6 +125,9 @@ void Engine::DrawHud(float elapsedTime) {
ss << " Rendered Chunks : " << m_renderCount;
PrintText(10, Height() - 35, ss.str());
ss.str("");
ss << " Bad Hits on Chunks : " << m_badHitCount;
PrintText(10, Height() - 45, 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("");
@@ -146,7 +155,9 @@ void Engine::DrawHud(float elapsedTime) {
glVertex2i(0, crossSize);
glEnd();
glEnable(GL_LIGHTING);
glDisable(GL_BLEND);
//glDisable(GL_BLEND);
//glBlendFuncSeparate(GL_SRC_COLOR, GL_ONE_MINUS_DST_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
@@ -198,25 +209,29 @@ void Engine::Render(float elapsedTime) {
m_player.ApplyTransformation(skybox, false); // Version d'ApplyTransformation qui ne tient compte que de la rotation
// (donc l'objet ne bouge pas relativement au joueur, ce qui est pratique pour une skybox!).
glDisable(GL_LIGHT0);
m_skybox.Render(skybox);
// G<>n<EFBFBD>ration/Update de Chunks.
//m_skybox.Render(skybox);
glEnable(GL_LIGHT0);
if (m_mouseL)
ChangeBlockAtCursor(BTYPE_DIRT);
else if (m_mouseR)
ChangeBlockAtCursor(BTYPE_AIR);
// G<>n<EFBFBD>ration/Update des Chunks.
m_textureAtlas.Bind();
int cx = m_player.GetPosition().x;
int cy = m_player.GetPosition().z;
static int frameGenerate = 0;
static int frameUpdate = 0;
int side = 0;
m_shader01.Use();
m_renderCount = 0;
if (frameGenerate > 0) --frameGenerate;
if (frameUpdate > 0) --frameUpdate;
if (!frameGenerate || !frameUpdate)
while (side * CHUNK_SIZE_X <= VIEW_DISTANCE) {
while (side * CHUNK_SIZE_X <= VIEW_DISTANCE * 2) {
int tx = -side, ty = -side;
for (; tx <= side; ++tx)
@@ -231,77 +246,69 @@ void Engine::Render(float elapsedTime) {
++side;
}
// 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();
// Rendering des Chunks.
m_shader01.Use();
m_renderCount = 0;
m_badHitCount = 0;
Vector3f angle;
Vector3f cursor;
Vector3f direct = m_player.GetDirection();
Vector3f pos = m_player.GetPosition() - direct;
direct.y = 0;
direct.Normalize();
direct.y = 0;
direct.Normalize();
pos.y = 1;
m_renderManifest.clear();
Vector3f viewL = renderpos - direct * CHUNK_SIZE_X * 2,
viewR = viewL;
for (int dist = VIEW_DISTANCE; dist >= 0; dist -= CHUNK_SIZE_X) {
// Configuration du radar.
angle.x = direct.z + direct.x;
angle.y = 0;
angle.z = direct.z - direct.x;
angle.Normalize();
viewL.Dot(direct);
viewR.Dot(direct);
float sinus = .01745240643; // sin(1 degr<67>)
float cosinus = .99984769515; // cos(1 degr<67>)
int echantillons = 90;
for (int x = -3; x <= VIEW_DISTANCE / CHUNK_SIZE_X; ++x) {
int chunkxL = -1, chunkyL = -1, chunkxR = -1, chunkyR = -1;
for (int radar = 0; radar < echantillons; ++radar) {
float x = angle.x;
float z = angle.z;
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();
}
angle.x = x * cosinus - z * sinus;
angle.z = z * cosinus + x * sinus;
angle.Normalize();
if (chunkxL == chunkxR) {
++chunkxR;
--chunkxL;
}
if (chunkyL == chunkyR) {
++chunkyR;
--chunkyL;
}
cursor = pos - direct * CHUNK_SIZE_X * 2 + angle * dist;
if (cursor.y >= 128.f || cursor.y >= 0.f) cursor.y = 1;
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) {
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);
bool valide = true;
if (m_world.ChunkAt(cursor)) {
int chx, chy;
m_world.ChunkAt(cursor)->GetPosition(chx, chy);
for (int index = 0; index < m_renderManifest.size(); ++index)
if (m_renderManifest[index] == Vector3i(chx, 0, chy)) {
valide = false;
++m_badHitCount;
}
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++) {
all.ApplyTranslation(chx * CHUNK_SIZE_X, 0, chy * CHUNK_SIZE_Z);
all.Use();
if (m_world.GetChunks().Get(chx, chy)) {
if (valide) {
all.ApplyTranslation(chx * CHUNK_SIZE_X, 0, chy * CHUNK_SIZE_Z);
all.Use();
float dist = (pos - cursor).Length();
float blend = ((float)VIEW_DISTANCE - dist * 2.f + 128.f) / (float)VIEW_DISTANCE;
glBlendColor(0.f,0.f,0.f,blend);
m_world.GetChunks().Get(chx, chy)->Render();
all.ApplyTranslation(-chx * CHUNK_SIZE_X, 0, -chy * CHUNK_SIZE_Z);
m_renderManifest.push_back(Vector3i(chx, 0, chy));
++m_renderCount;
}
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);
m_shader01.Disable();
if (m_wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
@@ -309,15 +316,19 @@ void Engine::Render(float elapsedTime) {
if (m_wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (m_player.GetPosition().y < -20.f)
m_player = Player(Vector3f(CHUNK_SIZE_X * WORLD_SIZE_X / 2, CHUNK_SIZE_Y + 1.7f, CHUNK_SIZE_Z * WORLD_SIZE_X / 2)); // Respawn si le bonho- joueur tombe en bas du monde.
m_player = Player(Vector3f(CHUNK_SIZE_X * WORLD_SIZE_X / 2, CHUNK_SIZE_Y + 1.8f, CHUNK_SIZE_Z * WORLD_SIZE_X / 2)); // Respawn si le bonho- joueur tombe en bas du monde.
}
void Engine::KeyPressEvent(unsigned char key)
{
switch (key) {
case 36: // ESC
for (int x = 0; x < WORLD_SIZE_X; ++x) // Les destructeurs de Chunks ont de la mis<69>re je les aide un peu!
for (int y = 0; y < WORLD_SIZE_Y; ++y)
if (m_world.GetChunks().Get(x,y))
m_world.GetChunks().Get(x, y)->~Chunk();
m_world.GetChunks().Reset(nullptr); // Hack cheap qui emp<6D>che d'avoir une exception en sortant du jeu
Stop();
break;
case 94: // F10
@@ -490,17 +501,80 @@ 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);
std::ostringstream pos;
pos << CHUNK_PATH << chx / CHUNK_SIZE_X << '_' << chy / CHUNK_SIZE_Z << ".chunk";
std::ifstream input(pos.str().c_str(), std::fstream::binary);
if (input.fail()) {
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) {
Vector3f perlin;
perlin.z = x * CHUNK_SIZE_X + CHUNK_SIZE_X * chx;
perlin.y = 0;
perlin.x = z * CHUNK_SIZE_Z + CHUNK_SIZE_Z * chy;
perlin.Normalize();
float height = m_perlin.Get(perlin.x, perlin.z) * 3 - 32;
for (int y = 0; y <= (int)height % CHUNK_SIZE_Y; ++y) {
chunk->SetBlock(x, y, z, BTYPE_METAL, &m_world);
}
}
for (int x = 0; x < CHUNK_SIZE_X; ++x)
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
Vector3f perlin;
perlin.x = x * CHUNK_SIZE_X + CHUNK_SIZE_X * chx;
perlin.y = 0;
perlin.z = z * CHUNK_SIZE_Z + CHUNK_SIZE_Z * chy;
perlin.Normalize();
float height = m_perlin.Get(perlin.x, perlin.z) + 16;
for (int y = 0; y <= (int)height % CHUNK_SIZE_Y; ++y) {
if (chunk->GetBlock(x, y, z) == BTYPE_AIR)
chunk->SetBlock(x, y, z, BTYPE_GRASS, &m_world);
}
}
for (int x = 0; x < CHUNK_SIZE_X; ++x)
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
for (int y = 0; y <= 10; ++y) {
if (chunk->GetBlock(x, y, z) == BTYPE_AIR)
chunk->SetBlock(x, y, z, BTYPE_ICE, &m_world);
}
}
for (int x = 0; x < CHUNK_SIZE_X; ++x)
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
for (int y = 0; y < CHUNK_SIZE_Y; ++y) {
Vector3f perlin;
perlin.x = x * CHUNK_SIZE_X + CHUNK_SIZE_X * chx;
perlin.y = (x + z) * CHUNK_SIZE_Y;
perlin.z = z * CHUNK_SIZE_Z + CHUNK_SIZE_Z * chy;
perlin.Normalize();
float height = m_perlin.Get(perlin.x, perlin.y, perlin.z);
if (chunk->GetBlock(x, y, z) != BTYPE_AIR && height > 18)
chunk->SetBlock(x, y, z, BTYPE_DIRT, &m_world);
}
}
}
else {
input.seekg(0, std::ios_base::end);
int size = input.tellg();
input.seekg(0, std::ios_base::beg);
char* data = new char[size];
input.read(data, size);
input.close();
m_world.GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, new Chunk(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, data));
delete[] data;
}
std::cout << "Chunk generated: " << chx / CHUNK_SIZE_X << ", " << chy / CHUNK_SIZE_Z << std::endl;
return true;
}
return false;
@@ -515,7 +589,7 @@ void Engine::UpdateWorld(int& generates, int& updates, int chx, int chy) {
}
}
void Engine::GetBlockAtCursor(BlockType blockType) {
void Engine::ChangeBlockAtCursor(BlockType blockType) {
Vector3f currentPos = m_player.GetPosition();
Vector3f currentBlock = currentPos;
Vector3f ray = m_player.GetDirection();
@@ -527,7 +601,7 @@ void Engine::GetBlockAtCursor(BlockType blockType) {
currentBlock += ray / 10.f;
BlockType bt = m_world.BlockAt(currentBlock);
if (bt != BTYPE_AIR)
found = true;
}
@@ -540,7 +614,7 @@ void Engine::GetBlockAtCursor(BlockType blockType) {
BlockType bt = m_world.BlockAt(currentBlock);
if (bt == BTYPE_AIR) {
if (bt == BTYPE_AIR) { // V<>rification pour <20>tre s<>r que le bloc <20> changer n'est pas dans le joueur.
int Bx = (int)currentBlock.x;
int By = (int)currentBlock.y;
int Bz = (int)currentBlock.z;
@@ -551,12 +625,12 @@ void Engine::GetBlockAtCursor(BlockType blockType) {
int PyC = (int)(currentPos.y - 1.7f);
int Pz = (int)currentPos.z;
if (!(Bx == Px &&
(By == PyA ||
By == PyB ||
By == PyC) &&
Bz == Pz))
found = true;
if (!(Bx == Px &&
(By == PyA ||
By == PyB ||
By == PyC) &&
Bz == Pz))
found = true;
}
}
}