persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
RenderManager.cpp
Go to the documentation of this file.
1#include "RenderManager.hpp"
2#include "core/logger.hpp"
3#include <nds.h>
4#include <string>
5
6static constexpr int32_t ONE_Q12 = 1 << 12;
7
18static int textureSizeEnum(int size)
19{
20 switch (size)
21 {
22 case 8:
23 return TEXTURE_SIZE_8;
24 case 16:
25 return TEXTURE_SIZE_16;
26 case 32:
27 return TEXTURE_SIZE_32;
28 case 64:
29 return TEXTURE_SIZE_64;
30 case 128:
31 return TEXTURE_SIZE_128;
32 case 256:
33 return TEXTURE_SIZE_256;
34 case 512:
35 return TEXTURE_SIZE_512;
36 case 1024:
37 return TEXTURE_SIZE_1024;
38 default:
39 logMessage(LogSeverity::WARNING, "Invalid texture size: " + std::to_string(size));
40 return TEXTURE_SIZE_8;
41 }
42}
43
45{
46 glInit();
47
48 glEnable(config.settings);
49 glClearColor(config.clearColor.red, config.clearColor.green, config.clearColor.blue, config.clearColor.alpha);
50 glClearPolyID(config.clearPolyID);
51 glClearDepth(config.clearDepth);
52
53 glViewport(0, 0, 255, 191);
54 glMatrixMode(GL_PROJECTION);
55 glLoadIdentity();
56 gluPerspectivef32((int)(config.fov * DEGREE_MODIFIER),
57 config.aspect.raw_value(),
58 config.nearPlane.raw_value(),
59 floattof32(config.farPlane));
60
61 if (config.outlineColor != -1)
62 {
63 glSetOutlineColor(0, config.outlineColor);
64 }
65
66 if (config.fogColor.red != -1)
67 {
68 glFogColor(config.fogColor.red, config.fogColor.green, config.fogColor.blue, config.fogColor.alpha);
69
70 // How much depth difference there is between table entries
71 glFogShift(config.shift);
72 // Depth at which the fog starts
73 glFogOffset(config.depth);
74
75 // generate linear density table
76 uint8_t density = 0;
77 for (uint8_t i = 0; i < 32; ++i) // it has 32 steps
78 {
79 glFogDensity(i, density);
80 // exponentially increase mass the furthur back the fog is
81 density += (config.mass * i) >> 2;
82
83 // entries are 7 bit, so cap the density to 127
84 if (density > 127)
85 {
86 density = 127;
87 }
88 }
89 }
90
91 glPolyFmt(config.polyParams);
92}
93
95{
96 activeTexture = -1;
97 glClearColor(0, 0, 0, 31);
98 glClearDepth(0x7FFF);
99 glFlush(0);
100}
101
103 const GL_TEXTURE_TYPE_ENUM texType,
104 const int sizeX,
105 const int sizeY,
106 int param,
107 const void* texture)
108{
109 if (texture == nullptr)
110 {
111 logMessage(LogSeverity::WARNING, "Called with null texture data");
112 return false;
113 }
114
115 if (textureID != -1 && textureID != 0)
116 {
117 glDeleteTextures(1, &textureID);
118 textureID = -1;
119 }
120
121 glGenTextures(1, &textureID);
122 glBindTexture(GL_TEXTURE_2D, textureID);
123
125 int res =
126 glTexImage2D(GL_TEXTURE_2D, 0, texType, textureSizeEnum(sizeX), textureSizeEnum(sizeY), 0, param, texture);
127 if (!res)
128 {
129 logMessage(LogSeverity::ERROR, "glTexImage2D failed to upload texture");
130 return false;
131 }
132 return true;
133}
134
136{
137 for (const Node& node : model.nodes)
138 {
139 for (const SubList_N& sl : node.subLists)
140 {
141 // Guard against texture ID's that are out of bounds or corrupted. Use no texture in that case.
142 const int textureID = sl.texSlot >= 0 && static_cast<size_t>(sl.texSlot) < model.textures.size()
143 ? model.textures[sl.texSlot].textureID
144 : -1;
145 renderDisplayList(sl.displayList, textureID);
146 }
147 }
148}
149
151 ae::q20_12_t posX,
152 ae::q20_12_t posY,
153 ae::q20_12_t posZ,
154 uint32_t rotX,
155 uint32_t rotY,
156 uint32_t rotZ,
157 ae::q20_12_t scale)
158{
159 glPushMatrix();
160 if (posX.raw_value() || posY.raw_value() || posZ.raw_value())
161 {
162 glTranslatef32(posX.raw_value(), posY.raw_value(), posZ.raw_value());
163 }
164 if (rotX)
165 {
166 glRotatef32(rotX, 1, 0, 0);
167 }
168 if (rotY)
169 {
170 glRotatef32(rotY, 0, 1, 0);
171 }
172 if (rotZ)
173 {
174 glRotatef32(rotZ, 0, 0, 1);
175 }
176 if (scale.raw_value() != ONE_Q12)
177 {
178 glScalef32(scale.raw_value(), scale.raw_value(), scale.raw_value());
179 }
180
181 for (const Node& node : model.nodes)
182 {
183 for (const SubList_N& sl : node.subLists)
184 {
185 // Guard against texture ID's that are out of bounds or corrupted. Use no texture in that case.
186 const int textureID = sl.texSlot >= 0 && static_cast<size_t>(sl.texSlot) < model.textures.size()
187 ? model.textures[sl.texSlot].textureID
188 : -1;
189 renderDisplayList(sl.displayList, textureID);
190 }
191 }
192
193 glPopMatrix(1);
194}
195
197 BillboardData bb, const int texture, bool faceCamera, ae::q20_12_t camX, ae::q20_12_t camY, ae::q20_12_t camZ)
198{
199 if (texture != -1 && texture != activeTexture)
200 {
201 glBindTexture(GL_TEXTURE_2D, texture);
202 activeTexture = texture;
203 }
204 glBegin(GL_QUADS);
205
206 ae::q4_12_t rX{1}, rY{0}, rZ{0};
207 ae::q4_12_t uX{0}, uY{1}, uZ{0};
208
209 if (faceCamera)
210 {
211 //cast to 32 bit type for conversion first
212 ae::q20_12_t bx = ae::q20_12_t::from_raw_value(static_cast<int32_t>(bb.x.raw_value()));
213 ae::q20_12_t bz = ae::q20_12_t::from_raw_value(static_cast<int32_t>(bb.z.raw_value()));
214
215 ae::q20_12_t dx = camX - bx;
216 ae::q20_12_t dz = camZ - bz;
217
218 // Offset to align model pivot with NDS camera origin
219 ae::q20_12_t dist = math.length(dx, dz, ae::q20_12_t{0});
220
221 if (dist > ae::q20_12_t{0.001})
222 {
223 dx = math.div(dx, dist);
224 dz = math.div(dz, dist);
225 }
226
227 // narrow type
228 rX = ae::q4_12_t::from_raw_value(static_cast<int16_t>(dz.raw_value()));
229 rZ = ae::q4_12_t::from_raw_value(static_cast<int16_t>((-dx).raw_value()));
230 }
231
232 ae::q4_12_t rx = rX * bb.halfWidth;
233 ae::q4_12_t ry = rY * bb.halfWidth;
234 ae::q4_12_t rz = rZ * bb.halfWidth;
235
236 ae::q4_12_t ux = uX * bb.halfHeight;
237 ae::q4_12_t uy = uY * bb.halfHeight;
238 ae::q4_12_t uz = uZ * bb.halfHeight;
239
240 glTexCoord2t16(bb.u0.raw_value(), bb.v1.raw_value());
241 glVertex3v16((bb.x - rx - ux).raw_value(), (bb.y - ry - uy).raw_value(), (bb.z - rz - uz).raw_value());
242
243 glTexCoord2t16(bb.u1.raw_value(), bb.v1.raw_value());
244 glVertex3v16((bb.x + rx - ux).raw_value(), (bb.y + ry - uy).raw_value(), (bb.z + rz - uz).raw_value());
245
246 glTexCoord2t16(bb.u1.raw_value(), bb.v0.raw_value());
247 glVertex3v16((bb.x + rx + ux).raw_value(), (bb.y + ry + uy).raw_value(), (bb.z + rz + uz).raw_value());
248
249 glTexCoord2t16(bb.u0.raw_value(), bb.v0.raw_value());
250 glVertex3v16((bb.x - rx + ux).raw_value(), (bb.y - ry + uy).raw_value(), (bb.z - rz + uz).raw_value());
251
252 glEnd();
253}
254
255void RenderManager::renderDisplayList(const void* displayList, const int texture)
256{
257 if (!displayList)
258 {
259 return;
260 }
261
262 if (texture != -1 && texture != activeTexture)
263 {
264 glBindTexture(GL_TEXTURE_2D, texture);
265 activeTexture = texture;
266 }
267
268 glCallList(displayList);
269}
270
272{
273 if (texture != -1)
274 {
275 glDeleteTextures(1, &texture);
276 texture = -1;
277 }
278}
static constexpr int32_t ONE_Q12
static int textureSizeEnum(int size)
Converts a raw texture dimension in pixels to the corresponding libnds TEXTURE_SIZE_* enum value.
Manager for hardware specific rendering functionse.
void cleanup3DView()
Cleans up the 3D view, resetting the state.
void renderTexturedBillboard(BillboardData bb, const int texture, bool faceCamera, ae::q20_12_t camX, ae::q20_12_t camY, ae::q20_12_t camZ)
Renders a textured billboard.
bool uploadTexture(int &textureID, const GL_TEXTURE_TYPE_ENUM texType, const int sizeX, const int sizeY, int param, const void *bitmap)
Uploads a texture to the GPU.
void initialize3DView(View3DConfig config)
Initialize a new 3D view.
void renderDisplayList(const void *displayList, const int texture)
Renders a textured model.
void renderModelComponent(MDL3Model &model)
Renders a MDL3 model.
void deleteTexture(int &texture)
Deletes a texture from the GPU and resets its ID.
static const ae::q20_12_t DEGREE_MODIFIER
MathManager & math
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.
ae::q4_12_t halfHeight
ae::q4_12_t halfWidth
Represents a 3D model with nodes, textures, and animations.
etl::vector< MDL3Texture, 8 > textures
etl::vector< Node, 64 > nodes
Represents a node in the 3D model.
etl::vector< SubList_N, 64 > subLists
int8_t green
int8_t blue
int8_t red
int8_t alpha
Represents a sublist of a node in the 3D model.
const uint32_t * displayList
int32_t texSlot
Data structure to store 3D view configuration parameters.
ae::q20_12_t aspect
uint8_t mass
How thick (translucent) the fog is.
rgb outlineColor
Outline color in RGB15 format. If -1, outline will not be enabled.
int settings
This is a bitmask of various settings, which will be passed to glEnable().
int farPlane
Defines how far the camera can see.
ae::q20_12_t nearPlane
Defines how close the cmaera can see.
RGBA fogColor
RGBA values for the foh color. If the r value is -1, fog will not be enabled.
uint32_t polyParams
This is a bitmask of various polygon parameters, which will be passed to glPolyFmt().
uint16_t depth
How far the fog is (0x0000 to 0x8000).