2121 */
2222public class Object3D {
2323 protected float [] vert ;
24+ protected float [] normal ;
2425 protected int [] index ;
2526 protected float [] tVert ; // the vertices transformed into screen space
2627 protected float mMinX , mMaxX , mMinY , mMaxY , mMinZ , mMaxZ ; // bounds in x,y & z
27- protected int mType = 2 ;
28+ protected int mType = 4 ;
2829
2930 public int getType () {
3031 return mType ;
@@ -34,15 +35,24 @@ public void setType(int type) {
3435 this .mType = type ;
3536 }
3637
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+ }
3747
3848 public void transform (Matrix m ) {
3949 for (int i = 0 ; i < vert .length ; i += 3 ) {
4050 m .mult3 (vert , i , tVert , i );
4151 }
4252 }
4353
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 ) {
4656 case 0 :
4757 raster_height (s , zbuff , img , width , height );
4858 break ;
@@ -55,6 +65,10 @@ public void render(Scene3D s, float[] zbuff, int[] img, int width, int height)
5565 case 3 :
5666 raster_lines (s , zbuff , img , width , height );
5767 break ;
68+ case 4 :
69+ raster_phong (s , zbuff , img , width , height );
70+ break ;
71+
5872 }
5973 }
6074
@@ -96,20 +110,24 @@ void raster_height(Scene3D s, float[] zbuff, int[] img, int w, int h) {
96110 }
97111 }
98112
113+ float mAmbient = 0.2f ;
114+ float mDefuse = 0.82f ;
99115
116+ // float mSpec = 0.2f;
100117 void raster_color (Scene3D s , float [] zbuff , int [] img , int w , int h ) {
101118 for (int i = 0 ; i < index .length ; i += 3 ) {
102119 int p1 = index [i ];
103120 int p2 = index [i + 1 ];
104121 int p3 = index [i + 2 ];
105122
106123 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+
108126 float height = (vert [p1 + 2 ] + vert [p3 + 2 ] + vert [p2 + 2 ]) / 3 ;
109127 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 ;
113131 int col = Scene3D .hsvToRgb (hue , sat , bright );
114132 Scene3D .triangle (zbuff , img , col , w , h , tVert [p1 ], tVert [p1 + 1 ],
115133 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) {
118136 }
119137 }
120138
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+
121184 void raster_outline (Scene3D s , float [] zBuff , int [] img , int w , int h ) {
122185 for (int i = 0 ; i < index .length ; i += 3 ) {
123186 int p1 = index [i ];
0 commit comments