Clean up some
This commit is contained in:
parent
1af829bf49
commit
f197dfcf02
54
Game.cpp
54
Game.cpp
@ -631,26 +631,6 @@ namespace ZL
|
||||
}
|
||||
else if (event.type == SDL_KEYUP)
|
||||
{
|
||||
if (event.key.keysym.sym == SDLK_a)
|
||||
{
|
||||
planetObject.x += 1;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_s)
|
||||
{
|
||||
planetObject.x -= 1;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_q)
|
||||
{
|
||||
|
||||
Environment::shipPosition = { 0, 0, 25000 };
|
||||
//Environment::shipPosition = { 50000, 50000, 50000 };
|
||||
|
||||
//planetObject.y += 1;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_w)
|
||||
{
|
||||
planetObject.y -= 1;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_i)
|
||||
{
|
||||
Environment::shipVelocity += 500.f;
|
||||
@ -667,40 +647,6 @@ namespace ZL
|
||||
{
|
||||
Environment::shipVelocity -= 50.f;
|
||||
}
|
||||
|
||||
if (event.key.keysym.sym == SDLK_1)
|
||||
{
|
||||
planetObject.planetData.ZScale = 1000.f;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_2)
|
||||
{
|
||||
planetObject.planetData.ZScale = 500.f;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_3)
|
||||
{
|
||||
planetObject.planetData.ZScale = 250.f;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_4)
|
||||
{
|
||||
planetObject.planetData.ZScale = 125.f;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_5)
|
||||
{
|
||||
planetObject.planetData.ZScale = 65.f;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_6)
|
||||
{
|
||||
planetObject.planetData.ZScale = 32.f;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_7)
|
||||
{
|
||||
planetObject.planetData.ZScale = 16.f;
|
||||
}
|
||||
if (event.key.keysym.sym == SDLK_8)
|
||||
{
|
||||
planetObject.planetData.ZScale = 8.f;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
render();
|
||||
|
||||
134
PlanetData.cpp
134
PlanetData.cpp
@ -11,40 +11,6 @@ namespace ZL {
|
||||
|
||||
// --- Êîíñòàíòû äèàïàçîíîâ (ïåðåíåñåíû èç PlanetObject.cpp) ---
|
||||
|
||||
static constexpr float FAR_Z_NEAR = 2000.0f;
|
||||
static constexpr float FAR_Z_FAR = 200000.0f;
|
||||
|
||||
static constexpr float TRANSITION_FAR_START = 3000.0f;
|
||||
static constexpr float MIDDLE_Z_NEAR = 300.f;
|
||||
static constexpr float MIDDLE_Z_FAR = 100000.f;
|
||||
|
||||
static constexpr float TRANSITION_MIDDLE_START = 500.f;
|
||||
static constexpr float NEAR_Z_NEAR = 80.0f;
|
||||
static constexpr float NEAR_Z_FAR = 20000.0f;
|
||||
|
||||
static constexpr float TRANSITION_NEAR_END = 100.f;
|
||||
|
||||
|
||||
//static constexpr float SUPER_NEAR_Z_NEAR = 5.0f;
|
||||
//static constexpr float SUPER_NEAR_Z_FAR = 15000.f;
|
||||
static constexpr float SUPER_NEAR_Z_NEAR = 100.0f;
|
||||
static constexpr float SUPER_NEAR_Z_FAR = 10000.0f;
|
||||
static constexpr float TRANSITION_SUPER_NEAR_END = 30.f;
|
||||
|
||||
/*
|
||||
static constexpr float FAR_Z_NEAR = 2000.0f;
|
||||
static constexpr float FAR_Z_FAR = 200000.0f;
|
||||
static constexpr float TRANSITION_FAR_START = 3000.0f;
|
||||
static constexpr float MIDDLE_Z_NEAR = 300.f;
|
||||
static constexpr float MIDDLE_Z_FAR = 100000.f;
|
||||
static constexpr float TRANSITION_MIDDLE_START = 500.f;
|
||||
static constexpr float NEAR_Z_NEAR = 80.0f;
|
||||
static constexpr float NEAR_Z_FAR = 20000.0f;
|
||||
static constexpr float TRANSITION_NEAR_END = 100.f;
|
||||
static constexpr float SUPER_NEAR_Z_NEAR = 5.0f;
|
||||
static constexpr float SUPER_NEAR_Z_FAR = 5000.0f;
|
||||
static constexpr float TRANSITION_SUPER_NEAR_END = 30.f;
|
||||
*/
|
||||
VertexID generateEdgeID(const VertexID& id1, const VertexID& id2) {
|
||||
return id1 < id2 ? id1 + "_" + id2 : id2 + "_" + id1;
|
||||
}
|
||||
@ -83,6 +49,12 @@ namespace ZL {
|
||||
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);
|
||||
@ -92,6 +64,10 @@ namespace ZL {
|
||||
return planetMeshLods.at(level);
|
||||
}
|
||||
|
||||
const LodLevel& PlanetData::getLodLevelNoDist(int level) const {
|
||||
return planetMeshLodsNoDist.at(level);
|
||||
}
|
||||
|
||||
const LodLevel& PlanetData::getAtmosphereLod() const {
|
||||
return planetAtmosphereLod;
|
||||
}
|
||||
@ -147,39 +123,6 @@ namespace ZL {
|
||||
currentZFar = 5000.f;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
if (dToPlanetSurface >= TRANSITION_FAR_START) {
|
||||
currentZNear = FAR_Z_NEAR;
|
||||
currentZFar = FAR_Z_FAR;
|
||||
}
|
||||
else if (dToPlanetSurface > TRANSITION_MIDDLE_START) {
|
||||
const float transitionLength = TRANSITION_FAR_START - TRANSITION_MIDDLE_START;
|
||||
float normalizedDist = (dToPlanetSurface - TRANSITION_MIDDLE_START) / transitionLength;
|
||||
alpha = 1.0f - normalizedDist;
|
||||
currentZNear = FAR_Z_NEAR * (1.0f - alpha) + MIDDLE_Z_NEAR * alpha;
|
||||
currentZFar = FAR_Z_FAR * (1.0f - alpha) + MIDDLE_Z_FAR * alpha;
|
||||
}
|
||||
|
||||
else if (dToPlanetSurface > TRANSITION_NEAR_END) {
|
||||
const float transitionLength = TRANSITION_MIDDLE_START - TRANSITION_NEAR_END;
|
||||
float normalizedDist = (dToPlanetSurface - TRANSITION_NEAR_END) / transitionLength;
|
||||
alpha = 1.0f - normalizedDist;
|
||||
currentZNear = MIDDLE_Z_NEAR * (1.0f - alpha) + NEAR_Z_NEAR * alpha;
|
||||
currentZFar = MIDDLE_Z_FAR * (1.0f - alpha) + NEAR_Z_FAR * alpha;
|
||||
}
|
||||
else if (dToPlanetSurface > TRANSITION_SUPER_NEAR_END) {
|
||||
const float transitionLength = TRANSITION_NEAR_END - TRANSITION_SUPER_NEAR_END;
|
||||
float normalizedDist = (dToPlanetSurface - TRANSITION_SUPER_NEAR_END) / transitionLength;
|
||||
alpha = 1.0f - normalizedDist;
|
||||
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 {
|
||||
currentZNear = SUPER_NEAR_Z_NEAR;
|
||||
currentZFar = SUPER_NEAR_Z_FAR;
|
||||
}
|
||||
*/
|
||||
return { currentZNear, currentZFar };
|
||||
}
|
||||
|
||||
@ -340,10 +283,6 @@ namespace ZL {
|
||||
return result;
|
||||
}
|
||||
|
||||
// ... Ðåàëèçàöèè subdivideTriangles, calculateSurfaceNormal, trianglesToVertices, generateSphere, findNeighbors
|
||||
// Ïåðåíåñèòå èõ èç PlanetObject.cpp "êàê åñòü", äîáàâèâ êëàññ PlanetData:: ïåðåä èìåíåì.
|
||||
// Óáåäèòåñü, ÷òî èñïîëüçóåòå perlin è planetMeshLods èç this.
|
||||
|
||||
std::vector<Triangle> PlanetData::subdivideTriangles(const std::vector<Triangle>& input, float noiseCoeff) {
|
||||
std::vector<Triangle> output;
|
||||
|
||||
@ -379,43 +318,6 @@ namespace ZL {
|
||||
return output;
|
||||
}
|
||||
|
||||
Vector3f PlanetData::calculateSurfaceNormal(Vector3f p_sphere, float noiseCoeff) {
|
||||
// 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, noiseCoeff);
|
||||
Vector3f p1 = p1_dir * perlin.getSurfaceHeight(p1_dir, noiseCoeff);
|
||||
Vector3f p2 = p2_dir * perlin.getSurfaceHeight(p2_dir, noiseCoeff);
|
||||
|
||||
// Âåêòîðà îò öåíòðàëüíîé òî÷êè ê ñîñåäÿì
|
||||
Vector3f v1 = p1 - p0;
|
||||
Vector3f v2 = p2 - p0;
|
||||
|
||||
// Íîðìàëü - ýòî ïåðïåíäèêóëÿð ê ýòèì äâóì âåêòîðàì
|
||||
// Ïîðÿäîê (v2, v1) èëè (v1, v2) çàâèñèò îò ñèñòåìû êîîðäèíàò,
|
||||
// çäåñü ïîäáèðàåì òàê, ÷òîáû íîðìàëü ñìîòðåëà íàðóæó.
|
||||
return (-v2.cross(v1)).normalized();
|
||||
}
|
||||
|
||||
LodLevel PlanetData::trianglesToVertices(const std::vector<Triangle>& geometry) {
|
||||
LodLevel result;
|
||||
result.triangles = geometry;
|
||||
@ -492,16 +394,6 @@ namespace ZL {
|
||||
t.ids[i] = initialVertexMap[t.data[i].normalized()];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. ÏÐÈÌÅÍßÅÌ ØÓÌ Ê ÈÑÕÎÄÍÛÌ ÂÅÐØÈÍÀÌ
|
||||
// ÂÀÆÍÎ: Ìû ïðèìåíÿåì øóì ÏÎÑËÅ íîðìàëèçàöèè, íî ïåðåä ðàçáèåíèåì.
|
||||
// Åñëè âû õîòèòå, ÷òîáû øóì áûë ïðèìåíåí òîëüêî ê êîíå÷íûì âåðøèíàì,
|
||||
// ïåðåìåñòèòå ýòîò áëîê ïîñëå øàãà 3. Îñòàâèì, êàê â âàøåì êîäå.
|
||||
// **ÏÐÈÌÅ×ÀÍÈÅ:** Åñëè øóì ïðèìåíåí ñåé÷àñ, òî âåðøèíû íà L>0 áóäóò èìåòü
|
||||
// êîîðäèíàòû (m_ab = (a_noisy + b_noisy)*0.5).
|
||||
// Åñëè âû õîòèòå, ÷òîáû òîëüêî ôèíàëüíûå âåðøèíû èìåëè øóì, ïðîïóñòèòå ýòîò áëîê.
|
||||
//  òåêóùåé çàäà÷å ýòî íå êðèòè÷íî, òàê êàê ìû èñïîëüçóåì ÒÎËÜÊÎ VertexID.
|
||||
|
||||
// 3. Ðàçáèâàåì N ðàç (â subdivideTriangles ãåíåðèðóþòñÿ ID íîâûõ âåðøèí)
|
||||
for (int i = 0; i < subdivisions; i++) {
|
||||
geometry = subdivideTriangles(geometry, noiseCoeff);
|
||||
@ -533,8 +425,6 @@ namespace ZL {
|
||||
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);
|
||||
// Îáðàòèòå âíèìàíèå: NormalData îñòàåòñÿ (dir), êàê â âàøåì êîäå
|
||||
//lodLevel.vertexData.NormalData[i] = dir;
|
||||
}
|
||||
|
||||
|
||||
@ -572,7 +462,6 @@ namespace ZL {
|
||||
};
|
||||
|
||||
Vector3f finalColor = baseColor + colorOffset;
|
||||
// ... (îãðàíè÷åíèÿ öâåòà)
|
||||
|
||||
lodLevel.vertexData.ColorData.push_back(finalColor);
|
||||
}
|
||||
@ -601,7 +490,4 @@ namespace ZL {
|
||||
}
|
||||
return std::vector<int>(neighbors.begin(), neighbors.end());
|
||||
}
|
||||
|
||||
// ... È îñòàëüíûå ìåòîäû ãåíåðàöèè ...
|
||||
|
||||
}
|
||||
@ -68,13 +68,12 @@ namespace ZL {
|
||||
static const float PLANET_RADIUS;
|
||||
static const Vector3f PLANET_CENTER_OFFSET;
|
||||
|
||||
float ZScale = 1000.f;
|
||||
|
||||
private:
|
||||
PerlinNoise perlin;
|
||||
PerlinNoise colorPerlin;
|
||||
|
||||
std::array<LodLevel, MAX_LOD_LEVELS> planetMeshLods;
|
||||
std::array<LodLevel, MAX_LOD_LEVELS> planetMeshLodsNoDist;
|
||||
LodLevel planetAtmosphereLod;
|
||||
|
||||
int currentLod; // Ëîãè÷åñêèé òåêóùèé óðîâåíü äåòàëèçàöèè
|
||||
@ -83,7 +82,6 @@ namespace ZL {
|
||||
|
||||
// Âíóòðåííèå ìåòîäû ãåíåðàöèè
|
||||
std::vector<Triangle> subdivideTriangles(const std::vector<Triangle>& inputTriangles, float noiseCoeff);
|
||||
Vector3f calculateSurfaceNormal(Vector3f p_sphere, float noiseCoeff);
|
||||
LodLevel trianglesToVertices(const std::vector<Triangle>& triangles);
|
||||
LodLevel generateSphere(int subdivisions, float noiseCoeff);
|
||||
|
||||
@ -98,6 +96,7 @@ namespace ZL {
|
||||
|
||||
// Ìåòîäû äîñòóïà ê äàííûì (äëÿ ðåíäåðåðà)
|
||||
const LodLevel& getLodLevel(int level) const;
|
||||
const LodLevel& getLodLevelNoDist(int level) const;
|
||||
const LodLevel& getAtmosphereLod() const;
|
||||
int getCurrentLodIndex() const;
|
||||
int getMaxLodIndex() const;
|
||||
|
||||
109
PlanetObject.cpp
109
PlanetObject.cpp
@ -34,12 +34,6 @@ namespace ZL {
|
||||
Vector3f y_axis = z_axis.cross(x_axis).normalized();
|
||||
|
||||
// 4. Формируем прямую матрицу поворота (Rotation/World Matrix).
|
||||
// Векторы базиса записываются в СТОЛБЦЫ.
|
||||
// В памяти Matrix3f m (std::array<float, 9>):
|
||||
// m[0]=X.x, m[1]=Y.x, m[2]=Z.x
|
||||
// m[3]=X.y, m[4]=Y.y, m[5]=Z.y
|
||||
// m[6]=X.z, m[7]=Y.z, m[8]=Z.z
|
||||
|
||||
Matrix3f rot;
|
||||
|
||||
// Столбец 0: Ось X
|
||||
@ -73,17 +67,9 @@ namespace ZL {
|
||||
// Берем максимальный LOD для начальной отрисовки
|
||||
int lodIndex = planetData.getMaxLodIndex();
|
||||
planetRenderStruct.data = planetData.getLodLevel(lodIndex).vertexData;
|
||||
/*planetRenderStruct.data.PositionData[0] = planetRenderStruct.data.PositionData[6 * 3];
|
||||
planetRenderStruct.data.PositionData[0+1] = planetRenderStruct.data.PositionData[6 * 3+1];
|
||||
planetRenderStruct.data.PositionData[0+2] = planetRenderStruct.data.PositionData[6 * 3+2];
|
||||
*/
|
||||
//planetRenderStruct.data.PositionData.resize(3);
|
||||
/*planetRenderStruct.data.NormalData[0] = Vector3f{1.0,1.0,1.0}.normalized();
|
||||
planetRenderStruct.data.NormalData[1] = Vector3f{ 1.0,1.0,1.0 }.normalized();
|
||||
planetRenderStruct.data.NormalData[2] = Vector3f{ 1.0,1.0,1.0 }.normalized();*/
|
||||
planetRenderStruct.RefreshVBO();
|
||||
|
||||
planetRenderStructCut.data = planetData.getLodLevel(lodIndex).vertexData;
|
||||
planetRenderStructCut.data = planetData.getLodLevelNoDist(lodIndex).vertexData;
|
||||
|
||||
planetRenderStructCut.data.PositionData.resize(3);
|
||||
|
||||
@ -202,7 +188,7 @@ namespace ZL {
|
||||
|
||||
|
||||
|
||||
Triangle tr = planetData.getLodLevel(planetData.getCurrentLodIndex()).triangles[0];
|
||||
Triangle tr = planetData.getLodLevelNoDist(planetData.getCurrentLodIndex()).triangles[0];
|
||||
|
||||
// 1. Получаем матрицу вращения (оси в столбцах)
|
||||
Matrix3f mr = GetRotationForTriangle(tr);
|
||||
@ -227,19 +213,16 @@ namespace ZL {
|
||||
float centerX = (minX + maxX) * 0.5f;
|
||||
float centerY = (minY + maxY) * 0.5f;
|
||||
|
||||
//float size = max(width, height);
|
||||
//float hSize = size * 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,
|
||||
//currentZNear, currentZFar);
|
||||
150, 200000);
|
||||
|
||||
renderer.PushMatrix();
|
||||
renderer.LoadIdentity();
|
||||
|
||||
|
||||
// Сдвигаем камеру по Z
|
||||
renderer.TranslateMatrix(Vector3f{ 0, 0, -45000 });
|
||||
|
||||
@ -249,21 +232,18 @@ namespace ZL {
|
||||
|
||||
// Извлекаем нормаль треугольника (это 3-й столбец нашей матрицы вращения)
|
||||
Vector3f planeNormal = { mr.m[2], mr.m[5], mr.m[8] };
|
||||
//Vector3f planeNormal = { 0,0,1 };
|
||||
|
||||
|
||||
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);
|
||||
|
||||
//glClear(GL_DEPTH_BUFFER_BIT);
|
||||
renderer.PopMatrix();
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK); // Отсекаем задние грани
|
||||
@ -311,7 +291,7 @@ namespace ZL {
|
||||
//drawYellowZone(renderer);
|
||||
drawStones(renderer);
|
||||
|
||||
//drawAtmosphere(renderer);
|
||||
drawAtmosphere(renderer);
|
||||
}
|
||||
|
||||
void PlanetObject::drawPlanet(Renderer& renderer)
|
||||
@ -322,8 +302,6 @@ namespace ZL {
|
||||
static const std::string vColorName = "vColor";
|
||||
static const std::string vNormalName = "vNormal";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string vTexCoord2Name = "vTexCoord2";
|
||||
//static const std::string vTexCoord3Name = "vTexCoord3";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
renderer.shaderManager.PushShader(defaultShaderName);
|
||||
@ -334,7 +312,6 @@ namespace ZL {
|
||||
renderer.EnableVertexAttribArray("vTangent");
|
||||
renderer.EnableVertexAttribArray("vBinormal");
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
//renderer.EnableVertexAttribArray(vTexCoord3Name);
|
||||
|
||||
float dist = planetData.distanceToPlanetSurface(Environment::shipPosition);
|
||||
auto zRange = planetData.calculateZRange(dist);
|
||||
@ -351,36 +328,12 @@ namespace ZL {
|
||||
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
|
||||
renderer.RotateMatrix(Environment::inverseShipMatrix);
|
||||
|
||||
//renderer.RotateMatrix(QuatToMatrix(QuatFromRotateAroundX(M_PI / 4.0)));
|
||||
//renderer.RotateMatrix(QuatToMatrix(QuatFromRotateAroundY(-M_PI / 4.0)));
|
||||
|
||||
//Environment::shipPosition.v[0] = -1130;
|
||||
//Environment::shipPosition.v[1] = 9693;
|
||||
//Environment::shipPosition.v[2] = -20500;
|
||||
|
||||
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]);
|
||||
|
||||
renderer.RenderUniform1f("uDistanceToPlanetSurface", dist);
|
||||
renderer.RenderUniform1f("uCurrentZFar", currentZFar);
|
||||
*/
|
||||
renderer.RenderUniform1i("Texture", 0);
|
||||
renderer.RenderUniform1i("BakedTexture", 1);
|
||||
|
||||
|
||||
Triangle tr = planetData.getLodLevel(planetData.getCurrentLodIndex()).triangles[0]; // Берем базовый треугольник
|
||||
@ -389,29 +342,23 @@ namespace ZL {
|
||||
// Позиция камеры (корабля) в мире
|
||||
renderer.RenderUniform3fv("uViewPos", &Environment::shipPosition.v[0]);
|
||||
|
||||
// Передаем матрицу вращения треугольника для перехода в Tangent Space
|
||||
renderer.RenderUniformMatrix3fv("uShipRotation", false, &Environment::inverseShipMatrix.m[0]);
|
||||
renderer.RenderUniformMatrix4fv("ModelViewMatrix", false, &viewMatrix.m[0]);
|
||||
|
||||
// Не забудьте масштаб эффекта (глубина камней)
|
||||
//renderer.RenderUniform1f("uHeightScale", 0.03f);
|
||||
//renderer.RenderUniform1f("uHeightScale", -0.01f);
|
||||
//renderer.RenderUniform1f("uHeightScale", 0.0f + x / 1000.f);
|
||||
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(vTexCoord3Name);
|
||||
//renderer.DisableVertexAttribArray(vTexCoord2Name);
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
renderer.DisableVertexAttribArray(vNormalName);
|
||||
renderer.DisableVertexAttribArray("vTangent");
|
||||
@ -426,13 +373,11 @@ namespace ZL {
|
||||
|
||||
void PlanetObject::drawStones(Renderer& renderer)
|
||||
{
|
||||
//static const std::string defaultShaderName = "defaultColor";
|
||||
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 vTexCoord3Name = "vTexCoord3";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
renderer.shaderManager.PushShader(defaultShaderName2);
|
||||
@ -441,7 +386,6 @@ namespace ZL {
|
||||
renderer.EnableVertexAttribArray(vColorName);
|
||||
renderer.EnableVertexAttribArray(vNormalName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
//renderer.EnableVertexAttribArray(vTexCoord3Name);
|
||||
|
||||
float dist = planetData.distanceToPlanetSurface(Environment::shipPosition);
|
||||
auto zRange = planetData.calculateZRange(dist);
|
||||
@ -459,44 +403,27 @@ 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]);
|
||||
|
||||
renderer.RenderUniform1f("uDistanceToPlanetSurface", dist);
|
||||
renderer.RenderUniform1f("uCurrentZFar", currentZFar);
|
||||
Vector3f color2 = { 1.0, 1.0, 1.0 };
|
||||
|
||||
renderer.RenderUniform3fv("uColor", &color2.v[0]);
|
||||
|
||||
renderer.RenderUniform3fv("uViewPos", &Environment::shipPosition.v[0]);
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK); // Отсекаем задние грани
|
||||
glCullFace(GL_BACK);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
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);
|
||||
|
||||
@ -47,9 +47,6 @@ namespace ZL {
|
||||
public:
|
||||
PlanetObject();
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
void init();
|
||||
void update(float deltaTimeMs);
|
||||
void bakeStoneTexture(Renderer& renderer);
|
||||
|
||||
32
Renderer.cpp
32
Renderer.cpp
@ -294,31 +294,6 @@ namespace ZL {
|
||||
glBufferData(GL_ARRAY_BUFFER, data.TexCoordData.size() * 8, &data.TexCoordData[0], GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
/*if (data.TexCoord2Data.size() > 0)
|
||||
{
|
||||
if (!texCoord2VBO)
|
||||
{
|
||||
texCoord2VBO = std::make_shared<VBOHolder>();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, texCoord2VBO->getBuffer());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, data.TexCoord2Data.size() * 8, &data.TexCoord2Data[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)
|
||||
@ -800,8 +775,6 @@ namespace ZL {
|
||||
static const std::string vBinormal("vBinormal");
|
||||
static const std::string vColor("vColor");
|
||||
static const std::string vTexCoord("vTexCoord");
|
||||
//static const std::string vTexCoord2("vTexCoord2");
|
||||
static const std::string vTexCoord3("vTexCoord3");
|
||||
static const std::string vPosition("vPosition");
|
||||
|
||||
//glBindVertexArray(VertexRenderStruct.vao->getBuffer());
|
||||
@ -832,11 +805,6 @@ 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());
|
||||
VertexAttribPointer3fv(vTexCoord3, 0, NULL);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.positionVBO->getBuffer());
|
||||
VertexAttribPointer3fv(vPosition, 0, NULL);
|
||||
|
||||
@ -44,8 +44,6 @@ namespace ZL {
|
||||
{
|
||||
std::vector<Vector3f> PositionData;
|
||||
std::vector<Vector2f> TexCoordData;
|
||||
//std::vector<Vector2f> TexCoord2Data;
|
||||
std::vector<Vector3f> TexCoord3Data;
|
||||
std::vector<Vector3f> NormalData;
|
||||
std::vector<Vector3f> TangentData;
|
||||
std::vector<Vector3f> BinormalData;
|
||||
@ -65,8 +63,6 @@ namespace ZL {
|
||||
std::shared_ptr<VAOHolder> vao;
|
||||
std::shared_ptr<VBOHolder> positionVBO;
|
||||
std::shared_ptr<VBOHolder> texCoordVBO;
|
||||
//std::shared_ptr<VBOHolder> texCoord2VBO;
|
||||
std::shared_ptr<VBOHolder> texCoord3VBO;
|
||||
std::shared_ptr<VBOHolder> normalVBO;
|
||||
std::shared_ptr<VBOHolder> tangentVBO;
|
||||
std::shared_ptr<VBOHolder> binormalVBO;
|
||||
|
||||
@ -6,7 +6,6 @@ attribute vec3 vBinormal;
|
||||
|
||||
varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent;
|
||||
//varying float realDist;
|
||||
varying vec3 worldPosition;
|
||||
|
||||
uniform mat4 ProjectionModelViewMatrix;
|
||||
|
||||
@ -2,10 +2,10 @@ varying vec2 TexCoord;
|
||||
varying vec3 vViewDirTangent;
|
||||
|
||||
uniform sampler2D Texture;
|
||||
uniform sampler2D BakedTexture;
|
||||
uniform float uHeightScale;
|
||||
uniform float uDistanceToPlanetSurface;
|
||||
uniform float uCurrentZFar;
|
||||
//varying float realDist;
|
||||
varying vec3 worldPosition;
|
||||
|
||||
uniform vec3 uViewPos;
|
||||
@ -26,7 +26,13 @@ void main() {
|
||||
|
||||
float realDist = distance(worldPosition, uViewPos);
|
||||
|
||||
vec4 finalColor = texture2D(Texture, finalTexCoord);
|
||||
|
||||
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)
|
||||
@ -61,30 +67,4 @@ void main() {
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, 1.0), FOG_COLOR, fogFactor);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
if (realDist < 500)
|
||||
{
|
||||
gl_FragColor = vec4(0.5, 0.0, 0.0, 1.0);
|
||||
}
|
||||
else if (realDist < 1000)
|
||||
{
|
||||
gl_FragColor = vec4(1.0, 0.2, 0.0, 1.0);
|
||||
}
|
||||
else if (realDist < 1800)
|
||||
{
|
||||
gl_FragColor = vec4(1.0, 0.7, 0.0, 1.0);
|
||||
}
|
||||
else if (realDist < 2400)
|
||||
{
|
||||
gl_FragColor = vec4(1.0, 0.7, 0.5, 1.0);
|
||||
}
|
||||
else if (realDist < 2700)
|
||||
{
|
||||
gl_FragColor = vec4(1.0, 0.7, 1.0, 1.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}*/
|
||||
}
|
||||
|
||||
@ -6,10 +6,7 @@ varying vec3 pos;
|
||||
|
||||
uniform sampler2D Texture;
|
||||
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); // Синий туман
|
||||
|
||||
varying vec3 worldPosition;
|
||||
@ -24,6 +21,8 @@ void main()
|
||||
|
||||
float realDist = distance(worldPosition, uViewPos);
|
||||
|
||||
float alphaFactor = clamp((2000 - realDist) / 500.0, 0.0, 1.0);
|
||||
|
||||
float fogFactor;
|
||||
if (uDistanceToPlanetSurface > 1000)
|
||||
{
|
||||
@ -35,7 +34,7 @@ void main()
|
||||
|
||||
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));
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, alphaFactor), FOG_COLOR, fogFactor*(1.0 - t));
|
||||
}
|
||||
else if (uDistanceToPlanetSurface > 40)
|
||||
{
|
||||
@ -48,13 +47,13 @@ void main()
|
||||
|
||||
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)));
|
||||
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, 1.0), FOG_COLOR, fogFactor);
|
||||
gl_FragColor = mix(vec4(finalColor.rgb, alphaFactor), FOG_COLOR, fogFactor);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user