-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRasterizer.cpp
More file actions
88 lines (70 loc) · 2.36 KB
/
Rasterizer.cpp
File metadata and controls
88 lines (70 loc) · 2.36 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
#include "Rasterizer.h"
#include "defs.h"
#include "Vector2.h"
#include "Vector3.h"
#include "Vector4.h"
#include "Matrix44.h"
#include <math.h>
#include <thread>
static inline bool isPointInTriangle(int ptx, int pty, const Vector2& v1,
const Vector2& v2, const Vector2& v3)
{
float wv1 = ((v2.y - v3.y) * (ptx - v3.x) +
(v3.x - v2.x) * (pty - v3.y)) /
((v2.y - v3.y) * (v1.x - v3.x) +
(v3.x - v2.x) * (v1.y - v3.y));
float wv2 = ((v3.y - v1.y) * (ptx - v3.x) +
(v1.x - v3.x) * (pty - v3.y)) /
((v2.y - v3.y) * (v1.x - v3.x) +
(v3.x - v2.x) * (v1.y - v3.y));
float wv3 = 1.0f - wv1 - wv2;
int one = (wv1 < -0.001);
int two = (wv2 < -0.001);
int three = (wv3 < -0.001);
//is the point in the triangle
return ((one == two) && (two == three));
}
void Rasterizer::presentFrame(){
if(renderCallback != nullptr){
std::thread renderThread(renderCallback);
pFrame->print();
renderThread.join();
}
}
void Rasterizer::initializeFramebuffer(int width, int height){
frameBuffers[0] = new Framebuffer(width, height);
frameBuffers[1] = new Framebuffer(width, height);
swapBuffers();
}
Rasterizer::Rasterizer(int width, int height)
:renderCallback(nullptr),
pFrame(nullptr),
rFrame(nullptr),
currentBuffer(0)
{
initializeFramebuffer(width, height);
}
Rasterizer::~Rasterizer(){
delete frameBuffers[0];
delete frameBuffers[1];
}
void Rasterizer::rasterizeTriangle(const Vector2& vv1, const Vector2& vv2, const Vector2& vv3){
Framebuffer* fb = rFrame;
int h_width = fb->getWidth() / 2, h_height = fb->getHeight() / 2;
int minx, maxx;
int miny, maxy;
Vector2 v1 = Vector2(vv1.x * h_width + h_width, -vv1.y * h_height + h_height);
Vector2 v2 = Vector2(vv2.x * h_width + h_width, -vv2.y * h_height + h_height);
Vector2 v3 = Vector2(vv3.x * h_width + h_width, -vv3.y * h_height + h_height);
minx = MAX(0, MIN(v1.x, MIN(v2.x, v3.x)));
miny = MAX(0, MIN(v1.y, MIN(v2.y, v3.y)));
maxx = MIN(fb->getWidth(), MAX(v1.x, MAX(v2.x, v3.x)) + 1);
maxy = MIN(fb->getHeight(), MAX(v1.y, MAX(v2.y, v3.y)) + 1);
for(int j = miny; j < maxy; j++){
for(int i = minx; i < maxx; i++){
if(isPointInTriangle(i, j, v1, v2, v3)){
fb->setPixel(i, j, '#', 0);
}
}
}
}