Skip to content

Commit bdc4117

Browse files
committed
Refactor 3D rendering code to use SDL_FPoint and SDL_Vertex for improved geometry handling and remove unused structures
1 parent 374b1e9 commit bdc4117

1 file changed

Lines changed: 38 additions & 77 deletions

File tree

  • test/dreamcast/3d

test/dreamcast/3d/3d.c

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ typedef struct {
1111
float x, y, z;
1212
} Vec3;
1313

14-
// 2D point for screen projection
15-
typedef struct {
16-
int x, y;
17-
} Point2D;
18-
1914
// Simple 3D cube vertices
2015
Vec3 cube_vertices[8] = {
2116
{-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, // Back face
@@ -39,24 +34,24 @@ int cube_faces[6][4] = {
3934
{1, 5, 6, 2} // Right
4035
};
4136

42-
// Face colors
43-
Uint32 face_colors[6] = {
44-
0xFF0000FF, // Red
45-
0xFF00FF00, // Green
46-
0xFFFF0000, // Blue
47-
0xFFFFFF00, // Cyan
48-
0xFFFF00FF, // Magenta
49-
0xFFFFFFFF // White
37+
// Face colors (using SDL_FColor for SDL3)
38+
SDL_FColor face_colors[6] = {
39+
{1.0f, 0.0f, 0.0f, 1.0f}, // Red
40+
{0.0f, 1.0f, 0.0f, 1.0f}, // Green
41+
{0.0f, 0.0f, 1.0f, 1.0f}, // Blue
42+
{0.0f, 1.0f, 1.0f, 1.0f}, // Cyan
43+
{1.0f, 0.0f, 1.0f, 1.0f}, // Magenta
44+
{1.0f, 1.0f, 1.0f, 1.0f} // White
5045
};
5146

52-
// Project 3D point to 2D screen coordinates
53-
Point2D project_3d_to_2d(Vec3 point, float distance) {
54-
Point2D result;
47+
// Project 3D point to 2D screen coordinates using SDL_FPoint
48+
SDL_FPoint project_3d_to_2d(Vec3 point, float distance) {
49+
SDL_FPoint result;
5550
float projected_x = (point.x * distance) / (point.z + distance);
5651
float projected_y = (point.y * distance) / (point.z + distance);
5752

58-
result.x = (int)(projected_x * 200 + SCREEN_WIDTH / 2);
59-
result.y = (int)(projected_y * 200 + SCREEN_HEIGHT / 2);
53+
result.x = projected_x * 200.0f + SCREEN_WIDTH / 2.0f;
54+
result.y = projected_y * 200.0f + SCREEN_HEIGHT / 2.0f;
6055

6156
return result;
6257
}
@@ -100,45 +95,14 @@ Vec3 calculate_face_normal(Vec3 v0, Vec3 v1, Vec3 v2) {
10095
return normal;
10196
}
10297

103-
// Draw a filled triangle using SDL3
104-
void draw_filled_triangle(SDL_Renderer* renderer, Point2D p1, Point2D p2, Point2D p3) {
105-
// Simple scanline triangle filling
106-
// Sort points by Y coordinate
107-
Point2D points[3] = {p1, p2, p3};
108-
109-
// Bubble sort by y coordinate
110-
for (int i = 0; i < 2; i++) {
111-
for (int j = 0; j < 2 - i; j++) {
112-
if (points[j].y > points[j + 1].y) {
113-
Point2D temp = points[j];
114-
points[j] = points[j + 1];
115-
points[j + 1] = temp;
116-
}
117-
}
118-
}
119-
120-
// Draw horizontal lines to fill the triangle
121-
for (int y = points[0].y; y <= points[2].y; y++) {
122-
int x_start = SCREEN_WIDTH, x_end = 0;
123-
124-
// Find intersection points with triangle edges
125-
for (int i = 0; i < 3; i++) {
126-
int j = (i + 1) % 3;
127-
Point2D p_i = points[i], p_j = points[j];
128-
129-
if ((p_i.y <= y && y < p_j.y) || (p_j.y <= y && y < p_i.y)) {
130-
if (p_j.y != p_i.y) {
131-
int x = p_i.x + (y - p_i.y) * (p_j.x - p_i.x) / (p_j.y - p_i.y);
132-
if (x < x_start) x_start = x;
133-
if (x > x_end) x_end = x;
134-
}
135-
}
136-
}
137-
138-
if (x_start <= x_end) {
139-
SDL_RenderLine(renderer, x_start, y, x_end, y);
140-
}
141-
}
98+
// Create SDL_Vertex from screen position and color
99+
SDL_Vertex create_vertex(SDL_FPoint pos, SDL_FColor color) {
100+
SDL_Vertex vertex;
101+
vertex.position = pos;
102+
vertex.color = color;
103+
vertex.tex_coord.x = 0.0f; // No texture
104+
vertex.tex_coord.y = 0.0f;
105+
return vertex;
142106
}
143107

144108
int main(int argc, char* argv[]) {
@@ -150,7 +114,7 @@ int main(int argc, char* argv[]) {
150114

151115
// Create window
152116
SDL_Window* window = SDL_CreateWindow(
153-
"SDL3 3D Demo",
117+
"SDL3 3D Demo - Using Built-in Geometry",
154118
SCREEN_WIDTH, SCREEN_HEIGHT,
155119
SDL_WINDOW_RESIZABLE
156120
);
@@ -213,7 +177,7 @@ int main(int argc, char* argv[]) {
213177
SDL_RenderClear(renderer);
214178

215179
// Transform and project vertices
216-
Point2D projected_vertices[8];
180+
SDL_FPoint projected_vertices[8];
217181
Vec3 transformed_vertices[8];
218182

219183
for (int i = 0; i < 8; i++) {
@@ -231,7 +195,7 @@ int main(int argc, char* argv[]) {
231195
}
232196

233197
if (render_mode == 0) {
234-
// Wireframe rendering
198+
// Wireframe rendering using individual SDL_RenderLine calls
235199
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
236200

237201
for (int i = 0; i < 12; i++) {
@@ -243,7 +207,7 @@ int main(int argc, char* argv[]) {
243207
projected_vertices[v2].x, projected_vertices[v2].y);
244208
}
245209
} else {
246-
// Filled rendering with backface culling
210+
// Filled rendering using SDL_RenderGeometry with backface culling
247211
for (int face = 0; face < 6; face++) {
248212
int v0 = cube_faces[face][0];
249213
int v1 = cube_faces[face][1];
@@ -259,24 +223,21 @@ int main(int argc, char* argv[]) {
259223

260224
// Simple backface culling (if normal.z > 0, face is facing camera)
261225
if (normal.z > 0) {
262-
// Set face color
263-
Uint32 color = face_colors[face];
264-
SDL_SetRenderDrawColor(renderer,
265-
(color >> 16) & 0xFF, // R
266-
(color >> 8) & 0xFF, // G
267-
color & 0xFF, // B
268-
255); // A
226+
// Create vertices for two triangles (quad = 2 triangles)
227+
SDL_Vertex triangle_vertices[6];
228+
229+
// First triangle: v0, v1, v2
230+
triangle_vertices[0] = create_vertex(projected_vertices[v0], face_colors[face]);
231+
triangle_vertices[1] = create_vertex(projected_vertices[v1], face_colors[face]);
232+
triangle_vertices[2] = create_vertex(projected_vertices[v2], face_colors[face]);
269233

270-
// Draw two triangles to make a quad
271-
draw_filled_triangle(renderer,
272-
projected_vertices[v0],
273-
projected_vertices[v1],
274-
projected_vertices[v2]);
234+
// Second triangle: v0, v2, v3
235+
triangle_vertices[3] = create_vertex(projected_vertices[v0], face_colors[face]);
236+
triangle_vertices[4] = create_vertex(projected_vertices[v2], face_colors[face]);
237+
triangle_vertices[5] = create_vertex(projected_vertices[v3], face_colors[face]);
275238

276-
draw_filled_triangle(renderer,
277-
projected_vertices[v0],
278-
projected_vertices[v2],
279-
projected_vertices[v3]);
239+
// Render the triangles using SDL3's geometry renderer
240+
SDL_RenderGeometry(renderer, NULL, triangle_vertices, 6, NULL, 0);
280241
}
281242
}
282243
}

0 commit comments

Comments
 (0)