21
21
*/
22
22
public class Object3D {
23
23
protected float [] vert ;
24
+ protected float [] normal ;
24
25
protected int [] index ;
25
26
protected float [] tVert ; // the vertices transformed into screen space
26
27
protected float mMinX , mMaxX , mMinY , mMaxY , mMinZ , mMaxZ ; // bounds in x,y & z
27
- protected int mType = 2 ;
28
+ protected int mType = 4 ;
28
29
29
30
public int getType () {
30
31
return mType ;
@@ -34,15 +35,24 @@ public void setType(int type) {
34
35
this .mType = type ;
35
36
}
36
37
38
+ public void makeVert (int n ) {
39
+ vert = new float [n * 3 ];
40
+ tVert = new float [n * 3 ];
41
+ normal = new float [n * 3 ];
42
+ }
43
+
44
+ public void makeIndexes (int n ) {
45
+ index = new int [n * 3 ];
46
+ }
37
47
38
48
public void transform (Matrix m ) {
39
49
for (int i = 0 ; i < vert .length ; i += 3 ) {
40
50
m .mult3 (vert , i , tVert , i );
41
51
}
42
52
}
43
53
44
- public void render (Scene3D s , float [] zbuff , int [] img , int width , int height ) {
45
- switch (mType ) {
54
+ public void render (Scene3D s , float [] zbuff , int [] img , int width , int height ) {
55
+ switch (mType ) {
46
56
case 0 :
47
57
raster_height (s , zbuff , img , width , height );
48
58
break ;
@@ -55,6 +65,10 @@ public void render(Scene3D s, float[] zbuff, int[] img, int width, int height)
55
65
case 3 :
56
66
raster_lines (s , zbuff , img , width , height );
57
67
break ;
68
+ case 4 :
69
+ raster_phong (s , zbuff , img , width , height );
70
+ break ;
71
+
58
72
}
59
73
}
60
74
@@ -96,20 +110,24 @@ void raster_height(Scene3D s, float[] zbuff, int[] img, int w, int h) {
96
110
}
97
111
}
98
112
113
+ float mAmbient = 0.2f ;
114
+ float mDefuse = 0.82f ;
99
115
116
+ // float mSpec = 0.2f;
100
117
void raster_color (Scene3D s , float [] zbuff , int [] img , int w , int h ) {
101
118
for (int i = 0 ; i < index .length ; i += 3 ) {
102
119
int p1 = index [i ];
103
120
int p2 = index [i + 1 ];
104
121
int p3 = index [i + 2 ];
105
122
106
123
VectorUtil .triangleNormal (tVert , p1 , p2 , p3 , s .tmpVec );
107
- float defuse = VectorUtil .dot (s .tmpVec , s .light );
124
+ float defuse = VectorUtil .dot (s .tmpVec , s .mTransformedLight );
125
+
108
126
float height = (vert [p1 + 2 ] + vert [p3 + 2 ] + vert [p2 + 2 ]) / 3 ;
109
127
height = (height - mMinZ ) / (mMaxZ - mMinZ );
110
- float bright = Math .max (0 , defuse );
111
- float hue = (float ) Math .sqrt (height );
112
- float sat = Math . max ( 0.5f , height ) ;
128
+ float bright = Math .min ( 1 , Math . max (0 , mDefuse * defuse + mAmbient ) );
129
+ float hue = (float ) ( height - Math .floor (height ) );
130
+ float sat = 0.8f ;
113
131
int col = Scene3D .hsvToRgb (hue , sat , bright );
114
132
Scene3D .triangle (zbuff , img , col , w , h , tVert [p1 ], tVert [p1 + 1 ],
115
133
tVert [p1 + 2 ], tVert [p2 ], tVert [p2 + 1 ],
@@ -118,6 +136,51 @@ void raster_color(Scene3D s, float[] zbuff, int[] img, int w, int h) {
118
136
}
119
137
}
120
138
139
+ private int color (float hue , float sat , float bright ) {
140
+ hue = hue (hue );
141
+ bright = bright (bright );
142
+ return Scene3D .hsvToRgb (hue , sat , bright );
143
+ }
144
+
145
+ private float hue (float hue ) {
146
+ return (float ) (hue - Math .floor (hue ));
147
+ }
148
+
149
+ private float bright (float bright ) {
150
+ return Math .min (1 , Math .max (0 , bright ));
151
+ }
152
+
153
+
154
+ void raster_phong (Scene3D s , float [] zbuff , int [] img , int w , int h ) {
155
+ for (int i = 0 ; i < index .length ; i += 3 ) {
156
+ int p1 = index [i ];
157
+ int p2 = index [i + 1 ];
158
+ int p3 = index [i + 2 ];
159
+
160
+ // VectorUtil.triangleNormal(tVert, p1, p2, p3, s.tmpVec);
161
+
162
+
163
+ float defuse1 = VectorUtil .dot (normal , p1 , s .mTransformedLight );
164
+ float defuse2 = VectorUtil .dot (normal , p2 , s .mTransformedLight );
165
+ float defuse3 = VectorUtil .dot (normal , p3 , s .mTransformedLight );
166
+ float col1_hue = hue ((vert [p1 + 2 ] - mMinZ ) / (mMaxZ - mMinZ ));
167
+ float col2_hue = hue ((vert [p2 + 2 ] - mMinZ ) / (mMaxZ - mMinZ ));
168
+ float col3_hue = hue ((vert [p3 + 2 ] - mMinZ ) / (mMaxZ - mMinZ ));
169
+ float col1_bright = bright (mDefuse * defuse1 + mAmbient );
170
+ float col2_bright = bright (mDefuse * defuse2 + mAmbient );
171
+ float col3_bright = bright (mDefuse * defuse3 + mAmbient );
172
+
173
+ Scene3D .trianglePhong (zbuff , img ,
174
+ col1_hue , col1_bright ,
175
+ col2_hue , col2_bright ,
176
+ col3_hue , col3_bright ,
177
+ w , h ,
178
+ tVert [p1 ], tVert [p1 + 1 ], tVert [p1 + 2 ],
179
+ tVert [p2 ], tVert [p2 + 1 ], tVert [p2 + 2 ],
180
+ tVert [p3 ], tVert [p3 + 1 ], tVert [p3 + 2 ]);
181
+ }
182
+ }
183
+
121
184
void raster_outline (Scene3D s , float [] zBuff , int [] img , int w , int h ) {
122
185
for (int i = 0 ; i < index .length ; i += 3 ) {
123
186
int p1 = index [i ];
0 commit comments