ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Rasterizer.cpp
Go to the documentation of this file.
1#include "Rasterizer.h"
2
4
5RGBColor Rasterizer::CheckRasterPixelAccel(Triangle2D** triangles, int numTriangles, Vector2D pixelRay) {
6 float zBuffer = 3.402823466e+38f;
7 int triangle = 0;
8 bool didIntersect = false;
9 float u = 0.0f, v = 0.0f, w = 0.0f;
10 Vector3D uvw;
11 RGBColor color;
12
13 #ifndef _ARM_MATH_H
14 for (int t = 0; t < numTriangles; t++) {
15 if (triangles[t]->averageDepth < zBuffer) {
16 if (triangles[t]->DidIntersect(pixelRay.X, pixelRay.Y, u, v, w)) {
17 uvw.X = u;
18 uvw.Y = v;
19 uvw.Z = w;
20 zBuffer = triangles[t]->averageDepth;
21 triangle = t;
22 didIntersect = true;
23 }
24 }
25 }
26 #else
27 //subtract for all triangles
28 //float v2lX = x - p1X;
29 //float v2lY = y - p1Y;
30 float32_t subA[numTriangles * 2];
31 float32_t subB[numTriangles * 2];
32 float32_t subDst[numTriangles * 2];
33
34 for (int i = 0; i < numTriangles; i++){
35 subA[i * 2 + 0] = pixelRay.X;//x
36 subA[i * 2 + 1] = pixelRay.Y;//y
37
38 subB[i * 2 + 0] = triangles[i]->p1X;//p1x
39 subB[i * 2 + 1] = triangles[i]->p1Y;//p1y
40 }
41
42 arm_sub_f32(subA, subB, subDst, numTriangles * 2);
43
44 //v w multiplication
45 // v2lX * v1Y | v1X * v2lY
46 // v0X * v2lY | v2lX * v0Y
47 float32_t multA[numTriangles * 4];
48 float32_t multB[numTriangles * 4];
49 float32_t multDst[numTriangles * 4];
50
51 for (int i = 0; i < numTriangles; i++){
52 multA[i * 4 + 0] = subDst[i * 2 + 0];//v2lx, from first set
53 multA[i * 4 + 1] = triangles[i]->v1X;
54 multA[i * 4 + 2] = triangles[i]->v0X;
55 multA[i * 4 + 3] = subDst[i * 2 + 0];//v2lx, from first set
56
57 multB[i * 4 + 0] = triangles[i]->v1Y;
58 multB[i * 4 + 1] = subDst[i * 2 + 1];//v2ly, from first set
59 multB[i * 4 + 2] = subDst[i * 2 + 1];//v2ly, from first set
60 multB[i * 4 + 3] = triangles[i]->v0Y;
61 }
62
63 arm_mult_f32(multA, multB, multDst, numTriangles * 4);
64
65 //v w subtraction (mult - mult)
66 float32_t sub1A[numTriangles * 2];
67 float32_t sub1B[numTriangles * 2];
68 float32_t sub1Dst[numTriangles * 2];
69
70 for (int i = 0; i < numTriangles; i++){
71 sub1A[i * 2 + 0] = multDst[i * 4];//v2lX * v1Y
72 sub1A[i * 2 + 1] = multDst[i * 4 + 2];//v0X * v2lY
73
74 sub1B[i * 2 + 0] = multDst[i * 4 + 1];//v1X * v2lY
75 sub1B[i * 2 + 1] = multDst[i * 4 + 3];//v2lX * v0Y
76 }
77
78 arm_sub_f32(sub1A, sub1B, sub1Dst, numTriangles * 2);
79
80 //v w denominator multiplication
81 float32_t mult1A[numTriangles * 2];
82 float32_t mult1B[numTriangles * 2];
83 float32_t mult1Dst[numTriangles * 2];
84
85 for (int i = 0; i < numTriangles; i++){
86 mult1A[i * 2 + 0] = sub1Dst[i * 2 + 0];//(v2lX * v1Y - v1X * v2lY)
87 mult1A[i * 2 + 1] = sub1Dst[i * 2 + 1];//(v0X * v2lY - v2lX * v0Y)
88
89 mult1B[i * 2 + 0] = triangles[i]->denominator;
90 mult1B[i * 2 + 1] = triangles[i]->denominator;
91 }
92
93 arm_mult_f32(mult1A, mult1B, mult1Dst, numTriangles * 2);//provides v and w
94
95 for (int i = 0; i < numTriangles; i++){
96 v = mult1Dst[i * 2 + 0];
97 w = mult1Dst[i * 2 + 1];
98 u = 1.0f - v - w;
99
100 if (triangles[i]->averageDepth < zBuffer) {
101 bool intersect = !(v < 0.0f || w < 0.0f || v > 1.0f || w > 1.0f) && u > 0.0f;
102 if (intersect){
103 uvw.X = u;
104 uvw.Y = v;
105 uvw.Z = w;
106 zBuffer = triangles[i]->averageDepth;
107 triangle = i;
108 didIntersect = true;
109 }
110 }
111 }
112 #endif
113
114 if (didIntersect) {
115 Vector3D intersect = (*triangles[triangle]->t3p1 * uvw.X) + (*triangles[triangle]->t3p2 * uvw.Y) + (*triangles[triangle]->t3p3 * uvw.Z);
116 Vector2D uv;
117
118 if (triangles[triangle]->hasUV) uv = *triangles[triangle]->p1UV * uvw.X + *triangles[triangle]->p2UV * uvw.Y + *triangles[triangle]->p3UV * uvw.Z;
119
120 color = triangles[triangle]->GetMaterial()->GetRGB(intersect, *triangles[triangle]->normal, Vector3D(uv.X, uv.Y, 0.0f));
121 }
122
123 return color;
124}
125
126RGBColor Rasterizer::CheckRasterPixel(Triangle2D** triangles, int numTriangles, Vector2D pixelRay) {
127 float zBuffer = 3.402823466e+38f;
128 int triangle = 0;
129 bool didIntersect = false;
130 float u = 0.0f, v = 0.0f, w = 0.0f;
131 Vector3D uvw;
132 RGBColor color;
133
134 for (int t = 0; t < numTriangles; ++t) {
135 if (triangles[t]->averageDepth < zBuffer) {
136 if (triangles[t]->DidIntersect(pixelRay.X, pixelRay.Y, u, v, w)) {
137 uvw.X = u;
138 uvw.Y = v;
139 uvw.Z = w;
140 zBuffer = triangles[t]->averageDepth;
141 triangle = t;
142 didIntersect = true;
143 }
144 }
145 }
146
147 if (didIntersect) {
148 Vector3D intersect = (*triangles[triangle]->t3p1 * uvw.X) + (*triangles[triangle]->t3p2 * uvw.Y) + (*triangles[triangle]->t3p3 * uvw.Z);
149 Vector2D uv;
150
151 if (triangles[triangle]->hasUV) uv = *triangles[triangle]->p1UV * uvw.X + *triangles[triangle]->p2UV * uvw.Y + *triangles[triangle]->p3UV * uvw.Z;
152
153 color = triangles[triangle]->GetMaterial()->GetRGB(intersect, *triangles[triangle]->normal, Vector3D(uv.X, uv.Y, 0.0f));
154 }
155
156 return color;
157}
158
160 if (camera->Is2D()) {
161 for (unsigned int i = 0; i < camera->GetPixelGroup()->GetPixelCount(); i++) {
162 Vector2D pixelRay = camera->GetPixelGroup()->GetCoordinate(i);
163 Vector3D pixelRay3D = Vector3D(pixelRay.X, pixelRay.Y, 0) + camera->GetTransform()->GetPosition();
164
165 RGBColor color = scene->GetObjects()[0]->GetMaterial()->GetRGB(pixelRay3D, Vector3D(), Vector3D());
166
167 camera->GetPixelGroup()->GetColor(i)->R = color.R;
168 camera->GetPixelGroup()->GetColor(i)->G = color.G;
169 camera->GetPixelGroup()->GetColor(i)->B = color.B;
170 }
171 } else {
172 camera->GetTransform()->SetBaseRotation(camera->GetCameraLayout()->GetRotation());//Set camera layout
173
174 rayDirection = camera->GetTransform()->GetRotation().Multiply(camera->GetLookOffset());//Apply offset to camera if set
175
176 //Set quad tree space to local camera coordinates
177 Vector2D minCoord = camera->GetCameraMinCoordinate();
178 Vector2D maxCoord = camera->GetCameraMaxCoordinate();
179
180 QuadTree tree = QuadTree(minCoord, maxCoord);
181
182 uint16_t tCount = 1;
183
184 for (uint8_t i = 0; i < scene->GetObjectCount(); ++i) {
185 if (scene->GetObjects()[i]->IsEnabled()) {
186 tCount += scene->GetObjects()[i]->GetTriangleGroup()->GetTriangleCount();
187 }
188 }
189
190 Triangle2D triangles[tCount];
191 uint16_t iterCount = 0;
192
193 for (uint8_t i = 0; i < scene->GetObjectCount(); ++i) {
194 if (scene->GetObjects()[i]->IsEnabled()) {
195 for (uint16_t j = 0; j < scene->GetObjects()[i]->GetTriangleGroup()->GetTriangleCount(); ++j) {
196 //Create 2D triangle mapping for rasterize, stores 3D coordinates for mapping material to 3d global coordinate space
197 triangles[iterCount] = Triangle2D(camera->GetLookOffset(), camera->GetTransform(), &scene->GetObjects()[i]->GetTriangleGroup()->GetTriangles()[j], scene->GetObjects()[i]->GetMaterial());
198
199 tree.Insert(&triangles[iterCount]);
200 iterCount++;
201 }
202 }
203 }
204
205 tree.Rebuild();
206
207 Vector2D scale, pixelRay, materialRay;
208 for (uint16_t i = 0; i < camera->GetPixelGroup()->GetPixelCount(); ++i) {
209 //Render camera in local camera space
210 pixelRay = camera->GetPixelGroup()->GetCoordinate(i);
211
212 Node* leafNode = tree.Intersect(pixelRay);
213
214 if (!leafNode || leafNode->GetCount() == 0) {
215 camera->GetPixelGroup()->GetColor(i)->R = 0;
216 camera->GetPixelGroup()->GetColor(i)->G = 0;
217 camera->GetPixelGroup()->GetColor(i)->B = 0;
218 continue;
219 }
220
221 //Render individual pixel, transformed to camera coordinate space
222 RGBColor color = CheckRasterPixel(leafNode->GetEntities(), leafNode->GetCount(), pixelRay);
223
224 camera->GetPixelGroup()->GetColor(i)->R = color.R;
225 camera->GetPixelGroup()->GetColor(i)->G = color.G;
226 camera->GetPixelGroup()->GetColor(i)->B = color.B;
227 }
228 }
229}
Provides functionality for rasterizing 3D scenes into 2D camera views.
Base class for managing camera properties and transformations.
Definition CameraBase.h:26
virtual IPixelGroup * GetPixelGroup()=0
Retrieves the associated pixel group.
Quaternion GetLookOffset()
Retrieves the camera's look offset.
virtual Vector2D GetCameraMinCoordinate()=0
Retrieves the minimum coordinate of the camera in 2D space.
virtual Vector2D GetCameraMaxCoordinate()=0
Retrieves the maximum coordinate of the camera in 2D space.
bool Is2D()
Checks if the camera operates in 2D mode.
CameraLayout * GetCameraLayout()
Retrieves the camera's layout.
Definition CameraBase.cpp:5
Transform * GetTransform()
Retrieves the camera's transformation data.
Definition CameraBase.cpp:9
Quaternion GetRotation()
Retrieves the camera's rotation.
virtual Vector2D GetCoordinate(uint16_t count)=0
Retrieves the coordinate of a specific pixel.
virtual RGBColor * GetColor(uint16_t count)=0
Retrieves the color of a specific pixel.
virtual Triangle3D * GetTriangles()=0
Retrieves the array of Triangle3D objects representing the triangles.
virtual int GetTriangleCount()=0
Retrieves the total number of triangles in the group.
virtual RGBColor GetRGB(const Vector3D &position, const Vector3D &normal, const Vector3D &uvw)=0
Pure virtual function to calculate color based on surface parameters.
Represents a node in a quadtree structure for spatial partitioning.
Definition Node.h:23
Triangle2D ** GetEntities()
Retrieves the entities contained in this node.
Definition Node.cpp:22
uint16_t GetCount()
Retrieves the current count of entities in the node.
Definition Node.cpp:26
bool IsEnabled()
Checks if the object is enabled.
Definition Object3D.cpp:17
Material * GetMaterial()
Retrieves the material assigned to the object.
Definition Object3D.cpp:76
ITriangleGroup * GetTriangleGroup()
Retrieves the modifiable geometry of the object.
Definition Object3D.cpp:72
Represents a quadtree for spatial partitioning of 2D entities.
Definition QuadTree.h:22
Node * Intersect(Node *node, const Vector2D &p)
Recursively finds the node intersecting with a given point.
Definition QuadTree.cpp:15
bool Insert(Triangle2D *triangle)
Inserts a triangle entity into the quadtree.
Definition QuadTree.cpp:7
void Rebuild()
Rebuilds the quadtree, recalculating all spatial partitions.
Definition QuadTree.cpp:29
A mathematical construct representing a rotation in 3D space.
Definition Quaternion.h:30
Quaternion Multiply(const Quaternion &quaternion) const
Multiplies (composes) this quaternion with another (order matters).
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
uint8_t B
Blue component of the color (0-255).
Definition RGBColor.h:27
uint8_t G
Green component of the color (0-255).
Definition RGBColor.h:26
uint8_t R
Red component of the color (0-255).
Definition RGBColor.h:25
static void Rasterize(Scene *scene, CameraBase *camera)
Rasterizes a 3D scene onto a 2D camera view.
static RGBColor CheckRasterPixel(Triangle2D **triangles, int numTriangles, Vector2D pixelRay)
Determines the color of a pixel by checking which triangle it intersects.
static RGBColor CheckRasterPixelAccel(Triangle2D **triangles, int numTriangles, Vector2D pixelRay)
Optimized version of CheckRasterPixel using acceleration structures.
Definition Rasterizer.cpp:5
static Quaternion rayDirection
Direction of the ray cast for rasterization.
Definition Rasterizer.h:29
Manages a collection of 3D objects and applies optional screen-space effects.
Definition Scene.h:25
uint8_t GetObjectCount()
Retrieves the current number of objects in the scene.
Definition Scene.cpp:62
Object3D ** GetObjects()
Retrieves all objects in the scene.
Definition Scene.cpp:58
Quaternion GetRotation()
Gets the current rotation of the object.
Definition Transform.cpp:61
void SetBaseRotation(const Quaternion &baseRotation)
Sets the base rotation of the object.
Definition Transform.cpp:45
Vector3D GetPosition()
Gets the position of the object.
Definition Transform.cpp:69
Represents a 2D triangle with support for UV mapping, depth, and intersection testing.
Definition Triangle2D.h:25
float p1X
Definition Triangle2D.h:28
float v0X
Definition Triangle2D.h:29
Material * GetMaterial()
Gets the material assigned to the triangle.
const Vector2D * p2UV
UV coordinates of the second vertex.
Definition Triangle2D.h:40
Vector3D * t3p1
Pointer to the first vertex in 3D space.
Definition Triangle2D.h:35
float v0Y
Definition Triangle2D.h:29
float averageDepth
Average depth of the triangle for rendering.
Definition Triangle2D.h:44
float denominator
Precomputed denominator for barycentric coordinate calculations.
Definition Triangle2D.h:27
Vector3D * t3p3
Pointer to the third vertex in 3D space.
Definition Triangle2D.h:37
float v1X
Definition Triangle2D.h:29
const Vector2D * p3UV
UV coordinates of the third vertex.
Definition Triangle2D.h:41
float v1Y
Definition Triangle2D.h:29
const Vector2D * p1UV
UV coordinates of the first vertex.
Definition Triangle2D.h:39
float p1Y
Definition Triangle2D.h:28
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
float X
The X-component of the 2D vector.
Definition Vector2D.h:29
float Y
The Y-component of the 2D vector.
Definition Vector2D.h:30
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
float Z
The Z-component of the 3D vector.
Definition Vector3D.h:30
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29