-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.cpp
194 lines (160 loc) · 4.66 KB
/
frame.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//#include <OpenEXR/ImfInputFile.h>
//#include <OpenEXR/ImfChannelList.h>
//#include <OpenEXR/ImfFrameBuffer.h>
//#include <OpenEXR/ImfRgbaFile.h>
#include <Imath/half.h>
#include"frame.h"
Frame::Frame(int x, int y, Imf::Rgba colour, int scale) :
x_res_(x),
y_res_(y),
scale_(scale),
current_x_(0.0),
current_y_(0.0)
{
rgba_data_ = new Imf::Rgba[x_res_ * y_res_];
for (int i = 0; i < x_res_ * y_res_; i++) {
rgba_data_[i] = colour;
}
}
bool Frame::WriteFrame(const char* filename)
{
try {
Imf::RgbaOutputFile out(filename, x_res_, y_res_, Imf::WRITE_RGBA);
out.setFrameBuffer(rgba_data_, 1, x_res_);
out.writePixels(y_res_);
return true;
}
catch (const std::exception& e) {
std::cout << "Failed to write frame:" << e.what();
return false;
}
}
void Frame::SetPixel(int x, int y, Imf::Rgba color)
{
if (0 <= x && x < x_res_ && 0 <= y && y < y_res_) {
//rgba_data_[y * x_res_ + x] = color;
int flip_y = y_res_ - 1- y;
// We're working in B&W so multiply the colour to make sure lines
// get darker as the cross
rgba_data_[flip_y * x_res_ + x].r *= color.r;
rgba_data_[flip_y * x_res_ + x].g *= color.g;
rgba_data_[flip_y * x_res_ + x].b *= color.b;
}
else {
//std::cout << "Pixel " << x << "," << y << " is outside frame size." << std::endl;
}
}
void Frame::SetPixel(int x, int y, double c)
{
Imf::Rgba color(c, c, c, 1.0);
SetPixel(x, y, color);
}
void Frame::DrawLine(double x0, double y0, double x1, double y1)
{
bool steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
double dx = x1 - x0;
double dy = y1 - y0;
double gradient;
if (dx == 0) {
gradient = 1.0;
} else {
gradient = dy / dx;
}
//https://stackoverflow.com/questions/68552675/what-is-the-endpoint-calculation-in-the-xiaolin-wu-algorithm-doing
// First end point
double x_end;
double y_end;
double x_gap;
int x_pxl_1;
int y_pxl_1;
int x_pxl_2;
int y_pxl_2;
double inter_y;
x_end = round(x0);
y_end = y0 + gradient * (x_end - x0);
x_gap = inv_frac_part(x0 + 0.5);
x_pxl_1 = (int)x_end;
y_pxl_1 = int_part(y_end);
if (steep) {
SetPixel(y_pxl_1, x_pxl_1, frac_part(y_end) * x_gap);
if (frac_part(y_end) != 0) {
SetPixel(y_pxl_1 + 1, x_pxl_1, inv_frac_part(y_end) * x_gap);
}
} else {
SetPixel(x_pxl_1, y_pxl_1, frac_part(y_end) * x_gap);
if (frac_part(y_end) != 0) {
SetPixel(x_pxl_1, y_pxl_1 + 1, inv_frac_part(y_end) * x_gap);
}
}
inter_y = y_end + gradient;
// handle second endpoint
x_end = round(x1);
y_end = y1 + gradient * (x_end - x1);
x_gap = frac_part(x1 + 0.5);
x_pxl_2 = x_end;
y_pxl_2 = int_part(y_end);
if (steep) {
SetPixel(y_pxl_2, x_pxl_2, frac_part(y_end) * x_gap);
if (frac_part(y_end) != 0) {
SetPixel(y_pxl_2 + 1, x_pxl_2, inv_frac_part(y_end) * x_gap);
}
} else {
SetPixel(x_pxl_2, y_pxl_2, frac_part(y_end) * x_gap);
if (frac_part(y_end) != 0) {
SetPixel(x_pxl_2, y_pxl_2 + 1, inv_frac_part(y_end) * x_gap);
}
}
if (steep) {
for (int x = x_pxl_1 + 1; x < x_pxl_2; x++) {
SetPixel(int_part(inter_y), x, frac_part(inter_y));
SetPixel(int_part(inter_y) + 1, x, inv_frac_part(inter_y));
inter_y = inter_y + gradient;
}
} else {
for (int x = x_pxl_1 + 1; x < x_pxl_2; x++) {
SetPixel(x, int_part(inter_y), frac_part(inter_y));
SetPixel(x, int_part(inter_y) + 1, inv_frac_part(inter_y));
inter_y = inter_y + gradient;
}
}
}
void Frame::DrawTo(double x, double y)
{
double scaled_x, scaled_y;
scaled_x = (x * scale_) + x_res_ * 0.5;
scaled_y = (y * scale_) + y_res_ * 0.5;
DrawLine(current_x_, current_y_, scaled_x, scaled_y);
current_x_ = scaled_x;
current_y_ = scaled_y;
//WriteFrame();
//std::cin.get();
}
void Frame::DrawTo(vec3 point)
{
DrawTo(point.x, point.y);
}
void Frame::MoveTo(double x, double y)
{
double scaled_x, scaled_y;
scaled_x = (x * scale_) + x_res_ * 0.5;
scaled_y = (y * scale_) + y_res_ * 0.5;
current_x_ = scaled_x;
current_y_ = scaled_y;
}
void Frame::MoveTo(vec3 point)
{
MoveTo(point.x, point.y);
}
vec3 Frame::PushVertThroughPipeline(vec3 vert, Matrix *C)
{
vec4 trans_vert = TransformVector(vert, C);
return DivideByW(trans_vert);
}