added new class camera

This commit is contained in:
Ariari04 2026-02-05 15:01:16 +06:00
parent 0a72040110
commit 47da35a401
8 changed files with 261 additions and 243 deletions

View File

@ -18,6 +18,8 @@ add_executable(space-game001
../src/Environment.h ../src/Environment.h
../src/render/Renderer.cpp ../src/render/Renderer.cpp
../src/render/Renderer.h ../src/render/Renderer.h
../src/render/Camera.cpp
../src/render/Camera.h
../src/render/ShaderManager.cpp ../src/render/ShaderManager.cpp
../src/render/ShaderManager.h ../src/render/ShaderManager.h
../src/render/TextureManager.cpp ../src/render/TextureManager.cpp

View File

@ -1,3 +1,4 @@
#include "GameConfig.h"
#include "Game.h" #include "Game.h"
#include "AnimatedModel.h" #include "AnimatedModel.h"
#include "BoneAnimatedModel.h" #include "BoneAnimatedModel.h"
@ -250,12 +251,7 @@ namespace ZL
renderer, CONST_ZIP_FILE, renderer, CONST_ZIP_FILE,
"resources/joystick_base.png", "resources/joystick_knob.png"); "resources/joystick_base.png", "resources/joystick_knob.png");
/*uiManager.setSliderCallback("musicVolumeSlider", [this](const std::string& name, float value) {
std::cerr << "Music volume slider changed to: " << value << std::endl;
musicVolume = value;
Environment::shipVelocity = musicVolume * 20.0f;
});
//#endif*/
cubemapTexture = std::make_shared<Texture>( cubemapTexture = std::make_shared<Texture>(
std::array<TextureDataStruct, 6>{ std::array<TextureDataStruct, 6>{
@ -275,24 +271,17 @@ namespace ZL
//Load texture //Load texture
//spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("resources/DefaultMaterial_BaseColor_shine.png", CONST_ZIP_FILE));
//spaceshipBase = LoadFromTextFile02("resources/spaceship006.txt", CONST_ZIP_FILE);
//spaceshipBase.RotateByMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(M_PI / 2.0, Eigen::Vector3f::UnitY())).toRotationMatrix());// QuatFromRotateAroundY(M_PI / 2.0).toRotationMatrix());
//spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/cap_D.png", CONST_ZIP_FILE));
//spaceshipBase = LoadFromTextFile02("./resources/spaceship006x.txt", CONST_ZIP_FILE);
//spaceshipBase.RotateByMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(-M_PI / 2.0, Eigen::Vector3f::UnitY())).toRotationMatrix());// QuatFromRotateAroundY(M_PI / 2.0).toRotationMatrix());
spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("resources/MainCharacter_Base_color_sRGB.png", CONST_ZIP_FILE)); spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("resources/MainCharacter_Base_color_sRGB.png", CONST_ZIP_FILE));
spaceshipBase = LoadFromTextFile02("resources/spaceshipnew001.txt", CONST_ZIP_FILE); spaceshipBase = LoadFromTextFile02("resources/spaceshipnew001.txt", CONST_ZIP_FILE);
spaceshipBase.RotateByMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitY())).toRotationMatrix());// QuatFromRotateAroundY(M_PI / 2.0).toRotationMatrix()); spaceshipBase.RotateByMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitY())).toRotationMatrix());// QuatFromRotateAroundY(M_PI / 2.0).toRotationMatrix());
//spaceshipBase.Move(Vector3f{ 1.2, 0, -5 });
//spaceshipBase.Move(Vector3f{ -0.52998, -13, 0 });
spaceshipBase.Move(Vector3f{ -0.52998, 0, -10 }); spaceshipBase.Move(Vector3f{ -0.52998, 0, -10 });
//spaceshipBase.Move(Vector3f{ 1.2, 0, -5 });
spaceship.AssignFrom(spaceshipBase); spaceship.AssignFrom(spaceshipBase);
@ -308,7 +297,7 @@ namespace ZL
for (int i = 0; i < boxCoordsArr.size(); i++) for (int i = 0; i < boxCoordsArr.size(); i++)
{ {
boxRenderArr[i].AssignFrom(boxBase); boxRenderArr[i].AssignFrom(boxBase);
//boxRenderArr[i].data = CreateBaseConvexPolyhedron(1999);
boxRenderArr[i].RefreshVBO(); boxRenderArr[i].RefreshVBO();
} }
@ -351,8 +340,13 @@ namespace ZL
static_cast<float>(Environment::width) / static_cast<float>(Environment::height), static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR); Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR);
renderer.PushMatrix(); renderer.PushMatrix();
renderer.LoadIdentity(); renderer.LoadIdentity();
renderer.RotateMatrix(Environment::inverseShipMatrix);
// Для скайбокса берем только вращение от камеры
Matrix4f view = camera.getViewMatrix();
view.block<3, 1>(0, 3) = Vector3f::Zero(); // Убираем смещение
renderer.PushSpecialMatrix(view);
Vector3f worldLightDir = Vector3f(1.0f, -1.0f, -1.0f).normalized(); Vector3f worldLightDir = Vector3f(1.0f, -1.0f, -1.0f).normalized();
@ -392,7 +386,8 @@ namespace ZL
CheckGlError(); CheckGlError();
renderer.PopMatrix(); renderer.PopMatrix(); // Pop special matrix
renderer.PopMatrix(); // Pop original push
renderer.PopProjectionMatrix(); renderer.PopProjectionMatrix();
renderer.DisableVertexAttribArray(vPositionName); renderer.DisableVertexAttribArray(vPositionName);
@ -417,20 +412,20 @@ namespace ZL
static_cast<float>(Environment::width) / static_cast<float>(Environment::height), static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR); Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR);
renderer.PushMatrix(); renderer.PushMatrix();
renderer.LoadIdentity();
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
renderer.TranslateMatrix({ 0, -6.f, 0 }); //Ship camera offset
// Корабль рисуется с учётом разницы между его ориентацией и камерой renderer.LoadIdentity();
// shipWorldOrientation - куда смотрит корабль в мире
// Environment::inverseShipMatrix - обратная матрица камеры renderer.PushSpecialMatrix(camera.getViewMatrix());
Matrix3f shipRelativeToCamera = Environment::inverseShipMatrix * shipWorldOrientation;
renderer.RotateMatrix(shipRelativeToCamera); renderer.PushMatrix();
renderer.TranslateMatrix(Environment::shipState.position);
renderer.RotateMatrix(shipWorldOrientation);
if (shipAlive) { if (shipAlive) {
glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID()); glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID());
renderer.DrawVertexRenderStruct(spaceship); renderer.DrawVertexRenderStruct(spaceship);
} }
renderer.PopMatrix();
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -453,7 +448,8 @@ namespace ZL
} }
glDisable(GL_BLEND); glDisable(GL_BLEND);
renderer.PopMatrix(); renderer.PopMatrix(); // Pop special matrix
renderer.PopMatrix(); // Pop original push
renderer.PopProjectionMatrix(); renderer.PopProjectionMatrix();
renderer.DisableVertexAttribArray(vPositionName); renderer.DisableVertexAttribArray(vPositionName);
renderer.DisableVertexAttribArray(vTexCoordName); renderer.DisableVertexAttribArray(vTexCoordName);
@ -479,15 +475,17 @@ namespace ZL
static_cast<float>(Environment::width) / static_cast<float>(Environment::height), static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR); Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR);
renderer.PushMatrix();
renderer.LoadIdentity();
renderer.PushSpecialMatrix(camera.getViewMatrix());
for (int i = 0; i < boxCoordsArr.size(); i++) for (int i = 0; i < boxCoordsArr.size(); i++)
{ {
if (!boxAlive[i]) continue; if (!boxAlive[i]) continue;
renderer.PushMatrix(); renderer.PushMatrix();
renderer.LoadIdentity(); // Коробки рисуются в мировых координатах
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom }); // Но у них есть offset { 0.f, 0.f, 45000.f }
renderer.RotateMatrix(Environment::inverseShipMatrix);
renderer.TranslateMatrix(-Environment::shipState.position);
renderer.TranslateMatrix({ 0.f, 0.f, 45000.f }); renderer.TranslateMatrix({ 0.f, 0.f, 45000.f });
renderer.TranslateMatrix(boxCoordsArr[i].pos); renderer.TranslateMatrix(boxCoordsArr[i].pos);
renderer.RotateMatrix(boxCoordsArr[i].m); renderer.RotateMatrix(boxCoordsArr[i].m);
@ -498,6 +496,8 @@ namespace ZL
renderer.PopMatrix(); renderer.PopMatrix();
} }
renderer.PopMatrix(); // Pop special matrix
renderer.PopMatrix(); // Pop original push
renderer.PopProjectionMatrix(); renderer.PopProjectionMatrix();
renderer.DisableVertexAttribArray(vPositionName); renderer.DisableVertexAttribArray(vPositionName);
renderer.DisableVertexAttribArray(vTexCoordName); renderer.DisableVertexAttribArray(vTexCoordName);
@ -542,8 +542,15 @@ namespace ZL
glViewport(0, 0, Environment::width, Environment::height); glViewport(0, 0, Environment::width, Environment::height);
glViewport(0, 0, Environment::width, Environment::height);
CheckGlError(); CheckGlError();
// Обновляем камеру
// camera.follow(Environment::shipState.position, shipWorldOrientation, Environment::zoom, 6.0f);
camera.followOrbit(Environment::shipState.position, camYaw, camPitch, Environment::zoom, 6.0f);
float skyPercent = 0.0; float skyPercent = 0.0;
float distance = planetObject.distanceToPlanetSurface(Environment::shipState.position); float distance = planetObject.distanceToPlanetSurface(Environment::shipState.position);
if (distance > 1500.f) if (distance > 1500.f)
@ -567,7 +574,9 @@ namespace ZL
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
} }
drawShip(); drawShip();
#if ENABLE_REMOTE_SHIPS
drawRemoteShips(); drawRemoteShips();
#endif
drawBoxes(); drawBoxes();
drawUI(); drawUI();
@ -615,21 +624,20 @@ namespace ZL
renderer.PushMatrix(); renderer.PushMatrix();
renderer.LoadIdentity(); renderer.LoadIdentity();
renderer.PushSpecialMatrix(camera.getViewMatrix());
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom }); renderer.PushMatrix();
renderer.TranslateMatrix({ 0, -6.f, 0 }); //Ship camera offset // Позиция удаленного игрока в мире
renderer.RotateMatrix(Environment::inverseShipMatrix); renderer.TranslateMatrix(playerState.position);
renderer.TranslateMatrix(-Environment::shipState.position);
Eigen::Vector3f relativePos = playerState.position;// -Environment::shipPosition;
renderer.TranslateMatrix(relativePos);
// 3. Поворот врага // 3. Поворот врага
renderer.RotateMatrix(playerState.rotation); renderer.RotateMatrix(playerState.rotation);
renderer.DrawVertexRenderStruct(spaceship); renderer.DrawVertexRenderStruct(spaceship);
renderer.PopMatrix(); renderer.PopMatrix();
renderer.PopMatrix();
renderer.PopMatrix();
} }
renderer.PopProjectionMatrix(); renderer.PopProjectionMatrix();
@ -668,126 +676,82 @@ namespace ZL
sparkEmitter.update(static_cast<float>(delta)); sparkEmitter.update(static_cast<float>(delta));
planetObject.update(static_cast<float>(delta)); planetObject.update(static_cast<float>(delta));
// Управление: Джойстик (движение) vs Камера (орбита)
float discreteMag = 0.0f;
int discreteAngle = -1; int discreteAngle = -1;
float discreteMag = 0;
// 1. Joystick Logic (Movement)
auto joystick = uiManager.findJoystick("shipJoystick"); auto joystick = uiManager.findJoystick("shipJoystick");
if (joystick && joystick->isActive) { if (isUsingJoystick && joystick && joystick->isActive) {
float joyX = joystick->getDirectionX(); // -1..1 float joyX = joystick->getDirectionX();
float joyY = joystick->getDirectionY(); // -1..1 float joyY = joystick->getDirectionY();
float magnitude = joystick->getMagnitude(); // 0..1 float magnitude = joystick->getMagnitude();
discreteMag = roundf(magnitude * 10) * 0.1f;
float angleY = std::atan2(-joyX, -joyY);
discreteAngle = static_cast<int>(angleY * 180.0f / M_PI);
if (discreteAngle < 0) discreteAngle += 360;
}
//<<<<<<< HEAD
// Управление кораблём через джойстик
/*auto joystick = uiManager.findJoystick("shipJoystick");
if (joystick && joystick->isActive) {
float joyX = joystick->getDirectionX(); // -1..1
float joyY = joystick->getDirectionY(); // -1..1
float magnitude = joystick->getMagnitude(); // 0..1
if (magnitude > 0.1f) { if (magnitude > 0.1f) {
// Скорость пропорциональна отклонению джойстика // Movement logic
Environment::shipState.velocity = magnitude * 500.0f * static_cast<float>(delta) / 100.0f;
// Направление джойстика относительно камеры
// joyY отрицательный = вперёд, joyX = влево/вправо
float angleY = std::atan2(-joyX, -joyY);
// Локальный поворот в пространстве камеры
Eigen::Quaternionf localRotation(Eigen::AngleAxisf(angleY, Eigen::Vector3f::UnitY()));
Matrix3f localRotMat = localRotation.toRotationMatrix();
// Преобразуем в мировую ориентацию: камера * локальный_поворот
shipWorldOrientation = Environment::shipState.rotation * localRotMat;
}
else {
Environment::shipState.velocity = 0.0f; Environment::shipState.velocity = 0.0f;
}
}
else if (Environment::tapDownHold && !uiManager.isUiInteraction())
{
float diffx = Environment::tapDownCurrentPos(0) - Environment::tapDownStartPos(0);
float diffy = Environment::tapDownCurrentPos(1) - Environment::tapDownStartPos(1);
// Только управление камерой через drag (вне зоны джойстика) float moveSpeed = 0.5f;
if (isDraggingCamera) {
if (std::abs(diffy) > 5.0f || std::abs(diffx) > 5.0f)
{
float rotationPower = std::sqrt(diffx * diffx + diffy * diffy);
float deltaAlpha = rotationPower * static_cast<float>(delta) * static_cast<float>(M_PI) / 15000.f;
Eigen::Vector3f rotationDirection(diffy, diffx, 0.0f); // Local movement vector
rotationDirection.normalize(); Vector3f localMove(joyX, 0.0f, joyY);
Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, rotationDirection)); // Transform to world space using current ship rotation
Matrix3f rotateMat = rotateQuat.toRotationMatrix(); Vector3f worldMove = Environment::shipState.rotation * localMove;
Environment::shipState.rotation = Environment::shipState.rotation * rotateMat; // Apply to position
Environment::inverseShipMatrix = Environment::shipState.rotation.inverse(); Environment::shipState.position += worldMove * moveSpeed * static_cast<float>(delta);
// плавное вращение (ВАЖНО!) // Calculate discrete values for network
Environment::tapDownStartPos = Environment::tapDownCurrentPos; // Angle 0-359
} float radians = atan2f(joyY, joyX);
}
}
float discreteMag;
int discreteAngle;
if (Environment::tapDownHold) {
float diffx = Environment::tapDownCurrentPos(0) - Environment::tapDownStartPos(0);
float diffy = Environment::tapDownCurrentPos(1) - Environment::tapDownStartPos(1);
float rawMag = sqrtf(diffx * diffx + diffy * diffy);
float maxRadius = 200.0f; // Максимальный вынос джойстика
if (rawMag > 10.0f) { // Мертвая зона
// 1. Дискретизируем отклонение (0.0 - 1.0 с шагом 0.1)
float normalizedMag = min(rawMag / maxRadius, 1.0f);
discreteMag = std::round(normalizedMag * 10.0f) / 10.0f;
// 2. Дискретизируем угол (0-359 градусов)
// atan2 возвращает радианы, переводим в градусы
float radians = atan2f(diffy, diffx);
discreteAngle = static_cast<int>(radians * 180.0f / M_PI); discreteAngle = static_cast<int>(radians * 180.0f / M_PI);
if (discreteAngle < 0) discreteAngle += 360; if (discreteAngle < 0) discreteAngle += 360;
} // Magnitude 0.0-1.0
else discreteMag = (std::min)(magnitude, 1.0f);
{ discreteMag = std::round(discreteMag * 10.0f) / 10.0f;
discreteAngle = -1;
discreteMag = 0.0f;
} }
} }
else // 2. Camera Drag Logic
{ else if (isDraggingCamera && Environment::tapDownHold && !uiManager.isUiInteraction()) {
discreteAngle = -1; float diffx = Environment::tapDownCurrentPos(0) - Environment::tapDownStartPos(0);
discreteMag = 0.0f; float diffy = Environment::tapDownCurrentPos(1) - Environment::tapDownStartPos(1);
>>>>>>> main*/
//}
if (std::abs(diffx) > 1.0f || std::abs(diffy) > 1.0f) {
float sensitivity = 0.01f; // Radians per pixel
camYaw -= diffx * sensitivity; // Yaw: Horizontal drag
camPitch -= diffy * sensitivity; // Pitch: Vertical drag
// Clamp pitch to avoid flipping
// -80 to 80 degrees in radians
float pitchLimit = 80.0f * static_cast<float>(M_PI) / 180.0f;
if (camPitch > pitchLimit) camPitch = pitchLimit;
if (camPitch < -pitchLimit) camPitch = -pitchLimit;
// Reset for incremental update
Environment::tapDownStartPos = Environment::tapDownCurrentPos;
}
}
// Network Update
if (discreteAngle != Environment::shipState.discreteAngle || discreteMag != Environment::shipState.discreteMag) { if (discreteAngle != Environment::shipState.discreteAngle || discreteMag != Environment::shipState.discreteMag) {
Environment::shipState.discreteAngle = discreteAngle; Environment::shipState.discreteAngle = discreteAngle;
Environment::shipState.discreteMag = discreteMag; Environment::shipState.discreteMag = discreteMag;
std::string msg = "UPD:" + std::to_string(now_ms) + ":" + Environment::shipState.formPingMessageContent(); std::string msg = "UPD:" + std::to_string(now_ms) + ":" + Environment::shipState.formPingMessageContent();
networkClient->Send(msg); networkClient->Send(msg);
std::cout << "Sending: " << msg << std::endl; //std::cout << "Sending: " << msg << std::endl;
} }
Environment::shipState.simulate_physics(delta); Environment::shipState.simulate_physics(delta);
Environment::inverseShipMatrix = Environment::shipState.rotation.inverse(); Environment::inverseShipMatrix = Environment::shipState.rotation.inverse();
@ -814,6 +778,7 @@ namespace ZL
projectileEmitter.setEmissionPoints(std::vector<Vector3f>()); projectileEmitter.setEmissionPoints(std::vector<Vector3f>());
} }
std::vector<Vector3f> shipCameraPoints; std::vector<Vector3f> shipCameraPoints;
for (const auto& lp : shipLocalEmissionPoints) { for (const auto& lp : shipLocalEmissionPoints) {
Vector3f adjusted = lp + Vector3f{ 0.0f, -Environment::zoom * 0.03f, 0.0f }; Vector3f adjusted = lp + Vector3f{ 0.0f, -Environment::zoom * 0.03f, 0.0f };
@ -1174,24 +1139,20 @@ namespace ZL
uiManager.onMouseDown(uiX, uiY); uiManager.onMouseDown(uiX, uiY);
bool uiHandled = false; // Check if joystick became active
auto joystick = uiManager.findJoystick("shipJoystick");
for (const auto& button : uiManager.findButton("") ? std::vector<std::shared_ptr<UiButton>>{} : std::vector<std::shared_ptr<UiButton>>{}) { if (joystick && joystick->isActive) {
(void)button; isUsingJoystick = true;
isDraggingCamera = false;
Environment::tapDownHold = false; // Don't trigger camera drag
} }
else if (!uiManager.isUiInteraction()) {
auto pressedSlider = [&]() -> std::shared_ptr<UiSlider> { // No UI interaction (and no joystick), so it's camera drag
for (const auto& slider : uiManager.findSlider("") ? std::vector<std::shared_ptr<UiSlider>>{} : std::vector<std::shared_ptr<UiSlider>>{}) { isUsingJoystick = false;
(void)slider;
}
return nullptr;
}();
if (!uiManager.isUiInteraction()) {
// Джойстик обрабатывает управление кораблём, поэтому
// любое нажатие вне UI элементов - это управление камерой
isDraggingShip = false;
isDraggingCamera = true; isDraggingCamera = true;
Environment::tapDownHold = true;
Environment::tapDownStartPos = Vector2f(static_cast<float>(mx), static_cast<float>(my));
Environment::tapDownCurrentPos = Environment::tapDownStartPos;
} }
} }
@ -1200,33 +1161,14 @@ namespace ZL
int uiX = mx; int uiX = mx;
int uiY = Environment::height - my; int uiY = Environment::height - my;
// Проверяем был ли активен джойстик до отпускания
auto joystick = uiManager.findJoystick("shipJoystick");
bool wasJoystickActive = joystick && joystick->isActive;
uiManager.onMouseUp(uiX, uiY); uiManager.onMouseUp(uiX, uiY);
// Сбрасываем состояние если отпустили джойстик if (isUsingJoystick) {
/* isUsingJoystick = false;
if (wasJoystickActive) {
Environment::shipVelocity = 0.0f;
shipMoveLockActive = false;
rotateShipMat = Matrix3f::Identity();
} }
if (!uiManager.isUiInteraction()) { isDraggingCamera = false;
Environment::tapDownHold = false; Environment::tapDownHold = false;
Environment::shipVelocity = 0.0f;
shipMoveLockActive = false;
if (isDraggingCamera) {
rotateShipMat = Matrix3f::Identity();
}
isDraggingShip = false;
isDraggingCamera = false;
}*/
} }
void Game::handleMotion(int mx, int my) void Game::handleMotion(int mx, int my)
@ -1236,9 +1178,9 @@ namespace ZL
uiManager.onMouseMove(uiX, uiY); uiManager.onMouseMove(uiX, uiY);
if (Environment::tapDownHold && !uiManager.isUiInteraction()) { if (isDraggingCamera && !uiManager.isUiInteraction()) {
Environment::tapDownCurrentPos(0) = mx; Environment::tapDownCurrentPos(0) = static_cast<float>(mx);
Environment::tapDownCurrentPos(1) = my; Environment::tapDownCurrentPos(1) = static_cast<float>(my);
} }
} }

View File

@ -3,6 +3,7 @@
#include "render/Renderer.h" #include "render/Renderer.h"
#include "Environment.h" #include "Environment.h"
#include "render/TextureManager.h" #include "render/TextureManager.h"
#include "render/Camera.h"
#include "SparkEmitter.h" #include "SparkEmitter.h"
#include "planet/PlanetObject.h" #include "planet/PlanetObject.h"
#include "UiManager.h" #include "UiManager.h"
@ -33,6 +34,7 @@ namespace ZL {
bool shouldExit() const { return Environment::exitGameLoop; } bool shouldExit() const { return Environment::exitGameLoop; }
Renderer renderer; Renderer renderer;
Camera camera;
TaskManager taskManager; TaskManager taskManager;
MainThreadHandler mainThreadHandler; MainThreadHandler mainThreadHandler;
@ -95,6 +97,11 @@ namespace ZL {
bool isDraggingShip = false; bool isDraggingShip = false;
bool isDraggingCamera = false; bool isDraggingCamera = false;
bool isUsingJoystick = false;
float camYaw = 0.0f;
float camPitch = 0.0f;
Matrix3f rotateShipMat = Matrix3f::Identity(); // Локальный поворот от джойстика Matrix3f rotateShipMat = Matrix3f::Identity(); // Локальный поворот от джойстика
Matrix3f shipWorldOrientation = Matrix3f::Identity(); // Ориентация корабля в мире (независимо от камеры) Matrix3f shipWorldOrientation = Matrix3f::Identity(); // Ориентация корабля в мире (независимо от камеры)
bool shipMoveLockActive = false; bool shipMoveLockActive = false;

5
src/GameConfig.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
// Feature flags to temporarily disable unstable features
#define ENABLE_STONES 0
#define ENABLE_REMOTE_SHIPS 0

View File

@ -1,4 +1,5 @@
#include "PlanetObject.h" #include "GameConfig.h"
#include "PlanetObject.h"
#include <random> #include <random>
#include <cmath> #include <cmath>
#include "render/OpenGlExtensions.h" #include "render/OpenGlExtensions.h"
@ -73,7 +74,7 @@ namespace ZL {
int lodIndex = planetData.getMaxLodIndex(); int lodIndex = planetData.getMaxLodIndex();
planetRenderStruct.data = planetData.getLodLevel(lodIndex).vertexData; planetRenderStruct.data = planetData.getLodLevel(lodIndex).vertexData;
//planetRenderStruct.data.PositionData.resize(9);
planetRenderStruct.RefreshVBO(); planetRenderStruct.RefreshVBO();
@ -111,34 +112,30 @@ namespace ZL {
// 2. Получаем список видимых треугольников // 2. Получаем список видимых треугольников
auto newIndices = planetData.getTrianglesUnderCameraNew2(Environment::shipState.position); auto newIndices = planetData.getTrianglesUnderCameraNew2(Environment::shipState.position);
std::sort(newIndices.begin(), newIndices.end()); std::sort(newIndices.begin(), newIndices.end());
// 3. Запуск генерации для новых индексов
for (int idx : newIndices) {
if (planetStones.statuses[idx] == ChunkStatus::Empty) {
planetStones.statuses[idx] = ChunkStatus::Generating;
// 3. Анализируем, что нужно загрузить // Запускаем задачу в фоне
for (int triIdx : newIndices) {
if (planetStones.statuses[triIdx] == ChunkStatus::Empty) {
// Помечаем, чтобы не спамить задачами
planetStones.statuses[triIdx] = ChunkStatus::Generating;
// Отправляем тяжелую математику в TaskManager taskManager.EnqueueBackgroundTask([this, idx]() {
taskManager.EnqueueBackgroundTask([this, triIdx]() { // Имитация тяжелой работы (генерация меша)
// Выполняется в фоновом потоке: только генерация геометрии в RAM // Тут мы просто копируем базовый камень + скейл/поворот
float scaleModifier = 1.0f; VertexDataStruct newData = planetStones.inflateOneDataOnly(idx, 0.75f); // 0.75f - scaleModifier
VertexDataStruct generatedData = planetStones.inflateOneDataOnly(triIdx, scaleModifier);
// Передаем задачу на загрузку в GPU в очередь главного потока // Когда готово — перекидываем в главный поток для загрузки в GPU
this->mainThreadHandler.EnqueueMainThreadTask([this, triIdx, data = std::move(generatedData)]() mutable { mainThreadHandler.EnqueueMainThreadTask([this, idx, data = std::move(newData)]() mutable {
// Проверяем актуальность: если треугольник всё еще в списке видимых // Проверяем, всё ли еще актуален этот чанк (вдруг игрок улетел)
auto it = std::find(triangleIndicesToDraw.begin(), triangleIndicesToDraw.end(), triIdx); // Но пока упростим: всегда грузим, а чистильщик (пункт 4) потом удалит если что
if (it != triangleIndicesToDraw.end()) { if (planetStones.statuses[idx] == ChunkStatus::Generating) {
stonesToRender[triIdx].data = std::move(data); stonesToRender[idx].data = std::move(data);
stonesToRender[triIdx].RefreshVBO(); // OpenGL вызов stonesToRender[idx].RefreshVBO();
planetStones.statuses[triIdx] = ChunkStatus::Live; planetStones.statuses[idx] = ChunkStatus::Live;
} }
else {
// Если уже не нужен — просто сбрасываем статус
planetStones.statuses[triIdx] = ChunkStatus::Empty;
}
});
}); });
});
} }
} }
@ -212,8 +209,7 @@ namespace ZL {
float centerX = (minX + maxX) * 0.5f; float centerX = (minX + maxX) * 0.5f;
float centerY = (minY + maxY) * 0.5f; float centerY = (minY + maxY) * 0.5f;
//width = width * 0.995;
//height = height * 0.995;
renderer.PushProjectionMatrix( renderer.PushProjectionMatrix(
centerX - width*0.5, centerX + width * 0.5, centerX - width*0.5, centerX + width * 0.5,
@ -263,7 +259,7 @@ namespace ZL {
{ {
if (stoneMapFB == nullptr) if (stoneMapFB == nullptr)
{ {
//stoneMapFB = std::make_unique<FrameBuffer>(512, 512, true);
stoneMapFB = std::make_unique<FrameBuffer>(512, 512); stoneMapFB = std::make_unique<FrameBuffer>(512, 512);
} }
stoneMapFB->Bind(); stoneMapFB->Bind();
@ -281,7 +277,9 @@ namespace ZL {
drawPlanet(renderer); drawPlanet(renderer);
#if ENABLE_STONES
drawStones(renderer); drawStones(renderer);
#endif
drawAtmosphere(renderer); drawAtmosphere(renderer);
} }
@ -429,15 +427,6 @@ namespace ZL {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, stoneTexture->getTexID()); glBindTexture(GL_TEXTURE_2D, stoneTexture->getTexID());
/*
for (int i : triangleIndicesToDraw)
//for (int i = 0; i < stonesToRender.size(); i++)
{
if (stonesToRender[i].data.PositionData.size() > 0)
{
renderer.DrawVertexRenderStruct(stonesToRender[i]);
}
}*/
for (int i : triangleIndicesToDraw) { for (int i : triangleIndicesToDraw) {
// КРИТИЧЕСКОЕ ИЗМЕНЕНИЕ: // КРИТИЧЕСКОЕ ИЗМЕНЕНИЕ:
// Проверяем, что данные не просто существуют, а загружены в GPU // Проверяем, что данные не просто существуют, а загружены в GPU

View File

@ -110,18 +110,7 @@ namespace ZL {
v(2) *= scaleFactors(2); v(2) *= scaleFactors(2);
} }
/*
// Случайный поворот (например, вокруг трех осей)
Vector4f qx = Eigen::Quaternionf(Eigen::AngleAxisf(getRandomFloat(engine, 0.0f, 360.0f), Eigen::Vector3f::UnitX()));//QuatFromRotateAroundX(getRandomFloat(engine, 0.0f, 360.0f));
Vector4f qy = Eigen::Quaternionf(Eigen::AngleAxisf(getRandomFloat(engine, 0.0f, 360.0f), Eigen::Vector3f::UnitY()));//QuatFromRotateAroundY(getRandomFloat(engine, 0.0f, 360.0f));
Vector4f qz = Eigen::Quaternionf(Eigen::AngleAxisf(getRandomFloat(engine, 0.0f, 360.0f), Eigen::Vector3f::UnitZ()));//QuatFromRotateAroundZ(getRandomFloat(engine, 0.0f, 360.0f));
Vector4f qFinal = slerp(qx, qy, 0.5f); // Простой пример комбинирования
qFinal = slerp(qFinal, qz, 0.5f).normalized();
Matrix3f rotationMatrix = QuatToMatrix(qFinal);
for (Vector3f& v : initialVertices) {
v = MultMatrixVector(rotationMatrix, v);
}*/
// 3. Сглаженные Нормали и Формирование Mesh // 3. Сглаженные Нормали и Формирование Mesh
VertexDataStruct result; VertexDataStruct result;
@ -259,18 +248,7 @@ namespace ZL {
getRandomFloat(engine, SCALE_MIN, SCALE_MAX) getRandomFloat(engine, SCALE_MIN, SCALE_MAX)
}; };
/*
if (tIdx == 0)
{
instance.scale = instance.scale * 0.7f;
}*/
/*
Vector4f qx = QuatFromRotateAroundX(getRandomFloat(engine, 0.0f, 360.0f));
Vector4f qy = QuatFromRotateAroundY(getRandomFloat(engine, 0.0f, 360.0f));
Vector4f qz = QuatFromRotateAroundZ(getRandomFloat(engine, 0.0f, 360.0f));
instance.rotation = slerp(slerp(qx, qy, 0.5f), qz, 0.5f).normalized();
*/
instance.rotation = Vector4f(0.f, 0.f, 0.f, 1.f); instance.rotation = Vector4f(0.f, 0.f, 0.f, 1.f);
group.allInstances[tIdx].push_back(instance); group.allInstances[tIdx].push_back(instance);
} }
@ -291,9 +269,15 @@ namespace ZL {
return result; return result;
} }
// Helper to get the singleton base stone data
const VertexDataStruct& GetBaseStone() {
static VertexDataStruct baseStone = CreateBaseConvexPolyhedron(1337);
return baseStone;
}
VertexRenderStruct StoneGroup::inflateOne(int index, float scaleModifier) VertexRenderStruct StoneGroup::inflateOne(int index, float scaleModifier)
{ {
static VertexDataStruct baseStone = CreateBaseConvexPolyhedron(1337); const VertexDataStruct& baseStone = GetBaseStone();
VertexRenderStruct result; VertexRenderStruct result;
@ -324,10 +308,11 @@ namespace ZL {
VertexDataStruct StoneGroup::inflateOneDataOnly(int index, float scaleModifier) VertexDataStruct StoneGroup::inflateOneDataOnly(int index, float scaleModifier)
{ {
static VertexDataStruct baseStone = CreateBaseConvexPolyhedron(1337); const VertexDataStruct& baseStone = GetBaseStone();
VertexDataStruct result; VertexDataStruct result;
// Pre-reserve logic? Optional but good.
// Since we don't know exact size (depends on stone count), maybe just standard push_back.
for (const auto& inst : allInstances[index]) { for (const auto& inst : allInstances[index]) {
Matrix3f rotMat = inst.rotation.toRotationMatrix(); Matrix3f rotMat = inst.rotation.toRotationMatrix();
@ -342,7 +327,13 @@ namespace ZL {
result.PositionData.push_back(rotMat * p + inst.position); result.PositionData.push_back(rotMat * p + inst.position);
result.NormalData.push_back(rotMat * n); result.NormalData.push_back(rotMat * n);
result.TexCoordData.push_back(baseStone.TexCoordData[j]);
if (j < baseStone.TexCoordData.size()) {
result.TexCoordData.push_back(baseStone.TexCoordData[j]);
}
else {
result.TexCoordData.push_back({ 0.f, 0.f });
}
} }
} }

57
src/render/Camera.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "render/Camera.h"
namespace ZL {
using Eigen::Vector3f;
using Eigen::Matrix3f;
using Eigen::Matrix4f;
Camera::Camera()
: position(Vector3f::Zero())
, rotation(Matrix3f::Identity())
{
}
void Camera::follow(const Vector3f& targetPos, const Matrix3f& targetRot, float distance, float height)
{
Vector3f offset(0.0f, height, distance);
Vector3f globalOffset = targetRot * offset;
position = targetPos + globalOffset;
rotation = targetRot;
}
void Camera::followOrbit(const Vector3f& targetPos, float yaw, float pitch, float distance, float height)
{
// Convert yaw/pitch to rotation matrix
// Yaw (Y-axis), Pitch (X-axis)
Eigen::AngleAxisf yawRot(yaw, Vector3f::UnitY());
Eigen::AngleAxisf pitchRot(pitch, Vector3f::UnitX());
// Combine rotations: Yaw * Pitch (order matters)
Eigen::Quaternionf q = yawRot * pitchRot;
rotation = q.toRotationMatrix();
Vector3f offset(0.0f, height, distance);
Vector3f globalOffset = rotation * offset;
position = targetPos + globalOffset;
}
Matrix4f Camera::getViewMatrix() const
{
// View Matrix = (Translate * Rotate)^-1
// = Rotate^-1 * Translate^-1
Matrix3f R_inv = rotation.transpose();
Vector3f T_inv = -position;
Matrix4f view = Matrix4f::Identity();
view.block<3,3>(0,0) = R_inv;
view.block<3,1>(0,3) = R_inv * T_inv;
return view;
}
}

25
src/render/Camera.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <Eigen/Dense>
namespace ZL {
class Camera {
public:
Camera();
// Обновление позиции камеры (например, слежение за кораблем)
void follow(const Eigen::Vector3f& targetPos, const Eigen::Matrix3f& targetRot, float distance, float height);
// Follow target with orbital rotation (yaw/pitch) independent of target rotation
void followOrbit(const Eigen::Vector3f& targetPos, float yaw, float pitch, float distance, float height);
// Получить матрицу вида (View Matrix) для шейдера
Eigen::Matrix4f getViewMatrix() const;
Eigen::Vector3f getPosition() const { return position; }
private:
Eigen::Vector3f position;
Eigen::Matrix3f rotation; // Ориентация камеры
};
}