Repository navigation
Expand file tree
/
Copy pathPlanet.cpp
More file actions
138 lines (115 loc) · 3.64 KB
/
Copy pathPlanet.cpp
File metadata and controls
138 lines (115 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "Planet.h"
#include "Moon.h"
#include <GL/glew.h>
#include <cmath>
#include <limits>
#include <random>
Planet::Planet(const std::string &name, float radius, const std::string &texturePath,
float orbitRadius, float orbitSpeed, float rotationSpeed,
const std::vector<std::string> &facts)
: CelestialBody(name, radius, texturePath),
m_orbitRadius(orbitRadius), m_orbitSpeed(orbitSpeed),
m_orbitAngle(0.0f), m_time(0.0f), m_facts(facts)
{
m_rotationSpeed = rotationSpeed;
updateOrbitalPosition();
}
void Planet::update(float deltaTime)
{
m_time += deltaTime;
m_orbitAngle += m_orbitSpeed * deltaTime;
updateOrbitalPosition();
m_rotation.y = m_time * m_rotationSpeed;
for (auto &moon : m_moons)
moon->update(deltaTime);
}
void Planet::render(Shader &shader, unsigned int sphereVAO, int vertexCount)
{
if (m_texture)
{
glActiveTexture(GL_TEXTURE0);
m_texture->bind();
shader.setInt("texture1", 0);
}
// Selection highlight: slight scale up
float scaleBoost = m_selected ? 1.15f : 1.0f;
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, m_position);
model = glm::rotate(model, m_rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::scale(model, glm::vec3(m_radius * scaleBoost));
shader.setMat4("model", model);
glBindVertexArray(sphereVAO);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
for (auto &moon : m_moons)
moon->render(shader, sphereVAO, vertexCount);
}
glm::mat4 Planet::getModelMatrix() const
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, m_position);
model = glm::rotate(model, m_rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::scale(model, glm::vec3(m_radius));
return model;
}
void Planet::addMoon(std::shared_ptr<Moon> moon)
{
m_moons.push_back(moon);
}
void Planet::updateOrbitalPosition()
{
m_position.x = m_orbitRadius * cos(m_orbitAngle);
m_position.z = m_orbitRadius * sin(m_orbitAngle);
m_position.y = 0.0f;
}
std::string Planet::chooseRandomFact()
{
if (m_facts.empty())
{
static const std::string empty = "";
return empty;
}
static thread_local std::mt19937 rng{std::random_device{}()};
std::uniform_int_distribution<int> dist(0, (int)m_facts.size() - 1);
int idx = dist(rng);
if (m_facts.size() > 1 && idx == m_lastFactIndex)
{
// simple no-repeat: pick a neighbor
idx = (idx + 1) % (int)m_facts.size();
}
m_lastFactIndex = idx;
return m_facts[idx];
}
const std::string &Planet::currentFact() const
{
if (m_facts.empty())
{
static const std::string empty = "";
return empty;
}
if (m_lastFactIndex < 0 || m_lastFactIndex >= (int)m_facts.size())
return m_facts.front();
return m_facts[m_lastFactIndex];
}
// Ray–sphere intersection against this planet's bounding sphere
bool Planet::intersectsRay(const glm::vec3 &rayOrigin, const glm::vec3 &rayDir, float &tOut) const
{
glm::vec3 oc = rayOrigin - m_position;
float a = glm::dot(rayDir, rayDir); // should be 1 if dir normalized
float b = 2.0f * glm::dot(oc, rayDir);
float c = glm::dot(oc, oc) - (m_radius * m_radius);
float discriminant = b * b - 4 * a * c;
if (discriminant < 0.0f)
return false;
float sqrtD = sqrtf(discriminant);
float t1 = (-b - sqrtD) / (2.0f * a);
float t2 = (-b + sqrtD) / (2.0f * a);
float t = std::numeric_limits<float>::infinity();
if (t1 > 0.0f)
t = t1;
if (t2 > 0.0f && t2 < t)
t = t2;
if (std::isinf(t))
return false;
tOut = t;
return true;
}