group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
GlowSphere.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "ModelLoader.hpp"
7
8#include <cmath>
9#include <glm/glm.hpp>
10#include <numbers>
11
26inline LoadedModel
27createGlowSphere(int stacks = 32, int slices = 32, float radius = 1.0f, glm::vec3 emissiveColor = {10.0f, 6.0f, 2.0f})
28{
29 LoadedModel model;
30 MeshData mesh;
31
32 // ── Vertices ──
33 // UV sphere: stacks (latitude) × slices (longitude).
34 mesh.vertices.reserve(static_cast<size_t>((stacks + 1) * (slices + 1)));
35
36 for (int i = 0; i <= stacks; ++i) {
37 const float phi = std::numbers::pi_v<float> * static_cast<float>(i) / static_cast<float>(stacks);
38 const float sinPhi = std::sin(phi);
39 const float cosPhi = std::cos(phi);
40
41 for (int j = 0; j <= slices; ++j) {
42 const float theta = 2.0f * std::numbers::pi_v<float> * static_cast<float>(j) / static_cast<float>(slices);
43 const float sinTheta = std::sin(theta);
44 const float cosTheta = std::cos(theta);
45
46 const glm::vec3 normal{sinPhi * cosTheta, cosPhi, sinPhi * sinTheta};
47 const glm::vec3 position = normal * radius;
48 const glm::vec2 uv{static_cast<float>(j) / static_cast<float>(slices),
49 static_cast<float>(i) / static_cast<float>(stacks)};
50
51 // Tangent = ∂P/∂θ normalised (longitude direction).
52 // At the poles sinPhi ≈ 0, so the tangent degenerates — pick an
53 // arbitrary perpendicular instead.
54 glm::vec3 tangent;
55 if (sinPhi > 1e-5f)
56 tangent = glm::normalize(glm::vec3{-sinTheta, 0.0f, cosTheta});
57 else
58 tangent = glm::vec3{1.0f, 0.0f, 0.0f};
59
60 mesh.vertices.push_back(ModelVertex{
61 .position = position,
62 .normal = normal,
63 .texCoord = uv,
64 .tangent = glm::vec4(tangent, 1.0f),
65 });
66 }
67 }
68
69 // ── Indices ──
70 // Two triangles per quad, winding CCW from outside.
71 mesh.indices.reserve(static_cast<size_t>(stacks * slices * 6));
72
73 for (int i = 0; i < stacks; ++i) {
74 for (int j = 0; j < slices; ++j) {
75 const uint32_t a = static_cast<uint32_t>(i * (slices + 1) + j);
76 const uint32_t b = a + static_cast<uint32_t>(slices + 1);
77
78 mesh.indices.push_back(a);
79 mesh.indices.push_back(b);
80 mesh.indices.push_back(a + 1);
81
82 mesh.indices.push_back(a + 1);
83 mesh.indices.push_back(b);
84 mesh.indices.push_back(b + 1);
85 }
86 }
87
88 // ── Material ──
89 // Bright emissive (HDR values) triggers bloom. Low roughness gives a
90 // smooth, slightly specular surface so it picks up some environment
91 // reflection on top of the glow.
92 mesh.material.baseColorFactor = {1.0f, 1.0f, 1.0f, 1.0f};
93 mesh.material.emissiveFactor = glm::vec4(emissiveColor, 0.0f);
94 mesh.material.metallicFactor = 0.0f;
95 mesh.material.roughnessFactor = 0.3f;
96 mesh.material.aoStrength = 1.0f;
97 mesh.material.normalScale = 1.0f;
99
100 // 1×1 white emissive texture — the PBR shader multiplies emissiveFactor
101 // by texEmissive, which falls back to the black fallback when no texture
102 // is present. Including a white pixel ensures the factor survives.
103 TextureData whiteTex;
104 whiteTex.pixels = {255, 255, 255, 255};
105 whiteTex.width = 1;
106 whiteTex.height = 1;
107 whiteTex.isSRGB = true;
108
109 model.textures.push_back(std::move(whiteTex));
110 mesh.emissiveTexIndex = 0; // Points to the white texture above.
111
112 model.meshes.push_back(std::move(mesh));
113 return model;
114}
LoadedModel createGlowSphere(int stacks=32, int slices=32, float radius=1.0f, glm::vec3 emissiveColor={10.0f, 6.0f, 2.0f})
Generate a procedural UV sphere as a LoadedModel with a bright emissive material suitable for testing...
Definition GlowSphere.hpp:27
Assimp-based model loading and CPU-side mesh/texture data types.
Everything returned by loadModel().
Definition ModelLoader.hpp:76
std::vector< MeshData > meshes
Definition ModelLoader.hpp:77
std::vector< TextureData > textures
Definition ModelLoader.hpp:78
float aoStrength
Definition ModelLoader.hpp:45
float metallicFactor
Default dielectric (non-metal).
Definition ModelLoader.hpp:43
glm::vec4 emissiveFactor
rgb in xyz, w unused.
Definition ModelLoader.hpp:47
AlphaMode alphaMode
Definition ModelLoader.hpp:48
glm::vec4 baseColorFactor
Definition ModelLoader.hpp:42
float roughnessFactor
Default mid-roughness.
Definition ModelLoader.hpp:44
float normalScale
Definition ModelLoader.hpp:46
CPU-side mesh data ready for GPU upload.
Definition ModelLoader.hpp:54
std::vector< uint32_t > indices
Definition ModelLoader.hpp:56
int emissiveTexIndex
Emissive texture.
Definition ModelLoader.hpp:61
std::vector< ModelVertex > vertices
Definition ModelLoader.hpp:55
MaterialData material
Scalar PBR factors.
Definition ModelLoader.hpp:62
One vertex in a loaded 3-D model (PBR-ready).
Definition ModelLoader.hpp:19
glm::vec3 position
Definition ModelLoader.hpp:20
RGBA pixel data for one texture (decoded from embedded PNG/JPEG).
Definition ModelLoader.hpp:67
int height
Definition ModelLoader.hpp:70
std::vector< uint8_t > pixels
Row-major RGBA, 4 bytes per pixel.
Definition ModelLoader.hpp:68
bool isSRGB
True for color textures (albedo, emissive); false for data (normal, MR).
Definition ModelLoader.hpp:71
int width
Definition ModelLoader.hpp:69