-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
264 lines (219 loc) · 6.37 KB
/
Copy pathwindow.cpp
File metadata and controls
264 lines (219 loc) · 6.37 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "graphics.hpp"
gp::zbuffer::zbuffer(float zval)
{
z=zval;
}
gp::mainWindow::mainWindow(int wd,int ht): sf::RenderWindow(sf::VideoMode(wd,ht), "Project!")
{
//Height And Width for buffers
width=wd;
height=ht;
//Image buffer
windowImage.create(width,height);
windowTexture.create(width,height);
//Z buffer
points=new zbuffer [width*height];
}
void gp::mainWindow::clearWindow()
{
//Clears the z-buffer
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
points[i+width*j]=zbuffer(-2000);
}
}
//Clears the intensity buffer
windowImage.create(width,height);
}
void gp::mainWindow::updateWindow()
{
windowTexture.update(windowImage);
windowSprite.setTexture(windowTexture);
draw(windowSprite);
display();
}
//Sets the viewing coordinate origin and the window size
void gp::mainWindow::setViewPoint(sf::Vector3f pt)
{
//Storing the viewing point reference point
viewPoint=pt;
}
//Set the point to look-at from the viewing coordinate origin
void gp::mainWindow::setLookAtPoint(sf::Vector3f pt)
{
lookAtPoint=pt;
}
void gp::mainWindow::setProjection(sf::Vector3f prp,float point)
{
//Projection Reference point
projectionPoint=prp;
//Position for projection plane
zviewPlane=point;
}
void gp::mainWindow::setViewVolume(sf::Vector3f wmin,sf::Vector3f wmax)
{
//Position of window in the projection plane
worldMin=wmin;
worldMax=wmax;
//Maximum and minimum value for z
zfront=worldMax.z;
zback=worldMin.z;
}
void gp::mainWindow::setAmbientLight(sf::Vector3f col)
{
Ia=col;
}
void gp::mainWindow::addLightSource(sf::Vector3f pt,sf::Vector3f color)
{
//Position of light source
light.Position=pt;
//Color of light source
light.Color=color;
}
void gp::mainWindow::calcTransformation()
{
if(viewPoint==lookAtPoint)
{
viewPoint=sf::Vector3f(0,0,10);
lookAtPoint=sf::Vector3f(0,0,0);
}
sf::Vector3f N=viewPoint-lookAtPoint;
//nVector is the positive z direction in the viewing axis
nVector=unitVector(N);
//Taking V vector as (0,1,0) the direction for positive x-axis is calculated from the cross product of N and V
sf::Vector3f VN=crossProduct(sf::Vector3f(0,1,0),N);
//uVector is the positive x direction in viewing axis
uVector=unitVector(VN);
//vVector is the actual positive y direction in viewing axis
vVector=crossProduct(nVector,uVector);
vVector=unitVector(vVector);
sf::Vector3f u=uVector;
sf::Vector3f v=vVector;
sf::Vector3f n=nVector;
//Translation Matrix for shifting the origin
gp::matrix t(
1, 0, 0, -viewPoint.x,
0, 1, 0, -viewPoint.y,
0, 0, 1, -viewPoint.z,
0, 0, 0, 1);
// Rotational Matrix for rotating the axes
gp::matrix r(
u.x, u.y, u.z ,0,
v.x, v.y, v.z, 0,
n.x, n.y, n.z, 0,
0, 0, 0, 1);
//Composite matrix of rotational and translation matrix
gp::matrix rt=r*t;
float xprp=projectionPoint.x;
float yprp=projectionPoint.y;
float zprp=projectionPoint.z;
float zvp=zviewPlane;
float dp=zprp-zvp;
float a=-1*(xprp-(worldMax.x+worldMin.x)/2.0)/zprp;
float b=-1*(yprp-(worldMax.y+worldMin.y)/2.0)/zprp;
//Matrix to align the frustum
gp::matrix mshear(
1, 0, a, -a*zprp,
0, 1, b, -b*zprp,
0, 0, 1, 0,
0, 0, 0, 1
);
//Matrix to shear the frustum to a parallelpipe
gp::matrix mscale(
1, 0, -xprp/dp, xprp*zvp/dp,
0, 1, -yprp/dp, yprp*zvp/dp,
0, 0, 1, 0,
0, 0, -1/dp, zprp/dp);
//Composite matrix for perspective projection transformation
gp::matrix mperspective=mscale*mshear;
//Transformation matrix
mtransform=mperspective*rt;
}
sf::Vector3f gp::mainWindow::transformPoint(sf::Vector3f pt)
{
//World Coordinate of point
gp::matrix wc(pt.x,
pt.y,
pt.z,
1);
//Projection of the given point
gp::matrix ppc=mtransform*wc;
//Converting the value of h to 1
ppc=ppc/ppc(3,0);
// Projection of points in projection plane
sf::Vector3f data=sf::Vector3f(ppc(0,0),ppc(1,0),ppc(2,0));
return data;
}
void gp::mainWindow::plotPoint(sf::Vector3f pt, sf::Vector3f Intensity)
{
// Normalizes the projection plane coordinates
// gp::matrix ppc(pt.x,pt.y,pt.z,1);
//
// sf::Vector3f vmin(0,0,0),vmax(1,1,1);
//
// float Dx=(vmax.x-vmin.x)/(windowSize.x);
// float Dy=(vmax.y-vmin.y)/(windowSize.y);
// float Dz=(vmax.z-vmin.z)/(zfront-zback);
//
// float Kx=vmin.x-windowPosition.x*Dx;
// float Ky=vmin.y-windowPosition.y*Dy;
// float Kz=vmin.z-zfront*Dz;
//
// //Matrix to normalise the view volume
// gp::matrix normalize(
// Dx, 0, 0, Kx,
// 0, Dy, 0, Ky,
// 0, 0, Dz, Kz,
// 0, 0, 0, 1
// );
//
// //Normalized co-ordinates
// gp::matrix npcoordinate(4,1);
// npcoordinate=normalize*ppc;
//
// sf::Vector3f pos;
// pos.x=width*npcoordinate(0,0);
// pos.y=height*(1-npcoordinate(1,0));
// pos.z=npcoordinate(2,0);
sf::Vector3f pos;
pos.x=pt.x;
pos.y=height-pt.y;
pos.z=pt.z;
if(Intensity.x>255)
Intensity.x=255;
else if(Intensity.x<0)
Intensity.x=0;
if(Intensity.y>255)
Intensity.y=255;
else if(Intensity.y<0)
Intensity.y=0;
if(Intensity.z>255)
Intensity.z=255;
else if(Intensity.z<0)
Intensity.z=0;
sf::Color color(Intensity.x,Intensity.y,Intensity.z);
int x=static_cast<int>(pos.x+0.5);
int y=static_cast<int>(pos.y+0.5);
float z=pos.z;
if(x<0 || x>=width || y<0 || y>=height)
return;
if(points[x+width*y].z<z)
{
points[x+width*y].z=z;
windowImage.setPixel(x,y,color);
}
}
void gp::mainWindow::saveScreenshot(void)
{
int count = 0;
string name,strCount;
srand(time(0));
count = 10000 + rand()%1000000;
stringstream holyiness;
holyiness << count;
holyiness >> strCount;
name = "Screenshot" + strCount + ".jpg";
windowImage.saveToFile(name.c_str());
}