77import java .util .ArrayList ;
88
99import javax .vecmath .Matrix4f ;
10+ import javax .vecmath .Vector3d ;
1011import javax .vecmath .Vector4f ;
1112
1213/**
@@ -21,6 +22,7 @@ public class SWRenderContext implements RenderContext {
2122
2223 private SceneManagerInterface sceneManager ;
2324 private BufferedImage colorBuffer ;
25+ private float [][] zBuffer ;
2426 private ArrayList <Triangle > triangles ;
2527
2628 private static Matrix4f c , p , m , d , transformation ;
@@ -61,6 +63,11 @@ public BufferedImage getColorBuffer() {
6163 */
6264 public void setViewportSize (int width , int height ) {
6365 colorBuffer = new BufferedImage (width , height , BufferedImage .TYPE_3BYTE_BGR );
66+ zBuffer = new float [colorBuffer .getWidth ()][colorBuffer .getHeight ()];
67+
68+ for (int i = 0 ; i < colorBuffer .getWidth (); i ++)
69+ for (int j = 0 ; j < colorBuffer .getHeight (); j ++)
70+ zBuffer [i ][j ] = Float .MIN_VALUE ;
6471 }
6572
6673 /**
@@ -91,6 +98,17 @@ else if (task == 2)
9198
9299 }
93100
101+ /**
102+ * draws a pixel p if it is in front of the pixel painted there before.
103+ *
104+ */
105+ private void drawPixel (Vector4f p ) {
106+ if (zBuffer [(int ) p .x ][(int ) p .y ] < 1 / p .w ) {
107+ colorBuffer .setRGB ((int ) (p .x / p .w ), (int ) (p .y / p .w ), Color .WHITE .getRGB ());
108+ zBuffer [(int ) p .x ][(int ) p .y ] = 1 / p .w ;
109+ }
110+ }
111+
94112 private void drawVertices (RenderItem renderItem ) {
95113
96114 float [] vertices = renderItem .getShape ().getVertexData ().getPositions ();
@@ -117,15 +135,15 @@ private void drawTriangleVertices(ArrayList<Triangle> triangles) {
117135
118136 p = new Vector4f (t .getP1 ());
119137 if (inBounds (p , p .w ))
120- colorBuffer . setRGB (( int ) ( p . x / p . w ), ( int ) ( p . y / p . w ), Color . WHITE . getRGB () );
138+ drawPixel ( p );
121139
122140 p = new Vector4f (t .getP2 ());
123141 if (inBounds (p , p .w ))
124- colorBuffer . setRGB (( int ) ( p . x / p . w ), ( int ) ( p . y / p . w ), Color . WHITE . getRGB () );
142+ drawPixel ( p );
125143
126144 p = new Vector4f (t .getP3 ());
127145 if (inBounds (p , p .w ))
128- colorBuffer . setRGB (( int ) ( p . x / p . w ), ( int ) ( p . y / p . w ), Color . WHITE . getRGB () );
146+ drawPixel ( p );
129147 }
130148 }
131149
@@ -142,9 +160,8 @@ private void drawTriangles(ArrayList<Triangle> triangles) {
142160 for (int j = (int ) boundingBox [2 ]; j < (int ) boundingBox [3 ]; j ++) {
143161 // loop over all pixels inside the bounding box
144162 pixel = new Vector4f (i , j , 0 , 1 );
145- if (t .isdrawn (pixel ))
146- colorBuffer .setRGB ((int )pixel .x , (int )pixel .y , t .color (pixel ));
147-
163+ if (t .isdrawn (pixel ))
164+ colorBuffer .setRGB ((int ) pixel .x , (int ) pixel .y , t .colorAt (pixel ).getRGB ());
148165 }
149166 }
150167 }
@@ -187,18 +204,24 @@ private ArrayList<Triangle> extractTriangles(RenderItem renderItem) {
187204 float [] positions = renderItem .getShape ().getVertexData ().getPositions ();
188205 int [] indices = renderItem .getShape ().getVertexData ().getIndices ();
189206 float [] colors = renderItem .getShape ().getVertexData ().getColors ();
190- float [] normals = renderItem .getShape ().getVertexData ().getNormals ();
207+ // float[] normals = renderItem.getShape().getVertexData().getNormals();
191208
192- Vector4f p1 , p2 , p3 ; // TODO
209+ Vector4f p1 , p2 , p3 ;
210+ Vector3d c1 , c2 , c3 ;
193211 for (int i = 0 ; i < indices .length ; i += 3 ) {
194212 p1 = new Vector4f (positions [indices [i + 0 ] * 3 + 0 ], positions [indices [i + 0 ] * 3 + 1 ],
195213 positions [indices [i + 0 ] * 3 + 2 ], 1 );
214+ c1 = new Vector3d (colors [indices [i + 0 ] * 3 + 0 ], colors [indices [i + 0 ] * 3 + 1 ], colors [indices [i + 0 ] * 3 + 2 ]);
215+
196216 p2 = new Vector4f (positions [indices [i + 1 ] * 3 + 0 ], positions [indices [i + 1 ] * 3 + 1 ],
197217 positions [indices [i + 1 ] * 3 + 2 ], 1 );
218+ c2 = new Vector3d (colors [indices [i + 1 ] * 3 + 0 ], colors [indices [i + 1 ] * 3 + 1 ], colors [indices [i + 1 ] * 3 + 2 ]);
219+
198220 p3 = new Vector4f (positions [indices [i + 2 ] * 3 + 0 ], positions [indices [i + 2 ] * 3 + 1 ],
199221 positions [indices [i + 2 ] * 3 + 2 ], 1 );
222+ c3 = new Vector3d (colors [indices [i + 2 ] * 3 + 0 ], colors [indices [i + 2 ] * 3 + 1 ], colors [indices [i + 2 ] * 3 + 2 ]);
200223
201- triangle = new Triangle (p1 , p2 , p3 );
224+ triangle = new Triangle (p1 , p2 , p3 , c1 , c2 , c3 );
202225 //triangle.calculateEdgeFunction();
203226
204227 triangles .add (triangle );
0 commit comments