#include #include #include #include namespace td { namespace render { namespace WorldLoader { const static int POSITION_VERTEX_SIZE = 3; // const static int TEXTURE_VERTEX_SIZE = 2; GL::VertexArray LoadWorldModel(const td::game::World* world) { std::vector positions; std::vector colors; for (const auto& [coords, chunk] : world->GetChunks()) { std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth; std::int32_t chunkY = coords.y * td::game::Chunk::ChunkHeight; for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++) { for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++) { int tileNumber = tileY * td::game::Chunk::ChunkWidth + tileX; td::game::TileIndex tileIndex = chunk->GetTileIndex(tileNumber); td::game::TilePtr tile = world->GetTilePtr(tileIndex); if (!tile) continue; positions.insert( positions.end(), {static_cast(chunkX + tileX + 1), 0, static_cast(chunkY + tileY), static_cast(chunkX + tileX), 0, static_cast(chunkY + tileY), static_cast(chunkX + tileX), 0, static_cast(chunkY + tileY + 1), static_cast(chunkX + tileX + 1), 0, static_cast(chunkY + tileY), static_cast(chunkX + tileX), 0, static_cast(chunkY + tileY + 1), static_cast(chunkX + tileX + 1), 0, static_cast(chunkY + tileY + 1)}); const td::Color* tileColor = world->GetTileColor(tile); for (int i = 0; i < 6; i++) { int color = 255; color |= tileColor->r << 24; color |= tileColor->g << 16; color |= tileColor->b << 8; int newColorIndex = colors.size(); colors.push_back(0); memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int)); } } } } for (int spawnColor = 0; spawnColor < 2; spawnColor++) { const game::Spawn& spawn = world->GetTeam(TeamColor(spawnColor)).GetSpawn(); float fromX = spawn.GetTopLeft().GetX(), toX = spawn.GetBottomRight().GetX(); float fromY = spawn.GetTopLeft().GetY(), toY = spawn.GetBottomRight().GetY(); positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY}); for (int i = 0; i < 6; i++) { int color = 255; color |= world->GetSpawnColor(TeamColor(spawnColor)).r << 24; color |= world->GetSpawnColor(TeamColor(spawnColor)).g << 16; color |= world->GetSpawnColor(TeamColor(spawnColor)).b << 8; int newColorIndex = colors.size(); colors.push_back(0); memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int)); } } for (int castleColor = 0; castleColor < 2; castleColor++) { const game::TeamCastle& castle = world->GetTeam(TeamColor(castleColor)).GetCastle(); float fromX = castle.GetTopLeft().GetX(), toX = castle.GetBottomRight().GetX(); float fromY = castle.GetTopLeft().GetY(), toY = castle.GetBottomRight().GetY(); positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY}); for (int i = 0; i < 6; i++) { int color = 255; color |= world->GetSpawnColor(TeamColor(castleColor)).r << 24; color |= world->GetSpawnColor(TeamColor(castleColor)).g << 16; color |= world->GetSpawnColor(TeamColor(castleColor)).b << 8; int newColorIndex = colors.size(); colors.push_back(0); memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int)); } } GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE); positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0); GL::VertexBuffer colorVBO(colors, 1); colorVBO.AddVertexAttribPointer(1, 1, 0); std::vector indexes(positions.size() / 3, 0); for (size_t i = 0; i < indexes.size(); i++) { indexes[i] = i + 1; } GL::ElementBuffer indexVBO(indexes); GL::VertexArray worldVao(std::move(indexVBO)); // each pos = 3 vertecies worldVao.Bind(); worldVao.BindVertexBuffer(std::move(positionVBO)); worldVao.BindVertexBuffer(std::move(colorVBO)); worldVao.Unbind(); return worldVao; } GL::VertexArray LoadTileSelectModel() { std::vector positions = { -0.5f, -0.5f, -1.0f, 0.5f, -0.5f, -1.0f, 0.0f, 0.5f, -1.0f, 1, .01, 1, 0, .01, 1, 0, 1, 1, }; int color = 255 << 24 | 255 << 16 | 255 << 8 | 150; float colorFloat; memcpy(reinterpret_cast(&colorFloat), &color, sizeof(float)); std::vector colors(6, colorFloat); GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE); positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0); GL::VertexBuffer colorVBO(colors, 1); colorVBO.AddVertexAttribPointer(1, 1, 0); std::vector indexes(positions.size() / 3, 0); // for (size_t i = 0; i < indexes.size(); i++) { // indexes[i] = i + 1; // } GL::ElementBuffer indexVBO(indexes); GL::VertexArray tileSelectVao(std::move(indexVBO)); tileSelectVao.Bind(); tileSelectVao.BindVertexBuffer(std::move(positionVBO)); tileSelectVao.BindVertexBuffer(std::move(colorVBO)); tileSelectVao.Unbind(); return tileSelectVao; } RenderData LoadTowerModel(const game::TowerPtr& tower) { RenderData renderData; float towerX, towerDX; float towerY, towerDY; if (tower->GetSize() == game::TowerSize::Little) { towerX = tower->GetCenterX() - 1.5f; towerDX = tower->GetCenterX() + 1.5f; towerY = tower->GetCenterY() - 1.5f; towerDY = tower->GetCenterY() + 1.5f; } else { towerX = tower->GetCenterX() - 2.5f; towerDX = tower->GetCenterX() + 2.5f; towerY = tower->GetCenterY() - 2.5f; towerDY = tower->GetCenterY() + 2.5f; } std::vector positions = {towerDX, 0.001, towerY, towerX, 0.001, towerY, towerX, 0.001, towerDY, towerDX, 0.001, towerY, towerX, 0.001, towerDY, towerDX, 0.001, towerDY}; renderData.positions = positions; std::uint8_t towerType = static_cast(tower->GetType()); std::uint8_t r = 10 * towerType + 40, g = 5 * towerType + 30, b = 10 * towerType + 20; float colorFloat; int color = r << 24 | g << 16 | b << 8 | 255; memcpy(&colorFloat, &color, sizeof(int)); std::vector colors(6, colorFloat); renderData.colors = colors; return renderData; } GL::VertexArray LoadMobModel() { std::vector positions = { -0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5 }; std::vector indexes(positions.size() / 3, 0); // for (size_t i = 0; i < indexes.size(); i++) { // indexes[i] = i + 1; // } GL::ElementBuffer indexVBO(indexes); GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE); positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0); GL::VertexArray mobVao(std::move(indexVBO)); // each pos = 1 color mobVao.Bind(); mobVao.BindVertexBuffer(std::move(positionVBO)); mobVao.Unbind(); return mobVao; } } // namespace WorldLoader } // namespace render } // namespace td