-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviewport.cpp
228 lines (216 loc) · 5.44 KB
/
viewport.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "viewport.h"
using namespace std;
viewport::viewport(double minX, double maxX, double minY, double maxY, double stretch_k, double theta, int pixelsHigh, int pixelsWide, int antialias, double colourScale, int colourOffset, int iterates)
{
this->pixelsWide = pixelsWide;
this->pixelsHigh = pixelsHigh;
this->antialias = antialias;
this->minX = minX;
this->maxX = maxX;
this->minY = minY;
this->maxY = maxY;
this->stretch_k = stretch_k;
this->theta = theta;
this->colourScale = colourScale;
this->colourOffset = colourOffset;
this->pixelMap = vector<vector<colour> >(pixelsWide, vector<colour>(pixelsHigh));
this->iterates = iterates;
}
void viewport::render()
{
double s_theta = sin(theta);
double c_theta = cos(theta);
double ss_theta = s_theta*s_theta;
double sc_theta = s_theta*c_theta;
double cc_theta = c_theta*c_theta;
int max = iterates;
double L;
if (stretch_k >= 1)
{
L = 2;
}
else
{
L = 2/(stretch_k*stretch_k);
}
for(int i=0; i<pixelsWide; ++i)
{
for(int j=0; j<pixelsHigh; ++j)
{
colour pixelColour(0,0,0,0);
for(int k=0; k<antialias; ++k)
{
for(int m=0; m<antialias; ++m)
{
double width = maxX - minX;
double x = minX + (i - 0.5+double(antialias)/2 + k/double(antialias))/pixelsWide * width;
double height = maxY - minY;
double y = minY + (j - 0.5+double(antialias)/2 + m/double(antialias))/pixelsHigh * height;
//cout << x << "," << y << ";";
double ca = x; //real part
double cb = y; //imaginary part
double za = x;
double zb = y;
double ztemp;
double zx;
double zy;
int itterations = 0;
while(za*za + zb*zb <= L*L && itterations <= max)
{
++itterations;
zx = za*(ss_theta + stretch_k*cc_theta) + zb*(stretch_k - 1)*sc_theta;
zy = za*(stretch_k - 1)*sc_theta + zb*(stretch_k*ss_theta + cc_theta);
za = zx*zx - zy*zy + ca;
zb = 2*zx*zy + cb;
/*
ztemp = za*za - zb*zb + ca;
zb = 2*za*zb + cb;
za = ztemp;
*/
}
colour currentColour;
if(za*za + zb*zb <= L*L)
{
currentColour = colour();
}
else
{
//currentColour = colour(255,255,255);
itterations *= colourScale;
itterations += colourOffset;
itterations = itterations%1536;
int red = 0;
int green = 0;
int blue = 0;
if(itterations < 256)
{
red = 255;
blue = itterations;
}
else if(itterations < 512)
{
blue = 255;
red = 512 - itterations;
}
else if(itterations < 768)
{
blue = 255;
green = itterations - 512;
}
else if(itterations < 1024)
{
green = 255;
blue = 1024 - itterations;
}
else if(itterations < 1280)
{
green = 255;
red = itterations - 1024;
}
else if(itterations < 1536)
{
red = 255;
green = 1536 - itterations;
}
currentColour = colour(red, green, blue);
//currentColour = colour((double(max-itterations)/double(max))*255,(double(max-itterations)/double(max))*255,(double(max-itterations)/double(max))*255);
}
pixelColour = pixelColour.add(currentColour.product(1.0/(antialias*antialias)));
}
}
pixelMap.at(i).at(j) = pixelColour;
}
}
}
void viewport::drawToTerminal()
{
colour foreColour;
for(int i=0; i<pixelMap.at(0).size(); ++i)
{
for(int j=0; j<pixelMap.size(); ++j)
{
//don't output colour codes unless the colour has changed, significantly faster on old terminals.
if(pixelMap.at(j).at(i) != foreColour)
{
foreColour = pixelMap.at(j).at(i);
cout << "\033[48;05;" << foreColour.toAnsi() << "m";
}
cout << " ";
}
foreColour=NULL;
cout << "\033[0m\n";
}
cout << endl;
}
void viewport::drawToUnicode()
{
colour foreColour;
colour backColour;
for(int i=0; i<pixelMap.at(0).size()-1; i+=2)
{
for(int j=0; j<pixelMap.size(); ++j)
{
//don't output colour codes unless the colour has changed, significantly faster on old terminals.
if(pixelMap.at(j).at(i) != foreColour)
{
foreColour = pixelMap.at(j).at(i);
cout << "\033[48;05;" << foreColour.toAnsi() << "m";
}
if(pixelMap.at(j).at(i+1) != backColour)
{
backColour = pixelMap.at(j).at(i+1);
cout << "\033[38;05;" << backColour.toAnsi() << "m";
}
cout << "▄";
}
foreColour=NULL;
backColour=NULL;
cout << "\033[0m\n";
}
cout << endl;
}
void viewport::drawToPPM()
{
ofstream outFile("test.ppm");
outFile << "P3" << endl;
outFile << pixelMap.size() << " " << pixelMap.at(0).size() << endl;
outFile << 255;
for(int i=0; i<pixelMap.at(0).size(); ++i)
{
for(int j=0; j<pixelMap.size(); ++j)
{
if(j%5 == 0)
{
outFile << endl;
}
outFile << " " << pixelMap.at(j).at(i).R;
outFile << " " << pixelMap.at(j).at(i).G;
outFile << " " << pixelMap.at(j).at(i).B;
}
outFile << endl;
}
outFile.close();
}
void viewport::drawToFile(string filename)
{
using stringUtils::toString;
FILE* output = popen(("convert ppm:- " + filename).c_str(), "w");
fprintf(output, "P3\n");
fprintf(output, "%i %i\n", pixelMap.size(), pixelMap.at(0).size());
fprintf(output, "255");
for(int i=0; i<pixelMap.at(0).size(); ++i)
{
for(int j=0; j<pixelMap.size(); ++j)
{
if(j%5 == 0)
{
fprintf(output, "\n");
}
fprintf(output, " %i", pixelMap.at(j).at(i).R);
fprintf(output, " %i", pixelMap.at(j).at(i).G);
fprintf(output, " %i", pixelMap.at(j).at(i).B);
}
fprintf(output, "\n");
}
pclose(output);
}