-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPixelShader.java
More file actions
221 lines (208 loc) · 6.11 KB
/
PixelShader.java
File metadata and controls
221 lines (208 loc) · 6.11 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
import java.util.*;
import java.io.*;
import java.applet.*;
import java.awt.*;
public class PixelShader
{
public Graphics page;
public int XBOUND;
public int YBOUND;
public double VIEWDIST;
public boolean LIGHTING=true;
public int WHITEVALUE=170;
public double LIGHTINGFACTOR=5;
public int DEPTHMODE=1;
public PixelShader(Graphics b, int xb, int yb, double vd)
{
page=b;
XBOUND=xb;
YBOUND=yb;
VIEWDIST=vd;
}
public void drawDepthSorted(ArrayList<Polygon> shapes)
{
ArrayList<LineP> lines= new ArrayList<LineP>();
ArrayList<Plane> planes = new ArrayList<Plane>();
ArrayList<Label> labels = new ArrayList<Label>();
for(int i=0;i<shapes.size();i++)
{
if(shapes.get(i).id.equals("Cube")) {
ArrayList<Plane> add;
add = ((Cube)(shapes.get(i))).getPlanes();
for(Plane P : add) {
planes.add(P);
}
}
else if(shapes.get(i).id.equals("Prism")) {
ArrayList<Plane> add;
add = ((Prism)(shapes.get(i))).getPlanes();
for(Plane P : add) {
planes.add(P);
}
}
else if(shapes.get(i).id.equals("Pyramid")) {
ArrayList<Plane> add;
add = ((Pyramid)(shapes.get(i))).getPlanes();
for(Plane P : add) {
planes.add(P);
}
}
else if(shapes.get(i).id.equals("Arrow3D")) {
ArrayList<Plane> add;
add = ((Arrow3D)(shapes.get(i))).getPlanes();
for(Plane P : add) {
planes.add(P);
}
}
else if(shapes.get(i).id.equals("Sphere")||shapes.get(i).id.equals("VSphere")) {
ArrayList<Plane> add;
add = ((Sphere)(shapes.get(i))).getPlanes();
for(Plane P : add) {
planes.add(P);
}
}
else if(shapes.get(i).id.equals("Plane")) {
planes.add(((Plane)(shapes.get(i))));
}
else if(shapes.get(i).id.equals("Label")) {
labels.add((Label)(shapes.get(i)));
}
else
for(Line3D l : shapes.get(i).lines)
lines.add(new LineP(l,shapes.get(i).color));
//shapes.get(i).color=Color.BLUE;
// if(!shapes.get(i).id.equals("Cube"))
// shapes.get(i).printPolygon(page,new Point(YBOUND/2,XBOUND/2),VIEWDIST);
// else
// ((Cube)(shapes.get(i))).drawFilled(page,new Point(YBOUND/2,XBOUND/2),VIEWDIST);
}
ArrayList<Double> pDists = new ArrayList<Double>();
ArrayList<Double> lDists = new ArrayList<Double>();
ArrayList<Double> lblDists = new ArrayList<Double>();
ArrayList<Integer> pI = new ArrayList<Integer>();
ArrayList<Integer> lI = new ArrayList<Integer>();
ArrayList<Integer> lblI = new ArrayList<Integer>();
if(DEPTHMODE==0) {
for(Plane pl : planes)
{
pDists.add(pl.getCenter().getDistTo(new Point3D(YBOUND/2,XBOUND/2,VIEWDIST)));
}
for(LineP l : lines)
{
lDists.add(l.line.getMidpoint().getDistTo(new Point3D(YBOUND/2,XBOUND/2,VIEWDIST)));
}
for(Label lbl : labels)
{
lblDists.add(lbl.point.getDistTo(new Point3D(YBOUND/2,XBOUND/2,VIEWDIST)));
}
}
else if(DEPTHMODE==1)
{
for(Plane pl : planes)
{
pDists.add(pl.getCenter().z+VIEWDIST);
}
for(LineP l : lines)
{
lDists.add(l.line.getMidpoint().z+VIEWDIST);
}
for(Label lbl : labels)
{
lblDists.add(lbl.point.z+VIEWDIST);
}
}
for(int i=0;i<lines.size();i++)
lI.add(i);
for(int i=0;i<planes.size();i++)
pI.add(i);
for(int i=0;i<labels.size();i++)
lblI.add(i);
for(int i=0;i<lI.size();i++)
for(int j=i+1;j<lI.size();j++)
{
if(lDists.get(lI.get(i))<lDists.get(lI.get(j)))
{
int temp=lI.get(i);
lI.set(i,lI.get(j));
lI.set(j,temp);
}
}
for(int i=0;i<pI.size();i++)
for(int j=i+1;j<pI.size();j++)
{
if(pDists.get(pI.get(i))<pDists.get(pI.get(j)))
{
int temp=pI.get(i);
pI.set(i,pI.get(j));
pI.set(j,temp);
}
}
for(int i=0;i<lblI.size();i++)
for(int j=i+1;j<lblI.size();j++)
{
if(lblDists.get(lblI.get(i))<lblDists.get(lblI.get(j)))
{
int temp=lblI.get(i);
lblI.set(i,lblI.get(j));
lblI.set(j,temp);
}
}
int iP=0,iL=0,iLBL=0;
while(iP<planes.size()||iL<lines.size()||iLBL<labels.size())
{
double pd=Double.MIN_VALUE,ld=Double.MIN_VALUE,lbld=Double.MIN_VALUE;
if(iP<planes.size())
pd=pDists.get(pI.get(iP));
if(iL<lines.size())
ld=lDists.get(lI.get(iL));
if(iLBL<labels.size())
lbld=lblDists.get(lblI.get(iLBL));
if(ld>pd&&ld>lbld)
{
lines.get(lI.get(iL)).printPolygon(page,new Point(YBOUND/2,XBOUND/2),VIEWDIST);
iL++;
}
else if(pd>lbld&&iP<planes.size())
{
Vect3D v = new Vect3D(new Point3D(YBOUND/2,XBOUND/2,-VIEWDIST),planes.get(pI.get(iP)).getCenter());
double cf=Math.pow(Math.abs(planes.get(pI.get(iP)).getNormal().dotProduct(v)/pDists.get(pI.get(iP))),LIGHTINGFACTOR);
if(LIGHTING) {
Color c= planes.get(pI.get(iP)).color;
int r = c.getRed(), g = c.getGreen(), b = c.getBlue(), a = c.getAlpha();
if(r<WHITEVALUE)
r=(int)((WHITEVALUE-r)*cf+r);
if(g<WHITEVALUE)
g=(int)((WHITEVALUE-g)*cf+g);
if(b<WHITEVALUE)
b=(int)((WHITEVALUE-b)*cf+b);
if(r>255)
r=255;
if(g>255)
g=255;
if(b>255)
b=255;
if(r<0)
r=0;
if(g<0)
g=0;
if(b<0)
b=0;
planes.get(pI.get(iP)).color=new Color(r,g,b,a);
}
planes.get(pI.get(iP)).printPolygon(page,new Point(YBOUND/2,XBOUND/2),VIEWDIST);
iP++;
}
else if(iLBL<labels.size())
{
labels.get(lblI.get(iLBL)).printPolygon(page,new Point(YBOUND/2,XBOUND/2),VIEWDIST);
iLBL++;
}
}
// for(Integer i : pI)
// planes.get(i).printPolygon(page,new Point(YBOUND/2,XBOUND/2),VIEWDIST);
// for(Integer i : lI) {
// LineP line = new LineP(lines.get(i));
// line.printPolygon(page,new Point(YBOUND/2,XBOUND/2),VIEWDIST);
// }
}
}