-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangles.cpp
More file actions
180 lines (151 loc) · 4.39 KB
/
Copy pathtriangles.cpp
File metadata and controls
180 lines (151 loc) · 4.39 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
#include "graphics.hpp"
//Vertices of triangle must be defined in anticlockwise order
gp::triangleStrip::triangleStrip(sf::Vector3f v1,sf::Vector3f v2,sf::Vector3f v3)
{
origVertices[0]=v1;
origVertices[1]=v2;
origVertices[2]=v3;
}
void gp::triangleStrip::calcParameter()
{
sf::Vector3f v1=origVertices[0];
sf::Vector3f v2=origVertices[1];
sf::Vector3f v3=origVertices[2];
//Computes the equation of surface in world coordinate
sf::Vector3f N=crossProduct(v2-v1,v3-v1);
origA=N.x;
origB=N.y;
origC=N.z;
origD=-1*dotProduct(N,v2);
}
void gp::triangleStrip::setWindow(gp::mainWindow *wd)
{
window=wd;
}
sf::Vector3f gp::triangleStrip::maxY()
{
sf::Vector3f temp=projVertices[0];
for(int i=1; i<3; i++)
{
if(projVertices[i].y > temp.y)
temp=projVertices[i];
}
return temp;
}
sf::Vector3f gp::triangleStrip::minY()
{
sf::Vector3f temp=projVertices[0];
for(int i=1; i<3; i++)
{
if(projVertices[i].y < temp.y)
temp=projVertices[i];
}
return temp;
}
sf::Vector3f gp::triangleStrip::getEnd(sf::Vector3f pt)
{
for(int i=0; i<3; i++)
{
if(projVertices[i]==pt)
{
if(i==2)
return projVertices[0];
else
return projVertices[i+1];
}
}
return pt;
}
sf::Vector3f gp::triangleStrip::getStart(sf::Vector3f pt)
{
for(int i=0; i<3; i++)
{
if(projVertices[i]==pt)
{
if(i==0)
return projVertices[2];
else
return projVertices[i-1];
}
}
return pt;
}
sf::Vector3f gp::triangleStrip::getIntensity(sf::Vector3f pt)
{
for(int i=0; i<3; i++)
{
if(projVertices[i]==pt)
return intensities[i];
}
return sf::Vector3f(0,0,0);
}
sf::Vector3f gp::triangleStrip::getNormal(sf::Vector3f pt)
{
for(int i=0; i<3; i++)
{
if(projVertices[i]==pt)
return normals[i];
}
return sf::Vector3f(0,0,0);
}
//Function to project the 3D points in projection plane
void gp::triangleStrip::project()
{
for(int i=0; i<3; i++)
projVertices[i]=window->transformPoint(origVertices[i]);
//Computes the equation of surface
sf::Vector3f v1=projVertices[0],v2=projVertices[1],v3=projVertices[2];
sf::Vector3f N=crossProduct(v2-v1,v3-v1);
projA=N.x;
projB=N.y;
projC=N.z;
projD=-1*dotProduct(N,v2);
}
void gp::triangleStrip::draw()
{
//Project the triangle into projection plane
project();
//Maximum and Minimum value of y in projection plane
sf::Vector3f localMax=maxY(),localMin=minY();
sf::Vector3f leftStart=localMax, leftEnd=getEnd(leftStart);
sf::Vector3f rightEnd=localMax, rightStart=getStart(rightEnd);
//Scan-Line algorithm to fill a triangle
do
{
leftEnd=getEnd(leftStart);
for(int yscan=leftStart.y; yscan>=leftEnd.y; yscan--)
{
float minX,maxX;
sf::Vector3f leftIntensity;
float x1=leftStart.x,y1=leftStart.y;
float x2=leftEnd.x,y2=leftEnd.y;
minX=(yscan-y1)*(x2-x1)/(y2-y1)+x1;
leftIntensity=(yscan-y2)/(y1-y2)*getIntensity(leftStart)+(y1-yscan)/(y1-y2)*getIntensity(leftEnd);
if(yscan<rightStart.y)
{
rightEnd=rightStart;
rightStart=getStart(rightEnd);
}
sf::Vector3f rightIntensity;
x1=rightStart.x;
y1=rightStart.y;
x2=rightEnd.x;
y2=rightEnd.y;
maxX=(yscan-y1)*(x2-x1)/(y2-y1)+x1;
rightIntensity=(yscan-y1)/(y2-y1)*getIntensity(rightEnd)+(y2-yscan)/(y2-y1)*getIntensity(rightStart);
if(minX>maxX)
{
swap(minX,maxX);
swap(leftIntensity,rightIntensity);
}
for(int x=minX; x<=maxX; x++)
{
float z=(-projA*x-projB*yscan-projD)/projC;
sf::Vector3f pointIntensity=(maxX-x)/(maxX-minX)*leftIntensity+(x-minX)/(maxX-minX)*rightIntensity;
window->plotPoint(sf::Vector3f(x,yscan,z),pointIntensity);
}
}
leftStart=leftEnd;
}
while(leftEnd!=localMin);
}