Compare commits
No commits in common. "0a5a3b653d97e864495d9f6ab1a485e2580453f2" and "7a1961b5d1e024686124c7e9998f18924b9137d9" have entirely different histories.
0a5a3b653d
...
7a1961b5d1
@ -440,18 +440,6 @@ add_executable(space-game001
|
||||
SparkEmitter.h
|
||||
PlanetObject.cpp
|
||||
PlanetObject.h
|
||||
PlanetData.cpp
|
||||
PlanetData.h
|
||||
Perlin.cpp
|
||||
Perlin.h
|
||||
StoneObject.cpp
|
||||
StoneObject.h
|
||||
FrameBuffer.cpp
|
||||
FrameBuffer.h
|
||||
UiManager.cpp
|
||||
UiManager.h
|
||||
Projectile.h
|
||||
Projectile.cpp
|
||||
)
|
||||
|
||||
# Установка проекта по умолчанию для Visual Studio
|
||||
|
||||
@ -1,49 +0,0 @@
|
||||
#include "FrameBuffer.h"
|
||||
#include <iostream>
|
||||
#include "Environment.h"
|
||||
|
||||
namespace ZL {
|
||||
|
||||
FrameBuffer::FrameBuffer(int w, int h) : width(w), height(h) {
|
||||
// 1. Ñîçäàåì FBO
|
||||
glGenFramebuffers(1, &fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
|
||||
// 2. Ñîçäàåì òåêñòóðó, êóäà áóäåì "ôîòîãðàôèðîâàòü"
|
||||
glGenTextures(1, &textureID);
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
|
||||
// Óñòàíàâëèâàåì ïàðàìåòðû òåêñòóðû
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// 3. Ïðèâÿçûâàåì òåêñòóðó ê FBO
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
|
||||
|
||||
// Ïðîâåðêà ãîòîâíîñòè
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
std::cerr << "Error: Framebuffer is not complete!" << std::endl;
|
||||
}
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
FrameBuffer::~FrameBuffer() {
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteTextures(1, &textureID);
|
||||
}
|
||||
|
||||
void FrameBuffer::Bind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
glViewport(0, 0, width, height); // Âàæíî: óñòàíàâëèâàåì âüþïîðò ïîä ðàçìåð òåêñòóðû
|
||||
}
|
||||
|
||||
void FrameBuffer::Unbind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
// Çäåñü æåëàòåëüíî âîçâðàùàòü âüþïîðò ê ðàçìåðàì ýêðàíà,
|
||||
// íàïðèìåð, ÷åðåç Environment::width/height
|
||||
glViewport(0, 0, Environment::width, Environment::height);
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
@ -1,29 +0,0 @@
|
||||
#pragma once
|
||||
#include "OpenGlExtensions.h"
|
||||
#include <memory>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
class FrameBuffer {
|
||||
private:
|
||||
GLuint fbo = 0;
|
||||
GLuint textureID = 0;
|
||||
int width, height;
|
||||
|
||||
public:
|
||||
FrameBuffer(int w, int h);
|
||||
~FrameBuffer();
|
||||
|
||||
// Çàïðåùàåì êîïèðîâàíèå, êàê â VBOHolder
|
||||
FrameBuffer(const FrameBuffer&) = delete;
|
||||
FrameBuffer& operator=(const FrameBuffer&) = delete;
|
||||
|
||||
void Bind(); // Íà÷àòü ðåíäåð â ýòîò áóôåð
|
||||
void Unbind(); // Âåðíóòüñÿ ê îáû÷íîìó ðåíäåðó â ýêðàí
|
||||
|
||||
GLuint getTextureID() const { return textureID; }
|
||||
int getWidth() const { return width; }
|
||||
int getHeight() const { return height; }
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
384
Game.cpp
384
Game.cpp
@ -6,7 +6,6 @@
|
||||
#include <iostream>
|
||||
#include "TextureManager.h"
|
||||
#include "TextModel.h"
|
||||
#include "StoneObject.h"
|
||||
#include <random>
|
||||
#include <cmath>
|
||||
|
||||
@ -18,13 +17,13 @@ namespace ZL
|
||||
const char* CONST_ZIP_FILE = "";
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
Vector4f generateRandomQuaternion(std::mt19937& gen)
|
||||
{
|
||||
|
||||
// Ðàñïðåäåëåíèå äëÿ ãåíåðàöèè ñëó÷àéíûõ êîîðäèíàò êâàòåðíèîíà
|
||||
std::normal_distribution<> distrib(0.0, 1.0);
|
||||
|
||||
// Ãåíåðèðóåì ÷åòûðå ñëó÷àéíûõ ÷èñëà èç íîðìàëüíîãî ðàñïðåäåëåíèÿ N(0, 1).
|
||||
// Íîðìàëèçàöèÿ ýòîãî âåêòîðà äàåò ðàâíîìåðíîå ðàñïðåäåëåíèå ïî 4D-ñôåðå (ò.å. êâàòåðíèîí åäèíè÷íîé äëèíû).
|
||||
Vector4f randomQuat = {
|
||||
(float)distrib(gen),
|
||||
(float)distrib(gen),
|
||||
@ -36,18 +35,25 @@ namespace ZL
|
||||
}
|
||||
|
||||
|
||||
// --- Îñíîâíàÿ ôóíêöèÿ ãåíåðàöèè ---
|
||||
std::vector<BoxCoords> generateRandomBoxCoords(int N)
|
||||
{
|
||||
// Êîíñòàíòû
|
||||
const float MIN_DISTANCE = 3.0f;
|
||||
const float MIN_DISTANCE_SQUARED = MIN_DISTANCE * MIN_DISTANCE;
|
||||
const float MIN_DISTANCE_SQUARED = MIN_DISTANCE * MIN_DISTANCE; // Ðàáîòàåì ñ êâàäðàòîì ðàññòîÿíèÿ
|
||||
const float MIN_COORD = -100.0f;
|
||||
const float MAX_COORD = 100.0f;
|
||||
const int MAX_ATTEMPTS = 1000;
|
||||
std::vector<BoxCoords> boxCoordsArr;
|
||||
const int MAX_ATTEMPTS = 1000; // Îãðàíè÷åíèå íà êîëè÷åñòâî ïîïûòîê, ÷òîáû èçáåæàòü áåñêîíå÷íîãî öèêëà
|
||||
|
||||
std::vector<BoxCoords> boxCoordsArr;
|
||||
boxCoordsArr.reserve(N); // Ðåçåðâèðóåì ïàìÿòü
|
||||
|
||||
// 1. Èíèöèàëèçàöèÿ ãåíåðàòîðà ïñåâäîñëó÷àéíûõ ÷èñåë
|
||||
// Èñïîëüçóåì Mersenne Twister (mt19937) êàê âûñîêîêà÷åñòâåííûé ãåíåðàòîð
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
|
||||
// 2. Îïðåäåëåíèå ðàâíîìåðíîãî ðàñïðåäåëåíèÿ äëÿ êîîðäèíàò [MIN_COORD, MAX_COORD]
|
||||
std::uniform_real_distribution<> distrib(MIN_COORD, MAX_COORD);
|
||||
|
||||
int generatedCount = 0;
|
||||
@ -57,41 +63,51 @@ namespace ZL
|
||||
bool accepted = false;
|
||||
int attempts = 0;
|
||||
|
||||
// Ïîïûòêà íàéòè ïîäõîäÿùèå êîîðäèíàòû
|
||||
while (!accepted && attempts < MAX_ATTEMPTS)
|
||||
{
|
||||
// Ãåíåðèðóåì íîâûå ñëó÷àéíûå êîîðäèíàòû
|
||||
Vector3f newPos(
|
||||
(float)distrib(gen),
|
||||
(float)distrib(gen),
|
||||
(float)distrib(gen)
|
||||
);
|
||||
|
||||
accepted = true;
|
||||
// Ïðîâåðêà ðàññòîÿíèÿ äî âñåõ óæå ñóùåñòâóþùèõ îáúåêòîâ
|
||||
accepted = true; // Ïðåäïîëàãàåì, ÷òî ïîäõîäèò, ïîêà íå äîêàçàíî îáðàòíîå
|
||||
for (const auto& existingBox : boxCoordsArr)
|
||||
{
|
||||
|
||||
// Ðàñ÷åò âåêòîðà ðàçíîñòè
|
||||
Vector3f diff = newPos - existingBox.pos;
|
||||
|
||||
// Ðàñ÷åò êâàäðàòà ðàññòîÿíèÿ
|
||||
float distanceSquared = diff.squaredNorm();
|
||||
|
||||
// Åñëè êâàäðàò ðàññòîÿíèÿ ìåíüøå êâàäðàòà ìèíèìàëüíîãî ðàññòîÿíèÿ
|
||||
if (distanceSquared < MIN_DISTANCE_SQUARED)
|
||||
{
|
||||
accepted = false;
|
||||
break;
|
||||
accepted = false; // Îòêëîíÿåì, ñëèøêîì áëèçêî
|
||||
break; // Íåò ñìûñëà ïðîâåðÿòü äàëüøå, åñëè îäíî íàðóøåíèå íàéäåíî
|
||||
}
|
||||
}
|
||||
|
||||
if (accepted)
|
||||
{
|
||||
// 2. Ãåíåðèðóåì ñëó÷àéíûé êâàòåðíèîí
|
||||
Vector4f randomQuat = generateRandomQuaternion(gen);
|
||||
|
||||
// 3. Ïðåîáðàçóåì åãî â ìàòðèöó âðàùåíèÿ
|
||||
Matrix3f randomMatrix = QuatToMatrix(randomQuat);
|
||||
|
||||
// 4. Äîáàâëÿåì îáúåêò ñ íîâîé ñëó÷àéíîé ìàòðèöåé
|
||||
boxCoordsArr.emplace_back(BoxCoords{ newPos, randomMatrix });
|
||||
generatedCount++;
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
|
||||
// Åñëè ïðåâûøåíî ìàêñèìàëüíîå êîëè÷åñòâî ïîïûòîê, âûõîäèì èç öèêëà,
|
||||
// ÷òîáû èçáåæàòü çàâèñàíèÿ, åñëè N ñëèøêîì âåëèêî èëè äèàïàçîí ñëèøêîì ìàë.
|
||||
if (!accepted) {
|
||||
std::cerr << "Ïðåäóïðåæäåíèå: Íå óäàëîñü ñãåíåðèðîâàòü " << N << " îáúåêòîâ. Ñãåíåðèðîâàíî: " << generatedCount << std::endl;
|
||||
break;
|
||||
@ -107,10 +123,11 @@ namespace ZL
|
||||
, newTickCount(0)
|
||||
, lastTickCount(0)
|
||||
{
|
||||
projectiles.reserve(maxProjectiles);
|
||||
for (int i = 0; i < maxProjectiles; ++i) {
|
||||
projectiles.emplace_back(std::make_unique<Projectile>());
|
||||
}
|
||||
std::vector<Vector3f> emissionPoints = {
|
||||
Vector3f{-2.1f, 0.9f, 5.0f},
|
||||
Vector3f{2.1f, 0.9f, 5.0f}
|
||||
};
|
||||
sparkEmitter = SparkEmitter(emissionPoints, 100.0f);
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
@ -136,60 +153,15 @@ namespace ZL
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./shaders/defaultColor.vertex", "./shaders/defaultColor_web.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("env", "./shaders/env.vertex", "./shaders/env_web.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultAtmosphere", "./shaders/defaultAtmosphere.vertex", "./shaders/defaultAtmosphere.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultColorPlanet", "./shaders/defaultColorPlanet.vertex", "./shaders/defaultColorPlanet_web.fragment", CONST_ZIP_FILE);
|
||||
|
||||
#else
|
||||
renderer.shaderManager.AddShaderFromFiles("default", "./shaders/default.vertex", "./shaders/default_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./shaders/defaultColor_fog.vertex", "./shaders/defaultColor_fog_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("env", "./shaders/env_sky.vertex", "./shaders/env_sky_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultAtmosphere", "./shaders/defaultAtmosphere.vertex", "./shaders/defaultAtmosphere.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultColor2", "./shaders/defaultColor_fog2.vertex", "./shaders/defaultColor_fog2_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultColorStones", "./shaders/defaultColor_fog_stones.vertex", "./shaders/defaultColor_fog_stones_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("planetBake", "./shaders/planet_bake.vertex", "./shaders/planet_bake_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("planetStone", "./shaders/planet_stone.vertex", "./shaders/planet_stone_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("planetLand", "./shaders/planet_land.vertex", "./shaders/planet_land_desktop.fragment", CONST_ZIP_FILE);
|
||||
|
||||
#endif
|
||||
|
||||
bool cfgLoaded = sparkEmitter.loadFromJsonFile("../config/spark_config.json", renderer, CONST_ZIP_FILE);
|
||||
bool projCfgLoaded = projectileEmitter.loadFromJsonFile("../config/spark_projectile_config.json", renderer, CONST_ZIP_FILE);
|
||||
projectileEmitter.setEmissionPoints(std::vector<Vector3f>());
|
||||
uiManager.loadFromFile("../config/ui.json", renderer, CONST_ZIP_FILE);
|
||||
|
||||
uiManager.setButtonCallback("playButton", [this](const std::string& name) {
|
||||
std::cerr << "Play button pressed: " << name << std::endl;
|
||||
});
|
||||
|
||||
uiManager.setButtonCallback("exitButton", [](const std::string& name) {
|
||||
Environment::exitGameLoop = true;
|
||||
});
|
||||
|
||||
uiManager.setButtonCallback("settingsButton", [this](const std::string& name) {
|
||||
if (uiManager.pushMenuFromFile("../config/settings.json", this->renderer, CONST_ZIP_FILE)) {
|
||||
|
||||
uiManager.setButtonCallback("Opt1", [this](const std::string& n) {
|
||||
std::cerr << "Opt1 pressed: " << n << std::endl;
|
||||
});
|
||||
|
||||
uiManager.setButtonCallback("Opt2", [this](const std::string& n) {
|
||||
std::cerr << "Opt2 pressed: " << n << std::endl;
|
||||
});
|
||||
|
||||
uiManager.setButtonCallback("backButton", [this](const std::string& n) {
|
||||
uiManager.popMenu();
|
||||
});
|
||||
}
|
||||
else {
|
||||
std::cerr << "Failed to open settings menu" << std::endl;
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
cubemapTexture = std::make_shared<Texture>(
|
||||
std::array<TextureDataStruct, 6>{
|
||||
CreateTextureDataFromBmp24("./resources/sky/space_rt.bmp", CONST_ZIP_FILE),
|
||||
@ -205,16 +177,15 @@ namespace ZL
|
||||
cubemap.RefreshVBO();
|
||||
|
||||
//Load texture
|
||||
spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/DefaultMaterial_BaseColor_shine.png", CONST_ZIP_FILE));
|
||||
spaceshipBase = LoadFromTextFile02("./resources/spaceship006.txt", CONST_ZIP_FILE);
|
||||
spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/DefaultMaterial_BaseColor.png", CONST_ZIP_FILE));
|
||||
spaceshipBase = LoadFromTextFile02("./resources/spaceship005.txt", CONST_ZIP_FILE);
|
||||
spaceshipBase.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundY(M_PI / 2.0)));
|
||||
//spaceshipBase.Move(Vector3f{ -0.52998, -13, 0 });
|
||||
//spaceshipBase.Move(Vector3f{ -0.52998, -10, 10 });
|
||||
spaceshipBase.Move(Vector3f{ -0.52998, -10, 10 });
|
||||
|
||||
spaceship.AssignFrom(spaceshipBase);
|
||||
spaceship.RefreshVBO();
|
||||
|
||||
|
||||
//Boxes
|
||||
boxTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/box/box.png", CONST_ZIP_FILE));
|
||||
boxBase = LoadFromTextFile02("./resources/box/box.txt", CONST_ZIP_FILE);
|
||||
@ -226,80 +197,76 @@ namespace ZL
|
||||
for (int i = 0; i < boxCoordsArr.size(); i++)
|
||||
{
|
||||
boxRenderArr[i].AssignFrom(boxBase);
|
||||
//boxRenderArr[i].data = CreateBaseConvexPolyhedron(1999);
|
||||
boxRenderArr[i].RefreshVBO();
|
||||
}
|
||||
|
||||
if (!cfgLoaded)
|
||||
{
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
sparkTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/spark.png", CONST_ZIP_FILE));
|
||||
|
||||
sparkEmitter.setTexture(sparkTexture);
|
||||
|
||||
|
||||
/* buttonTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/button.png", CONST_ZIP_FILE));
|
||||
buttonTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/button.png", CONST_ZIP_FILE));
|
||||
|
||||
button.data.PositionData.push_back({ 100, 100, 0 });
|
||||
button.data.PositionData.push_back({ 100, 150, 0 });
|
||||
button.data.PositionData.push_back({ 300, 150, 0 });
|
||||
button.data.PositionData.push_back({ 100, 100, 0 });
|
||||
button.data.PositionData.push_back({ 300, 150, 0 });
|
||||
button.data.PositionData.push_back({ 300, 100, 0 });
|
||||
button.data.PositionData.push_back({ 100, 100, 0 });
|
||||
button.data.PositionData.push_back({ 100, 150, 0 });
|
||||
button.data.PositionData.push_back({ 300, 150, 0 });
|
||||
button.data.PositionData.push_back({ 100, 100, 0 });
|
||||
button.data.PositionData.push_back({ 300, 150, 0 });
|
||||
button.data.PositionData.push_back({ 300, 100, 0 });
|
||||
|
||||
button.data.TexCoordData.push_back({ 0,0 });
|
||||
button.data.TexCoordData.push_back({ 0,1 });
|
||||
button.data.TexCoordData.push_back({ 1,1 });
|
||||
button.data.TexCoordData.push_back({ 0,0 });
|
||||
button.data.TexCoordData.push_back({ 1,1 });
|
||||
button.data.TexCoordData.push_back({ 1,0 });
|
||||
button.data.TexCoordData.push_back({ 0,0 });
|
||||
button.data.TexCoordData.push_back({ 0,1 });
|
||||
button.data.TexCoordData.push_back({ 1,1 });
|
||||
button.data.TexCoordData.push_back({ 0,0 });
|
||||
button.data.TexCoordData.push_back({ 1,1 });
|
||||
button.data.TexCoordData.push_back({ 1,0 });
|
||||
|
||||
button.RefreshVBO();*/
|
||||
/*
|
||||
musicVolumeBarTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/musicVolumeBarTexture.png", CONST_ZIP_FILE));
|
||||
button.RefreshVBO();
|
||||
|
||||
musicVolumeBar.data.PositionData.push_back({ 1190, 100, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1190, 600, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1200, 600, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1190, 100, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1200, 600, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1200, 100, 0 });
|
||||
musicVolumeBarTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/musicVolumeBarTexture.png", CONST_ZIP_FILE));
|
||||
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 0,0 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 0,1 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 1,1 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 0,0 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 1,1 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 1,0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1190, 100, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1190, 600, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1200, 600, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1190, 100, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1200, 600, 0 });
|
||||
musicVolumeBar.data.PositionData.push_back({ 1200, 100, 0 });
|
||||
|
||||
musicVolumeBar.RefreshVBO();
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 0,0 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 0,1 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 1,1 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 0,0 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 1,1 });
|
||||
musicVolumeBar.data.TexCoordData.push_back({ 1,0 });
|
||||
|
||||
musicVolumeBar.RefreshVBO();
|
||||
|
||||
|
||||
musicVolumeBarButtonTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/musicVolumeBarButton.png", CONST_ZIP_FILE));
|
||||
musicVolumeBarButtonTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/musicVolumeBarButton.png", CONST_ZIP_FILE));
|
||||
|
||||
float musicVolumeBarButtonButtonCenterY = 350.0f;
|
||||
float musicVolumeBarButtonButtonCenterY = 350.0f;
|
||||
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX - musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY - musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX - musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY + musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX + musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY + musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX - musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY - musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX + musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY + musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX + musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY - musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX - musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY - musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX - musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY + musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX + musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY + musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX - musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY - musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX + musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY + musicVolumeBarButtonButtonRadius, 0 });
|
||||
musicVolumeBarButton.data.PositionData.push_back({ musicVolumeBarButtonButtonCenterX + musicVolumeBarButtonButtonRadius, musicVolumeBarButtonButtonCenterY - musicVolumeBarButtonButtonRadius, 0 });
|
||||
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 0,0 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 0,1 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 1,1 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 0,0 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 1,1 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 1,0 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 0,0 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 0,1 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 1,1 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 0,0 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 1,1 });
|
||||
musicVolumeBarButton.data.TexCoordData.push_back({ 1,0 });
|
||||
|
||||
musicVolumeBarButton.RefreshVBO();*/
|
||||
musicVolumeBarButton.RefreshVBO();
|
||||
renderer.InitOpenGL();
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
planetObject.init();
|
||||
|
||||
rockTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/rock.png", ""));
|
||||
|
||||
}
|
||||
|
||||
void Game::drawCubemap(float skyPercent)
|
||||
@ -359,18 +326,10 @@ namespace ZL
|
||||
renderer.LoadIdentity();
|
||||
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
|
||||
|
||||
renderer.TranslateMatrix({ 0, -Environment::zoom * 0.03f, 0 });
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(spaceship);
|
||||
|
||||
for (const auto& p : projectiles) {
|
||||
if (p && p->isActive()) {
|
||||
p->draw(renderer);
|
||||
}
|
||||
}
|
||||
|
||||
sparkEmitter.draw(renderer, Environment::zoom, Environment::width, Environment::height);
|
||||
projectileEmitter.draw(renderer, Environment::zoom, Environment::width, Environment::height);
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
@ -406,12 +365,10 @@ namespace ZL
|
||||
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
|
||||
renderer.RotateMatrix(Environment::inverseShipMatrix);
|
||||
renderer.TranslateMatrix(-Environment::shipPosition);
|
||||
renderer.TranslateMatrix({ 0.f, 0.f, 45000.f });
|
||||
renderer.TranslateMatrix(boxCoordsArr[i].pos);
|
||||
renderer.RotateMatrix(boxCoordsArr[i].m);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, boxTexture->getTexID());
|
||||
//glBindTexture(GL_TEXTURE_2D, rockTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(boxRenderArr[i]);
|
||||
|
||||
renderer.PopMatrix();
|
||||
@ -423,7 +380,7 @@ namespace ZL
|
||||
renderer.shaderManager.PopShader();
|
||||
CheckGlError();
|
||||
}
|
||||
/*void Game::UpdateVolumeKnob() {
|
||||
void Game::UpdateVolumeKnob() {
|
||||
float musicVolumeBarButtonButtonCenterY = volumeBarMinY + musicVolume * (volumeBarMaxY - volumeBarMinY);
|
||||
|
||||
auto& pos = musicVolumeBarButton.data.PositionData;
|
||||
@ -438,6 +395,7 @@ namespace ZL
|
||||
musicVolumeBarButton.RefreshVBO();
|
||||
|
||||
}
|
||||
|
||||
void Game::UpdateVolumeFromMouse(int mouseX, int mouseY) {
|
||||
|
||||
int uiX = mouseX;
|
||||
@ -453,8 +411,7 @@ namespace ZL
|
||||
if (t > 1.0f) t = 1.0f;
|
||||
musicVolume = t;
|
||||
UpdateVolumeKnob();
|
||||
}*/
|
||||
|
||||
}
|
||||
void Game::drawUI()
|
||||
{
|
||||
static const std::string defaultShaderName = "default";
|
||||
@ -463,6 +420,7 @@ namespace ZL
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
renderer.shaderManager.PushShader(defaultShaderName);
|
||||
@ -470,27 +428,26 @@ namespace ZL
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
|
||||
//renderer.PushProjectionMatrix(Environment::width, Environment::height, -1, 1);
|
||||
//renderer.PushMatrix();
|
||||
renderer.PushProjectionMatrix(Environment::width, Environment::height, -1, 1);
|
||||
renderer.PushMatrix();
|
||||
|
||||
//renderer.LoadIdentity();
|
||||
|
||||
//glBindTexture(GL_TEXTURE_2D, buttonTexture->getTexID());
|
||||
//renderer.DrawVertexRenderStruct(button);
|
||||
renderer.LoadIdentity();
|
||||
|
||||
|
||||
//glBindTexture(GL_TEXTURE_2D, musicVolumeBarTexture->getTexID());
|
||||
//renderer.DrawVertexRenderStruct(musicVolumeBar);
|
||||
glBindTexture(GL_TEXTURE_2D, buttonTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(button);
|
||||
|
||||
//glBindTexture(GL_TEXTURE_2D, musicVolumeBarButtonTexture->getTexID());
|
||||
//renderer.DrawVertexRenderStruct(musicVolumeBarButton);
|
||||
glBindTexture(GL_TEXTURE_2D, musicVolumeBarTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(musicVolumeBar);
|
||||
|
||||
//renderer.PopMatrix();
|
||||
//renderer.PopProjectionMatrix();
|
||||
glBindTexture(GL_TEXTURE_2D, musicVolumeBarButtonTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(musicVolumeBarButton);
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
renderer.DisableVertexAttribArray(vPositionName);
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
uiManager.draw(renderer);
|
||||
|
||||
renderer.shaderManager.PopShader();
|
||||
CheckGlError();
|
||||
}
|
||||
@ -510,8 +467,8 @@ namespace ZL
|
||||
CheckGlError();
|
||||
|
||||
float skyPercent = 0.0;
|
||||
float distance = planetObject.distanceToPlanetSurface(Environment::shipPosition);
|
||||
if (distance > 1900.f)
|
||||
float distance = planetObject.distanceToPlanetSurface();
|
||||
if (distance > 2000.f)
|
||||
{
|
||||
skyPercent = 0.0f;
|
||||
}
|
||||
@ -521,18 +478,17 @@ namespace ZL
|
||||
}
|
||||
else
|
||||
{
|
||||
skyPercent = (1900.f - distance) / 900.f;
|
||||
skyPercent = (2000.f - distance) / 1000.f;
|
||||
}
|
||||
|
||||
|
||||
drawCubemap(skyPercent);
|
||||
planetObject.draw(renderer);
|
||||
if (planetObject.distanceToPlanetSurface(Environment::shipPosition) > 100.f)
|
||||
if (planetObject.planetIsFar())
|
||||
{
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
drawShip();
|
||||
drawBoxes();
|
||||
//drawBoxes();
|
||||
|
||||
drawUI();
|
||||
|
||||
@ -553,13 +509,13 @@ namespace ZL
|
||||
|
||||
//gameObjects.updateScene(delta);
|
||||
sparkEmitter.update(static_cast<float>(delta));
|
||||
planetObject.update(static_cast<float>(delta));
|
||||
|
||||
if (Environment::tapDownHold) {
|
||||
|
||||
float diffx = Environment::tapDownCurrentPos.v[0] - Environment::tapDownStartPos.v[0];
|
||||
float diffy = Environment::tapDownCurrentPos.v[1] - Environment::tapDownStartPos.v[1];
|
||||
|
||||
|
||||
if (abs(diffy) > 5.0 || abs(diffx) > 5.0) //threshold
|
||||
{
|
||||
|
||||
@ -591,74 +547,14 @@ namespace ZL
|
||||
{
|
||||
Vector3f velocityDirection = { 0,0, -Environment::shipVelocity * delta / 1000.f };
|
||||
Vector3f velocityDirectionAdjusted = MultMatrixVector(Environment::shipMatrix, velocityDirection);
|
||||
|
||||
Environment::shipPosition = Environment::shipPosition + velocityDirectionAdjusted;
|
||||
}
|
||||
|
||||
for (auto& p : projectiles) {
|
||||
if (p && p->isActive()) {
|
||||
p->update(static_cast<float>(delta), renderer);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Vector3f> projCameraPoints;
|
||||
for (const auto& p : projectiles) {
|
||||
if (p && p->isActive()) {
|
||||
Vector3f worldPos = p->getPosition();
|
||||
Vector3f rel = worldPos - Environment::shipPosition;
|
||||
Vector3f camPos = MultMatrixVector(Environment::inverseShipMatrix, rel);
|
||||
projCameraPoints.push_back(camPos);
|
||||
}
|
||||
}
|
||||
if (!projCameraPoints.empty()) {
|
||||
projectileEmitter.setEmissionPoints(projCameraPoints);
|
||||
projectileEmitter.emit();
|
||||
}
|
||||
else {
|
||||
projectileEmitter.setEmissionPoints(std::vector<Vector3f>());
|
||||
}
|
||||
|
||||
std::vector<Vector3f> shipCameraPoints;
|
||||
for (const auto& lp : shipLocalEmissionPoints) {
|
||||
Vector3f adjusted = lp + Vector3f{ 0.0f, -Environment::zoom * 0.03f, 0.0f };
|
||||
shipCameraPoints.push_back(adjusted);
|
||||
}
|
||||
if (!shipCameraPoints.empty()) {
|
||||
sparkEmitter.setEmissionPoints(shipCameraPoints);
|
||||
}
|
||||
|
||||
sparkEmitter.update(static_cast<float>(delta));
|
||||
projectileEmitter.update(static_cast<float>(delta));
|
||||
|
||||
lastTickCount = newTickCount;
|
||||
}
|
||||
}
|
||||
|
||||
void Game::fireProjectiles() {
|
||||
std::vector<Vector3f> localOffsets = {
|
||||
Vector3f{ -1.5f, 0.9f, 5.0f },
|
||||
Vector3f{ 1.5f, 0.9f, 5.0f }
|
||||
};
|
||||
|
||||
const float projectileSpeed = 60.0f;
|
||||
const float lifeMs = 5000.0f;
|
||||
const float size = 0.5f;
|
||||
|
||||
Vector3f localForward = { 0,0,-1 };
|
||||
Vector3f worldForward = MultMatrixVector(Environment::shipMatrix, localForward).normalized();
|
||||
|
||||
for (const auto& lo : localOffsets) {
|
||||
Vector3f worldPos = Environment::shipPosition + MultMatrixVector(Environment::shipMatrix, lo);
|
||||
Vector3f worldVel = worldForward * projectileSpeed;
|
||||
|
||||
for (auto& p : projectiles) {
|
||||
if (!p->isActive()) {
|
||||
p->init(worldPos, worldVel, lifeMs, size, projectileTexture, renderer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game::render() {
|
||||
SDL_GL_MakeCurrent(ZL::Environment::window, glContext);
|
||||
ZL::CheckGlError();
|
||||
@ -676,73 +572,51 @@ namespace ZL
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
Environment::exitGameLoop = true;
|
||||
|
||||
}
|
||||
else if (event.type == SDL_MOUSEBUTTONDOWN) {
|
||||
// 1. Îáðàáîòêà íàæàòèÿ êíîïêè ìûøè
|
||||
|
||||
int mx = event.button.x;
|
||||
int my = event.button.y;
|
||||
|
||||
std::cout << mx << " " << my << '\n';
|
||||
int uiX = mx;
|
||||
int uiY = Environment::height - my;
|
||||
|
||||
uiManager.onMouseDown(uiX, uiY);
|
||||
|
||||
bool uiHandled = false;
|
||||
|
||||
if (event.button.button == SDL_BUTTON_LEFT && !uiManager.isUiInteraction()) {
|
||||
uint64_t now = SDL_GetTicks64();
|
||||
if (now - lastProjectileFireTime >= static_cast<uint64_t>(projectileCooldownMs)) {
|
||||
lastProjectileFireTime = now;
|
||||
fireProjectiles();
|
||||
}
|
||||
if (uiX >= volumeBarMinX - 40 && uiX <= volumeBarMaxX + 40 &&
|
||||
uiY >= volumeBarMinY - 40 && uiY <= volumeBarMaxY + 40) {
|
||||
isDraggingVolume = true;
|
||||
UpdateVolumeFromMouse(mx, my);
|
||||
}
|
||||
|
||||
for (const auto& button : uiManager.findButton("") ? std::vector<std::shared_ptr<UiButton>>{} : std::vector<std::shared_ptr<UiButton>>{}) {
|
||||
(void)button;
|
||||
}
|
||||
|
||||
auto pressedSlider = [&]() -> std::shared_ptr<UiSlider> {
|
||||
for (const auto& slider : uiManager.findSlider("") ? std::vector<std::shared_ptr<UiSlider>>{} : std::vector<std::shared_ptr<UiSlider>>{}) {
|
||||
(void)slider;
|
||||
}
|
||||
return nullptr;
|
||||
}();
|
||||
|
||||
if (!uiManager.isUiInteraction()) {
|
||||
else {
|
||||
Environment::tapDownHold = true;
|
||||
// Êîîðäèíàòû íà÷àëüíîãî íàæàòèÿ
|
||||
Environment::tapDownStartPos.v[0] = mx;
|
||||
Environment::tapDownStartPos.v[1] = my;
|
||||
Environment::tapDownStartPos.v[0] = event.button.x;
|
||||
Environment::tapDownStartPos.v[1] = event.button.y;
|
||||
// Íà÷àëüíàÿ ïîçèöèÿ òàêæå ñòàíîâèòñÿ òåêóùåé
|
||||
Environment::tapDownCurrentPos.v[0] = mx;
|
||||
Environment::tapDownCurrentPos.v[1] = my;
|
||||
Environment::tapDownCurrentPos.v[0] = event.button.x;
|
||||
Environment::tapDownCurrentPos.v[1] = event.button.y;
|
||||
}
|
||||
|
||||
}
|
||||
else if (event.type == SDL_MOUSEBUTTONUP) {
|
||||
// 2. Îáðàáîòêà îòïóñêàíèÿ êíîïêè ìûøè
|
||||
int mx = event.button.x;
|
||||
int my = event.button.y;
|
||||
int uiX = mx;
|
||||
int uiY = Environment::height - my;
|
||||
|
||||
uiManager.onMouseUp(uiX, uiY);
|
||||
|
||||
if (!uiManager.isUiInteraction()) {
|
||||
Environment::tapDownHold = false;
|
||||
}
|
||||
isDraggingVolume = false;
|
||||
Environment::tapDownHold = false;
|
||||
}
|
||||
else if (event.type == SDL_MOUSEMOTION) {
|
||||
// 3. Îáðàáîòêà ïåðåìåùåíèÿ ìûøè
|
||||
int mx = event.motion.x;
|
||||
int my = event.motion.y;
|
||||
int uiX = mx;
|
||||
int uiY = Environment::height - my;
|
||||
|
||||
uiManager.onMouseMove(uiX, uiY);
|
||||
|
||||
if (Environment::tapDownHold && !uiManager.isUiInteraction()) {
|
||||
Environment::tapDownCurrentPos.v[0] = mx;
|
||||
Environment::tapDownCurrentPos.v[1] = my;
|
||||
if (isDraggingVolume) {
|
||||
// Äâèãàåì ìûøü ïî ñëàéäåðó — ìåíÿåì ãðîìêîñòü è ïîçèöèþ êðóæêà
|
||||
UpdateVolumeFromMouse(mx, my);
|
||||
}
|
||||
if (Environment::tapDownHold) {
|
||||
// Îáíîâëåíèå òåêóùåé ïîçèöèè, åñëè êíîïêà óäåðæèâàåòñÿ
|
||||
Environment::tapDownCurrentPos.v[0] = event.motion.x;
|
||||
Environment::tapDownCurrentPos.v[1] = event.motion.y;
|
||||
}
|
||||
}
|
||||
else if (event.type == SDL_MOUSEWHEEL) {
|
||||
|
||||
40
Game.h
40
Game.h
@ -6,8 +6,6 @@
|
||||
#include "TextureManager.h"
|
||||
#include "SparkEmitter.h"
|
||||
#include "PlanetObject.h"
|
||||
#include "UiManager.h"
|
||||
#include "Projectile.h"
|
||||
|
||||
namespace ZL {
|
||||
|
||||
@ -39,8 +37,6 @@ namespace ZL {
|
||||
void drawBoxes();
|
||||
void drawUI();
|
||||
|
||||
void fireProjectiles();
|
||||
|
||||
SDL_Window* window;
|
||||
SDL_GLContext glContext;
|
||||
Renderer renderer;
|
||||
@ -48,36 +44,31 @@ namespace ZL {
|
||||
size_t newTickCount;
|
||||
size_t lastTickCount;
|
||||
|
||||
std::shared_ptr<Texture> rockTexture;
|
||||
|
||||
|
||||
|
||||
std::vector<BoxCoords> boxCoordsArr;
|
||||
std::vector<VertexRenderStruct> boxRenderArr;
|
||||
|
||||
|
||||
//std::shared_ptr<Texture> buttonTexture;
|
||||
//VertexRenderStruct button;
|
||||
std::shared_ptr<Texture> buttonTexture;
|
||||
VertexRenderStruct button;
|
||||
|
||||
//std::shared_ptr<Texture> musicVolumeBarTexture;
|
||||
//VertexRenderStruct musicVolumeBar;
|
||||
std::shared_ptr<Texture> musicVolumeBarTexture;
|
||||
VertexRenderStruct musicVolumeBar;
|
||||
|
||||
//std::shared_ptr<Texture> musicVolumeBarButtonTexture;
|
||||
//VertexRenderStruct musicVolumeBarButton;
|
||||
std::shared_ptr<Texture> musicVolumeBarButtonTexture;
|
||||
VertexRenderStruct musicVolumeBarButton;
|
||||
|
||||
|
||||
//bool isDraggingVolume = false;
|
||||
|
||||
bool isDraggingVolume = false;
|
||||
float musicVolume = 0.0f;
|
||||
float volumeBarMinX = 1190.0f;
|
||||
float volumeBarMaxX = 1200.0f;
|
||||
float volumeBarMinY = 100.0f;
|
||||
float volumeBarMaxY = 600.0f;
|
||||
//float musicVolumeBarButtonButtonCenterX = 1195.0f;
|
||||
//float musicVolumeBarButtonButtonRadius = 25.0f;
|
||||
//void UpdateVolumeFromMouse(int mouseX, int mouseY);
|
||||
//void UpdateVolumeKnob();
|
||||
|
||||
float musicVolumeBarButtonButtonCenterX = 1195.0f;
|
||||
float musicVolumeBarButtonButtonRadius = 25.0f;
|
||||
void UpdateVolumeFromMouse(int mouseX, int mouseY);
|
||||
void UpdateVolumeKnob();
|
||||
|
||||
static const size_t CONST_TIMER_INTERVAL = 10;
|
||||
static const size_t CONST_MAX_TIME_INTERVAL = 1000;
|
||||
@ -94,16 +85,7 @@ namespace ZL {
|
||||
VertexDataStruct boxBase;
|
||||
|
||||
SparkEmitter sparkEmitter;
|
||||
SparkEmitter projectileEmitter;
|
||||
PlanetObject planetObject;
|
||||
UiManager uiManager;
|
||||
|
||||
std::vector<std::unique_ptr<Projectile>> projectiles;
|
||||
std::shared_ptr<Texture> projectileTexture;
|
||||
float projectileCooldownMs = 500.0f;
|
||||
uint64_t lastProjectileFireTime = 0;
|
||||
int maxProjectiles = 32;
|
||||
std::vector<Vector3f> shipLocalEmissionPoints;
|
||||
};
|
||||
|
||||
|
||||
|
||||
73
Perlin.cpp
73
Perlin.cpp
@ -1,73 +0,0 @@
|
||||
#include "Perlin.h"
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
PerlinNoise::PerlinNoise() {
|
||||
p.resize(256);
|
||||
std::iota(p.begin(), p.end(), 0);
|
||||
// Ïåðåìåøèâàåì äëÿ ñëó÷àéíîñòè (ìîæíî çàäàòü seed)
|
||||
std::default_random_engine engine(77777);
|
||||
std::shuffle(p.begin(), p.end(), engine);
|
||||
p.insert(p.end(), p.begin(), p.end()); // Äóáëèðóåì äëÿ ïåðåïîëíåíèÿ
|
||||
}
|
||||
|
||||
PerlinNoise::PerlinNoise(uint64_t seed) {
|
||||
p.resize(256);
|
||||
std::iota(p.begin(), p.end(), 0);
|
||||
// Ïåðåìåøèâàåì äëÿ ñëó÷àéíîñòè (èñïîëüçóåì ïåðåäàííûé seed)
|
||||
std::default_random_engine engine(static_cast<unsigned int>(seed));
|
||||
std::shuffle(p.begin(), p.end(), engine);
|
||||
p.insert(p.end(), p.begin(), p.end()); // Äóáëèðóåì äëÿ ïåðåïîëíåíèÿ
|
||||
}
|
||||
|
||||
float PerlinNoise::fade(float t) { return t * t * t * (t * (t * 6 - 15) + 10); }
|
||||
|
||||
float PerlinNoise::lerp(float t, float a, float b) { return a + t * (b - a); }
|
||||
|
||||
float PerlinNoise::grad(int hash, float x, float y, float z) {
|
||||
int h = hash & 15;
|
||||
float u = h < 8 ? x : y;
|
||||
float v = h < 4 ? y : (h == 12 || h == 14 ? x : z);
|
||||
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
|
||||
}
|
||||
|
||||
float PerlinNoise::noise(float x, float y, float z) {
|
||||
int X = (int)floor(x) & 255;
|
||||
int Y = (int)floor(y) & 255;
|
||||
int Z = (int)floor(z) & 255;
|
||||
|
||||
x -= floor(x);
|
||||
y -= floor(y);
|
||||
z -= floor(z);
|
||||
|
||||
float u = fade(x);
|
||||
float v = fade(y);
|
||||
float w = fade(z);
|
||||
|
||||
int A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z;
|
||||
int B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z;
|
||||
|
||||
return lerp(w, lerp(v, lerp(u, grad(p[AA], x, y, z), grad(p[BA], x - 1, y, z)),
|
||||
lerp(u, grad(p[AB], x, y - 1, z), grad(p[BB], x - 1, y - 1, z))),
|
||||
lerp(v, lerp(u, grad(p[AA + 1], x, y, z - 1), grad(p[BA + 1], x - 1, y, z - 1)),
|
||||
lerp(u, grad(p[AB + 1], x, y - 1, z - 1), grad(p[BB + 1], x - 1, y - 1, z - 1))));
|
||||
}
|
||||
|
||||
float PerlinNoise::getSurfaceHeight(Vector3f pos, float noiseCoeff) {
|
||||
// ×àñòîòà øóìà (÷åì áîëüøå, òåì áîëüøå "õîëìîâ")
|
||||
float frequency = 7.0f;
|
||||
|
||||
// Ïîëó÷àåì çíà÷åíèå øóìà (îáû÷íî îò -1 äî 1)
|
||||
float noiseValue = noise(pos.v[0] * frequency, pos.v[1] * frequency, pos.v[2] * frequency);
|
||||
|
||||
// Ìàñøòàáèðóåì: õîòèì îòêëîíåíèå îò 1.0 äî 1.1 (ïðèìåðíî)
|
||||
float height = 1.0f + (noiseValue * noiseCoeff);
|
||||
|
||||
return height;
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
24
Perlin.h
24
Perlin.h
@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include "ZLMath.h"
|
||||
|
||||
namespace ZL {
|
||||
|
||||
class PerlinNoise {
|
||||
std::vector<int> p;
|
||||
public:
|
||||
PerlinNoise();
|
||||
PerlinNoise(uint64_t seed);
|
||||
|
||||
float fade(float t);
|
||||
float lerp(float t, float a, float b);
|
||||
float grad(int hash, float x, float y, float z);
|
||||
|
||||
float noise(float x, float y, float z);
|
||||
|
||||
float getSurfaceHeight(Vector3f pos, float noiseCoeff);
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
493
PlanetData.cpp
493
PlanetData.cpp
@ -1,493 +0,0 @@
|
||||
#include "PlanetData.h"
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
const float PlanetData::PLANET_RADIUS = 20000.f;
|
||||
const Vector3f PlanetData::PLANET_CENTER_OFFSET = Vector3f{ 0.f, 0.f, 0.0f };
|
||||
|
||||
// --- Êîíñòàíòû äèàïàçîíîâ (ïåðåíåñåíû èç PlanetObject.cpp) ---
|
||||
|
||||
VertexID generateEdgeID(const VertexID& id1, const VertexID& id2) {
|
||||
return id1 < id2 ? id1 + "_" + id2 : id2 + "_" + id1;
|
||||
}
|
||||
|
||||
// Âñïîìîãàòåëüíàÿ ôóíêöèÿ äëÿ ïðîåêöèè (ëîêàëüíàÿ)
|
||||
static Vector3f projectPointOnPlane(const Vector3f& P, const Vector3f& A, const Vector3f& B, const Vector3f& C) {
|
||||
Vector3f AB = B + A * (-1.0f);
|
||||
Vector3f AC = C + A * (-1.0f);
|
||||
Vector3f N = AB.cross(AC).normalized();
|
||||
Vector3f AP = P + A * (-1.0f);
|
||||
float distance = N.dot(AP);
|
||||
return P + N * (-distance);
|
||||
}
|
||||
|
||||
PlanetData::PlanetData()
|
||||
: perlin(77777)
|
||||
, colorPerlin(123123)
|
||||
, currentLod(0)
|
||||
{
|
||||
currentLod = planetMeshLods.size() - 1; // Start with max LOD
|
||||
|
||||
initialVertexMap = {
|
||||
{{ 0.0f, 1.0f, 0.0f}, "A"},
|
||||
{{ 0.0f, -1.0f, 0.0f}, "B"},
|
||||
{{ 1.0f, 0.0f, 0.0f}, "C"},
|
||||
{{-1.0f, 0.0f, 0.0f}, "D"},
|
||||
{{ 0.0f, 0.0f, 1.0f}, "E"},
|
||||
{{ 0.0f, 0.0f, -1.0f}, "F"}
|
||||
};
|
||||
}
|
||||
|
||||
void PlanetData::init() {
|
||||
for (int i = 0; i < planetMeshLods.size(); i++) {
|
||||
planetMeshLods[i] = generateSphere(i, 0.025f);
|
||||
planetMeshLods[i].Scale(PLANET_RADIUS);
|
||||
planetMeshLods[i].Move(PLANET_CENTER_OFFSET);
|
||||
}
|
||||
|
||||
for (int i = 0; i < planetMeshLodsNoDist.size(); i++) {
|
||||
planetMeshLodsNoDist[i] = generateSphere(i, 0);
|
||||
planetMeshLodsNoDist[i].Scale(PLANET_RADIUS);
|
||||
planetMeshLodsNoDist[i].Move(PLANET_CENTER_OFFSET);
|
||||
}
|
||||
|
||||
planetAtmosphereLod = generateSphere(5, 0);
|
||||
planetAtmosphereLod.Scale(PLANET_RADIUS * 1.03);
|
||||
planetAtmosphereLod.Move(PLANET_CENTER_OFFSET);
|
||||
}
|
||||
|
||||
const LodLevel& PlanetData::getLodLevel(int level) const {
|
||||
return planetMeshLods.at(level);
|
||||
}
|
||||
|
||||
const LodLevel& PlanetData::getLodLevelNoDist(int level) const {
|
||||
return planetMeshLodsNoDist.at(level);
|
||||
}
|
||||
|
||||
const LodLevel& PlanetData::getAtmosphereLod() const {
|
||||
return planetAtmosphereLod;
|
||||
}
|
||||
|
||||
int PlanetData::getCurrentLodIndex() const {
|
||||
return currentLod;
|
||||
}
|
||||
|
||||
int PlanetData::getMaxLodIndex() const {
|
||||
return static_cast<int>(planetMeshLods.size() - 1);
|
||||
}
|
||||
|
||||
std::pair<float, float> PlanetData::calculateZRange(float dToPlanetSurface) {
|
||||
|
||||
|
||||
float currentZNear;
|
||||
float currentZFar;
|
||||
float alpha;
|
||||
|
||||
if (dToPlanetSurface > 2000)
|
||||
{
|
||||
currentZNear = 1000;
|
||||
currentZFar = currentZNear * 100;
|
||||
}
|
||||
else if (dToPlanetSurface > 1200)
|
||||
{
|
||||
currentZNear = 500;
|
||||
currentZFar = currentZNear * 100;
|
||||
}
|
||||
else if (dToPlanetSurface > 650)
|
||||
{
|
||||
currentZNear = 250;
|
||||
currentZFar = currentZNear * 100;
|
||||
}
|
||||
else if (dToPlanetSurface > 160)
|
||||
{
|
||||
currentZNear = 125;
|
||||
currentZFar = currentZNear * 150;
|
||||
}
|
||||
else if (dToPlanetSurface > 100)
|
||||
{
|
||||
currentZNear = 65;
|
||||
currentZFar = currentZNear * 170;
|
||||
}
|
||||
else if (dToPlanetSurface > 40)
|
||||
{
|
||||
currentZNear = 32;
|
||||
currentZFar = 10000.f;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentZNear = 16;
|
||||
currentZFar = 5000.f;
|
||||
}
|
||||
|
||||
return { currentZNear, currentZFar };
|
||||
}
|
||||
|
||||
|
||||
float PlanetData::distanceToPlanetSurface(const Vector3f& viewerPosition) {
|
||||
Vector3f shipLocalPosition = viewerPosition - PLANET_CENTER_OFFSET;
|
||||
|
||||
// Èñïîëüçóåì getTrianglesUnderCamera äëÿ ïîèñêà
|
||||
// ÂÍÈÌÀÍÈÅ: Çäåñü ðåêóðñèÿ ëîãèêè. Ìåòîä getTrianglesUnderCamera èñïîëüçóåò âíóòðè viewerPosition.
|
||||
// ×òîáû íå äóáëèðîâàòü êîä, ïåðåïèøåì ëîãèêó ïîèñêà çäåñü èëè áóäåì èñïîëüçîâàòü óæå íàéäåííûé ðàíåå.
|
||||
// Äëÿ ïðîñòîòû îñòàâèì ëîãèêó êàê â îðèãèíàëå, íî àäàïòèðîâàííóþ.
|
||||
|
||||
// ÂÀÆÍÎ: getTrianglesUnderCamera - ýòî "òÿæåëàÿ" ôóíêöèÿ. Â îðèãèíàëå îíà âûçûâàëàñü âíóòðè distanceToPlanetSurface.
|
||||
// Íî îíà òðåáóåò LOD. Èñïîëüçóåì MAX LOD äëÿ òî÷íîñòè.
|
||||
|
||||
// Ðåêóðñèâíûé ïîèñê òðåóãîëüíèêîâ ïîä êàìåðîé.
|
||||
// Çäåñü íàì íóæíî ðåàëèçîâàòü àíàëîã triangleUnderCamera, íî íå çàâèñÿùèé îò ãëîáàëüíîãî ñîñòîÿíèÿ.
|
||||
// Â îðèãèíàëå ôóíêöèÿ triangleUnderCamera áûëà ðåêóðñèâíîé.
|
||||
// Íèæå ÿ ðåàëèçóþ èòåðàòèâíûé èëè ðåêóðñèâíûé ïîäõîä âíóòðè getTrianglesUnderCamera.
|
||||
|
||||
// Âðåìåííîå ðåøåíèå: ïîëíàÿ êîïèÿ ëîãèêè ïîèñêà òðåóãîëüíèêà
|
||||
// Íî íàì íóæåí äîñòóï ê ìåòîäó.
|
||||
std::vector<int> targetTriangles = getTrianglesUnderCamera(viewerPosition);
|
||||
|
||||
if (targetTriangles.empty()) {
|
||||
return (shipLocalPosition.length() - PLANET_RADIUS);
|
||||
}
|
||||
|
||||
float lowestDistance;
|
||||
|
||||
int tri_index = targetTriangles[0];
|
||||
const auto& posData = planetMeshLods[currentLod].vertexData.PositionData;
|
||||
|
||||
size_t data_index = tri_index * 3;
|
||||
if (data_index + 2 >= posData.size()) {
|
||||
return (shipLocalPosition.length() - PLANET_RADIUS);
|
||||
}
|
||||
|
||||
const Vector3f& A = posData[data_index];
|
||||
const Vector3f& B = posData[data_index + 1];
|
||||
const Vector3f& C = posData[data_index + 2];
|
||||
|
||||
Vector3f P_proj = projectPointOnPlane(shipLocalPosition, A, B, C);
|
||||
Vector3f P_closest = P_proj;
|
||||
|
||||
lowestDistance = (shipLocalPosition - P_closest).length();
|
||||
|
||||
if (targetTriangles.size() <= 1)
|
||||
{
|
||||
return lowestDistance;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < targetTriangles.size(); i++)
|
||||
{
|
||||
int tri_index = targetTriangles[i];
|
||||
const auto& posData = planetMeshLods[currentLod].vertexData.PositionData;
|
||||
|
||||
size_t data_index = tri_index * 3;
|
||||
if (data_index + 2 >= posData.size()) {
|
||||
return (shipLocalPosition.length() - PLANET_RADIUS);
|
||||
}
|
||||
|
||||
const Vector3f& A = posData[data_index];
|
||||
const Vector3f& B = posData[data_index + 1];
|
||||
const Vector3f& C = posData[data_index + 2];
|
||||
|
||||
Vector3f P_proj = projectPointOnPlane(shipLocalPosition, A, B, C);
|
||||
Vector3f P_closest = P_proj;
|
||||
|
||||
if (lowestDistance < (shipLocalPosition - P_closest).length())
|
||||
{
|
||||
lowestDistance = (shipLocalPosition - P_closest).length();
|
||||
}
|
||||
}
|
||||
|
||||
return lowestDistance;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Ðåàëèçàöèÿ getTrianglesUnderCamera (áûâøàÿ triangleUnderCamera) ---
|
||||
// Âñïîìîãàòåëüíàÿ ðåêóðñèâíàÿ ôóíêöèÿ, ñêðûòàÿ îò ïóáëè÷íîãî API
|
||||
static std::vector<int> recursiveTriangleSearch(int lod, const Vector3f& pos, const std::array<LodLevel, MAX_LOD_LEVELS>& meshes) {
|
||||
std::vector<int> r;
|
||||
// Ëîãèêà óðîâíÿ 0 (áàçîâûé îêòàýäð)
|
||||
if (lod == 0) {
|
||||
if (pos.v[1] >= 0) {
|
||||
if (pos.v[0] >= 0 && pos.v[2] >= 0) r.push_back(0);
|
||||
if (pos.v[0] >= 0 && pos.v[2] <= 0) r.push_back(1);
|
||||
if (pos.v[0] <= 0 && pos.v[2] <= 0) r.push_back(2);
|
||||
if (pos.v[0] <= 0 && pos.v[2] >= 0) r.push_back(3);
|
||||
}
|
||||
if (pos.v[1] <= 0) {
|
||||
if (pos.v[0] >= 0 && pos.v[2] >= 0) r.push_back(4);
|
||||
if (pos.v[0] <= 0 && pos.v[2] >= 0) r.push_back(5);
|
||||
if (pos.v[0] <= 0 && pos.v[2] <= 0) r.push_back(6);
|
||||
if (pos.v[0] >= 0 && pos.v[2] <= 0) r.push_back(7);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Ðåêóðñèâíûé øàã
|
||||
std::vector<int> r0 = recursiveTriangleSearch(lod - 1, pos, meshes);
|
||||
for (int tri0 : r0) {
|
||||
Vector3f a = meshes[lod - 1].vertexData.PositionData[tri0 * 3];
|
||||
Vector3f b = meshes[lod - 1].vertexData.PositionData[tri0 * 3 + 1];
|
||||
Vector3f c = meshes[lod - 1].vertexData.PositionData[tri0 * 3 + 2];
|
||||
|
||||
std::vector<int> result = PlanetData::find_sub_triangle_spherical(a, b, c, pos);
|
||||
for (int trix : result) {
|
||||
r.push_back(tri0 * 4 + trix);
|
||||
}
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
std::vector<int> PlanetData::getTrianglesUnderCamera(const Vector3f& viewerPosition) {
|
||||
// Âûçûâàåì ðåêóðñèþ ñ òåêóùèì LOD
|
||||
return recursiveTriangleSearch(currentLod, viewerPosition, planetMeshLods);
|
||||
}
|
||||
|
||||
// --- Îñòàëüíûå ìåòîäû (check_spherical_side, generateSphere è ò.ä.) ---
|
||||
// (Êîïèðóéòå ðåàëèçàöèþ èç ñòàðîãî PlanetObject.cpp,
|
||||
// çàìåíÿÿ îáðàùåíèÿ ê Environment::shipPosition íà àðãóìåíòû, åñëè íóæíî,
|
||||
// è èñïîëüçóÿ this->planetMeshLods)
|
||||
|
||||
float PlanetData::check_spherical_side(const Vector3f& V1, const Vector3f& V2, const Vector3f& P, const Vector3f& V3, float epsilon) {
|
||||
Vector3f N_plane = V1.cross(V2);
|
||||
float sign_P = P.dot(N_plane);
|
||||
float sign_V3 = V3.dot(N_plane);
|
||||
return sign_P * sign_V3;
|
||||
}
|
||||
|
||||
bool PlanetData::is_inside_spherical_triangle(const Vector3f& P, const Vector3f& V1, const Vector3f& V2, const Vector3f& V3, float epsilon) {
|
||||
if (check_spherical_side(V1, V2, P, V3, epsilon) < -epsilon) return false;
|
||||
if (check_spherical_side(V2, V3, P, V1, epsilon) < -epsilon) return false;
|
||||
if (check_spherical_side(V3, V1, P, V2, epsilon) < -epsilon) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<int> PlanetData::find_sub_triangle_spherical(const Vector3f& a_orig, const Vector3f& b_orig, const Vector3f& c_orig, const Vector3f& px_orig) {
|
||||
const float EPSILON = 1e-6f;
|
||||
std::vector<int> result;
|
||||
const Vector3f a = a_orig.normalized();
|
||||
const Vector3f b = b_orig.normalized();
|
||||
const Vector3f c = c_orig.normalized();
|
||||
const Vector3f pxx_normalized = px_orig.normalized();
|
||||
const Vector3f m_ab = ((a + b) * 0.5f).normalized();
|
||||
const Vector3f m_bc = ((b + c) * 0.5f).normalized();
|
||||
const Vector3f m_ac = ((a + c) * 0.5f).normalized();
|
||||
|
||||
if (is_inside_spherical_triangle(pxx_normalized, a, m_ab, m_ac, EPSILON)) result.push_back(0);
|
||||
if (is_inside_spherical_triangle(pxx_normalized, m_ab, b, m_bc, EPSILON)) result.push_back(1);
|
||||
if (is_inside_spherical_triangle(pxx_normalized, m_ac, m_bc, c, EPSILON)) result.push_back(2);
|
||||
if (is_inside_spherical_triangle(pxx_normalized, m_ab, m_bc, m_ac, EPSILON)) result.push_back(3);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<Triangle> PlanetData::subdivideTriangles(const std::vector<Triangle>& input, float noiseCoeff) {
|
||||
std::vector<Triangle> output;
|
||||
|
||||
for (const auto& t : input) {
|
||||
// Âåðøèíû è èõ ID
|
||||
const Vector3f& a = t.data[0];
|
||||
const Vector3f& b = t.data[1];
|
||||
const Vector3f& c = t.data[2];
|
||||
const VertexID& id_a = t.ids[0];
|
||||
const VertexID& id_b = t.ids[1];
|
||||
const VertexID& id_c = t.ids[2];
|
||||
|
||||
// 1. Âû÷èñëÿåì ñåðåäèíû (êîîðäèíàòû)
|
||||
Vector3f m_ab = ((a + b) * 0.5f).normalized();
|
||||
Vector3f m_bc = ((b + c) * 0.5f).normalized();
|
||||
Vector3f m_ac = ((a + c) * 0.5f).normalized();
|
||||
|
||||
Vector3f pm_ab = m_ab * perlin.getSurfaceHeight(m_ab, noiseCoeff);
|
||||
Vector3f pm_bc = m_bc * perlin.getSurfaceHeight(m_bc, noiseCoeff);
|
||||
Vector3f pm_ac = m_ac * perlin.getSurfaceHeight(m_ac, noiseCoeff);
|
||||
|
||||
// 2. Âû÷èñëÿåì ID íîâûõ âåðøèí
|
||||
VertexID id_mab = generateEdgeID(id_a, id_b);
|
||||
VertexID id_mbc = generateEdgeID(id_b, id_c);
|
||||
VertexID id_mac = generateEdgeID(id_a, id_c);
|
||||
|
||||
// 3. Ôîðìèðóåì 4 íîâûõ òðåóãîëüíèêà
|
||||
output.emplace_back(Triangle{ {a, pm_ab, pm_ac}, {id_a, id_mab, id_mac} }); // 0
|
||||
output.emplace_back(Triangle{ {pm_ab, b, pm_bc}, {id_mab, id_b, id_mbc} }); // 1
|
||||
output.emplace_back(Triangle{ {pm_ac, pm_bc, c}, {id_mac, id_mbc, id_c} }); // 2
|
||||
output.emplace_back(Triangle{ {pm_ab, pm_bc, pm_ac}, {id_mab, id_mbc, id_mac} }); // 3
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
LodLevel PlanetData::trianglesToVertices(const std::vector<Triangle>& geometry) {
|
||||
LodLevel result;
|
||||
result.triangles = geometry;
|
||||
|
||||
size_t vertexCount = geometry.size() * 3;
|
||||
result.vertexData.PositionData.reserve(vertexCount);
|
||||
result.vertexData.NormalData.reserve(vertexCount);
|
||||
result.vertexData.TexCoordData.reserve(vertexCount);
|
||||
result.vertexData.TangentData.reserve(vertexCount); // Äîáàâëÿåì ðåçåðâ
|
||||
result.vertexData.BinormalData.reserve(vertexCount);
|
||||
result.VertexIDs.reserve(vertexCount);
|
||||
|
||||
const std::array<Vector2f, 3> triangleUVs = {
|
||||
Vector2f(0.5f, 1.0f),
|
||||
Vector2f(0.0f, 0.0f),
|
||||
Vector2f(1.0f, 0.0f)
|
||||
};
|
||||
|
||||
for (const auto& t : geometry) {
|
||||
// --- Âû÷èñëÿåì ëîêàëüíûé áàçèñ òðåóãîëüíèêà (êàê â GetRotationForTriangle) ---
|
||||
Vector3f vA = t.data[0];
|
||||
Vector3f vB = t.data[1];
|
||||
Vector3f vC = t.data[2];
|
||||
|
||||
Vector3f x_axis = (vC - vB).normalized(); // Íàïðàâëåíèå U
|
||||
|
||||
Vector3f edge1 = vB - vA;
|
||||
Vector3f edge2 = vC - vA;
|
||||
Vector3f z_axis = edge1.cross(edge2).normalized(); // Íîðìàëü ïëîñêîñòè
|
||||
|
||||
// Ïðîâåðêà íàïðàâëåíèÿ íîðìàëè íàðóæó (îò öåíòðà ïëàíåòû)
|
||||
Vector3f centerToTri = (vA + vB + vC).normalized();
|
||||
if (z_axis.dot(centerToTri) < 0) {
|
||||
z_axis = z_axis * -1.0f;
|
||||
}
|
||||
|
||||
Vector3f y_axis = z_axis.cross(x_axis).normalized(); // Íàïðàâëåíèå V
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
result.vertexData.PositionData.push_back(t.data[i]);
|
||||
result.vertexData.NormalData.push_back(z_axis); // Ïëîñêàÿ íîðìàëü ãðàíè äëÿ Parallax
|
||||
result.vertexData.TexCoordData.push_back(triangleUVs[i]);
|
||||
|
||||
// Çàïèñûâàåì âû÷èñëåííûé áàçèñ â êàæäóþ âåðøèíó òðåóãîëüíèêà
|
||||
result.vertexData.TangentData.push_back(x_axis);
|
||||
result.vertexData.BinormalData.push_back(y_axis);
|
||||
|
||||
result.VertexIDs.push_back(t.ids[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
LodLevel PlanetData::generateSphere(int subdivisions, float noiseCoeff) {
|
||||
// 1. Èñõîäíûé îêòàýäð è ïðèñâîåíèå ID
|
||||
std::vector<Triangle> geometry = {
|
||||
// Âåðõíÿÿ ïîëóñôåðà (Y > 0)
|
||||
{{ 0.0f, 1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}}, // 0
|
||||
{{ 0.0f, 1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}, { 1.0f, 0.0f, 0.0f}}, // 1
|
||||
{{ 0.0f, 1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}}, // 2
|
||||
{{ 0.0f, 1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}}, // 3
|
||||
// Íèæíÿÿ ïîëóñôåðà (Y < 0)
|
||||
{{ 0.0f, -1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}}, // 4
|
||||
{{ 0.0f, -1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}}, // 5
|
||||
{{ 0.0f, -1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}, {-1.0f, 0.0f, 0.0f}}, // 6
|
||||
{{ 0.0f, -1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}} // 7
|
||||
};
|
||||
|
||||
// Ïðèñâîåíèå ID èñõîäíûì âåðøèíàì
|
||||
for (auto& t : geometry) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
// Èñïîëüçóåì map äëÿ ïîëó÷åíèÿ ID ïî ÷èñòûì êîîðäèíàòàì (íîðì. == ÷èñòûå)
|
||||
t.ids[i] = initialVertexMap[t.data[i].normalized()];
|
||||
}
|
||||
}
|
||||
// 3. Ðàçáèâàåì N ðàç (â subdivideTriangles ãåíåðèðóþòñÿ ID íîâûõ âåðøèí)
|
||||
for (int i = 0; i < subdivisions; i++) {
|
||||
geometry = subdivideTriangles(geometry, noiseCoeff);
|
||||
}
|
||||
|
||||
// 4. Ãåíåðèðóåì PositionData, NormalData è VertexIDs
|
||||
LodLevel lodLevel = trianglesToVertices(geometry);
|
||||
|
||||
// 5. Ñîçäàíèå V2T-Map íà îñíîâå VertexIDs (ÒÎÏÎËÎÃÈ×ÅÑÊÈÉ ÊËÞ×)
|
||||
V2TMap v2tMap;
|
||||
const auto& finalVertexIDs = lodLevel.VertexIDs;
|
||||
size_t num_triangles = geometry.size();
|
||||
|
||||
for (size_t i = 0; i < num_triangles; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
VertexID v_id = finalVertexIDs[i * 3 + j];
|
||||
|
||||
// Åñëè êëþ÷ óæå åñòü, ïðîñòî äîáàâëÿåì èíäåêñ
|
||||
v2tMap[v_id].push_back((int)i);
|
||||
}
|
||||
}
|
||||
lodLevel.v2tMap = v2tMap;
|
||||
|
||||
// 6. Ïðèìåíåíèå ôèíàëüíîãî øóìà (åñëè âû õîòåëè, ÷òîáû øóì áûë òîëüêî çäåñü)
|
||||
// Çäåñü ìû äîëæíû áûëè áû ïðèìåíèòü øóì ê buffer.PositionData,
|
||||
// íî â âàøåì èñõîäíîì êîäå øóì ïðèìåíÿëñÿ ðàíåå.
|
||||
// Ïðåäïîëàãàåì, ÷òî øóì áóäåò ïðèìåíåí çäåñü (èíà÷å v2tMap íå áóäåò ñîîòâåòñòâîâàòü).
|
||||
// ÂÎÑÑÒÀÍÎÂÈÌ ØÀÃ 2, íî äëÿ ôèíàëüíîé ãåîìåòðèè:
|
||||
for (size_t i = 0; i < lodLevel.vertexData.PositionData.size(); i++) {
|
||||
Vector3f dir = lodLevel.vertexData.PositionData[i].normalized();
|
||||
lodLevel.vertexData.PositionData[i] = dir * perlin.getSurfaceHeight(dir, noiseCoeff);
|
||||
}
|
||||
|
||||
|
||||
// 7. Ãåíåðàöèÿ ColorData
|
||||
lodLevel.vertexData.ColorData.reserve(geometry.size() * 3);
|
||||
const Vector3f baseColor = { 0.498f, 0.416f, 0.0f };
|
||||
const float colorFrequency = 5.0f;
|
||||
const float colorAmplitude = 0.2f;
|
||||
const Vector3f offsetR = { 0.1f, 0.2f, 0.3f };
|
||||
const Vector3f offsetG = { 0.5f, 0.4f, 0.6f };
|
||||
const Vector3f offsetB = { 0.9f, 0.8f, 0.7f };
|
||||
|
||||
|
||||
for (size_t i = 0; i < geometry.size(); i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
// Èñïîëüçóåì íîðìàëèçîâàííûé âåêòîð èç PositionData (êîòîðûé ðàâåí NormalData)
|
||||
Vector3f dir = lodLevel.vertexData.NormalData[i * 3 + j];
|
||||
|
||||
// Âû÷èñëåíèå öâåòîâîãî øóìà
|
||||
float noiseR = colorPerlin.noise(
|
||||
(dir.v[0] + offsetR.v[0]) * colorFrequency,
|
||||
(dir.v[1] + offsetR.v[1]) * colorFrequency,
|
||||
(dir.v[2] + offsetR.v[2]) * colorFrequency
|
||||
);
|
||||
// ... (àíàëîãè÷íî äëÿ noiseG è noiseB)
|
||||
|
||||
// Çäåñü ìû èñïîëüçóåì çàãëóøêè, òàê êàê íåò ïîëíîãî îïðåäåëåíèÿ PerlinNoise
|
||||
float noiseG = 0.0f;
|
||||
float noiseB = 0.0f;
|
||||
|
||||
Vector3f colorOffset = {
|
||||
noiseR * colorAmplitude,
|
||||
noiseG * colorAmplitude,
|
||||
noiseB * colorAmplitude
|
||||
};
|
||||
|
||||
Vector3f finalColor = baseColor + colorOffset;
|
||||
|
||||
lodLevel.vertexData.ColorData.push_back(finalColor);
|
||||
}
|
||||
}
|
||||
|
||||
return lodLevel;
|
||||
}
|
||||
|
||||
|
||||
// Ïðèìåð findNeighbors:
|
||||
std::vector<int> PlanetData::findNeighbors(int index, int lod) {
|
||||
const V2TMap& v2tMap = planetMeshLods[lod].v2tMap;
|
||||
const auto& vertexIDs = planetMeshLods[lod].VertexIDs;
|
||||
if ((index * 3 + 2) >= vertexIDs.size()) return {};
|
||||
std::set<int> neighbors;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
VertexID v_id = vertexIDs[index * 3 + i];
|
||||
auto it = v2tMap.find(v_id);
|
||||
if (it != v2tMap.end()) {
|
||||
for (int tri_index : it->second) {
|
||||
if (tri_index != index) {
|
||||
neighbors.insert(tri_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::vector<int>(neighbors.begin(), neighbors.end());
|
||||
}
|
||||
}
|
||||
117
PlanetData.h
117
PlanetData.h
@ -1,117 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "ZLMath.h"
|
||||
#include "Perlin.h"
|
||||
#include "Renderer.h" // Äëÿ VertexDataStruct
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
using VertexID = std::string;
|
||||
using V2TMap = std::map<VertexID, std::vector<int>>;
|
||||
|
||||
VertexID generateEdgeID(const VertexID& id1, const VertexID& id2);
|
||||
|
||||
constexpr static int MAX_LOD_LEVELS = 6;
|
||||
//constexpr static int MAX_LOD_LEVELS = 3;
|
||||
|
||||
struct Triangle
|
||||
{
|
||||
std::array<Vector3f, 3> data;
|
||||
std::array<VertexID, 3> ids;
|
||||
|
||||
Triangle(Vector3f p1, Vector3f p2, Vector3f p3)
|
||||
: data{ p1, p2, p3 }
|
||||
{
|
||||
}
|
||||
|
||||
Triangle(std::array<Vector3f, 3> idata, std::array<VertexID, 3> iids)
|
||||
: data{ idata }
|
||||
, ids{ iids }
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct LodLevel
|
||||
{
|
||||
std::vector<Triangle> triangles;
|
||||
VertexDataStruct vertexData;
|
||||
std::vector<VertexID> VertexIDs;
|
||||
V2TMap v2tMap;
|
||||
|
||||
void Scale(float s)
|
||||
{
|
||||
vertexData.Scale(s);
|
||||
for (auto& t : triangles) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
t.data[i] = t.data[i] * s;
|
||||
}
|
||||
}
|
||||
}
|
||||
void Move(Vector3f pos)
|
||||
{
|
||||
vertexData.Move(pos);
|
||||
for (auto& t : triangles) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
t.data[i] = t.data[i] + pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class PlanetData {
|
||||
public:
|
||||
static const float PLANET_RADIUS;
|
||||
static const Vector3f PLANET_CENTER_OFFSET;
|
||||
|
||||
private:
|
||||
PerlinNoise perlin;
|
||||
PerlinNoise colorPerlin;
|
||||
|
||||
std::array<LodLevel, MAX_LOD_LEVELS> planetMeshLods;
|
||||
std::array<LodLevel, MAX_LOD_LEVELS> planetMeshLodsNoDist;
|
||||
LodLevel planetAtmosphereLod;
|
||||
|
||||
int currentLod; // Ëîãè÷åñêèé òåêóùèé óðîâåíü äåòàëèçàöèè
|
||||
|
||||
std::map<Vector3f, VertexID> initialVertexMap;
|
||||
|
||||
// Âíóòðåííèå ìåòîäû ãåíåðàöèè
|
||||
std::vector<Triangle> subdivideTriangles(const std::vector<Triangle>& inputTriangles, float noiseCoeff);
|
||||
LodLevel trianglesToVertices(const std::vector<Triangle>& triangles);
|
||||
LodLevel generateSphere(int subdivisions, float noiseCoeff);
|
||||
|
||||
// Âñïîìîãàòåëüíûå ìåòîäû ìàòåìàòèêè
|
||||
static float check_spherical_side(const Vector3f& V1, const Vector3f& V2, const Vector3f& P, const Vector3f& V3, float epsilon = 1e-6f);
|
||||
static bool is_inside_spherical_triangle(const Vector3f& P, const Vector3f& V1, const Vector3f& V2, const Vector3f& V3, float epsilon);
|
||||
|
||||
public:
|
||||
PlanetData();
|
||||
|
||||
void init();
|
||||
|
||||
// Ìåòîäû äîñòóïà ê äàííûì (äëÿ ðåíäåðåðà)
|
||||
const LodLevel& getLodLevel(int level) const;
|
||||
const LodLevel& getLodLevelNoDist(int level) const;
|
||||
const LodLevel& getAtmosphereLod() const;
|
||||
int getCurrentLodIndex() const;
|
||||
int getMaxLodIndex() const;
|
||||
|
||||
// Ëîãèêà
|
||||
std::pair<float, float> calculateZRange(float distanceToSurface);
|
||||
float distanceToPlanetSurface(const Vector3f& viewerPosition);
|
||||
|
||||
// Âîçâðàùàåò èíäåêñû òðåóãîëüíèêîâ, âèäèìûõ êàìåðîé
|
||||
std::vector<int> getTrianglesUnderCamera(const Vector3f& viewerPosition);
|
||||
|
||||
std::vector<int> findNeighbors(int index, int lod);
|
||||
|
||||
static std::vector<int> find_sub_triangle_spherical(const Vector3f& a_orig, const Vector3f& b_orig, const Vector3f& c_orig, const Vector3f& px_orig);
|
||||
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
868
PlanetObject.cpp
868
PlanetObject.cpp
@ -3,392 +3,258 @@
|
||||
#include <cmath>
|
||||
#include "OpenGlExtensions.h"
|
||||
#include "Environment.h"
|
||||
#include "StoneObject.h"
|
||||
|
||||
namespace ZL {
|
||||
|
||||
static constexpr float PLANET_RADIUS = 20000.f;
|
||||
static Vector3f PLANET_CENTER_OFFSET = Vector3f{ 0.f, 0.f, 0.0f };
|
||||
//static Vector3f PLANET_CENTER_OFFSET = Vector3f{ 0.f, 0.f, -45000.f };
|
||||
|
||||
Matrix3f GetRotationForTriangle(const Triangle& tri) {
|
||||
// Для треугольника №0:
|
||||
// tri.data[0] = {0, 20000, 0} (A)
|
||||
// tri.data[1] = {0, 0, 20000} (B)
|
||||
// tri.data[2] = {20000, 0, 0} (C)
|
||||
// --- 1. Дальний диапазон (FAR) ---
|
||||
static constexpr float FAR_Z_NEAR = 2000.0f;
|
||||
static constexpr float FAR_Z_FAR = 200000.0f;
|
||||
|
||||
Vector3f vA = tri.data[0];
|
||||
Vector3f vB = tri.data[1];
|
||||
Vector3f vC = tri.data[2];
|
||||
// Дистанция, где НАЧИНАЕТСЯ переход FAR -> MIDDLE
|
||||
static constexpr float TRANSITION_FAR_START = 3000.0f;
|
||||
|
||||
// 1. Вычисляем ось X (горизонталь).
|
||||
// Нам нужна грань BC: от (0, 0, 20000) до (20000, 0, 0)
|
||||
Vector3f x_axis = (vC - vB).normalized();
|
||||
// --- 2. Средний диапазон (MIDDLE) ---
|
||||
static constexpr float MIDDLE_Z_NEAR = 500.f;
|
||||
static constexpr float MIDDLE_Z_FAR = 50000.f;
|
||||
|
||||
|
||||
// 2. Вычисляем нормаль (ось Z).
|
||||
// Порядок cross product (AB x AC) определит "лицевую" сторону.
|
||||
Vector3f edge1 = vB - vA;
|
||||
Vector3f edge2 = vC - vA;
|
||||
Vector3f z_axis = edge1.cross(edge2).normalized();
|
||||
// Дистанция, где ЗАВЕРШАЕТСЯ переход FAR -> MIDDLE и НАЧИНАЕТСЯ MIDDLE -> NEAR
|
||||
static constexpr float TRANSITION_MIDDLE_START = 500.f;
|
||||
|
||||
// 3. Вычисляем ось Y (вертикаль).
|
||||
// В ортонормированном базисе Y всегда перпендикулярна Z и X.
|
||||
Vector3f y_axis = z_axis.cross(x_axis).normalized();
|
||||
// --- 3. Ближний диапазон (NEAR) ---
|
||||
// Новые константы для максимальной точности
|
||||
static constexpr float NEAR_Z_NEAR = 100.0f;
|
||||
static constexpr float NEAR_Z_FAR = 10000.0f;
|
||||
|
||||
// 4. Формируем прямую матрицу поворота (Rotation/World Matrix).
|
||||
Matrix3f rot;
|
||||
|
||||
// Столбец 0: Ось X
|
||||
rot.m[0] = x_axis.v[0];
|
||||
rot.m[3] = x_axis.v[1];
|
||||
rot.m[6] = x_axis.v[2];
|
||||
// Дистанция, где ЗАВЕРШАЕТСЯ переход MIDDLE -> NEAR
|
||||
static constexpr float TRANSITION_NEAR_END = 100.f;
|
||||
|
||||
// Столбец 1: Ось Y
|
||||
rot.m[1] = y_axis.v[0];
|
||||
rot.m[4] = y_axis.v[1];
|
||||
rot.m[7] = y_axis.v[2];
|
||||
// --- 3. Ближний диапазон (NEAR) ---
|
||||
// Новые константы для максимальной точности
|
||||
static constexpr float SUPER_NEAR_Z_NEAR = 5.0f;
|
||||
static constexpr float SUPER_NEAR_Z_FAR = 5000.0f;
|
||||
|
||||
// Столбец 2: Ось Z
|
||||
rot.m[2] = z_axis.v[0];
|
||||
rot.m[5] = z_axis.v[1];
|
||||
rot.m[8] = z_axis.v[2];
|
||||
|
||||
return rot;
|
||||
// Дистанция, где ЗАВЕРШАЕТСЯ переход MIDDLE -> NEAR
|
||||
static constexpr float TRANSITION_SUPER_NEAR_END = 10.f;
|
||||
|
||||
std::pair<float, float> calculateZRange(const Vector3f& shipPosition) {
|
||||
|
||||
// 1. Вычисление расстояния до поверхности планеты
|
||||
const Vector3f planetWorldPosition = PLANET_CENTER_OFFSET;
|
||||
const float distanceToPlanetCenter = (planetWorldPosition - shipPosition).length();
|
||||
const float distanceToPlanetSurface = distanceToPlanetCenter - PLANET_RADIUS;
|
||||
std::cout << "distanceToPlanetSurface " << distanceToPlanetSurface << std::endl;
|
||||
|
||||
float currentZNear;
|
||||
float currentZFar;
|
||||
float alpha; // Коэффициент интерполяции для текущего сегмента
|
||||
|
||||
// Диапазон I: Далеко (FAR) -> Средне (MIDDLE)
|
||||
if (distanceToPlanetSurface >= TRANSITION_FAR_START) {
|
||||
// Полностью дальний диапазон
|
||||
currentZNear = FAR_Z_NEAR;
|
||||
currentZFar = FAR_Z_FAR;
|
||||
|
||||
}
|
||||
else if (distanceToPlanetSurface > TRANSITION_MIDDLE_START) {
|
||||
// Плавный переход от FAR к MIDDLE
|
||||
const float transitionLength = TRANSITION_FAR_START - TRANSITION_MIDDLE_START;
|
||||
|
||||
// Нормализация расстояния, 0 при TRANSITION_FAR_START (Далеко), 1 при TRANSITION_MIDDLE_START (Близко)
|
||||
float normalizedDist = (distanceToPlanetSurface - TRANSITION_MIDDLE_START) / transitionLength;
|
||||
alpha = 1.0f - normalizedDist; // alpha = 0 (Далеко) ... 1 (Близко)
|
||||
|
||||
// Интерполяция: FAR * (1-alpha) + MIDDLE * alpha
|
||||
currentZNear = FAR_Z_NEAR * (1.0f - alpha) + MIDDLE_Z_NEAR * alpha;
|
||||
currentZFar = FAR_Z_FAR * (1.0f - alpha) + MIDDLE_Z_FAR * alpha;
|
||||
|
||||
// Диапазон II: Средне (MIDDLE) -> Близко (NEAR)
|
||||
}
|
||||
else if (distanceToPlanetSurface > TRANSITION_NEAR_END) {
|
||||
// Плавный переход от MIDDLE к NEAR
|
||||
const float transitionLength = TRANSITION_MIDDLE_START - TRANSITION_NEAR_END;
|
||||
|
||||
// Нормализация расстояния, 0 при TRANSITION_MIDDLE_START (Далеко), 1 при TRANSITION_NEAR_END (Близко)
|
||||
float normalizedDist = (distanceToPlanetSurface - TRANSITION_NEAR_END) / transitionLength;
|
||||
alpha = 1.0f - normalizedDist; // alpha = 0 (Далеко) ... 1 (Близко)
|
||||
|
||||
// Интерполяция: MIDDLE * (1-alpha) + NEAR * alpha
|
||||
currentZNear = MIDDLE_Z_NEAR * (1.0f - alpha) + NEAR_Z_NEAR * alpha;
|
||||
currentZFar = MIDDLE_Z_FAR * (1.0f - alpha) + NEAR_Z_FAR * alpha;
|
||||
|
||||
}
|
||||
else if (distanceToPlanetSurface > TRANSITION_SUPER_NEAR_END) {
|
||||
// Плавный переход от MIDDLE к NEAR
|
||||
const float transitionLength = TRANSITION_NEAR_END - TRANSITION_SUPER_NEAR_END;
|
||||
|
||||
// Нормализация расстояния, 0 при TRANSITION_MIDDLE_START (Далеко), 1 при TRANSITION_NEAR_END (Близко)
|
||||
float normalizedDist = (distanceToPlanetSurface - TRANSITION_SUPER_NEAR_END) / transitionLength;
|
||||
alpha = 1.0f - normalizedDist; // alpha = 0 (Далеко) ... 1 (Близко)
|
||||
|
||||
// Интерполяция: MIDDLE * (1-alpha) + NEAR * alpha
|
||||
currentZNear = NEAR_Z_NEAR * (1.0f - alpha) + SUPER_NEAR_Z_NEAR * alpha;
|
||||
currentZFar = NEAR_Z_FAR * (1.0f - alpha) + SUPER_NEAR_Z_FAR * alpha;
|
||||
|
||||
}
|
||||
else {
|
||||
// Полностью ближний диапазон (distanceToPlanetSurface <= TRANSITION_NEAR_END)
|
||||
currentZNear = SUPER_NEAR_Z_NEAR;
|
||||
currentZFar = SUPER_NEAR_Z_FAR;
|
||||
}
|
||||
|
||||
return { currentZNear, currentZFar };
|
||||
}
|
||||
|
||||
PerlinNoise::PerlinNoise() {
|
||||
p.resize(256);
|
||||
std::iota(p.begin(), p.end(), 0);
|
||||
// Перемешиваем для случайности (можно задать seed)
|
||||
std::default_random_engine engine(77777);
|
||||
std::shuffle(p.begin(), p.end(), engine);
|
||||
p.insert(p.end(), p.begin(), p.end()); // Дублируем для переполнения
|
||||
}
|
||||
|
||||
PerlinNoise::PerlinNoise(uint64_t seed) {
|
||||
p.resize(256);
|
||||
std::iota(p.begin(), p.end(), 0);
|
||||
// Перемешиваем для случайности (используем переданный seed)
|
||||
std::default_random_engine engine(seed); // <-- Использование сида
|
||||
std::shuffle(p.begin(), p.end(), engine);
|
||||
p.insert(p.end(), p.begin(), p.end()); // Дублируем для переполнения
|
||||
}
|
||||
|
||||
float PerlinNoise::fade(float t) { return t * t * t * (t * (t * 6 - 15) + 10); }
|
||||
float PerlinNoise::lerp(float t, float a, float b) { return a + t * (b - a); }
|
||||
float PerlinNoise::grad(int hash, float x, float y, float z) {
|
||||
int h = hash & 15;
|
||||
float u = h < 8 ? x : y;
|
||||
float v = h < 4 ? y : (h == 12 || h == 14 ? x : z);
|
||||
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
|
||||
}
|
||||
|
||||
float PerlinNoise::noise(float x, float y, float z) {
|
||||
int X = (int)floor(x) & 255;
|
||||
int Y = (int)floor(y) & 255;
|
||||
int Z = (int)floor(z) & 255;
|
||||
|
||||
|
||||
x -= floor(x);
|
||||
y -= floor(y);
|
||||
z -= floor(z);
|
||||
|
||||
float u = fade(x);
|
||||
float v = fade(y);
|
||||
float w = fade(z);
|
||||
|
||||
int A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z;
|
||||
int B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z;
|
||||
|
||||
return lerp(w, lerp(v, lerp(u, grad(p[AA], x, y, z), grad(p[BA], x - 1, y, z)),
|
||||
lerp(u, grad(p[AB], x, y - 1, z), grad(p[BB], x - 1, y - 1, z))),
|
||||
lerp(v, lerp(u, grad(p[AA + 1], x, y, z - 1), grad(p[BA + 1], x - 1, y, z - 1)),
|
||||
lerp(u, grad(p[AB + 1], x, y - 1, z - 1), grad(p[BB + 1], x - 1, y - 1, z - 1))));
|
||||
}
|
||||
|
||||
float PerlinNoise::getSurfaceHeight(Vector3f pos) {
|
||||
// Частота шума (чем больше, тем больше "холмов")
|
||||
float frequency = 7.0f;
|
||||
|
||||
|
||||
// Получаем значение шума (обычно от -1 до 1)
|
||||
float noiseValue = noise(pos.v[0] * frequency, pos.v[1] * frequency, pos.v[2] * frequency);
|
||||
|
||||
// Переводим из диапазона [-1, 1] в [0, 1]
|
||||
//noiseValue = (noiseValue + 1.0f) * 0.5f;
|
||||
|
||||
// Масштабируем: хотим отклонение от 1.0 до 1.1
|
||||
// Значит амплитуда = 0.1
|
||||
float height = 1.0f; // * 0.2 даст вариацию высоты
|
||||
//float height = 1.0f - (noiseValue * 0.01f); // * 0.2 даст вариацию высоты
|
||||
|
||||
return height;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool PlanetObject::planetIsFar()
|
||||
{
|
||||
const Vector3f planetWorldPosition = PLANET_CENTER_OFFSET;
|
||||
const float distanceToPlanetCenter = (planetWorldPosition - Environment::shipPosition).length();
|
||||
const float distanceToPlanetSurface = distanceToPlanetCenter - PLANET_RADIUS;
|
||||
|
||||
return distanceToPlanetSurface > 0.1*TRANSITION_MIDDLE_START;
|
||||
}
|
||||
|
||||
float PlanetObject::distanceToPlanetSurface()
|
||||
{
|
||||
const Vector3f planetWorldPosition = PLANET_CENTER_OFFSET;
|
||||
const float distanceToPlanetCenter = (planetWorldPosition - Environment::shipPosition).length();
|
||||
const float distanceToPlanetSurface = distanceToPlanetCenter - PLANET_RADIUS;
|
||||
return distanceToPlanetSurface;
|
||||
|
||||
}
|
||||
|
||||
|
||||
PlanetObject::PlanetObject()
|
||||
: perlin(77777)
|
||||
, colorPerlin(123123)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void PlanetObject::init() {
|
||||
// 1. Инициализируем данные (генерация мешей)
|
||||
planetData.init();
|
||||
|
||||
// 2. Забираем данные для VBO
|
||||
// Берем максимальный LOD для начальной отрисовки
|
||||
int lodIndex = planetData.getMaxLodIndex();
|
||||
planetRenderStruct.data = planetData.getLodLevel(lodIndex).vertexData;
|
||||
planetMesh = generateSphere(8);
|
||||
|
||||
planetMesh.Scale(PLANET_RADIUS);
|
||||
planetMesh.Move(PLANET_CENTER_OFFSET);
|
||||
|
||||
planetRenderStruct.data = planetMesh;
|
||||
planetRenderStruct.RefreshVBO();
|
||||
|
||||
planetRenderStructCut.data = planetData.getLodLevelNoDist(lodIndex).vertexData;
|
||||
sandTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/sand.png", ""));
|
||||
//sandTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/rock.png", ""));
|
||||
|
||||
planetRenderStructCut.data.PositionData.resize(3);
|
||||
planetAtmosphere.data = generateSphere(5);
|
||||
planetAtmosphere.data.Scale(PLANET_RADIUS*1.03);
|
||||
planetAtmosphere.data.Move(PLANET_CENTER_OFFSET);
|
||||
|
||||
planetRenderStructCut.RefreshVBO();
|
||||
//sandTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/sand2.png", ""));
|
||||
sandTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/sand2.png", ""));
|
||||
stoneTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/rock.png", ""));
|
||||
|
||||
// Атмосфера
|
||||
planetAtmosphereRenderStruct.data = planetData.getAtmosphereLod().vertexData;
|
||||
planetAtmosphereRenderStruct.RefreshVBO();
|
||||
|
||||
|
||||
planetStones = CreateStoneGroupData(778, planetData.getLodLevel(lodIndex));
|
||||
planetStones.inflate({ 0/*,1,2,3,4,5,6,7*/ });
|
||||
planetStonesToBakeRenderStruct.AssignFrom(planetStones.mesh);
|
||||
planetStonesToBakeRenderStruct.RefreshVBO();
|
||||
|
||||
|
||||
planetAtmosphere.RefreshVBO();
|
||||
}
|
||||
|
||||
void PlanetObject::prepareDrawData() {
|
||||
if (!drawDataDirty) return;
|
||||
|
||||
drawDataDirty = false;
|
||||
}
|
||||
|
||||
|
||||
void PlanetObject::update(float deltaTimeMs) {
|
||||
// 1. Получаем базовые треугольники под камерой
|
||||
auto lr = planetData.getTrianglesUnderCamera(Environment::shipPosition);
|
||||
int currentLod = planetData.getCurrentLodIndex();
|
||||
|
||||
// Временный вектор для сбора новых индексов
|
||||
std::vector<int> newIndices;
|
||||
std::set<int> used;
|
||||
|
||||
// Рекурсивно (или итеративно, как у тебя) собираем индексы видимых зон
|
||||
for (int i : lr) {
|
||||
if (used.insert(i).second) newIndices.push_back(i);
|
||||
|
||||
auto neighbors = planetData.findNeighbors(i, currentLod);
|
||||
for (int n : neighbors) {
|
||||
if (used.insert(n).second) newIndices.push_back(n);
|
||||
|
||||
auto neighbors2 = planetData.findNeighbors(n, currentLod);
|
||||
for (int n2 : neighbors2) {
|
||||
if (used.insert(n2).second) newIndices.push_back(n2);
|
||||
|
||||
auto neighbors3 = planetData.findNeighbors(n, currentLod);
|
||||
|
||||
for (int n3 : neighbors3) {
|
||||
if (used.insert(n3).second) newIndices.push_back(n3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Сортируем новый список, чтобы порядок не влиял на сравнение
|
||||
std::sort(newIndices.begin(), newIndices.end());
|
||||
|
||||
// 3. Сравниваем с тем, что было нарисовано в прошлый раз
|
||||
if (newIndices != triangleIndicesToDraw) {
|
||||
// Обновляем список индексов (используем move для эффективности)
|
||||
triangleIndicesToDraw = std::move(newIndices);
|
||||
|
||||
// --- ОБНОВЛЯЕМ ЖЕЛТУЮ ЗОНУ (только когда изменился состав треугольников) ---
|
||||
const auto& fullMesh = planetData.getLodLevel(currentLod).vertexData;
|
||||
planetRenderYellowStruct.data.PositionData.clear();
|
||||
planetRenderYellowStruct.data.TexCoordData.clear();
|
||||
|
||||
for (int i : triangleIndicesToDraw) {
|
||||
// Копируем геометрию для подсветки
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
planetRenderYellowStruct.data.PositionData.push_back(fullMesh.PositionData[i * 3 + j]);
|
||||
planetRenderYellowStruct.data.TexCoordData.push_back(fullMesh.TexCoordData[i * 3 + j]);
|
||||
}
|
||||
}
|
||||
if (planetRenderYellowStruct.data.PositionData.size() > 0)
|
||||
{
|
||||
planetRenderYellowStruct.RefreshVBO();
|
||||
}
|
||||
|
||||
// --- ОБНОВЛЯЕМ КАМНИ (через новую структуру StoneGroup) ---
|
||||
if (triangleIndicesToDraw.size() > 0)
|
||||
{
|
||||
planetStones.inflate(triangleIndicesToDraw);
|
||||
// Используем AssignFrom, он внутри сам вызывает RefreshVBO
|
||||
planetStonesRenderStruct.AssignFrom(planetStones.mesh);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void PlanetObject::bakeStoneTexture(Renderer& renderer) {
|
||||
glViewport(0, 0, 512, 512);
|
||||
glClearColor(224 / 255.f, 201 / 255.f, 167 / 255.f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
|
||||
static const std::string defaultShaderName2 = "planetBake";
|
||||
static const std::string vPositionName = "vPosition";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
|
||||
renderer.shaderManager.PushShader(defaultShaderName2);
|
||||
renderer.RenderUniform1i(textureUniformName, 0);
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
|
||||
float dist = planetData.distanceToPlanetSurface(Environment::shipPosition);
|
||||
auto zRange = planetData.calculateZRange(dist);
|
||||
const float currentZNear = zRange.first;
|
||||
const float currentZFar = zRange.second;
|
||||
|
||||
|
||||
|
||||
Triangle tr = planetData.getLodLevelNoDist(planetData.getCurrentLodIndex()).triangles[0];
|
||||
|
||||
// 1. Получаем матрицу вращения (оси в столбцах)
|
||||
Matrix3f mr = GetRotationForTriangle(tr);
|
||||
|
||||
// 2. Трансформируем вершины в локальное пространство экрана, чтобы найти габариты
|
||||
// Используем MultMatrixVector(Matrix, Vector).
|
||||
// Если ваша функция считает V * M, то передайте Inverse(mr).
|
||||
Vector3f rA = MultMatrixVector(mr, tr.data[0]);
|
||||
Vector3f rB = MultMatrixVector(mr, tr.data[1]);
|
||||
Vector3f rC = MultMatrixVector(mr, tr.data[2]);
|
||||
|
||||
// 3. Вычисляем реальные границы треугольника после поворота
|
||||
float minX = min(rA.v[0], min(rB.v[0], rC.v[0]));
|
||||
float maxX = max(rA.v[0], max(rB.v[0], rC.v[0]));
|
||||
float minY = min(rA.v[1], min(rB.v[1], rC.v[1]));
|
||||
float maxY = max(rA.v[1], max(rB.v[1], rC.v[1]));
|
||||
|
||||
|
||||
// Находим центр и размеры
|
||||
float width = maxX - minX;
|
||||
float height = maxY - minY;
|
||||
float centerX = (minX + maxX) * 0.5f;
|
||||
float centerY = (minY + maxY) * 0.5f;
|
||||
|
||||
width = width * 0.995;
|
||||
height = height * 0.995;
|
||||
|
||||
renderer.PushProjectionMatrix(
|
||||
centerX - width*0.5, centerX + width * 0.5,
|
||||
centerY - height * 0.5, centerY + height * 0.5,
|
||||
150, 200000);
|
||||
|
||||
renderer.PushMatrix();
|
||||
renderer.LoadIdentity();
|
||||
// Сдвигаем камеру по Z
|
||||
renderer.TranslateMatrix(Vector3f{ 0, 0, -45000 });
|
||||
|
||||
// Применяем вращение
|
||||
renderer.RotateMatrix(mr);
|
||||
|
||||
|
||||
// Извлекаем нормаль треугольника (это 3-й столбец нашей матрицы вращения)
|
||||
Vector3f planeNormal = { mr.m[2], mr.m[5], mr.m[8] };
|
||||
|
||||
renderer.RenderUniform3fv("uPlanePoint", &tr.data[0].v[0]);
|
||||
renderer.RenderUniform3fv("uPlaneNormal", &planeNormal.v[0]);
|
||||
renderer.RenderUniform1f("uMaxHeight", StoneParams::BASE_SCALE * 1.1f); // Соответствует BASE_SCALE + perturbation
|
||||
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, sandTexture->getTexID());
|
||||
renderer.PushMatrix();
|
||||
|
||||
renderer.DrawVertexRenderStruct(planetRenderStructCut);
|
||||
renderer.PopMatrix();
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK); // Отсекаем задние грани
|
||||
if (planetStonesToBakeRenderStruct.data.PositionData.size() > 0)
|
||||
{
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, stoneTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(planetStonesToBakeRenderStruct);
|
||||
CheckGlError();
|
||||
}
|
||||
glDisable(GL_CULL_FACE); // Не забываем выключить, чтобы не сломать остальной рендер
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
renderer.DisableVertexAttribArray(vPositionName);
|
||||
renderer.shaderManager.PopShader();
|
||||
CheckGlError();
|
||||
}
|
||||
|
||||
|
||||
void PlanetObject::draw(Renderer& renderer) {
|
||||
|
||||
prepareDrawData();
|
||||
|
||||
{
|
||||
if (stoneMapFB == nullptr)
|
||||
{
|
||||
stoneMapFB = std::make_unique<FrameBuffer>(512, 512);
|
||||
}
|
||||
stoneMapFB->Bind();
|
||||
|
||||
bakeStoneTexture(renderer);
|
||||
|
||||
stoneMapFB->Unbind();
|
||||
|
||||
}
|
||||
|
||||
//bakeStoneTexture(renderer);
|
||||
|
||||
glViewport(0, 0, Environment::width, Environment::height);
|
||||
//--------------------------
|
||||
|
||||
|
||||
drawPlanet(renderer);
|
||||
//drawYellowZone(renderer);
|
||||
drawStones(renderer);
|
||||
|
||||
drawAtmosphere(renderer);
|
||||
}
|
||||
|
||||
void PlanetObject::drawPlanet(Renderer& renderer)
|
||||
{
|
||||
static const std::string defaultShaderName = "planetLand";
|
||||
|
||||
static const std::string defaultShaderName = "defaultColor";
|
||||
static const std::string vPositionName = "vPosition";
|
||||
static const std::string vColorName = "vColor";
|
||||
static const std::string vNormalName = "vNormal";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
//static const std::string vTexCoord3Name = "vTexCoord3";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
renderer.shaderManager.PushShader(defaultShaderName);
|
||||
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vColorName);
|
||||
renderer.EnableVertexAttribArray(vNormalName);
|
||||
renderer.EnableVertexAttribArray("vTangent");
|
||||
renderer.EnableVertexAttribArray("vBinormal");
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
|
||||
float dist = planetData.distanceToPlanetSurface(Environment::shipPosition);
|
||||
auto zRange = planetData.calculateZRange(dist);
|
||||
const float currentZNear = zRange.first;
|
||||
const float currentZFar = zRange.second;
|
||||
|
||||
// 2. Применяем динамическую матрицу проекции
|
||||
renderer.PushPerspectiveProjectionMatrix(1.0 / 1.5,
|
||||
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
|
||||
currentZNear, currentZFar);
|
||||
|
||||
renderer.PushMatrix();
|
||||
renderer.LoadIdentity();
|
||||
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
|
||||
renderer.RotateMatrix(Environment::inverseShipMatrix);
|
||||
|
||||
renderer.TranslateMatrix(-Environment::shipPosition);
|
||||
|
||||
const Matrix4f viewMatrix = renderer.GetCurrentModelViewMatrix();
|
||||
|
||||
renderer.RenderUniform1i("Texture", 0);
|
||||
renderer.RenderUniform1i("BakedTexture", 1);
|
||||
|
||||
|
||||
Triangle tr = planetData.getLodLevel(planetData.getCurrentLodIndex()).triangles[0]; // Берем базовый треугольник
|
||||
Matrix3f mr = GetRotationForTriangle(tr); // Та же матрица, что и при запекании
|
||||
|
||||
// Позиция камеры (корабля) в мире
|
||||
renderer.RenderUniform3fv("uViewPos", &Environment::shipPosition.v[0]);
|
||||
|
||||
//renderer.RenderUniform1f("uHeightScale", 0.03f);
|
||||
renderer.RenderUniform1f("uHeightScale", 0.0f);
|
||||
|
||||
renderer.RenderUniform1f("uDistanceToPlanetSurface", dist);
|
||||
renderer.RenderUniform1f("uCurrentZFar", currentZFar);
|
||||
|
||||
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, stoneMapFB->getTextureID());
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, sandTexture->getTexID());
|
||||
|
||||
renderer.DrawVertexRenderStruct(planetRenderStruct);
|
||||
CheckGlError();
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
renderer.DisableVertexAttribArray(vNormalName);
|
||||
renderer.DisableVertexAttribArray("vTangent");
|
||||
renderer.DisableVertexAttribArray("vBinormal");
|
||||
renderer.DisableVertexAttribArray(vColorName);
|
||||
renderer.DisableVertexAttribArray(vPositionName);
|
||||
renderer.shaderManager.PopShader();
|
||||
CheckGlError();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void PlanetObject::drawStones(Renderer& renderer)
|
||||
{
|
||||
static const std::string defaultShaderName2 = "planetStone";
|
||||
static const std::string vPositionName = "vPosition";
|
||||
static const std::string vColorName = "vColor";
|
||||
static const std::string vNormalName = "vNormal";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
renderer.shaderManager.PushShader(defaultShaderName2);
|
||||
renderer.RenderUniform1i(textureUniformName, 0);
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vColorName);
|
||||
renderer.EnableVertexAttribArray(vNormalName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
//renderer.EnableVertexAttribArray(vTexCoord3Name);
|
||||
|
||||
float dist = planetData.distanceToPlanetSurface(Environment::shipPosition);
|
||||
auto zRange = planetData.calculateZRange(dist);
|
||||
const auto zRange = calculateZRange(Environment::shipPosition);
|
||||
const float currentZNear = zRange.first;
|
||||
const float currentZFar = zRange.second;
|
||||
|
||||
@ -403,27 +269,41 @@ namespace ZL {
|
||||
renderer.RotateMatrix(Environment::inverseShipMatrix);
|
||||
renderer.TranslateMatrix(-Environment::shipPosition);
|
||||
|
||||
|
||||
const Matrix4f viewMatrix = renderer.GetCurrentModelViewMatrix();
|
||||
|
||||
|
||||
Vector3f lightDir_World = Vector3f(1.0f, 0.0f, -1.0f).normalized();
|
||||
// В OpenGL/шейдерах удобнее работать с вектором, указывающим ОТ источника к поверхности.
|
||||
Vector3f lightDirection_World = -lightDir_World; // Вектор, направленный от источника
|
||||
Vector3f lightDirection_View;
|
||||
|
||||
lightDirection_View.v[0] = viewMatrix.m[0] * lightDirection_World.v[0] + viewMatrix.m[4] * lightDirection_World.v[1] + viewMatrix.m[8] * lightDirection_World.v[2];
|
||||
lightDirection_View.v[1] = viewMatrix.m[1] * lightDirection_World.v[0] + viewMatrix.m[5] * lightDirection_World.v[1] + viewMatrix.m[9] * lightDirection_World.v[2];
|
||||
lightDirection_View.v[2] = viewMatrix.m[2] * lightDirection_World.v[0] + viewMatrix.m[6] * lightDirection_World.v[1] + viewMatrix.m[10] * lightDirection_World.v[2];
|
||||
lightDirection_View = lightDirection_View.normalized(); // Нормализуем на всякий случай
|
||||
|
||||
// Установка uniform-переменной
|
||||
// Предполагается, что RenderUniform3fv определена в Renderer.h
|
||||
renderer.RenderUniform3fv("uLightDirection", &lightDirection_View.v[0]);
|
||||
renderer.RenderUniformMatrix4fv("ModelViewMatrix", false, &viewMatrix.m[0]);
|
||||
|
||||
float dist = distanceToPlanetSurface();
|
||||
|
||||
|
||||
renderer.RenderUniform1f("uDistanceToPlanetSurface", dist);
|
||||
renderer.RenderUniform1f("uCurrentZFar", currentZFar);
|
||||
renderer.RenderUniform3fv("uViewPos", &Environment::shipPosition.v[0]);
|
||||
//glEnable(GL_BLEND);
|
||||
//glBlendFunc(GL_SRC_ALPHA, GL_ONE);// Аддитивное смешивание для эффекта свечения
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glBindTexture(GL_TEXTURE_2D, sandTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(planetRenderStruct);
|
||||
//glDisable(GL_BLEND);
|
||||
CheckGlError();
|
||||
|
||||
if (planetStonesRenderStruct.data.PositionData.size() > 0)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, stoneTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(planetStonesRenderStruct);
|
||||
CheckGlError();
|
||||
}
|
||||
glDisable(GL_BLEND);
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
//renderer.DisableVertexAttribArray(vTexCoord3Name);
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
renderer.DisableVertexAttribArray(vNormalName);
|
||||
renderer.DisableVertexAttribArray(vColorName);
|
||||
@ -431,79 +311,7 @@ namespace ZL {
|
||||
renderer.shaderManager.PopShader();
|
||||
CheckGlError();
|
||||
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void PlanetObject::drawYellowZone(Renderer& renderer)
|
||||
{
|
||||
|
||||
static const std::string defaultShaderName = "planetLand";
|
||||
static const std::string vPositionName = "vPosition";
|
||||
static const std::string vColorName = "vColor";
|
||||
static const std::string vNormalName = "vNormal";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
float dist = planetData.distanceToPlanetSurface(Environment::shipPosition);
|
||||
auto zRange = planetData.calculateZRange(dist);
|
||||
const float currentZNear = zRange.first;
|
||||
const float currentZFar = zRange.second;
|
||||
|
||||
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
if (planetRenderYellowStruct.data.PositionData.size() > 0)
|
||||
{
|
||||
|
||||
renderer.shaderManager.PushShader(defaultShaderName);
|
||||
renderer.RenderUniform1i(textureUniformName, 0);
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vColorName);
|
||||
renderer.EnableVertexAttribArray(vNormalName);
|
||||
renderer.EnableVertexAttribArray("vTangent");
|
||||
renderer.EnableVertexAttribArray("vBinormal");
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
|
||||
// 2. Применяем динамическую матрицу проекции
|
||||
renderer.PushPerspectiveProjectionMatrix(1.0 / 1.5,
|
||||
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
|
||||
currentZNear, currentZFar);
|
||||
|
||||
renderer.PushMatrix();
|
||||
renderer.LoadIdentity();
|
||||
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
|
||||
renderer.RotateMatrix(Environment::inverseShipMatrix);
|
||||
|
||||
renderer.TranslateMatrix(-Environment::shipPosition);
|
||||
|
||||
|
||||
|
||||
renderer.RenderUniform1f("uDistanceToPlanetSurface", dist);
|
||||
renderer.RenderUniform1f("uCurrentZFar", currentZFar);
|
||||
|
||||
renderer.RenderUniform3fv("uViewPos", &Environment::shipPosition.v[0]);
|
||||
|
||||
Vector3f color2 = { 1.0, 1.0, 0.0 };
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, sandTexture->getTexID());
|
||||
|
||||
renderer.RenderUniform3fv("uColor", &color2.v[0]);
|
||||
renderer.DrawVertexRenderStruct(planetRenderYellowStruct);
|
||||
//glDisable(GL_BLEND);
|
||||
CheckGlError();
|
||||
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
renderer.DisableVertexAttribArray(vNormalName);
|
||||
renderer.DisableVertexAttribArray("vTangent");
|
||||
renderer.DisableVertexAttribArray("vBinormal");
|
||||
renderer.DisableVertexAttribArray(vColorName);
|
||||
renderer.DisableVertexAttribArray(vPositionName);
|
||||
renderer.shaderManager.PopShader();
|
||||
CheckGlError();
|
||||
|
||||
}
|
||||
drawAtmosphere(renderer);
|
||||
}
|
||||
|
||||
void PlanetObject::drawAtmosphere(Renderer& renderer) {
|
||||
@ -518,8 +326,8 @@ namespace ZL {
|
||||
renderer.EnableVertexAttribArray(vNormalName);
|
||||
|
||||
|
||||
float dist = planetData.distanceToPlanetSurface(Environment::shipPosition);
|
||||
auto zRange = planetData.calculateZRange(dist);
|
||||
|
||||
const auto zRange = calculateZRange(Environment::shipPosition);
|
||||
const float currentZNear = zRange.first;
|
||||
const float currentZFar = zRange.second;
|
||||
|
||||
@ -542,12 +350,13 @@ namespace ZL {
|
||||
|
||||
renderer.RenderUniformMatrix4fv("ModelViewMatrix", false, &viewMatrix.m[0]);
|
||||
|
||||
float dist = distanceToPlanetSurface();
|
||||
renderer.RenderUniform1f("uDistanceToPlanetSurface", dist);
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE);// Аддитивное смешивание для эффекта свечения
|
||||
|
||||
renderer.DrawVertexRenderStruct(planetAtmosphereRenderStruct);
|
||||
renderer.DrawVertexRenderStruct(planetAtmosphere);
|
||||
glDisable(GL_BLEND);
|
||||
glDepthMask(GL_TRUE);
|
||||
renderer.PopMatrix();
|
||||
@ -560,9 +369,212 @@ namespace ZL {
|
||||
|
||||
}
|
||||
|
||||
float PlanetObject::distanceToPlanetSurface(const Vector3f& viewerPosition)
|
||||
{
|
||||
return planetData.distanceToPlanetSurface(viewerPosition);
|
||||
void PlanetObject::update(float deltaTimeMs) {
|
||||
|
||||
}
|
||||
|
||||
std::vector<Triangle> PlanetObject::subdivideTriangles(const std::vector<Triangle>& inputTriangles) {
|
||||
std::vector<Triangle> output;
|
||||
output.reserve(inputTriangles.size() * 4);
|
||||
|
||||
for (const auto& t : inputTriangles) {
|
||||
Vector3f a = t.data[0];
|
||||
Vector3f b = t.data[1];
|
||||
Vector3f c = t.data[2];
|
||||
|
||||
// 1. Вычисляем "сырые" середины
|
||||
Vector3f m_ab = (a + b) * 0.5f;
|
||||
Vector3f m_bc = (b + c) * 0.5f;
|
||||
Vector3f m_ac = (a + c) * 0.5f;
|
||||
|
||||
// 2. Нормализуем их (получаем идеальную сферу радиуса 1)
|
||||
m_ab = m_ab.normalized();
|
||||
m_bc = m_bc.normalized();
|
||||
m_ac = m_ac.normalized();
|
||||
|
||||
// 3. ПРИМЕНЯЕМ ШУМ: Смещаем точку по радиусу
|
||||
m_ab = m_ab * perlin.getSurfaceHeight(m_ab);
|
||||
m_bc = m_bc * perlin.getSurfaceHeight(m_bc);
|
||||
m_ac = m_ac * perlin.getSurfaceHeight(m_ac);
|
||||
|
||||
// 4. Формируем новые треугольники
|
||||
output.emplace_back(a, m_ab, m_ac);
|
||||
output.emplace_back(m_ab, b, m_bc);
|
||||
output.emplace_back(m_ac, m_bc, c);
|
||||
output.emplace_back(m_ab, m_bc, m_ac);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
Vector3f PlanetObject::calculateSurfaceNormal(Vector3f p_sphere) {
|
||||
// p_sphere - это нормализованный вектор (точка на идеальной сфере)
|
||||
|
||||
float theta = 0.01f; // Шаг для "щупанья" соседей (epsilon)
|
||||
|
||||
// Нам нужно найти два вектора, касательных к сфере в точке p_sphere.
|
||||
// Для этого берем любой вектор (например UP), делаем Cross Product, чтобы получить касательную.
|
||||
// Если p_sphere совпадает с UP, берем RIGHT.
|
||||
Vector3f up = Vector3f(0.0f, 1.0f, 0.0f);
|
||||
if (abs(p_sphere.dot(up)) > 0.99f) {
|
||||
up = Vector3f(1.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
Vector3f tangentX = (up.cross(p_sphere)).normalized();
|
||||
Vector3f tangentY = (p_sphere.cross(tangentX)).normalized();
|
||||
|
||||
// Точки на идеальной сфере со смещением
|
||||
Vector3f p0_dir = p_sphere;
|
||||
Vector3f p1_dir = (p_sphere + tangentX * theta).normalized();
|
||||
Vector3f p2_dir = (p_sphere + tangentY * theta).normalized();
|
||||
|
||||
// Реальные точки на искаженной поверхности
|
||||
// p = dir * height(dir)
|
||||
Vector3f p0 = p0_dir * perlin.getSurfaceHeight(p0_dir);
|
||||
Vector3f p1 = p1_dir * perlin.getSurfaceHeight(p1_dir);
|
||||
Vector3f p2 = p2_dir * perlin.getSurfaceHeight(p2_dir);
|
||||
|
||||
// Вектора от центральной точки к соседям
|
||||
Vector3f v1 = p1 - p0;
|
||||
Vector3f v2 = p2 - p0;
|
||||
|
||||
// Нормаль - это перпендикуляр к этим двум векторам
|
||||
// Порядок (v2, v1) или (v1, v2) зависит от системы координат,
|
||||
// здесь подбираем так, чтобы нормаль смотрела наружу.
|
||||
return (-v2.cross(v1)).normalized();
|
||||
}
|
||||
|
||||
VertexDataStruct PlanetObject::trianglesToVertices(const std::vector<Triangle>& triangles) {
|
||||
VertexDataStruct buffer;
|
||||
buffer.PositionData.reserve(triangles.size() * 3);
|
||||
buffer.NormalData.reserve(triangles.size() * 3);
|
||||
buffer.TexCoordData.reserve(triangles.size() * 3); // <-- РЕЗЕРВИРУЕМ
|
||||
//buffer.TexCoord3Data.reserve(triangles.size() * 3); // <-- РЕЗЕРВИРУЕМ
|
||||
|
||||
// Стандартные UV-координаты для покрытия одного треугольника
|
||||
// Покрывает текстурой всю грань.
|
||||
const std::array<Vector2f, 3> triangleUVs = {
|
||||
Vector2f(0.0f, 0.0f),
|
||||
Vector2f(1.0f, 0.0f),
|
||||
Vector2f(0.0f, 1.0f)
|
||||
};
|
||||
/*
|
||||
const std::array<Vector3f, 3> barycentricCoords = {
|
||||
Vector3f(1.0f, 0.0f, 0.0f), // Вершина 1
|
||||
Vector3f(0.0f, 1.0f, 0.0f), // Вершина 2
|
||||
Vector3f(0.0f, 0.0f, 1.0f) // Вершина 3
|
||||
};*/
|
||||
|
||||
for (const auto& t : triangles) {
|
||||
// Проходим по всем 3 вершинам треугольника
|
||||
for (int i = 0; i < 3; i++) {
|
||||
// p_geometry - это уже точка на поверхности (с шумом)
|
||||
Vector3f p_geometry = t.data[i];
|
||||
|
||||
// Нам нужно восстановить направление от центра к этой точке,
|
||||
// чтобы передать его в функцию расчета нормали.
|
||||
// Так как (0,0,0) - центр, то normalize(p) даст нам направление.
|
||||
Vector3f p_dir = p_geometry.normalized();
|
||||
|
||||
// Считаем аналитическую нормаль для этой конкретной точки
|
||||
Vector3f normal = calculateSurfaceNormal(p_dir);
|
||||
|
||||
buffer.PositionData.push_back({ p_geometry });
|
||||
buffer.NormalData.push_back({ normal });
|
||||
//buffer.TexCoord3Data.push_back(barycentricCoords[i]);
|
||||
buffer.TexCoordData.push_back(triangleUVs[i]);
|
||||
|
||||
}
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
VertexDataStruct PlanetObject::generateSphere(int subdivisions) {
|
||||
// 1. Исходный октаэдр
|
||||
std::vector<Triangle> geometry = {
|
||||
Triangle{{ 0.0f, 1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}}, // Top-Front-Right
|
||||
Triangle{{ 0.0f, 1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}}, // Top-Right-Back
|
||||
Triangle{{ 0.0f, 1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}, {-1.0f, 0.0f, 0.0f}}, // Top-Back-Left
|
||||
Triangle{{ 0.0f, 1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}}, // Top-Left-Front
|
||||
Triangle{{ 0.0f, -1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}}, // Bottom-Right-Front
|
||||
Triangle{{ 0.0f, -1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}}, // Bottom-Front-Left
|
||||
Triangle{{ 0.0f, -1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}}, // Bottom-Left-Back
|
||||
Triangle{{ 0.0f, -1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}, { 1.0f, 0.0f, 0.0f}} // Bottom-Back-Right
|
||||
};
|
||||
|
||||
// 2. ПРИМЕНЯЕМ ШУМ К ИСХОДНЫМ ВЕРШИНАМ
|
||||
for (auto& t : geometry) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
Vector3f dir = t.data[i].normalized();
|
||||
t.data[i] = dir * perlin.getSurfaceHeight(dir);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Разбиваем N раз
|
||||
for (int i = 0; i < subdivisions; i++) {
|
||||
geometry = subdivideTriangles(geometry);
|
||||
}
|
||||
|
||||
// 4. Генерируем вершины И НОРМАЛИ с помощью trianglesToVertices
|
||||
// ЭТО ЗАПОЛНИТ PositionData И NormalData
|
||||
VertexDataStruct buffer = trianglesToVertices(geometry);
|
||||
|
||||
// Теперь нам нужно заполнить ColorData, используя ту же логику, что и раньше.
|
||||
// Сначала резервируем место, так как trianglesToVertices не заполняет ColorData
|
||||
buffer.ColorData.reserve(geometry.size() * 3);
|
||||
|
||||
// Базовый цвет и параметры
|
||||
const Vector3f baseColor = { 0.498f, 0.416f, 0.0f };
|
||||
const float colorFrequency = 5.0f;
|
||||
const float colorAmplitude = 0.2f;
|
||||
const Vector3f offsetR = { 0.1f, 0.2f, 0.3f };
|
||||
const Vector3f offsetG = { 0.5f, 0.4f, 0.6f };
|
||||
const Vector3f offsetB = { 0.9f, 0.8f, 0.7f };
|
||||
|
||||
|
||||
for (const auto& t : geometry) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
// ВАЖНО: Мы используем геометрию из t.data[i] для получения направления,
|
||||
// а не PositionData из buffer, поскольку там может не быть нужного порядка.
|
||||
Vector3f p_geometry = t.data[i];
|
||||
Vector3f dir = p_geometry.normalized();
|
||||
|
||||
// Вычисление цветового шума (как вы делали)
|
||||
float noiseR = colorPerlin.noise(
|
||||
(dir.v[0] + offsetR.v[0]) * colorFrequency,
|
||||
(dir.v[1] + offsetR.v[1]) * colorFrequency,
|
||||
(dir.v[2] + offsetR.v[2]) * colorFrequency
|
||||
);
|
||||
float noiseG = colorPerlin.noise(
|
||||
(dir.v[0] + offsetG.v[0]) * colorFrequency,
|
||||
(dir.v[1] + offsetG.v[1]) * colorFrequency,
|
||||
(dir.v[2] + offsetG.v[2]) * colorFrequency
|
||||
);
|
||||
float noiseB = colorPerlin.noise(
|
||||
(dir.v[0] + offsetB.v[0]) * colorFrequency,
|
||||
(dir.v[1] + offsetB.v[1]) * colorFrequency,
|
||||
(dir.v[2] + offsetB.v[2]) * colorFrequency
|
||||
);
|
||||
|
||||
Vector3f colorOffset = {
|
||||
noiseR * colorAmplitude,
|
||||
noiseG * colorAmplitude,
|
||||
noiseB * colorAmplitude
|
||||
};
|
||||
|
||||
Vector3f finalColor = baseColor + colorOffset;
|
||||
|
||||
finalColor.v[0] = max(0.0f, min(1.0f, finalColor.v[0]));
|
||||
finalColor.v[1] = max(0.0f, min(1.0f, finalColor.v[1]));
|
||||
finalColor.v[2] = max(0.0f, min(1.0f, finalColor.v[2]));
|
||||
|
||||
// ДОБАВЛЯЕМ ТОЛЬКО ЦВЕТ, так как PositionData и NormalData уже заполнены!
|
||||
buffer.ColorData.push_back(finalColor);
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
102
PlanetObject.h
102
PlanetObject.h
@ -11,53 +11,69 @@
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include "Perlin.h"
|
||||
#include "PlanetData.h"
|
||||
#include "StoneObject.h"
|
||||
#include "FrameBuffer.h"
|
||||
|
||||
namespace ZL {
|
||||
|
||||
class PlanetObject {
|
||||
public:
|
||||
// Агрегация: логика и данные теперь здесь
|
||||
PlanetData planetData;
|
||||
struct Triangle
|
||||
{
|
||||
std::array<Vector3f, 3> data;
|
||||
|
||||
// Данные только для рендеринга (OpenGL specific)
|
||||
VertexRenderStruct planetRenderStruct;
|
||||
VertexRenderStruct planetRenderStructCut;
|
||||
VertexRenderStruct planetRenderYellowStruct;
|
||||
VertexRenderStruct planetAtmosphereRenderStruct;
|
||||
VertexRenderStruct planetStonesRenderStruct;
|
||||
VertexRenderStruct planetStonesToBakeRenderStruct;
|
||||
StoneGroup planetStones;
|
||||
|
||||
std::vector<int> triangleIndicesToDraw;
|
||||
|
||||
std::shared_ptr<Texture> sandTexture;
|
||||
std::shared_ptr<Texture> stoneTexture;
|
||||
|
||||
bool drawDataDirty = true;
|
||||
void prepareDrawData();
|
||||
|
||||
std::unique_ptr<FrameBuffer> stoneMapFB;
|
||||
|
||||
public:
|
||||
PlanetObject();
|
||||
|
||||
void init();
|
||||
void update(float deltaTimeMs);
|
||||
void bakeStoneTexture(Renderer& renderer);
|
||||
void draw(Renderer& renderer);
|
||||
void drawStones(Renderer& renderer);
|
||||
void drawPlanet(Renderer& renderer);
|
||||
void drawYellowZone(Renderer& renderer);
|
||||
void drawAtmosphere(Renderer& renderer);
|
||||
|
||||
|
||||
float distanceToPlanetSurface(const Vector3f& viewerPosition);
|
||||
Triangle(Vector3f p1, Vector3f p2, Vector3f p3)
|
||||
: data{ p1, p2, p3 }
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class PerlinNoise {
|
||||
std::vector<int> p;
|
||||
public:
|
||||
PerlinNoise();
|
||||
PerlinNoise(uint64_t seed);
|
||||
|
||||
float fade(float t);
|
||||
float lerp(float t, float a, float b);
|
||||
float grad(int hash, float x, float y, float z);
|
||||
|
||||
float noise(float x, float y, float z);
|
||||
|
||||
float getSurfaceHeight(Vector3f pos);
|
||||
};
|
||||
|
||||
|
||||
class PlanetObject {
|
||||
private:
|
||||
PerlinNoise perlin;
|
||||
PerlinNoise colorPerlin;
|
||||
void prepareDrawData();
|
||||
VertexDataStruct planetMesh;
|
||||
VertexRenderStruct planetRenderStruct;
|
||||
|
||||
VertexRenderStruct planetAtmosphere;
|
||||
|
||||
|
||||
std::shared_ptr<Texture> sandTexture;
|
||||
|
||||
public:
|
||||
PlanetObject();
|
||||
|
||||
void init();
|
||||
|
||||
void update(float deltaTimeMs);
|
||||
|
||||
void draw(Renderer& renderer);
|
||||
void drawAtmosphere(Renderer& renderer);
|
||||
|
||||
bool planetIsFar();
|
||||
float distanceToPlanetSurface();
|
||||
|
||||
|
||||
private:
|
||||
bool drawDataDirty = true;
|
||||
|
||||
std::vector<Triangle> subdivideTriangles(const std::vector<Triangle>& inputTriangles);
|
||||
Vector3f calculateSurfaceNormal(Vector3f p_sphere);
|
||||
VertexDataStruct trianglesToVertices(const std::vector<Triangle>& triangles);
|
||||
VertexDataStruct generateSphere(int subdivisions);
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
@ -1,71 +0,0 @@
|
||||
#include "Projectile.h"
|
||||
|
||||
namespace ZL {
|
||||
|
||||
Projectile::Projectile()
|
||||
: pos({ 0,0,0 })
|
||||
, vel({ 0,0,0 })
|
||||
, life(0.0f)
|
||||
, maxLife(0.0f)
|
||||
, active(false)
|
||||
, size(0.5f)
|
||||
, texture(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
void Projectile::init(const Vector3f& startPos, const Vector3f& startVel, float lifeMs, float s, std::shared_ptr<Texture> tex, Renderer& renderer) {
|
||||
pos = startPos;
|
||||
vel = startVel;
|
||||
life = 0.0f;
|
||||
maxLife = lifeMs;
|
||||
size = s;
|
||||
texture = tex;
|
||||
active = true;
|
||||
|
||||
rebuildMesh(renderer);
|
||||
mesh.RefreshVBO();
|
||||
}
|
||||
|
||||
void Projectile::update(float deltaMs, Renderer& renderer) {
|
||||
if (!active) return;
|
||||
|
||||
pos = pos + vel * (deltaMs / 1000.0f);
|
||||
life += deltaMs;
|
||||
if (life >= maxLife) {
|
||||
active = false;
|
||||
return;
|
||||
}
|
||||
|
||||
rebuildMesh(renderer);
|
||||
mesh.RefreshVBO();
|
||||
}
|
||||
|
||||
void Projectile::rebuildMesh(Renderer&) {
|
||||
float half = size * 0.5f;
|
||||
|
||||
mesh.data.PositionData.clear();
|
||||
mesh.data.TexCoordData.clear();
|
||||
|
||||
mesh.data.PositionData.push_back({ pos.v[0] - half, pos.v[1] - half, pos.v[2] });
|
||||
mesh.data.PositionData.push_back({ pos.v[0] - half, pos.v[1] + half, pos.v[2] });
|
||||
mesh.data.PositionData.push_back({ pos.v[0] + half, pos.v[1] + half, pos.v[2] });
|
||||
|
||||
mesh.data.PositionData.push_back({ pos.v[0] - half, pos.v[1] - half, pos.v[2] });
|
||||
mesh.data.PositionData.push_back({ pos.v[0] + half, pos.v[1] + half, pos.v[2] });
|
||||
mesh.data.PositionData.push_back({ pos.v[0] + half, pos.v[1] - half, pos.v[2] });
|
||||
|
||||
mesh.data.TexCoordData.push_back({ 0.0f, 0.0f });
|
||||
mesh.data.TexCoordData.push_back({ 0.0f, 1.0f });
|
||||
mesh.data.TexCoordData.push_back({ 1.0f, 1.0f });
|
||||
mesh.data.TexCoordData.push_back({ 0.0f, 0.0f });
|
||||
mesh.data.TexCoordData.push_back({ 1.0f, 1.0f });
|
||||
mesh.data.TexCoordData.push_back({ 1.0f, 0.0f });
|
||||
}
|
||||
|
||||
void Projectile::draw(Renderer& renderer) const {
|
||||
if (!active || !texture) return;
|
||||
glBindTexture(GL_TEXTURE_2D, texture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(mesh);
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
36
Projectile.h
36
Projectile.h
@ -1,36 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "ZLMath.h"
|
||||
#include "Renderer.h"
|
||||
#include "TextureManager.h"
|
||||
#include <memory>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
class Projectile {
|
||||
public:
|
||||
Projectile();
|
||||
~Projectile() = default;
|
||||
|
||||
void init(const Vector3f& startPos, const Vector3f& startVel, float lifeMs, float size, std::shared_ptr<Texture> tex, Renderer& renderer);
|
||||
void update(float deltaMs, Renderer& renderer);
|
||||
void draw(Renderer& renderer) const;
|
||||
|
||||
bool isActive() const { return active; }
|
||||
|
||||
Vector3f getPosition() const { return pos; }
|
||||
|
||||
private:
|
||||
Vector3f pos;
|
||||
Vector3f vel;
|
||||
float life;
|
||||
float maxLife;
|
||||
bool active;
|
||||
float size;
|
||||
VertexRenderStruct mesh;
|
||||
std::shared_ptr<Texture> texture;
|
||||
|
||||
void rebuildMesh(Renderer& renderer);
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
43
Renderer.cpp
43
Renderer.cpp
@ -294,6 +294,19 @@ namespace ZL {
|
||||
glBufferData(GL_ARRAY_BUFFER, data.TexCoordData.size() * 8, &data.TexCoordData[0], GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
if (data.TexCoord3Data.size() > 0)
|
||||
{
|
||||
if (!texCoord3VBO)
|
||||
{
|
||||
texCoord3VBO = std::make_shared<VBOHolder>();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, texCoord3VBO->getBuffer());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, data.TexCoord3Data.size() * 12, &data.TexCoord3Data[0], GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
|
||||
if (data.NormalData.size() > 0)
|
||||
{
|
||||
if (!normalVBO)
|
||||
@ -429,18 +442,6 @@ namespace ZL {
|
||||
throw std::runtime_error("Projection matrix stack overflow!!!!");
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::PushProjectionMatrix(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar)
|
||||
{
|
||||
Matrix4f m = MakeOrthoMatrix(xmin, xmax, ymin, ymax, zNear, zFar);
|
||||
ProjectionMatrixStack.push(m);
|
||||
SetMatrix();
|
||||
|
||||
if (ProjectionMatrixStack.size() > CONST_MATRIX_STACK_SIZE)
|
||||
{
|
||||
throw std::runtime_error("Projection matrix stack overflow!!!!");
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::PushPerspectiveProjectionMatrix(float fovY, float aspectRatio, float zNear, float zFar)
|
||||
{
|
||||
@ -688,18 +689,6 @@ namespace ZL {
|
||||
glDisableVertexAttribArray(shader->attribList[attribName]);
|
||||
}
|
||||
|
||||
void Renderer::RenderUniformMatrix3fv(const std::string& uniformName, bool transpose, const float* value)
|
||||
{
|
||||
auto shader = shaderManager.GetCurrentShader();
|
||||
|
||||
auto uniform = shader->uniformList.find(uniformName);
|
||||
|
||||
if (uniform != shader->uniformList.end())
|
||||
{
|
||||
glUniformMatrix3fv(uniform->second, 1, transpose, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Renderer::RenderUniformMatrix4fv(const std::string& uniformName, bool transpose, const float* value)
|
||||
{
|
||||
@ -775,6 +764,7 @@ namespace ZL {
|
||||
static const std::string vBinormal("vBinormal");
|
||||
static const std::string vColor("vColor");
|
||||
static const std::string vTexCoord("vTexCoord");
|
||||
static const std::string vTexCoord3("vTexCoord3");
|
||||
static const std::string vPosition("vPosition");
|
||||
|
||||
//glBindVertexArray(VertexRenderStruct.vao->getBuffer());
|
||||
@ -805,6 +795,11 @@ namespace ZL {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.texCoordVBO->getBuffer());
|
||||
VertexAttribPointer2fv(vTexCoord, 0, NULL);
|
||||
}
|
||||
if (VertexRenderStruct.data.TexCoord3Data.size() > 0)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.texCoord3VBO->getBuffer());
|
||||
VertexAttribPointer2fv(vTexCoord3, 0, NULL);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.positionVBO->getBuffer());
|
||||
VertexAttribPointer3fv(vPosition, 0, NULL);
|
||||
|
||||
@ -44,6 +44,7 @@ namespace ZL {
|
||||
{
|
||||
std::vector<Vector3f> PositionData;
|
||||
std::vector<Vector2f> TexCoordData;
|
||||
std::vector<Vector3f> TexCoord3Data;
|
||||
std::vector<Vector3f> NormalData;
|
||||
std::vector<Vector3f> TangentData;
|
||||
std::vector<Vector3f> BinormalData;
|
||||
@ -63,6 +64,7 @@ namespace ZL {
|
||||
std::shared_ptr<VAOHolder> vao;
|
||||
std::shared_ptr<VBOHolder> positionVBO;
|
||||
std::shared_ptr<VBOHolder> texCoordVBO;
|
||||
std::shared_ptr<VBOHolder> texCoord3VBO;
|
||||
std::shared_ptr<VBOHolder> normalVBO;
|
||||
std::shared_ptr<VBOHolder> tangentVBO;
|
||||
std::shared_ptr<VBOHolder> binormalVBO;
|
||||
@ -93,7 +95,6 @@ namespace ZL {
|
||||
void InitOpenGL();
|
||||
|
||||
void PushProjectionMatrix(float width, float height, float zNear = 0.f, float zFar = 1.f);
|
||||
void PushProjectionMatrix(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar);
|
||||
void PushPerspectiveProjectionMatrix(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
void PopProjectionMatrix();
|
||||
|
||||
@ -120,7 +121,6 @@ namespace ZL {
|
||||
void DisableVertexAttribArray(const std::string& attribName);
|
||||
|
||||
|
||||
void RenderUniformMatrix3fv(const std::string& uniformName, bool transpose, const float* value);
|
||||
void RenderUniformMatrix4fv(const std::string& uniformName, bool transpose, const float* value);
|
||||
void RenderUniform1i(const std::string& uniformName, const int value);
|
||||
void RenderUniform3fv(const std::string& uniformName, const float* value);
|
||||
|
||||
@ -11,8 +11,6 @@ namespace ZL {
|
||||
|
||||
char infoLog[CONST_INFOLOG_LENGTH];
|
||||
int infoLogLength;
|
||||
char infoLog2[CONST_INFOLOG_LENGTH];
|
||||
int infoLogLength2;
|
||||
|
||||
int vertexShaderCompiled;
|
||||
int fragmentShaderCompiled;
|
||||
@ -39,8 +37,7 @@ namespace ZL {
|
||||
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentShaderCompiled);
|
||||
glGetShaderInfoLog(fragmentShader, CONST_INFOLOG_LENGTH, &infoLogLength2, infoLog2);
|
||||
|
||||
glGetShaderInfoLog(fragmentShader, CONST_INFOLOG_LENGTH, &infoLogLength, infoLog);
|
||||
|
||||
if (!vertexShaderCompiled)
|
||||
{
|
||||
|
||||
260
SparkEmitter.cpp
260
SparkEmitter.cpp
@ -2,19 +2,13 @@
|
||||
#include <random>
|
||||
#include <cmath>
|
||||
#include "OpenGlExtensions.h"
|
||||
#include <fstream>
|
||||
#include "external/nlohmann/json.hpp"
|
||||
#include <iostream>
|
||||
#include "Environment.h"
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
namespace ZL {
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
SparkEmitter::SparkEmitter()
|
||||
: emissionRate(100.0f), isActive(true), drawDataDirty(true), maxParticles(200),
|
||||
shaderProgramName("default"), particleSize(0.04f), biasX(0.3f), configured(false) {
|
||||
: emissionRate(100.0f), isActive(true), drawDataDirty(true), maxParticles(200) {
|
||||
particles.resize(maxParticles);
|
||||
drawPositions.reserve(maxParticles * 6);
|
||||
drawTexCoords.reserve(maxParticles * 6);
|
||||
@ -25,8 +19,7 @@ namespace ZL {
|
||||
|
||||
SparkEmitter::SparkEmitter(const std::vector<Vector3f>& positions, float rate)
|
||||
: emissionPoints(positions), emissionRate(rate), isActive(true),
|
||||
drawDataDirty(true), maxParticles(positions.size() * 100),
|
||||
shaderProgramName("default"), particleSize(0.04f), biasX(0.3f), configured(false) {
|
||||
drawDataDirty(true), maxParticles(positions.size() * 100) {
|
||||
particles.resize(maxParticles);
|
||||
drawPositions.reserve(maxParticles * 6);
|
||||
drawTexCoords.reserve(maxParticles * 6);
|
||||
@ -39,8 +32,7 @@ namespace ZL {
|
||||
std::shared_ptr<Texture> tex,
|
||||
float rate)
|
||||
: emissionPoints(positions), texture(tex), emissionRate(rate),
|
||||
isActive(true), drawDataDirty(true), maxParticles(positions.size() * 100),
|
||||
shaderProgramName("default"), particleSize(0.04f), biasX(0.3f), configured(false) {
|
||||
isActive(true), drawDataDirty(true), maxParticles(positions.size() * 100) {
|
||||
particles.resize(maxParticles);
|
||||
drawPositions.reserve(maxParticles * 6);
|
||||
drawTexCoords.reserve(maxParticles * 6);
|
||||
@ -81,7 +73,7 @@ namespace ZL {
|
||||
for (const auto& [particlePtr, depth] : sortedParticles) {
|
||||
const auto& particle = *particlePtr;
|
||||
Vector3f pos = particle.position;
|
||||
float size = particleSize * particle.scale;
|
||||
float size = 0.04f * particle.scale;
|
||||
|
||||
drawPositions.push_back({ pos.v[0] - size, pos.v[1] - size, pos.v[2] });
|
||||
drawTexCoords.push_back({ 0.0f, 0.0f });
|
||||
@ -106,16 +98,12 @@ namespace ZL {
|
||||
}
|
||||
|
||||
void SparkEmitter::draw(Renderer& renderer, float zoom, int screenWidth, int screenHeight) {
|
||||
if (!configured) {
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
if (getActiveParticleCount() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!texture) {
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
return;
|
||||
}
|
||||
|
||||
prepareDrawData();
|
||||
@ -128,11 +116,12 @@ namespace ZL {
|
||||
sparkQuad.data.TexCoordData = drawTexCoords;
|
||||
sparkQuad.RefreshVBO();
|
||||
|
||||
static const std::string defaultShaderName = "default";
|
||||
static const std::string vPositionName = "vPosition";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
renderer.shaderManager.PushShader(shaderProgramName);
|
||||
renderer.shaderManager.PushShader(defaultShaderName);
|
||||
renderer.RenderUniform1i(textureUniformName, 0);
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
@ -143,7 +132,7 @@ namespace ZL {
|
||||
glBindTexture(GL_TEXTURE_2D, texture->getTexID());
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE);// Аддитивное смешивание для эффекта свечения
|
||||
|
||||
renderer.PushMatrix();
|
||||
renderer.LoadIdentity();
|
||||
@ -162,10 +151,6 @@ namespace ZL {
|
||||
}
|
||||
|
||||
void SparkEmitter::update(float deltaTimeMs) {
|
||||
if (!configured) {
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
auto currentTime = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
currentTime - lastEmissionTime).count();
|
||||
@ -212,10 +197,6 @@ namespace ZL {
|
||||
}
|
||||
|
||||
void SparkEmitter::emit() {
|
||||
if (!configured) {
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
if (emissionPoints.empty()) return;
|
||||
bool emitted = false;
|
||||
|
||||
@ -262,51 +243,39 @@ namespace ZL {
|
||||
|
||||
void SparkEmitter::setEmissionPoints(const std::vector<Vector3f>& positions) {
|
||||
emissionPoints = positions;
|
||||
maxParticles = positions.size() * 100;
|
||||
particles.resize(maxParticles);
|
||||
drawDataDirty = true;
|
||||
}
|
||||
|
||||
void SparkEmitter::initParticle(SparkParticle& particle, int emitterIndex) {
|
||||
if (!configured) {
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
particle.velocity = getRandomVelocity(emitterIndex);
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
std::uniform_real_distribution<float> scaleDist(scaleRange.min, scaleRange.max);
|
||||
particle.scale = scaleDist(gen);
|
||||
|
||||
std::uniform_real_distribution<float> lifeDist(lifeTimeRange.min, lifeTimeRange.max);
|
||||
particle.maxLifeTime = lifeDist(gen);
|
||||
particle.scale = 1.0f;
|
||||
particle.maxLifeTime = 800.0f + (rand() % 400);
|
||||
particle.emitterIndex = emitterIndex;
|
||||
}
|
||||
|
||||
Vector3f SparkEmitter::getRandomVelocity(int emitterIndex) {
|
||||
if (!configured) {
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
std::uniform_real_distribution<float> angleDist(0.0f, 2.0f * static_cast<float>(M_PI));
|
||||
std::uniform_real_distribution<float> speedDist(speedRange.min, speedRange.max);
|
||||
std::uniform_real_distribution<float> zSpeedDist(zSpeedRange.min, zSpeedRange.max);
|
||||
static std::uniform_real_distribution<> angleDist(0, 2 * M_PI);
|
||||
static std::uniform_real_distribution<> speedDist(0.5f, 2.0f);
|
||||
static std::uniform_real_distribution<> zSpeedDist(1.0f, 3.0f);
|
||||
|
||||
float angle = angleDist(gen);
|
||||
float speed = speedDist(gen);
|
||||
float zSpeed = zSpeedDist(gen);
|
||||
|
||||
// Теперь biasX берется из JSON
|
||||
if (emitterIndex == 0) {
|
||||
return Vector3f{
|
||||
cosf(angle) * speed - biasX,
|
||||
cosf(angle) * speed - 0.3f,
|
||||
sinf(angle) * speed,
|
||||
zSpeed
|
||||
};
|
||||
}
|
||||
else {
|
||||
return Vector3f{
|
||||
cosf(angle) * speed + biasX,
|
||||
cosf(angle) * speed + 0.3f,
|
||||
sinf(angle) * speed,
|
||||
zSpeed
|
||||
};
|
||||
@ -327,197 +296,4 @@ namespace ZL {
|
||||
return count;
|
||||
}
|
||||
|
||||
bool SparkEmitter::loadFromJsonFile(const std::string& path, Renderer& renderer, const std::string& zipFile) {
|
||||
std::cout << "Loading spark config from: " << path << std::endl;
|
||||
std::ifstream in(path);
|
||||
if (!in.is_open()) {
|
||||
std::cerr << "Failed to open JSON file: " << path << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
json j;
|
||||
try {
|
||||
in >> j;
|
||||
std::cout << "JSON parsed successfully" << std::endl;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "JSON parse error: " << e.what() << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
std::cout << "JSON content: " << j.dump(2) << std::endl;
|
||||
|
||||
auto requireKey = [&](const std::string& key) {
|
||||
if (!j.contains(key)) {
|
||||
std::cerr << "Missing required key in spark JSON: " << key << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
};
|
||||
|
||||
requireKey("emissionPoints");
|
||||
requireKey("texture");
|
||||
requireKey("speedRange");
|
||||
requireKey("zSpeedRange");
|
||||
requireKey("scaleRange");
|
||||
requireKey("lifeTimeRange");
|
||||
requireKey("emissionRate");
|
||||
requireKey("maxParticles");
|
||||
requireKey("particleSize");
|
||||
requireKey("biasX");
|
||||
requireKey("shaderProgramName");
|
||||
|
||||
emissionRate = j["emissionRate"].get<float>();
|
||||
|
||||
maxParticles = j["maxParticles"].get<int>();
|
||||
particles.resize(maxParticles);
|
||||
drawPositions.reserve(maxParticles * 6);
|
||||
drawTexCoords.reserve(maxParticles * 6);
|
||||
std::cout << "Max particles: " << maxParticles << std::endl;
|
||||
|
||||
particleSize = j["particleSize"].get<float>();
|
||||
std::cout << "Particle size: " << particleSize << std::endl;
|
||||
|
||||
biasX = j["biasX"].get<float>();
|
||||
std::cout << "Bias X: " << biasX << std::endl;
|
||||
|
||||
// emissionPoints
|
||||
std::vector<Vector3f> points;
|
||||
if (j.contains("emissionPoints") && j["emissionPoints"].is_array()) {
|
||||
for (const auto& el : j["emissionPoints"]) {
|
||||
if (el.contains("position") && el["position"].is_array()) {
|
||||
auto arr = el["position"];
|
||||
points.push_back(Vector3f{ arr[0].get<float>(), arr[1].get<float>(), arr[2].get<float>() });
|
||||
std::cout << "Fixed point: [" << arr[0] << ", " << arr[1] << ", " << arr[2] << "]" << std::endl;
|
||||
}
|
||||
else if (el.contains("positionRange") && el["positionRange"].is_object()) {
|
||||
auto pr = el["positionRange"];
|
||||
auto minArr = pr["min"];
|
||||
auto maxArr = pr["max"];
|
||||
int count = 1;
|
||||
if (el.contains("count")) count = el["count"].get<int>();
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_real_distribution<float> dx(minArr[0].get<float>(), maxArr[0].get<float>());
|
||||
std::uniform_real_distribution<float> dy(minArr[1].get<float>(), maxArr[1].get<float>());
|
||||
std::uniform_real_distribution<float> dz(minArr[2].get<float>(), maxArr[2].get<float>());
|
||||
|
||||
for (int k = 0; k < count; ++k) {
|
||||
Vector3f randomPoint{ dx(gen), dy(gen), dz(gen) };
|
||||
points.push_back(randomPoint);
|
||||
std::cout << "Random point " << k + 1 << ": [" << randomPoint.v[0]
|
||||
<< ", " << randomPoint.v[1] << ", " << randomPoint.v[2] << "]" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!points.empty()) {
|
||||
setEmissionPoints(points);
|
||||
std::cout << "Total emission points: " << emissionPoints.size() << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cerr << "Emission points parsed but empty" << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cerr << "emissionPoints is not array" << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
// Ranges
|
||||
if (j.contains("speedRange") && j["speedRange"].is_array()) {
|
||||
auto a = j["speedRange"];
|
||||
speedRange.min = a[0].get<float>();
|
||||
speedRange.max = a[1].get<float>();
|
||||
std::cout << "Speed range: [" << speedRange.min << ", " << speedRange.max << "]" << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cerr << "speedRange missing or invalid" << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
if (j.contains("zSpeedRange") && j["zSpeedRange"].is_array()) {
|
||||
auto a = j["zSpeedRange"];
|
||||
zSpeedRange.min = a[0].get<float>();
|
||||
zSpeedRange.max = a[1].get<float>();
|
||||
std::cout << "Z speed range: [" << zSpeedRange.min << ", " << zSpeedRange.max << "]" << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cerr << "zSpeedRange missing or invalid" << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
if (j.contains("scaleRange") && j["scaleRange"].is_array()) {
|
||||
auto a = j["scaleRange"];
|
||||
scaleRange.min = a[0].get<float>();
|
||||
scaleRange.max = a[1].get<float>();
|
||||
std::cout << "Scale range: [" << scaleRange.min << ", " << scaleRange.max << "]" << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cerr << "scaleRange missing or invalid" << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
if (j.contains("lifeTimeRange") && j["lifeTimeRange"].is_array()) {
|
||||
auto a = j["lifeTimeRange"];
|
||||
lifeTimeRange.min = a[0].get<float>();
|
||||
lifeTimeRange.max = a[1].get<float>();
|
||||
std::cout << "Life time range: [" << lifeTimeRange.min << ", " << lifeTimeRange.max << "]" << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cerr << "lifeTimeRange missing or invalid" << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
// texture
|
||||
if (j.contains("texture") && j["texture"].is_string()) {
|
||||
std::string texPath = j["texture"].get<std::string>();
|
||||
std::cout << "Loading texture: " << texPath << std::endl;
|
||||
|
||||
try {
|
||||
auto texData = CreateTextureDataFromPng(texPath.c_str(), zipFile.c_str());
|
||||
texture = std::make_shared<Texture>(texData);
|
||||
std::cout << "Texture loaded successfully, ID: " << texture->getTexID() << std::endl;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Texture load error: " << e.what() << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cerr << "No texture specified or invalid type in JSON" << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
// shaders
|
||||
if (j.contains("shaderProgramName") && j["shaderProgramName"].is_string()) {
|
||||
shaderProgramName = j["shaderProgramName"].get<std::string>();
|
||||
std::cout << "Shader program name: " << shaderProgramName << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cerr << "shaderProgramName missing or invalid" << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
|
||||
if (j.contains("vertexShader") && j.contains("fragmentShader")
|
||||
&& j["vertexShader"].is_string() && j["fragmentShader"].is_string()) {
|
||||
std::string v = j["vertexShader"].get<std::string>();
|
||||
std::string f = j["fragmentShader"].get<std::string>();
|
||||
std::cout << "Loading shaders - vertex: " << v << ", fragment: " << f << std::endl;
|
||||
|
||||
try {
|
||||
renderer.shaderManager.AddShaderFromFiles(shaderProgramName, v, f, zipFile.c_str());
|
||||
std::cout << "Shaders loaded successfully" << std::endl;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Shader load error: " << e.what() << std::endl;
|
||||
throw std::runtime_error("Failed to load spark emitter config file!");
|
||||
}
|
||||
}
|
||||
|
||||
drawDataDirty = true;
|
||||
configured = true;
|
||||
std::cout << "SparkEmitter configuration loaded successfully!" << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
@ -5,7 +5,6 @@
|
||||
#include "TextureManager.h"
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
@ -38,19 +37,7 @@ namespace ZL {
|
||||
std::shared_ptr<Texture> texture;
|
||||
|
||||
int maxParticles;
|
||||
float particleSize;
|
||||
float biasX;
|
||||
|
||||
// Ranges (used when config supplies intervals)
|
||||
struct FloatRange { float min; float max; };
|
||||
FloatRange speedRange; // XY speed
|
||||
FloatRange zSpeedRange; // Z speed
|
||||
FloatRange scaleRange;
|
||||
FloatRange lifeTimeRange;
|
||||
|
||||
std::string shaderProgramName;
|
||||
|
||||
bool configured;
|
||||
void prepareDrawData();
|
||||
|
||||
public:
|
||||
@ -62,13 +49,6 @@ namespace ZL {
|
||||
|
||||
void setEmissionPoints(const std::vector<Vector3f>& positions);
|
||||
void setTexture(std::shared_ptr<Texture> tex);
|
||||
void setEmissionRate(float rate) { emissionRate = rate; }
|
||||
void setShaderProgramName(const std::string& name) { shaderProgramName = name; }
|
||||
void setMaxParticles(int max) { maxParticles = max; particles.resize(maxParticles); }
|
||||
void setParticleSize(float size) { particleSize = size; }
|
||||
void setBiasX(float bias) { biasX = bias; }
|
||||
|
||||
bool loadFromJsonFile(const std::string& path, Renderer& renderer, const std::string& zipFile = "");
|
||||
|
||||
void update(float deltaTimeMs);
|
||||
void emit();
|
||||
@ -77,7 +57,6 @@ namespace ZL {
|
||||
|
||||
const std::vector<SparkParticle>& getParticles() const;
|
||||
size_t getActiveParticleCount() const;
|
||||
std::shared_ptr<Texture> getTexture() const { return texture; }
|
||||
|
||||
private:
|
||||
void initParticle(SparkParticle& particle, int emitterIndex);
|
||||
|
||||
276
StoneObject.cpp
276
StoneObject.cpp
@ -1,276 +0,0 @@
|
||||
#include "StoneObject.h"
|
||||
|
||||
#include "Utils.h"
|
||||
#include <GL/gl.h>
|
||||
#include <random>
|
||||
#include <cmath>
|
||||
#include "Renderer.h"
|
||||
#include "PlanetData.h"
|
||||
|
||||
namespace ZL {
|
||||
|
||||
|
||||
// --- ÊÎÍÑÒÀÍÒÛ ÏÀÐÀÌÅÒÐÎÂ (êàê âû ïðîñèëè) ---
|
||||
const float StoneParams::BASE_SCALE = 17.0f; // Îáùèé ðàçìåð êàìíÿ
|
||||
//const float StoneParams::BASE_SCALE = 5000.0f; // Îáùèé ðàçìåð êàìíÿ
|
||||
//const float StoneParams::MIN_AXIS_SCALE = 1.0f; // Ìèíèìàëüíîå ðàñòÿæåíèå/ñæàòèå ïî îñè
|
||||
//const float StoneParams::MAX_AXIS_SCALE = 1.0f; // Ìàêñèìàëüíîå ðàñòÿæåíèå/ñæàòèå ïî îñè
|
||||
//const float StoneParams::MIN_PERTURBATION = 0.0f; // Ìèíèìàëüíîå ðàäèàëüíîå âîçìóùåíèå âåðøèíû
|
||||
//const float StoneParams::MAX_PERTURBATION = 0.0f; // Ìàêñèìàëüíîå ðàäèàëüíîå âîçìóùåíèå âåðøèíû
|
||||
const float StoneParams::MIN_AXIS_SCALE = 0.5f; // Ìèíèìàëüíîå ðàñòÿæåíèå/ñæàòèå ïî îñè
|
||||
const float StoneParams::MAX_AXIS_SCALE = 1.5f; // Ìàêñèìàëüíîå ðàñòÿæåíèå/ñæàòèå ïî îñè
|
||||
const float StoneParams::MIN_PERTURBATION = 0.05f; // Ìèíèìàëüíîå ðàäèàëüíîå âîçìóùåíèå âåðøèíû
|
||||
const float StoneParams::MAX_PERTURBATION = 0.25f; // Ìàêñèìàëüíîå ðàäèàëüíîå âîçìóùåíèå âåðøèíû
|
||||
const int StoneParams::STONES_PER_TRIANGLE = 100;
|
||||
|
||||
// Âñïîìîãàòåëüíàÿ ôóíêöèÿ äëÿ ïîëó÷åíèÿ ñëó÷àéíîãî ÷èñëà â äèàïàçîíå [min, max]
|
||||
float getRandomFloat(std::mt19937& gen, float min, float max) {
|
||||
std::uniform_real_distribution<> distrib(min, max);
|
||||
return static_cast<float>(distrib(gen));
|
||||
}
|
||||
|
||||
// Âñïîìîãàòåëüíàÿ ôóíêöèÿ äëÿ ãåíåðàöèè ñëó÷àéíîé òî÷êè íà òðåóãîëüíèêå
|
||||
// Èñïîëüçóåò áàðèöåíòðè÷åñêèå êîîðäèíàòû
|
||||
Vector3f GetRandomPointOnTriangle(const Triangle& t, std::mt19937& engine) {
|
||||
std::uniform_real_distribution<> distrib(0.0f, 1.0f);
|
||||
|
||||
float r1 = getRandomFloat(engine, 0.0f, 1.0f);
|
||||
float r2 = getRandomFloat(engine, 0.0f, 1.0f);
|
||||
|
||||
// Ïðåîáðàçîâàíèå r1, r2 äëÿ ïîëó÷åíèÿ ðàâíîìåðíîãî ðàñïðåäåëåíèÿ
|
||||
float a = 1.0f - std::sqrt(r1);
|
||||
float b = std::sqrt(r1) * r2;
|
||||
float c = 1.0f - a - b; // c = sqrt(r1) * (1 - r2)
|
||||
|
||||
// Áàðèöåíòðè÷åñêèå êîîðäèíàòû
|
||||
// P = a*p1 + b*p2 + c*p3
|
||||
Vector3f p1_term = t.data[0] * a;
|
||||
Vector3f p2_term = t.data[1] * b;
|
||||
Vector3f p3_term = t.data[2] * c;
|
||||
|
||||
return p1_term + p2_term + p3_term;
|
||||
}
|
||||
|
||||
|
||||
// Èêîñàýäð (íà îñíîâå çîëîòîãî ñå÷åíèÿ phi)
|
||||
// Êîîðäèíàòû ìîãóò áûòü âû÷èñëåíû çàðàíåå äëÿ êîíñòàíòíîãî èêîñàýäðà.
|
||||
// Çäåñü òîëüêî îáúÿâëåíèå, ÷òîáû ïîêàçàòü èäåþ.
|
||||
|
||||
VertexDataStruct CreateBaseConvexPolyhedron(uint64_t seed) {
|
||||
|
||||
// const size_t SUBDIVISION_LEVEL = 1; // Óðîâåíü ïîäðàçäåëåíèÿ (äëÿ áîëåå êðóãëîãî êàìíÿ, ïîêà îïóñòèì)
|
||||
|
||||
std::mt19937 engine(static_cast<unsigned int>(seed));
|
||||
|
||||
// Çîëîòîå ñå÷åíèå
|
||||
const float t = (1.0f + std::sqrt(5.0f)) / 2.0f;
|
||||
|
||||
// 12 âåðøèí èêîñàýäðà
|
||||
std::vector<Vector3f> initialVertices = {
|
||||
{ -1, t, 0 }, { 1, t, 0 }, { -1, -t, 0 }, { 1, -t, 0 },
|
||||
{ 0, -1, t }, { 0, 1, t }, { 0, -1, -t }, { 0, 1, -t },
|
||||
{ t, 0, -1 }, { t, 0, 1 }, { -t, 0, -1 }, { -t, 0, 1 }
|
||||
};
|
||||
|
||||
// 20 òðåóãîëüíûõ ãðàíåé (èíäåêñû âåðøèí)
|
||||
std::vector<std::array<int, 3>> faces = {
|
||||
// 5 òðåóãîëüíèêîâ âîêðóã âåðøèíû 0
|
||||
{0, 11, 5}, {0, 5, 1}, {0, 1, 7}, {0, 7, 10}, {0, 10, 11},
|
||||
// 5 ñìåæíûõ ïîëîñ
|
||||
{1, 5, 9}, {5, 11, 4}, {11, 10, 2}, {10, 7, 6}, {7, 1, 8},
|
||||
// 5 òðåóãîëüíèêîâ âîêðóã âåðøèíû 3
|
||||
{3, 9, 4}, {3, 4, 2}, {3, 2, 6}, {3, 6, 8}, {3, 8, 9},
|
||||
// 5 ñìåæíûõ ïîëîñ
|
||||
{4, 9, 5}, {2, 4, 11}, {6, 2, 10}, {8, 6, 7}, {9, 8, 1}
|
||||
};
|
||||
|
||||
// 1. Íîðìàëèçàöèÿ è Âîçìóùåíèå (Perturbation)
|
||||
for (Vector3f& v : initialVertices) {
|
||||
v = v.normalized() * StoneParams::BASE_SCALE; // Íîðìàëèçàöèÿ ê ñôåðå ðàäèóñà BASE_SCALE
|
||||
|
||||
// Ðàäèàëüíîå âîçìóùåíèå:
|
||||
float perturbation = getRandomFloat(engine, StoneParams::MIN_PERTURBATION, StoneParams::MAX_PERTURBATION);
|
||||
v = v * (1.0f + perturbation);
|
||||
}
|
||||
|
||||
// 2. Òðàíñôîðìàöèÿ (Ìàñøòàáèðîâàíèå è Ïîâîðîò)
|
||||
|
||||
// Ñëó÷àéíûå ìàñøòàáû ïî îñÿì
|
||||
Vector3f scaleFactors = {
|
||||
getRandomFloat(engine, StoneParams::MIN_AXIS_SCALE, StoneParams::MAX_AXIS_SCALE),
|
||||
getRandomFloat(engine, StoneParams::MIN_AXIS_SCALE, StoneParams::MAX_AXIS_SCALE),
|
||||
getRandomFloat(engine, StoneParams::MIN_AXIS_SCALE, StoneParams::MAX_AXIS_SCALE)
|
||||
};
|
||||
|
||||
// Ïðèìåíÿåì ìàñøòàáèðîâàíèå
|
||||
for (Vector3f& v : initialVertices) {
|
||||
v.v[0] *= scaleFactors.v[0];
|
||||
v.v[1] *= scaleFactors.v[1];
|
||||
v.v[2] *= scaleFactors.v[2];
|
||||
}
|
||||
|
||||
// Ñëó÷àéíûé ïîâîðîò (íàïðèìåð, âîêðóã òðåõ îñåé)
|
||||
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));
|
||||
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
|
||||
VertexDataStruct result;
|
||||
// Êàðòà äëÿ íàêîïëåíèÿ íîðìàëåé ïî óíèêàëüíûì ïîçèöèÿì âåðøèí
|
||||
// (Òðåáóåò îïðåäåëåííîãî îïåðàòîðà < äëÿ Vector3f â ZLMath.h, êîòîðûé ó âàñ åñòü)
|
||||
std::map<Vector3f, Vector3f> smoothNormalsMap;
|
||||
|
||||
// Ïðåäâàðèòåëüíîå çàïîëíåíèå êàðòû íîðìàëÿìè
|
||||
for (const auto& face : faces) {
|
||||
Vector3f p1 = initialVertices[face[0]];
|
||||
Vector3f p2 = initialVertices[face[1]];
|
||||
Vector3f p3 = initialVertices[face[2]];
|
||||
|
||||
// Íîðìàëü ãðàíè: (p2 - p1) x (p3 - p1)
|
||||
Vector3f faceNormal = (p2 - p1).cross(p3 - p1).normalized();
|
||||
|
||||
smoothNormalsMap[p1] = smoothNormalsMap[p1] + faceNormal;
|
||||
smoothNormalsMap[p2] = smoothNormalsMap[p2] + faceNormal;
|
||||
smoothNormalsMap[p3] = smoothNormalsMap[p3] + faceNormal;
|
||||
}
|
||||
|
||||
// Íîðìàëèçàöèÿ íàêîïëåííûõ íîðìàëåé
|
||||
for (auto& pair : smoothNormalsMap) {
|
||||
pair.second = pair.second.normalized();
|
||||
}
|
||||
|
||||
|
||||
// 4. Ôèíàëüíîå çàïîëíåíèå VertexDataStruct è Òåêñòóðíûå Êîîðäèíàòû
|
||||
for (const auto& face : faces) {
|
||||
Vector3f p1 = initialVertices[face[0]];
|
||||
Vector3f p2 = initialVertices[face[1]];
|
||||
Vector3f p3 = initialVertices[face[2]];
|
||||
|
||||
// Ïîçèöèè
|
||||
result.PositionData.push_back(p1);
|
||||
result.PositionData.push_back(p2);
|
||||
result.PositionData.push_back(p3);
|
||||
|
||||
// Ñãëàæåííûå Íîðìàëè (èç êàðòû)
|
||||
result.NormalData.push_back(smoothNormalsMap[p1]);
|
||||
result.NormalData.push_back(smoothNormalsMap[p2]);
|
||||
result.NormalData.push_back(smoothNormalsMap[p3]);
|
||||
|
||||
// Òåêñòóðíûå Êîîðäèíàòû (Ïëàíàðíàÿ ïðîåêöèÿ íà ïëîñêîñòü ãðàíè)
|
||||
// p1 -> (0, 0), p2 -> (L_12, 0), p3 -> (L_13 * cos(angle), L_13 * sin(angle))
|
||||
// Ãäå L_xy - äëèíà âåêòîðà, angle - óãîë ìåæäó p2-p1 è p3-p1
|
||||
|
||||
Vector3f uAxis = (p2 - p1).normalized();
|
||||
Vector3f vRaw = p3 - p1;
|
||||
|
||||
// Ïðîåêöèÿ vRaw íà uAxis
|
||||
float uProjLen = vRaw.dot(uAxis);
|
||||
|
||||
// Âåêòîð V ïåðïåíäèêóëÿðíûé U: vRaw - uProj
|
||||
Vector3f vAxisRaw = vRaw - (uAxis * uProjLen);
|
||||
|
||||
// Äëèíà îñè V
|
||||
float vLen = vAxisRaw.length();
|
||||
|
||||
// Íîðìàëèçîâàííàÿ îñü V
|
||||
Vector3f vAxis = vAxisRaw.normalized();
|
||||
|
||||
// Êîîðäèíàòû (u, v) äëÿ p1, p2, p3 îòíîñèòåëüíî p1
|
||||
Vector2f uv1 = { 0.0f, 0.0f };
|
||||
Vector2f uv2 = { (p2 - p1).length(), 0.0f }; // p2-p1 âäîëü îñè U
|
||||
Vector2f uv3 = { uProjLen, vLen }; // p3-p1: u-êîìïîíåíòà = uProjLen, v-êîìïîíåíòà = vLen
|
||||
|
||||
// Íàõîäèì ìàêñèìàëüíûé ðàçìåð, ÷òîáû ìàñøòàáèðîâàòü â [0, 1]
|
||||
float maxUV = max(uv2.v[0], max(uv3.v[0], uv3.v[1]));
|
||||
|
||||
if (maxUV > 0.000001f) {
|
||||
// Ìàñøòàáèðóåì:
|
||||
result.TexCoordData.push_back(uv1);
|
||||
result.TexCoordData.push_back(uv2 * (1.f / maxUV));
|
||||
result.TexCoordData.push_back(uv3 * (1.f / maxUV));
|
||||
}
|
||||
else {
|
||||
// Ïðåäîòâðàùåíèå äåëåíèÿ íà íîëü äëÿ âûðîæäåííûõ ãðàíåé
|
||||
result.TexCoordData.push_back({ 0.0f, 0.0f });
|
||||
result.TexCoordData.push_back({ 0.0f, 0.0f });
|
||||
result.TexCoordData.push_back({ 0.0f, 0.0f });
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
StoneGroup CreateStoneGroupData(uint64_t globalSeed, const LodLevel& planetLodLevel) {
|
||||
StoneGroup group;
|
||||
|
||||
// Ðåçåðâèðóåì ìåñòî ïîä âñå òðåóãîëüíèêè òåêóùåãî LOD
|
||||
group.allInstances.resize(planetLodLevel.triangles.size());
|
||||
|
||||
for (size_t tIdx = 0; tIdx < planetLodLevel.triangles.size(); ++tIdx) {
|
||||
const Triangle& tri = planetLodLevel.triangles[tIdx];
|
||||
std::mt19937 engine(static_cast<unsigned int>(globalSeed));
|
||||
|
||||
for (int i = 0; i < StoneParams::STONES_PER_TRIANGLE; ++i) {
|
||||
StoneInstance instance;
|
||||
instance.seed = globalSeed;// + tIdx * 1000 + i; // Óíèêàëüíûé ñèä äëÿ êàæäîãî êàìíÿ
|
||||
|
||||
instance.position = GetRandomPointOnTriangle(tri, engine);
|
||||
|
||||
//instance.position = Vector3f(5000.0f, 5000.0f, 5000.0f);
|
||||
// Ãåíåðèðóåì ñëó÷àéíûå ïàðàìåòðû îäèí ðàç
|
||||
instance.scale = {
|
||||
getRandomFloat(engine, 0.5f, 1.5f),
|
||||
getRandomFloat(engine, 0.5f, 1.5f),
|
||||
getRandomFloat(engine, 0.5f, 1.5f)
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
group.allInstances[tIdx].push_back(instance);
|
||||
}
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
void StoneGroup::inflate(const std::vector<int>& triangleIndices) {
|
||||
// 1. Î÷èùàåì òåêóùèé ìåø ïåðåä çàïîëíåíèåì
|
||||
mesh.PositionData.clear();
|
||||
mesh.NormalData.clear();
|
||||
mesh.TexCoordData.clear();
|
||||
|
||||
static VertexDataStruct baseStone = CreateBaseConvexPolyhedron(1337);
|
||||
|
||||
// 2. Íàïîëíÿåì ìåø òîëüêî äëÿ âèäèìûõ òðåóãîëüíèêîâ
|
||||
for (int tIdx : triangleIndices) {
|
||||
if (tIdx >= allInstances.size()) continue;
|
||||
|
||||
for (const auto& inst : allInstances[tIdx]) {
|
||||
Matrix3f rotMat = QuatToMatrix(inst.rotation);
|
||||
|
||||
for (size_t j = 0; j < baseStone.PositionData.size(); ++j) {
|
||||
Vector3f p = baseStone.PositionData[j];
|
||||
Vector3f n = baseStone.NormalData[j];
|
||||
|
||||
// Ìàñøòàá -> Ïîâîðîò -> Ñìåùåíèå
|
||||
p.v[0] *= inst.scale.v[0];
|
||||
p.v[1] *= inst.scale.v[1];
|
||||
p.v[2] *= inst.scale.v[2];
|
||||
|
||||
mesh.PositionData.push_back(MultMatrixVector(rotMat, p) + inst.position);
|
||||
mesh.NormalData.push_back(MultMatrixVector(rotMat, n));
|
||||
mesh.TexCoordData.push_back(baseStone.TexCoordData[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace ZL
|
||||
@ -1,41 +0,0 @@
|
||||
#pragma once
|
||||
#include "ZLMath.h"
|
||||
#include "Renderer.h"
|
||||
#include "PlanetData.h"
|
||||
|
||||
namespace ZL {
|
||||
|
||||
struct StoneParams
|
||||
{
|
||||
static const float BASE_SCALE; // Îáùèé ðàçìåð êàìíÿ
|
||||
static const float MIN_AXIS_SCALE; // Ìèíèìàëüíîå ðàñòÿæåíèå/ñæàòèå ïî îñè
|
||||
static const float MAX_AXIS_SCALE; // Ìàêñèìàëüíîå ðàñòÿæåíèå/ñæàòèå ïî îñè
|
||||
static const float MIN_PERTURBATION; // Ìèíèìàëüíîå ðàäèàëüíîå âîçìóùåíèå âåðøèíû
|
||||
static const float MAX_PERTURBATION; // Ìàêñèìàëüíîå ðàäèàëüíîå âîçìóùåíèå âåðøèíû
|
||||
static const int STONES_PER_TRIANGLE;
|
||||
|
||||
};
|
||||
|
||||
struct StoneInstance {
|
||||
uint64_t seed;
|
||||
Vector3f position;
|
||||
Vector3f scale;
|
||||
Vector4f rotation;
|
||||
};
|
||||
|
||||
struct StoneGroup {
|
||||
// mesh.PositionData è ïðî÷èå áóäóò çàïîëíÿòüñÿ â inflate()
|
||||
VertexDataStruct mesh;
|
||||
|
||||
// Âíåøíèé âåêòîð — èíäåêñ òðåóãîëüíèêà ïëàíåòû,
|
||||
// âíóòðåííèé — ñïèñîê êàìíåé íà ýòîì òðåóãîëüíèêå
|
||||
std::vector<std::vector<StoneInstance>> allInstances;
|
||||
|
||||
// Î÷èùàåò ñòàðóþ ãåîìåòðèþ è ãåíåðèðóåò íîâóþ äëÿ óêàçàííûõ èíäåêñîâ
|
||||
void inflate(const std::vector<int>& triangleIndices);
|
||||
};
|
||||
|
||||
// Òåïåðü âîçâðàùàåò çàãîòîâêó ñî âñåìè ïàðàìåòðàìè, íî áåç òÿæåëîãî ìåøà
|
||||
StoneGroup CreateStoneGroupData(uint64_t globalSeed, const LodLevel& planetLodLevel);
|
||||
|
||||
} // namespace ZL
|
||||
530
UiManager.cpp
530
UiManager.cpp
@ -1,530 +0,0 @@
|
||||
#include "UiManager.h"
|
||||
#include "Utils.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
void UiButton::buildMesh() {
|
||||
mesh.data.PositionData.clear();
|
||||
mesh.data.TexCoordData.clear();
|
||||
|
||||
float x0 = rect.x;
|
||||
float y0 = rect.y;
|
||||
float x1 = rect.x + rect.w;
|
||||
float y1 = rect.y + rect.h;
|
||||
|
||||
mesh.data.PositionData.push_back({ x0, y0, 0 });
|
||||
mesh.data.TexCoordData.push_back({ 0, 0 });
|
||||
|
||||
mesh.data.PositionData.push_back({ x0, y1, 0 });
|
||||
mesh.data.TexCoordData.push_back({ 0, 1 });
|
||||
|
||||
mesh.data.PositionData.push_back({ x1, y1, 0 });
|
||||
mesh.data.TexCoordData.push_back({ 1, 1 });
|
||||
|
||||
mesh.data.PositionData.push_back({ x0, y0, 0 });
|
||||
mesh.data.TexCoordData.push_back({ 0, 0 });
|
||||
|
||||
mesh.data.PositionData.push_back({ x1, y1, 0 });
|
||||
mesh.data.TexCoordData.push_back({ 1, 1 });
|
||||
|
||||
mesh.data.PositionData.push_back({ x1, y0, 0 });
|
||||
mesh.data.TexCoordData.push_back({ 1, 0 });
|
||||
|
||||
mesh.RefreshVBO();
|
||||
}
|
||||
|
||||
void UiButton::draw(Renderer& renderer) const {
|
||||
if (!texNormal) return;
|
||||
const std::shared_ptr<Texture>* tex = &texNormal;
|
||||
switch (state) {
|
||||
case ButtonState::Normal: tex = &texNormal; break;
|
||||
case ButtonState::Hover: tex = &texHover; break;
|
||||
case ButtonState::Pressed: tex = &texPressed; break;
|
||||
}
|
||||
if (!(*tex)) return;
|
||||
|
||||
static const std::string vPositionName = "vPosition";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
renderer.RenderUniform1i(textureUniformName, 0);
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, (*tex)->getTexID());
|
||||
renderer.DrawVertexRenderStruct(mesh);
|
||||
|
||||
renderer.DisableVertexAttribArray(vPositionName);
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
}
|
||||
|
||||
void UiSlider::buildTrackMesh() {
|
||||
trackMesh.data.PositionData.clear();
|
||||
trackMesh.data.TexCoordData.clear();
|
||||
|
||||
float x0 = rect.x;
|
||||
float y0 = rect.y;
|
||||
float x1 = rect.x + rect.w;
|
||||
float y1 = rect.y + rect.h;
|
||||
|
||||
trackMesh.data.PositionData.push_back({ x0, y0, 0 });
|
||||
trackMesh.data.TexCoordData.push_back({ 0, 0 });
|
||||
|
||||
trackMesh.data.PositionData.push_back({ x0, y1, 0 });
|
||||
trackMesh.data.TexCoordData.push_back({ 0, 1 });
|
||||
|
||||
trackMesh.data.PositionData.push_back({ x1, y1, 0 });
|
||||
trackMesh.data.TexCoordData.push_back({ 1, 1 });
|
||||
|
||||
trackMesh.data.PositionData.push_back({ x0, y0, 0 });
|
||||
trackMesh.data.TexCoordData.push_back({ 0, 0 });
|
||||
|
||||
trackMesh.data.PositionData.push_back({ x1, y1, 0 });
|
||||
trackMesh.data.TexCoordData.push_back({ 1, 1 });
|
||||
|
||||
trackMesh.data.PositionData.push_back({ x1, y0, 0 });
|
||||
trackMesh.data.TexCoordData.push_back({ 1, 0 });
|
||||
|
||||
trackMesh.RefreshVBO();
|
||||
}
|
||||
|
||||
void UiSlider::buildKnobMesh() {
|
||||
knobMesh.data.PositionData.clear();
|
||||
knobMesh.data.TexCoordData.clear();
|
||||
|
||||
float kw = vertical ? rect.w * 4.0f : rect.w * 0.5f;
|
||||
float kh = vertical ? rect.w * 4.0f : rect.h * 0.5f;
|
||||
|
||||
float cx = rect.x + rect.w * 0.5f;
|
||||
float cy = rect.y + (vertical ? (value * rect.h) : (rect.h * 0.5f));
|
||||
|
||||
float x0 = cx - kw * 0.5f;
|
||||
float y0 = cy - kh * 0.5f;
|
||||
float x1 = cx + kw * 0.5f;
|
||||
float y1 = cy + kh * 0.5f;
|
||||
|
||||
knobMesh.data.PositionData.push_back({ x0, y0, 0 });
|
||||
knobMesh.data.TexCoordData.push_back({ 0, 0 });
|
||||
|
||||
knobMesh.data.PositionData.push_back({ x0, y1, 0 });
|
||||
knobMesh.data.TexCoordData.push_back({ 0, 1 });
|
||||
|
||||
knobMesh.data.PositionData.push_back({ x1, y1, 0 });
|
||||
knobMesh.data.TexCoordData.push_back({ 1, 1 });
|
||||
|
||||
knobMesh.data.PositionData.push_back({ x0, y0, 0 });
|
||||
knobMesh.data.TexCoordData.push_back({ 0, 0 });
|
||||
|
||||
knobMesh.data.PositionData.push_back({ x1, y1, 0 });
|
||||
knobMesh.data.TexCoordData.push_back({ 1, 1 });
|
||||
|
||||
knobMesh.data.PositionData.push_back({ x1, y0, 0 });
|
||||
knobMesh.data.TexCoordData.push_back({ 1, 0 });
|
||||
|
||||
knobMesh.RefreshVBO();
|
||||
}
|
||||
|
||||
void UiSlider::draw(Renderer& renderer) const {
|
||||
static const std::string vPositionName = "vPosition";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
renderer.RenderUniform1i(textureUniformName, 0);
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
|
||||
if (texTrack) {
|
||||
glBindTexture(GL_TEXTURE_2D, texTrack->getTexID());
|
||||
renderer.DrawVertexRenderStruct(trackMesh);
|
||||
}
|
||||
if (texKnob) {
|
||||
glBindTexture(GL_TEXTURE_2D, texKnob->getTexID());
|
||||
renderer.DrawVertexRenderStruct(knobMesh);
|
||||
}
|
||||
|
||||
renderer.DisableVertexAttribArray(vPositionName);
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
}
|
||||
|
||||
void UiManager::loadFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile) {
|
||||
std::ifstream in(path);
|
||||
if (!in.is_open()) {
|
||||
std::cerr << "UiManager: failed to open " << path << std::endl;
|
||||
throw std::runtime_error("Failed to load UI file: " + path);
|
||||
}
|
||||
|
||||
json j;
|
||||
try {
|
||||
in >> j;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: json parse error: " << e.what() << std::endl;
|
||||
throw std::runtime_error("Failed to load UI file: " + path);
|
||||
}
|
||||
|
||||
if (!j.contains("root") || !j["root"].is_object()) {
|
||||
std::cerr << "UiManager: root node missing or invalid" << std::endl;
|
||||
throw std::runtime_error("Failed to load UI file: " + path);
|
||||
}
|
||||
|
||||
root = parseNode(j["root"], renderer, zipFile);
|
||||
layoutNode(root);
|
||||
buttons.clear();
|
||||
sliders.clear();
|
||||
collectButtonsAndSliders(root);
|
||||
|
||||
for (auto& b : buttons) {
|
||||
b->buildMesh();
|
||||
}
|
||||
for (auto& s : sliders) {
|
||||
s->buildTrackMesh();
|
||||
s->buildKnobMesh();
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<UiNode> UiManager::parseNode(const json& j, Renderer& renderer, const std::string& zipFile) {
|
||||
auto node = std::make_shared<UiNode>();
|
||||
if (j.contains("type") && j["type"].is_string()) node->type = j["type"].get<std::string>();
|
||||
if (j.contains("name") && j["name"].is_string()) node->name = j["name"].get<std::string>();
|
||||
|
||||
if (j.contains("x")) node->rect.x = j["x"].get<float>();
|
||||
if (j.contains("y")) node->rect.y = j["y"].get<float>();
|
||||
if (j.contains("width")) node->rect.w = j["width"].get<float>();
|
||||
if (j.contains("height")) node->rect.h = j["height"].get<float>();
|
||||
|
||||
if (j.contains("orientation") && j["orientation"].is_string()) node->orientation = j["orientation"].get<std::string>();
|
||||
if (j.contains("spacing")) node->spacing = j["spacing"].get<float>();
|
||||
|
||||
if (node->type == "Button") {
|
||||
auto btn = std::make_shared<UiButton>();
|
||||
btn->name = node->name;
|
||||
btn->rect = node->rect;
|
||||
|
||||
if (!j.contains("textures") || !j["textures"].is_object()) {
|
||||
std::cerr << "UiManager: Button '" << btn->name << "' missing textures" << std::endl;
|
||||
throw std::runtime_error("UI button textures missing");
|
||||
}
|
||||
auto t = j["textures"];
|
||||
auto loadTex = [&](const std::string& key)->std::shared_ptr<Texture> {
|
||||
if (!t.contains(key) || !t[key].is_string()) return nullptr;
|
||||
std::string path = t[key].get<std::string>();
|
||||
try {
|
||||
auto data = CreateTextureDataFromPng(path.c_str(), zipFile.c_str());
|
||||
return std::make_shared<Texture>(data);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: failed load texture " << path << " : " << e.what() << std::endl;
|
||||
throw std::runtime_error("UI texture load failed: " + path);
|
||||
}
|
||||
};
|
||||
|
||||
btn->texNormal = loadTex("normal");
|
||||
btn->texHover = loadTex("hover");
|
||||
btn->texPressed = loadTex("pressed");
|
||||
|
||||
node->button = btn;
|
||||
}
|
||||
else if (node->type == "Slider") {
|
||||
auto s = std::make_shared<UiSlider>();
|
||||
s->name = node->name;
|
||||
s->rect = node->rect;
|
||||
|
||||
if (!j.contains("textures") || !j["textures"].is_object()) {
|
||||
std::cerr << "UiManager: Slider '" << s->name << "' missing textures" << std::endl;
|
||||
throw std::runtime_error("UI slider textures missing");
|
||||
}
|
||||
auto t = j["textures"];
|
||||
auto loadTex = [&](const std::string& key)->std::shared_ptr<Texture> {
|
||||
if (!t.contains(key) || !t[key].is_string()) return nullptr;
|
||||
std::string path = t[key].get<std::string>();
|
||||
try {
|
||||
auto data = CreateTextureDataFromPng(path.c_str(), zipFile.c_str());
|
||||
return std::make_shared<Texture>(data);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: failed load texture " << path << " : " << e.what() << std::endl;
|
||||
throw std::runtime_error("UI texture load failed: " + path);
|
||||
}
|
||||
};
|
||||
|
||||
s->texTrack = loadTex("track");
|
||||
s->texKnob = loadTex("knob");
|
||||
|
||||
if (j.contains("value")) s->value = j["value"].get<float>();
|
||||
if (j.contains("orientation")) {
|
||||
std::string orient = j["orientation"].get<std::string>();
|
||||
std::transform(orient.begin(), orient.end(), orient.begin(), ::tolower);
|
||||
s->vertical = (orient != "horizontal");
|
||||
}
|
||||
|
||||
node->slider = s;
|
||||
}
|
||||
|
||||
if (j.contains("children") && j["children"].is_array()) {
|
||||
for (const auto& ch : j["children"]) {
|
||||
node->children.push_back(parseNode(ch, renderer, zipFile));
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void UiManager::layoutNode(const std::shared_ptr<UiNode>& node) {
|
||||
for (auto& child : node->children) {
|
||||
child->rect.x += node->rect.x;
|
||||
child->rect.y += node->rect.y;
|
||||
}
|
||||
|
||||
if (node->type == "LinearLayout") {
|
||||
std::string orient = node->orientation;
|
||||
std::transform(orient.begin(), orient.end(), orient.begin(), ::tolower);
|
||||
|
||||
float cursorX = node->rect.x;
|
||||
float cursorY = node->rect.y;
|
||||
for (auto& child : node->children) {
|
||||
if (orient == "horizontal") {
|
||||
child->rect.x = cursorX;
|
||||
child->rect.y = node->rect.y;
|
||||
cursorX += child->rect.w + node->spacing;
|
||||
}
|
||||
else {
|
||||
child->rect.x = node->rect.x;
|
||||
child->rect.y = cursorY;
|
||||
cursorY += child->rect.h + node->spacing;
|
||||
}
|
||||
layoutNode(child);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (auto& child : node->children) {
|
||||
layoutNode(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UiManager::collectButtonsAndSliders(const std::shared_ptr<UiNode>& node) {
|
||||
if (node->button) {
|
||||
buttons.push_back(node->button);
|
||||
}
|
||||
if (node->slider) {
|
||||
sliders.push_back(node->slider);
|
||||
}
|
||||
for (auto& c : node->children) collectButtonsAndSliders(c);
|
||||
}
|
||||
|
||||
bool UiManager::setButtonCallback(const std::string& name, std::function<void(const std::string&)> cb) {
|
||||
auto b = findButton(name);
|
||||
if (!b) {
|
||||
std::cerr << "UiManager: setButtonCallback failed, button not found: " << name << std::endl;
|
||||
return false;
|
||||
}
|
||||
b->onClick = std::move(cb);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UiManager::addSlider(const std::string& name, const UiRect& rect, Renderer& renderer, const std::string& zipFile,
|
||||
const std::string& trackPath, const std::string& knobPath, float initialValue, bool vertical) {
|
||||
|
||||
auto s = std::make_shared<UiSlider>();
|
||||
s->name = name;
|
||||
s->rect = rect;
|
||||
s->value = std::clamp(initialValue, 0.0f, 1.0f);
|
||||
s->vertical = vertical;
|
||||
|
||||
try {
|
||||
if (!trackPath.empty()) {
|
||||
auto data = CreateTextureDataFromPng(trackPath.c_str(), zipFile.c_str());
|
||||
s->texTrack = std::make_shared<Texture>(data);
|
||||
}
|
||||
if (!knobPath.empty()) {
|
||||
auto data = CreateTextureDataFromPng(knobPath.c_str(), zipFile.c_str());
|
||||
s->texKnob = std::make_shared<Texture>(data);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: addSlider failed to load textures: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
s->buildTrackMesh();
|
||||
s->buildKnobMesh();
|
||||
|
||||
sliders.push_back(s);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<UiSlider> UiManager::findSlider(const std::string& name) {
|
||||
for (auto& s : sliders) if (s->name == name) return s;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool UiManager::setSliderCallback(const std::string& name, std::function<void(const std::string&, float)> cb) {
|
||||
auto s = findSlider(name);
|
||||
if (!s) {
|
||||
std::cerr << "UiManager: setSliderCallback failed, slider not found: " << name << std::endl;
|
||||
return false;
|
||||
}
|
||||
s->onValueChanged = std::move(cb);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UiManager::setSliderValue(const std::string& name, float value) {
|
||||
auto s = findSlider(name);
|
||||
if (!s) return false;
|
||||
value = std::clamp(value, 0.0f, 1.0f);
|
||||
if (fabs(s->value - value) < 1e-6f) return true;
|
||||
s->value = value;
|
||||
s->buildKnobMesh();
|
||||
if (s->onValueChanged) s->onValueChanged(s->name, s->value);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UiManager::pushMenuFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile) {
|
||||
MenuState prev;
|
||||
prev.root = root;
|
||||
prev.buttons = buttons;
|
||||
prev.sliders = sliders;
|
||||
prev.pressedButton = pressedButton;
|
||||
prev.pressedSlider = pressedSlider;
|
||||
prev.path = "";
|
||||
|
||||
try {
|
||||
loadFromFile(path, renderer, zipFile);
|
||||
menuStack.push_back(std::move(prev));
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: pushMenuFromFile failed to load " << path << " : " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool UiManager::popMenu() {
|
||||
if (menuStack.empty()) {
|
||||
std::cerr << "UiManager: popMenu called but menu stack is empty" << std::endl;
|
||||
return false;
|
||||
}
|
||||
auto s = menuStack.back();
|
||||
menuStack.pop_back();
|
||||
|
||||
root = s.root;
|
||||
buttons = s.buttons;
|
||||
sliders = s.sliders;
|
||||
pressedButton = s.pressedButton;
|
||||
pressedSlider = s.pressedSlider;
|
||||
|
||||
for (auto& b : buttons) {
|
||||
if (b) b->buildMesh();
|
||||
}
|
||||
for (auto& sl : sliders) {
|
||||
if (sl) { sl->buildTrackMesh(); sl->buildKnobMesh(); }
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void UiManager::clearMenuStack() {
|
||||
menuStack.clear();
|
||||
}
|
||||
|
||||
void UiManager::draw(Renderer& renderer) {
|
||||
renderer.PushProjectionMatrix(Environment::width, Environment::height, -1, 1);
|
||||
renderer.PushMatrix();
|
||||
renderer.LoadIdentity();
|
||||
|
||||
for (const auto& b : buttons) {
|
||||
b->draw(renderer);
|
||||
}
|
||||
for (const auto& s : sliders) {
|
||||
s->draw(renderer);
|
||||
}
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
}
|
||||
|
||||
void UiManager::onMouseMove(int x, int y) {
|
||||
for (auto& b : buttons) {
|
||||
if (b->rect.contains((float)x, (float)y)) {
|
||||
if (b->state != ButtonState::Pressed) b->state = ButtonState::Hover;
|
||||
}
|
||||
else {
|
||||
if (b->state != ButtonState::Pressed) b->state = ButtonState::Normal;
|
||||
}
|
||||
}
|
||||
|
||||
if (pressedSlider) {
|
||||
auto s = pressedSlider;
|
||||
float t;
|
||||
if (s->vertical) {
|
||||
t = (y - s->rect.y) / s->rect.h;
|
||||
}
|
||||
else {
|
||||
t = (x - s->rect.x) / s->rect.w;
|
||||
}
|
||||
if (t < 0.0f) t = 0.0f;
|
||||
if (t > 1.0f) t = 1.0f;
|
||||
s->value = t;
|
||||
s->buildKnobMesh();
|
||||
if (s->onValueChanged) s->onValueChanged(s->name, s->value);
|
||||
}
|
||||
}
|
||||
|
||||
void UiManager::onMouseDown(int x, int y) {
|
||||
for (auto& b : buttons) {
|
||||
if (b->rect.contains((float)x, (float)y)) {
|
||||
b->state = ButtonState::Pressed;
|
||||
pressedButton = b;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& s : sliders) {
|
||||
if (s->rect.contains((float)x, (float)y)) {
|
||||
pressedSlider = s;
|
||||
float t;
|
||||
if (s->vertical) {
|
||||
t = (y - s->rect.y) / s->rect.h;
|
||||
}
|
||||
else {
|
||||
t = (x - s->rect.x) / s->rect.w;
|
||||
}
|
||||
if (t < 0.0f) t = 0.0f;
|
||||
if (t > 1.0f) t = 1.0f;
|
||||
s->value = t;
|
||||
s->buildKnobMesh();
|
||||
if (s->onValueChanged) s->onValueChanged(s->name, s->value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UiManager::onMouseUp(int x, int y) {
|
||||
for (auto& b : buttons) {
|
||||
bool contains = b->rect.contains((float)x, (float)y);
|
||||
if (b->state == ButtonState::Pressed) {
|
||||
if (contains && pressedButton == b) {
|
||||
if (b->onClick) {
|
||||
b->onClick(b->name);
|
||||
}
|
||||
}
|
||||
b->state = contains ? ButtonState::Hover : ButtonState::Normal;
|
||||
}
|
||||
}
|
||||
pressedButton.reset();
|
||||
|
||||
if (pressedSlider) {
|
||||
pressedSlider.reset();
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<UiButton> UiManager::findButton(const std::string& name) {
|
||||
for (auto& b : buttons) if (b->name == name) return b;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
133
UiManager.h
133
UiManager.h
@ -1,133 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Renderer.h"
|
||||
#include "TextureManager.h"
|
||||
#include "Environment.h"
|
||||
#include "external/nlohmann/json.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
struct UiRect {
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
float w = 0;
|
||||
float h = 0;
|
||||
bool contains(float px, float py) const {
|
||||
return px >= x && px <= x + w && py >= y && py <= y + h;
|
||||
}
|
||||
};
|
||||
|
||||
enum class ButtonState {
|
||||
Normal,
|
||||
Hover,
|
||||
Pressed
|
||||
};
|
||||
|
||||
struct UiButton {
|
||||
std::string name;
|
||||
UiRect rect;
|
||||
std::shared_ptr<Texture> texNormal;
|
||||
std::shared_ptr<Texture> texHover;
|
||||
std::shared_ptr<Texture> texPressed;
|
||||
ButtonState state = ButtonState::Normal;
|
||||
|
||||
VertexRenderStruct mesh;
|
||||
|
||||
std::function<void(const std::string&)> onClick;
|
||||
|
||||
void buildMesh();
|
||||
void draw(Renderer& renderer) const;
|
||||
};
|
||||
|
||||
struct UiSlider {
|
||||
std::string name;
|
||||
UiRect rect;
|
||||
std::shared_ptr<Texture> texTrack;
|
||||
std::shared_ptr<Texture> texKnob;
|
||||
|
||||
VertexRenderStruct trackMesh;
|
||||
VertexRenderStruct knobMesh;
|
||||
|
||||
float value = 0.0f;
|
||||
bool vertical = true;
|
||||
|
||||
std::function<void(const std::string&, float)> onValueChanged;
|
||||
|
||||
void buildTrackMesh();
|
||||
void buildKnobMesh();
|
||||
void draw(Renderer& renderer) const;
|
||||
};
|
||||
|
||||
struct UiNode {
|
||||
std::string type;
|
||||
UiRect rect;
|
||||
std::string name;
|
||||
std::vector<std::shared_ptr<UiNode>> children;
|
||||
std::shared_ptr<UiButton> button;
|
||||
std::shared_ptr<UiSlider> slider;
|
||||
std::string orientation = "vertical";
|
||||
float spacing = 0.0f;
|
||||
};
|
||||
|
||||
class UiManager {
|
||||
public:
|
||||
UiManager() = default;
|
||||
|
||||
void loadFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile = "");
|
||||
|
||||
void draw(Renderer& renderer);
|
||||
|
||||
void onMouseMove(int x, int y);
|
||||
void onMouseDown(int x, int y);
|
||||
void onMouseUp(int x, int y);
|
||||
|
||||
bool isUiInteraction() const {
|
||||
return pressedButton != nullptr || pressedSlider != nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<UiButton> findButton(const std::string& name);
|
||||
|
||||
bool setButtonCallback(const std::string& name, std::function<void(const std::string&)> cb);
|
||||
|
||||
bool addSlider(const std::string& name, const UiRect& rect, Renderer& renderer, const std::string& zipFile,
|
||||
const std::string& trackPath, const std::string& knobPath, float initialValue = 0.0f, bool vertical = true);
|
||||
|
||||
std::shared_ptr<UiSlider> findSlider(const std::string& name);
|
||||
bool setSliderCallback(const std::string& name, std::function<void(const std::string&, float)> cb);
|
||||
bool setSliderValue(const std::string& name, float value); // programmatic set (clamped 0..1)
|
||||
|
||||
bool pushMenuFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile = "");
|
||||
bool popMenu();
|
||||
void clearMenuStack();
|
||||
|
||||
private:
|
||||
std::shared_ptr<UiNode> parseNode(const json& j, Renderer& renderer, const std::string& zipFile);
|
||||
void layoutNode(const std::shared_ptr<UiNode>& node);
|
||||
void collectButtonsAndSliders(const std::shared_ptr<UiNode>& node);
|
||||
|
||||
std::shared_ptr<UiNode> root;
|
||||
std::vector<std::shared_ptr<UiButton>> buttons;
|
||||
std::vector<std::shared_ptr<UiSlider>> sliders;
|
||||
|
||||
std::shared_ptr<UiButton> pressedButton;
|
||||
std::shared_ptr<UiSlider> pressedSlider;
|
||||
|
||||
struct MenuState {
|
||||
std::shared_ptr<UiNode> root;
|
||||
std::vector<std::shared_ptr<UiButton>> buttons;
|
||||
std::vector<std::shared_ptr<UiSlider>> sliders;
|
||||
std::shared_ptr<UiButton> pressedButton;
|
||||
std::shared_ptr<UiSlider> pressedSlider;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
std::vector<MenuState> menuStack;
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
42
ZLMath.cpp
42
ZLMath.cpp
@ -189,38 +189,6 @@ namespace ZL {
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar)
|
||||
{
|
||||
float width = xmax - xmin;
|
||||
float height = ymax - ymin;
|
||||
float depthRange = zFar - zNear;
|
||||
|
||||
if (width <= 0 || height <= 0 || depthRange <= 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid dimensions for orthogonal matrix");
|
||||
}
|
||||
|
||||
Matrix4f r;
|
||||
|
||||
// Масштабирование
|
||||
r.m[0] = 2.f / width;
|
||||
r.m[5] = 2.f / height;
|
||||
r.m[10] = -1.f / depthRange;
|
||||
|
||||
// Обнуление неиспользуемых компонентов
|
||||
r.m[1] = r.m[2] = r.m[3] = 0;
|
||||
r.m[4] = r.m[6] = r.m[7] = 0;
|
||||
r.m[8] = r.m[9] = r.m[11] = 0;
|
||||
|
||||
// Трансляция (смещение)
|
||||
r.m[12] = -(xmax + xmin) / width;
|
||||
r.m[13] = -(ymax + ymin) / height;
|
||||
r.m[14] = zNear / depthRange;
|
||||
r.m[15] = 1.f;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix4f MakePerspectiveMatrix(float fovY, float aspectRatio, float zNear, float zFar)
|
||||
{
|
||||
float tanHalfFovy = tan(fovY / 2.f);
|
||||
@ -641,16 +609,6 @@ namespace ZL {
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector2f operator*(Vector2f v, float scale)
|
||||
{
|
||||
Vector2f r = v;
|
||||
|
||||
r.v[0] = v.v[0] * scale;
|
||||
r.v[1] = v.v[1] * scale;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector3f operator*(Vector3f v, float scale)
|
||||
{
|
||||
Vector3f r = v;
|
||||
|
||||
17
ZLMath.h
17
ZLMath.h
@ -13,9 +13,6 @@ namespace ZL {
|
||||
|
||||
Vector4f normalized() const {
|
||||
double norm = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
|
||||
if (norm <= 0.000001f) {
|
||||
return { 0,0, 0, 0};
|
||||
}
|
||||
Vector4f r;
|
||||
|
||||
r.v[0] = v[0] / norm;
|
||||
@ -46,9 +43,6 @@ namespace ZL {
|
||||
|
||||
Vector3f normalized() const {
|
||||
double norm = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
||||
if (norm <= 0.000001f) {
|
||||
return { 0,0,0 };
|
||||
}
|
||||
Vector3f r;
|
||||
|
||||
r.v[0] = v[0] / norm;
|
||||
@ -79,11 +73,10 @@ namespace ZL {
|
||||
);
|
||||
}
|
||||
|
||||
bool operator<(const Vector3f& other) const {
|
||||
if (v[0] != other.v[0]) return v[0] < other.v[0];
|
||||
if (v[1] != other.v[1]) return v[1] < other.v[1];
|
||||
return v[2] < other.v[2];
|
||||
}
|
||||
// Îïåðàòîð âû÷èòàíèÿ
|
||||
/*Vector3f operator-(const Vector3f& other) const {
|
||||
return Vector3f(v[0] - other.v[0], v[1] - other.v[1], v[2] - other.v[2]);
|
||||
}*/
|
||||
};
|
||||
|
||||
|
||||
@ -148,7 +141,6 @@ namespace ZL {
|
||||
Matrix4f operator*(const Matrix4f& m1, const Matrix4f& m2);
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float width, float height, float zNear, float zFar);
|
||||
Matrix4f MakeOrthoMatrix(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar);
|
||||
|
||||
Matrix4f MakePerspectiveMatrix(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
|
||||
@ -160,7 +152,6 @@ namespace ZL {
|
||||
Vector4f QuatFromRotateAroundY(float angle);
|
||||
Vector4f QuatFromRotateAroundZ(float angle);
|
||||
|
||||
Vector2f operator*(Vector2f v, float scale);
|
||||
Vector3f operator*(Vector3f v, float scale);
|
||||
Vector4f operator*(Vector4f v, float scale);
|
||||
|
||||
|
||||
@ -1,72 +0,0 @@
|
||||
{
|
||||
"root": {
|
||||
"type": "FrameLayout",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"width": 1280,
|
||||
"height": 720,
|
||||
"children": [
|
||||
{
|
||||
"type": "FrameLayout",
|
||||
"name": "centerPanel",
|
||||
"x": 480,
|
||||
"y": 160,
|
||||
"width": 320,
|
||||
"height": 400,
|
||||
"children": [
|
||||
{
|
||||
"type": "LinearLayout",
|
||||
"name": "settingsButtons",
|
||||
"orientation": "vertical",
|
||||
"spacing": 10,
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"width": 300,
|
||||
"height": 300,
|
||||
"children": [
|
||||
{
|
||||
"type": "Button",
|
||||
"name": "Opt1",
|
||||
"x": 100,
|
||||
"y": 300,
|
||||
"width": 200,
|
||||
"height": 50,
|
||||
"textures": {
|
||||
"normal": "./resources/button.png",
|
||||
"hover": "./resources/button.png",
|
||||
"pressed": "./resources/button.png"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Button",
|
||||
"name": "Opt2",
|
||||
"x": 100,
|
||||
"y": 200,
|
||||
"width": 200,
|
||||
"height": 50,
|
||||
"textures": {
|
||||
"normal": "./resources/button.png",
|
||||
"hover": "./resources/button.png",
|
||||
"pressed": "./resources/button.png"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Button",
|
||||
"name": "backButton",
|
||||
"x": 100,
|
||||
"y": 100,
|
||||
"width": 200,
|
||||
"height": 50,
|
||||
"textures": {
|
||||
"normal": "./resources/button.png",
|
||||
"hover": "./resources/button.png",
|
||||
"pressed": "./resources/button.png"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
{
|
||||
"emissionRate": 100.0,
|
||||
"maxParticles": 200,
|
||||
"particleSize": 0.04,
|
||||
"biasX": 0.3,
|
||||
"emissionPoints": [
|
||||
{
|
||||
"position": [-2.1, 0.4, 5.0]
|
||||
},
|
||||
{
|
||||
"position": [2.1, 0.4, 5.0]
|
||||
}
|
||||
],
|
||||
"speedRange": [0.5, 2.0],
|
||||
"zSpeedRange": [1.0, 3.0],
|
||||
"scaleRange": [0.8, 1.2],
|
||||
"lifeTimeRange": [600.0, 1400.0],
|
||||
"texture": "./resources/spark.png",
|
||||
"shaderProgramName": "default",
|
||||
"vertexShader": "./shaders/spark.vertex",
|
||||
"fragmentShader": "./shaders/spark.fragment"
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
{
|
||||
"emissionPoints": [
|
||||
{ "position": [0.0, 0.0, 0.0] }
|
||||
],
|
||||
"texture": "./resources/sand.png",
|
||||
"speedRange": [10.0, 30.0],
|
||||
"zSpeedRange": [-1.0, 1.0],
|
||||
"scaleRange": [0.5, 1.0],
|
||||
"lifeTimeRange": [200.0, 800.0],
|
||||
"emissionRate": 50.0,
|
||||
"maxParticles": 10,
|
||||
"particleSize": 0.09,
|
||||
"biasX": 0.1,
|
||||
"shaderProgramName": "default"
|
||||
}
|
||||
@ -1,86 +0,0 @@
|
||||
{
|
||||
"root": {
|
||||
"type": "FrameLayout",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"width": 1280,
|
||||
"height": 720,
|
||||
"children": [
|
||||
{
|
||||
"type": "FrameLayout",
|
||||
"name": "leftPanel",
|
||||
"x": 100,
|
||||
"y": 100,
|
||||
"width": 320,
|
||||
"height": 400,
|
||||
"children": [
|
||||
{
|
||||
"type": "LinearLayout",
|
||||
"name": "mainButtons",
|
||||
"orientation": "vertical",
|
||||
"spacing": 10,
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"width": 300,
|
||||
"height": 300,
|
||||
"children": [
|
||||
{
|
||||
"type": "Button",
|
||||
"name": "playButton",
|
||||
"x": 100,
|
||||
"y": 300,
|
||||
"width": 200,
|
||||
"height": 50,
|
||||
"textures": {
|
||||
"normal": "./resources/button.png",
|
||||
"hover": "./resources/button.png",
|
||||
"pressed": "./resources/button.png"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Button",
|
||||
"name": "settingsButton",
|
||||
"x": 100,
|
||||
"y": 200,
|
||||
"width": 200,
|
||||
"height": 50,
|
||||
"textures": {
|
||||
"normal": "./resources/sand.png",
|
||||
"hover": "./resources/sand.png",
|
||||
"pressed": "./resources/sand.png"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Button",
|
||||
"name": "exitButton",
|
||||
"x": 100,
|
||||
"y": 100,
|
||||
"width": 200,
|
||||
"height": 50,
|
||||
"textures": {
|
||||
"normal": "./resources/rock.png",
|
||||
"hover": "./resources/rock.png",
|
||||
"pressed": "./resources/rock.png"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "Slider",
|
||||
"name": "musicVolumeSlider",
|
||||
"x": 1140,
|
||||
"y": 100,
|
||||
"width": 10,
|
||||
"height": 500,
|
||||
"value": 0.5,
|
||||
"orientation": "vertical",
|
||||
"textures": {
|
||||
"track": "./resources/musicVolumeBarTexture.png",
|
||||
"knob": "./resources/musicVolumeBarButton.png"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
25526
external/nlohmann/json.hpp
vendored
25526
external/nlohmann/json.hpp
vendored
File diff suppressed because it is too large
Load Diff
BIN
resources/DefaultMaterial_BaseColor.png
(Stored with Git LFS)
BIN
resources/DefaultMaterial_BaseColor.png
(Stored with Git LFS)
Binary file not shown.
BIN
resources/DefaultMaterial_BaseColor_shine.png
(Stored with Git LFS)
BIN
resources/DefaultMaterial_BaseColor_shine.png
(Stored with Git LFS)
Binary file not shown.
BIN
resources/rockx.png
(Stored with Git LFS)
BIN
resources/rockx.png
(Stored with Git LFS)
Binary file not shown.
BIN
resources/sand2.png
(Stored with Git LFS)
BIN
resources/sand2.png
(Stored with Git LFS)
Binary file not shown.
BIN
resources/sandx.png
(Stored with Git LFS)
BIN
resources/sandx.png
(Stored with Git LFS)
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -1,35 +0,0 @@
|
||||
// Вершинный шейдер (Vertex Shader)
|
||||
|
||||
attribute vec3 vPosition;
|
||||
attribute vec3 vColor;
|
||||
attribute vec3 vNormal;
|
||||
attribute vec2 vTexCoord;
|
||||
|
||||
varying vec3 color;
|
||||
varying vec3 normal;
|
||||
varying vec2 TexCoord;
|
||||
varying float viewZ; // <--- НОВОЕ: Z-координата в пространстве вида (View Space)
|
||||
varying vec3 pos;
|
||||
|
||||
uniform mat4 ProjectionModelViewMatrix;
|
||||
uniform mat4 ModelViewMatrix; // Нужна для преобразования vPosition в View Space
|
||||
uniform vec3 uLightDirection;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Преобразование позиции в пространство вида (View Space)
|
||||
vec4 viewPosition = ModelViewMatrix * vec4(vPosition.xyz, 1.0);
|
||||
|
||||
// Сохраняем отрицательную Z-координату. В OpenGL Z-координата (глубина)
|
||||
// в пространстве вида обычно отрицательна, но для расчета тумана
|
||||
// удобнее использовать положительное значение.
|
||||
viewZ = -viewPosition.z;
|
||||
|
||||
pos = vPosition.xyz;
|
||||
|
||||
gl_Position = ProjectionModelViewMatrix * vec4(vPosition.xyz, 1.0);
|
||||
|
||||
normal = normalize((ModelViewMatrix * vec4(vNormal, 0.0)).xyz);
|
||||
color = vColor;
|
||||
TexCoord = vTexCoord;
|
||||
}
|
||||
@ -1,97 +0,0 @@
|
||||
// ---Фрагментный шейдер (Fragment Shader)
|
||||
|
||||
varying vec3 color;
|
||||
varying vec3 normal;
|
||||
varying vec2 TexCoord;
|
||||
varying float viewZ;
|
||||
varying vec3 pos;
|
||||
|
||||
uniform sampler2D Texture;
|
||||
uniform vec3 uLightDirection;
|
||||
uniform float uDistanceToPlanetSurface;
|
||||
uniform float uCurrentZFar;
|
||||
|
||||
// Константы для тумана:
|
||||
//const vec4 FOG_COLOR = vec4(0.0, 0.3, 0.3, 1.0); // Синий туман
|
||||
const vec4 FOG_COLOR = vec4(0.0, 0.5, 1.0, 1.0); // Синий туман
|
||||
|
||||
// Параметры "Distance Fog"
|
||||
const float DIST_FOG_MAX = 2000.0;
|
||||
const float DIST_FOG_MIN = 1000.0;
|
||||
|
||||
// Параметры "Z-Far Fog"
|
||||
// Насколько близко к Zfar должен начинаться туман (например, 10% от Zfar)
|
||||
const float Z_FOG_START_RATIO = 0.9;
|
||||
// Какую долю от Zfar должен покрывать туман (например, 10% от Zfar)
|
||||
const float Z_FOG_RANGE_RATIO = 0.1;
|
||||
uniform vec3 uColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// ... (1. Получаем цвет и 2. Расчет освещения)
|
||||
vec4 textureColor = texture2D(Texture, TexCoord);
|
||||
vec3 finalColor = textureColor.rgb;
|
||||
|
||||
float diffuse = max(0.0, dot(normal, uLightDirection));
|
||||
float ambient = 0.2;
|
||||
float lightingFactor = min(1.0, ambient + diffuse);
|
||||
vec3 litColor = finalColor * lightingFactor;
|
||||
|
||||
|
||||
// 3. Расчет коэффициента тумана (fogFactor)
|
||||
float fogFactor = 0.0;
|
||||
|
||||
// --- 3.1. Расчет коэффициента тумана на основе расстояния до поверхности (Distance Fog) ---
|
||||
// Этот туман работает на больших расстояниях и постепенно исчезает,
|
||||
// уступая место Z-Far Fog при приближении к планете.
|
||||
|
||||
if (uDistanceToPlanetSurface >= DIST_FOG_MIN && uDistanceToPlanetSurface <DIST_FOG_MAX) {
|
||||
// Нормализация: 0.0f при DIST_FOG_MAX, 1.0f при DIST_FOG_MIN
|
||||
float distRange = DIST_FOG_MAX - DIST_FOG_MIN;
|
||||
float distNormalized = clamp((uDistanceToPlanetSurface - DIST_FOG_MIN) / distRange, 0.0, 1.0);
|
||||
|
||||
// alpha = 0.0 (Далеко) ... 1.0 (Близко к 1000.f)
|
||||
fogFactor = 1.0 - distNormalized;
|
||||
}
|
||||
if (uDistanceToPlanetSurface < DIST_FOG_MIN)
|
||||
{
|
||||
fogFactor = 1.0;
|
||||
}
|
||||
// Если uDistanceToPlanetSurface < 1000.0f, то fogFactor = 0.0f (пока не пересчитан ниже),
|
||||
// что позволяет Z-Far Fog взять контроль.
|
||||
|
||||
|
||||
// --- 3.2. Расчет коэффициента тумана на основе Z-глубины (Z-Far Fog) ---
|
||||
// Этот туман работает всегда, но его эффект усиливается при уменьшении uCurrentZFar,
|
||||
// гарантируя, что все, что подходит к границе uCurrentZFar, будет скрыто.
|
||||
|
||||
// Точка начала тумана: 90% от uCurrentZFar
|
||||
float farFogStart = uCurrentZFar * 0.075;
|
||||
// Длина перехода тумана: 10% от uCurrentZFar
|
||||
float depthRange = uCurrentZFar * 0.1;
|
||||
|
||||
|
||||
float farFogFactor = 0.0;
|
||||
|
||||
if (depthRange > 0.0) {
|
||||
// Нормализация Z-глубины: 0.0f при farFogStart, 1.0f при farFogStart + depthRange
|
||||
farFogFactor = clamp((abs(viewZ) - farFogStart) / depthRange, 0.0, 1.0);
|
||||
}
|
||||
|
||||
// --- 3.3. Объединение ---
|
||||
|
||||
// Когда мы далеко (fogFactor > 0.0), Distance Fog доминирует.
|
||||
// Когда мы близко (fogFactor = 0.0), Z-Far Fog берет управление.
|
||||
// Если оба активны, берем максимум (например, когда фрагмент далек от игрока, но игрок все еще в зоне Distance Fog).
|
||||
//fogFactor = max(fogFactor, farFogFactor);
|
||||
fogFactor = fogFactor * farFogFactor;
|
||||
|
||||
|
||||
// 4. Смешивание цвета с туманом
|
||||
|
||||
//vec3 mountainColor = vec3((length(pos+vec3(0.0,0.0,45000.0))-20000.0)/100.0, 0.5,0.0);
|
||||
//gl_FragColor = mix(vec4(uColor* finalColor.rgb, 1.0), FOG_COLOR, fogFactor);
|
||||
//gl_FragColor = vec4((length(pos+vec3(0.0,0.0,45000.0))-20000.0)/100.0, 0.0,0.0, 1.0);
|
||||
//gl_FragColor = vec4(fogFactor, 0.5,0.5, 1.0);
|
||||
gl_FragColor = vec4(textureColor.rgb, 1.0);
|
||||
}
|
||||
@ -12,7 +12,6 @@ uniform float uDistanceToPlanetSurface;
|
||||
uniform float uCurrentZFar;
|
||||
|
||||
// Константы для тумана:
|
||||
//const vec4 FOG_COLOR = vec4(0.0, 0.5, 0.5, 1.0); // Синий туман
|
||||
const vec4 FOG_COLOR = vec4(0.0, 0.5, 1.0, 1.0); // Синий туман
|
||||
|
||||
// Параметры "Distance Fog"
|
||||
@ -21,17 +20,16 @@ const float DIST_FOG_MIN = 1000.0;
|
||||
|
||||
// Параметры "Z-Far Fog"
|
||||
// Насколько близко к Zfar должен начинаться туман (например, 10% от Zfar)
|
||||
const float Z_FOG_START_RATIO = 0.8;
|
||||
const float Z_FOG_START_RATIO = 0.9;
|
||||
// Какую долю от Zfar должен покрывать туман (например, 10% от Zfar)
|
||||
const float Z_FOG_RANGE_RATIO = 0.15;
|
||||
const float Z_FOG_RANGE_RATIO = 0.1;
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
// ... (1. Получаем цвет и 2. Расчет освещения)
|
||||
vec4 textureColor = texture2D(Texture, TexCoord);
|
||||
//vec3 finalColor = textureColor.rgb * color;
|
||||
vec3 finalColor = textureColor.rgb;
|
||||
vec3 finalColor = textureColor.rgb * color;
|
||||
|
||||
float diffuse = max(0.0, dot(normal, uLightDirection));
|
||||
float ambient = 0.2;
|
||||
@ -67,10 +65,20 @@ void main()
|
||||
// гарантируя, что все, что подходит к границе uCurrentZFar, будет скрыто.
|
||||
|
||||
// Точка начала тумана: 90% от uCurrentZFar
|
||||
float farFogStart = uCurrentZFar * 0.075;
|
||||
float farFogStart = 2000.f;//uCurrentZFar * 0.7;
|
||||
// Длина перехода тумана: 10% от uCurrentZFar
|
||||
float depthRange = uCurrentZFar * 0.1;
|
||||
float depthRange = 2000.f;//uCurrentZFar * 0.25;
|
||||
|
||||
if (abs(uDistanceToPlanetSurface) < 410.f)
|
||||
{
|
||||
farFogStart = 1800.f * abs(uDistanceToPlanetSurface-10.f) * 0.0025 + 200.f;
|
||||
depthRange = farFogStart;
|
||||
}
|
||||
if (abs(uDistanceToPlanetSurface) < 10.f)
|
||||
{
|
||||
farFogStart = 200.f;
|
||||
depthRange = 200.f;
|
||||
}
|
||||
|
||||
float farFogFactor = 0.0;
|
||||
|
||||
@ -93,5 +101,5 @@ void main()
|
||||
//vec3 mountainColor = vec3((length(pos+vec3(0.0,0.0,45000.0))-20000.0)/100.0, 0.5,0.0);
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, 0.5), FOG_COLOR, fogFactor);
|
||||
//gl_FragColor = vec4((length(pos+vec3(0.0,0.0,45000.0))-20000.0)/100.0, 0.0,0.0, 1.0);
|
||||
//gl_FragColor = vec4(finalColor.rgb, 1.0);
|
||||
//gl_FragColor = vec4(fogFactor, 0.5,0.5, 1.0);
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
attribute vec3 vPosition;
|
||||
attribute vec2 vTexCoord;
|
||||
attribute vec3 vNormal;
|
||||
attribute vec3 vTangent; // Новые атрибуты
|
||||
attribute vec3 vBinormal;
|
||||
|
||||
varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent;
|
||||
|
||||
uniform mat4 ProjectionModelViewMatrix;
|
||||
uniform vec3 uViewPos;
|
||||
|
||||
void main() {
|
||||
gl_Position = ProjectionModelViewMatrix * vec4(vPosition, 1.0);
|
||||
TexCoord = vTexCoord;
|
||||
|
||||
vec3 viewDirWorld = normalize(uViewPos - vPosition);
|
||||
|
||||
// Строим матрицу перехода из атрибутов
|
||||
// Так как базис ортонормирован, TBN^-1 == TBN_transpose
|
||||
vViewDirTangent = vec3(
|
||||
dot(viewDirWorld, vTangent),
|
||||
dot(viewDirWorld, vBinormal),
|
||||
dot(viewDirWorld, vNormal)
|
||||
);
|
||||
}
|
||||
@ -1,69 +0,0 @@
|
||||
/*varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent;
|
||||
|
||||
uniform sampler2D Texture; // Нам нужен только Alpha канал (высота)
|
||||
uniform float uHeightScale;
|
||||
|
||||
void main() {
|
||||
vec3 viewDir = normalize(vViewDirTangent);
|
||||
float height = texture2D(Texture, TexCoord).a;
|
||||
|
||||
// Рассчитываем вектор смещения P
|
||||
//vec2 p = viewDir.xy * (height * uHeightScale) / viewDir.z;
|
||||
//vec2 p = vec2(viewDir.y, -viewDir.x) * (height * uHeightScale);
|
||||
//vec2 p = viewDir.xy * (height * uHeightScale);
|
||||
vec2 p = vec2(viewDir.x, -viewDir.y) * (height * uHeightScale);
|
||||
vec2 finalTexCoord = TexCoord + p;
|
||||
|
||||
// 1. Визуализация сетки по смещенным координатам
|
||||
// Если сетка кривая или ломается на стыках — значит T, B, N векторы не сошлись
|
||||
vec2 grid = fract(finalTexCoord * 20.0); // 20 ячеек сетки
|
||||
float line = (step(0.9, grid.x) + step(0.9, grid.y));
|
||||
|
||||
// 2. Визуализация вектора смещения через цвет
|
||||
// Красный = смещение по U, Зеленый = смещение по V
|
||||
vec3 offsetColor = vec3(p * 10.0 + 0.5, 0.0); // Умножаем на 10 для видимости
|
||||
|
||||
vec3 finalColor = mix(offsetColor, vec3(1.0), line); // Накладываем сетку поверх цвета
|
||||
|
||||
// 3. Подмешиваем карту высот, чтобы видеть "объемы"
|
||||
gl_FragColor = vec4(finalColor * height, 1.0);
|
||||
}*/
|
||||
|
||||
|
||||
varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent;
|
||||
|
||||
uniform sampler2D Texture;
|
||||
uniform float uHeightScale;
|
||||
|
||||
void main() {
|
||||
vec3 viewDir = normalize(vViewDirTangent);
|
||||
|
||||
// Получаем высоту из альфа-канала запеченной текстуры
|
||||
float height = texture2D(Texture, TexCoord).a;
|
||||
|
||||
// Смещение. Знак минус используется, если мы хотим "вдавить" камни
|
||||
// Деление на viewDir.z помогает избежать сильных искажений под углом
|
||||
//vec2 p = viewDir.xy * (height * uHeightScale) / viewDir.z;
|
||||
vec2 p = vec2(viewDir.x, -viewDir.y) * (height * uHeightScale);
|
||||
//vec2 p = vec2(0,0);
|
||||
vec2 finalTexCoord = TexCoord + p;
|
||||
|
||||
gl_FragColor = texture2D(Texture, finalTexCoord);
|
||||
}
|
||||
|
||||
/*
|
||||
varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent; // Тот самый вектор из VS
|
||||
|
||||
void main() {
|
||||
// 1. Нормализуем входящий вектор
|
||||
vec3 v = normalize(vViewDirTangent);
|
||||
|
||||
// 2. Преобразуем компоненты из [-1, 1] в [0, 1] для визуализации
|
||||
// X -> Red, Y -> Green, Z -> Blue
|
||||
vec3 debugColor = v * 0.5 + 0.5;
|
||||
|
||||
gl_FragColor = vec4(debugColor, 1.0);
|
||||
}*/
|
||||
@ -1,31 +0,0 @@
|
||||
// Vertex Shader (Bake Stage)
|
||||
attribute vec3 vPosition;
|
||||
attribute vec2 vTexCoord;
|
||||
|
||||
varying vec2 TexCoord;
|
||||
varying float vHeight;
|
||||
|
||||
// Данные о плоскости треугольника планеты
|
||||
uniform vec3 uPlanePoint; // Любая вершина треугольника (например, tri.data[0])
|
||||
uniform vec3 uPlaneNormal; // Нормаль треугольника планеты (ось Z из GetRotationForTriangle)
|
||||
uniform float uMaxHeight; // Максимальный размер камня (BASE_SCALE)
|
||||
|
||||
uniform mat4 ProjectionModelViewMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
// 1. Вычисляем вектор от плоскости до текущей вершины камня
|
||||
vec3 vecToVertex = vPosition - uPlanePoint;
|
||||
|
||||
// 2. Проецируем этот вектор на нормаль плоскости (скалярное произведение)
|
||||
// Это и есть кратчайшее расстояние от точки до плоскости
|
||||
float distance = dot(vecToVertex, uPlaneNormal);
|
||||
|
||||
// 3. Нормализуем высоту для записи в текстуру (0.0 - уровень земли, 1.0 - пик)
|
||||
// Clamp гарантирует, что значения не выйдут за пределы, если камень "утоплен"
|
||||
vHeight = clamp(distance / uMaxHeight, 0.0, 1.0);
|
||||
|
||||
TexCoord = vTexCoord;
|
||||
|
||||
gl_Position = ProjectionModelViewMatrix * vec4(vPosition, 1.0);
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
varying vec2 TexCoord;
|
||||
varying float vHeight;
|
||||
|
||||
uniform sampler2D Texture;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 stoneColor = texture2D(Texture, TexCoord);
|
||||
|
||||
gl_FragColor = vec4(stoneColor.rgb, vHeight);
|
||||
//gl_FragColor = vec4(vHeight, vHeight, vHeight, vHeight);
|
||||
//gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
attribute vec3 vPosition;
|
||||
attribute vec2 vTexCoord;
|
||||
attribute vec3 vNormal;
|
||||
attribute vec3 vTangent; // Новые атрибуты
|
||||
attribute vec3 vBinormal;
|
||||
|
||||
varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent;
|
||||
varying vec3 worldPosition;
|
||||
|
||||
uniform mat4 ProjectionModelViewMatrix;
|
||||
uniform mat4 ModelViewMatrix;
|
||||
|
||||
uniform vec3 uViewPos;
|
||||
|
||||
void main() {
|
||||
gl_Position = ProjectionModelViewMatrix * vec4(vPosition, 1.0);
|
||||
TexCoord = vTexCoord;
|
||||
|
||||
vec3 viewDirWorld = normalize(uViewPos - vPosition);
|
||||
|
||||
// Строим матрицу перехода из атрибутов
|
||||
// Так как базис ортонормирован, TBN^-1 == TBN_transpose
|
||||
vViewDirTangent = vec3(
|
||||
dot(viewDirWorld, vTangent),
|
||||
dot(viewDirWorld, vBinormal),
|
||||
dot(viewDirWorld, vNormal)
|
||||
);
|
||||
vec4 viewPosition = ModelViewMatrix * vec4(vPosition.xyz, 1.0);
|
||||
|
||||
worldPosition = vPosition;
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent;
|
||||
|
||||
uniform sampler2D Texture;
|
||||
uniform sampler2D BakedTexture;
|
||||
uniform float uHeightScale;
|
||||
uniform float uDistanceToPlanetSurface;
|
||||
uniform float uCurrentZFar;
|
||||
varying vec3 worldPosition;
|
||||
|
||||
uniform vec3 uViewPos;
|
||||
const vec4 FOG_COLOR = vec4(0.0, 0.5, 1.0, 1.0); // Синий туман
|
||||
|
||||
void main() {
|
||||
vec3 viewDir = normalize(vViewDirTangent);
|
||||
|
||||
// Получаем высоту из альфа-канала запеченной текстуры
|
||||
float height = texture2D(Texture, TexCoord).a;
|
||||
|
||||
// Смещение. Знак минус используется, если мы хотим "вдавить" камни
|
||||
// Деление на viewDir.z помогает избежать сильных искажений под углом
|
||||
//vec2 p = viewDir.xy * (height * uHeightScale) / viewDir.z;
|
||||
vec2 p = vec2(viewDir.x, -viewDir.y) * (height * uHeightScale);
|
||||
//vec2 p = vec2(0,0);
|
||||
vec2 finalTexCoord = TexCoord + p;
|
||||
|
||||
float realDist = distance(worldPosition, uViewPos);
|
||||
|
||||
|
||||
float textureMixFactor = clamp((2000 - realDist) / 500.0, 0.0, 1.0);
|
||||
|
||||
vec4 bakedTextureColor = texture2D(BakedTexture, finalTexCoord);
|
||||
vec4 textureColor = texture2D(Texture, TexCoord);
|
||||
|
||||
vec4 finalColor = textureMixFactor*textureColor + (1 - textureMixFactor) * bakedTextureColor;
|
||||
|
||||
float fogFactor;
|
||||
if (uDistanceToPlanetSurface > 1000)
|
||||
{
|
||||
gl_FragColor = vec4(finalColor.rgb, 1.0);
|
||||
}
|
||||
else if (uDistanceToPlanetSurface > 100)
|
||||
{
|
||||
float t = clamp((uDistanceToPlanetSurface - 100) / 900.0, 0.0, 1.0); // 1 upstairs, 0 downstairs
|
||||
|
||||
fogFactor = clamp((realDist - 2400) / (300.0*(1 + 10*t)), 0.0, 1.0);
|
||||
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, 1.0), FOG_COLOR, fogFactor*(1.0 - t));
|
||||
}
|
||||
else if (uDistanceToPlanetSurface > 40)
|
||||
{
|
||||
//From 100 to 40:
|
||||
//(1000 < realDist < 1800)
|
||||
|
||||
float t = clamp((uDistanceToPlanetSurface - 40) / 60.0, 0.0, 1.0); // 1 upstairs, 0 downstairs
|
||||
|
||||
fogFactor = clamp((realDist - 2400) / 300.0, 0.0, 1.0); // old fog factor
|
||||
|
||||
float fogFactor2 = clamp((realDist - 1000) / 800.0, 0.0, 1.0);
|
||||
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, 1.0), FOG_COLOR, max(fogFactor, fogFactor2*(1.0 - t)));
|
||||
}
|
||||
else
|
||||
{
|
||||
fogFactor = clamp((realDist - 1000) / (800.0), 0.0, 1.0);
|
||||
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, 1.0), FOG_COLOR, fogFactor);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
|
||||
varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent; // Тот самый вектор из VS
|
||||
|
||||
void main() {
|
||||
// 1. Нормализуем входящий вектор
|
||||
vec3 v = normalize(vViewDirTangent);
|
||||
|
||||
// 2. Преобразуем компоненты из [-1, 1] в [0, 1] для визуализации
|
||||
// X -> Red, Y -> Green, Z -> Blue
|
||||
vec3 debugColor = v * 0.5 + 0.5;
|
||||
|
||||
gl_FragColor = vec4(debugColor, 1.0);
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent;
|
||||
|
||||
uniform sampler2D Texture; // Нам нужен только Alpha канал (высота)
|
||||
uniform float uHeightScale;
|
||||
|
||||
void main() {
|
||||
vec3 viewDir = normalize(vViewDirTangent);
|
||||
float height = texture2D(Texture, TexCoord).a;
|
||||
|
||||
// Рассчитываем вектор смещения P
|
||||
//vec2 p = viewDir.xy * (height * uHeightScale) / viewDir.z;
|
||||
//vec2 p = vec2(viewDir.y, -viewDir.x) * (height * uHeightScale);
|
||||
//vec2 p = viewDir.xy * (height * uHeightScale);
|
||||
vec2 p = vec2(viewDir.x, -viewDir.y) * (height * uHeightScale);
|
||||
vec2 finalTexCoord = TexCoord + p;
|
||||
|
||||
// 1. Визуализация сетки по смещенным координатам
|
||||
// Если сетка кривая или ломается на стыках — значит T, B, N векторы не сошлись
|
||||
vec2 grid = fract(finalTexCoord * 20.0); // 20 ячеек сетки
|
||||
float line = (step(0.9, grid.x) + step(0.9, grid.y));
|
||||
|
||||
// 2. Визуализация вектора смещения через цвет
|
||||
// Красный = смещение по U, Зеленый = смещение по V
|
||||
vec3 offsetColor = vec3(p * 10.0 + 0.5, 0.0); // Умножаем на 10 для видимости
|
||||
|
||||
vec3 finalColor = mix(offsetColor, vec3(1.0), line); // Накладываем сетку поверх цвета
|
||||
|
||||
// 3. Подмешиваем карту высот, чтобы видеть "объемы"
|
||||
gl_FragColor = vec4(finalColor * height, 1.0);
|
||||
}
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
// Вершинный шейдер (Vertex Shader)
|
||||
|
||||
attribute vec3 vPosition;
|
||||
attribute vec2 vTexCoord;
|
||||
|
||||
varying vec2 TexCoord;
|
||||
varying float viewZ;
|
||||
varying vec3 pos;
|
||||
varying vec3 worldPosition;
|
||||
|
||||
|
||||
uniform mat4 ProjectionModelViewMatrix;
|
||||
uniform mat4 ModelViewMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Преобразование позиции в пространство вида (View Space)
|
||||
vec4 viewPosition = ModelViewMatrix * vec4(vPosition.xyz, 1.0);
|
||||
|
||||
// Сохраняем отрицательную Z-координату. В OpenGL Z-координата (глубина)
|
||||
// в пространстве вида обычно отрицательна, но для расчета тумана
|
||||
// удобнее использовать положительное значение.
|
||||
viewZ = -viewPosition.z;
|
||||
|
||||
pos = vPosition.xyz;
|
||||
|
||||
gl_Position = ProjectionModelViewMatrix * vec4(vPosition.xyz, 1.0);
|
||||
|
||||
TexCoord = vTexCoord;
|
||||
worldPosition = vPosition;
|
||||
}
|
||||
@ -1,59 +0,0 @@
|
||||
// ---Фрагментный шейдер (Fragment Shader)
|
||||
|
||||
varying vec2 TexCoord;
|
||||
varying float viewZ;
|
||||
varying vec3 pos;
|
||||
|
||||
uniform sampler2D Texture;
|
||||
uniform float uDistanceToPlanetSurface;
|
||||
|
||||
const vec4 FOG_COLOR = vec4(0.0, 0.5, 1.0, 1.0); // Синий туман
|
||||
|
||||
varying vec3 worldPosition;
|
||||
|
||||
uniform vec3 uViewPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
|
||||
vec4 textureColor = texture2D(Texture, TexCoord);
|
||||
vec3 finalColor = textureColor.rgb;
|
||||
|
||||
float realDist = distance(worldPosition, uViewPos);
|
||||
|
||||
float alphaFactor = clamp((2000 - realDist) / 500.0, 0.0, 1.0);
|
||||
|
||||
float fogFactor;
|
||||
if (uDistanceToPlanetSurface > 1000)
|
||||
{
|
||||
gl_FragColor = vec4(finalColor.rgb, 1.0);
|
||||
}
|
||||
else if (uDistanceToPlanetSurface > 100)
|
||||
{
|
||||
float t = clamp((uDistanceToPlanetSurface - 100) / 900.0, 0.0, 1.0); // 1 upstairs, 0 downstairs
|
||||
|
||||
fogFactor = clamp((realDist - 2400) / (300.0*(1 + 10*t)), 0.0, 1.0);
|
||||
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, alphaFactor), FOG_COLOR, fogFactor*(1.0 - t));
|
||||
}
|
||||
else if (uDistanceToPlanetSurface > 40)
|
||||
{
|
||||
//From 100 to 40:
|
||||
//(1000 < realDist < 1800)
|
||||
|
||||
float t = clamp((uDistanceToPlanetSurface - 40) / 60.0, 0.0, 1.0); // 1 upstairs, 0 downstairs
|
||||
|
||||
fogFactor = clamp((realDist - 2400) / 300.0, 0.0, 1.0); // old fog factor
|
||||
|
||||
float fogFactor2 = clamp((realDist - 1000) / 800.0, 0.0, 1.0);
|
||||
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, alphaFactor), FOG_COLOR, max(fogFactor, fogFactor2*(1.0 - t)));
|
||||
}
|
||||
else
|
||||
{
|
||||
fogFactor = clamp((realDist - 1000) / (800.0), 0.0, 1.0);
|
||||
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, alphaFactor), FOG_COLOR, fogFactor);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
//precision mediump float;
|
||||
uniform sampler2D Texture;
|
||||
varying vec2 texCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 color = texture2D(Texture,texCoord).rgba;
|
||||
//gl_FragColor = vec4(color.rgb*0.9 + vec3(0.1, 0.1, 0.1), 1.0);
|
||||
|
||||
gl_FragColor = color;
|
||||
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
attribute vec3 vPosition;
|
||||
attribute vec2 vTexCoord;
|
||||
varying vec2 texCoord;
|
||||
|
||||
uniform mat4 ProjectionModelViewMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = ProjectionModelViewMatrix * vec4(vPosition.xyz, 1.0);
|
||||
texCoord = vTexCoord;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user