Skip to content
This repository was archived by the owner on Dec 24, 2019. It is now read-only.

Commit b3350e1

Browse files
committed
finished taks 2
1 parent 79917e4 commit b3350e1

2 files changed

Lines changed: 100 additions & 40 deletions

File tree

Serie 3/Computergrafik-Basecode/jrtr/src/main/java/jrtr/SWRenderContext.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88

99
import javax.vecmath.Matrix4f;
10+
import javax.vecmath.Vector3d;
1011
import 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);

Serie 3/Computergrafik-Basecode/jrtr/src/main/java/jrtr/Triangle.java

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package jrtr;
22

3+
import java.awt.Color;
4+
35
import javax.vecmath.Matrix3f;
46
import javax.vecmath.Matrix4f;
57
import javax.vecmath.SingularMatrixException;
8+
import javax.vecmath.Vector3d;
69
import javax.vecmath.Vector3f;
710
import javax.vecmath.Vector4f;
811

912
public class Triangle {
10-
private static Vector4f p1, p2, p3;
13+
private static Vector4f p1, p2, p3; // coordinates of points
14+
private static Vector3d c1, c2, c3; // colors to points
1115

1216
private boolean isTransformed = false;
1317

@@ -21,6 +25,18 @@ public Triangle(Vector4f p1, Vector4f p2, Vector4f p3) {
2125
calculateEdgeFunction();
2226
}
2327

28+
public Triangle(Vector4f p1, Vector4f p2, Vector4f p3, Vector3d c1, Vector3d c2, Vector3d c3) {
29+
Triangle.p1 = p1;
30+
Triangle.p2 = p2;
31+
Triangle.p3 = p3;
32+
33+
calculateEdgeFunction();
34+
35+
Triangle.c1 = c1;
36+
Triangle.c2 = c2;
37+
Triangle.c3 = c3;
38+
}
39+
2440
public Vector4f getP1() {
2541
return p1;
2642
}
@@ -111,62 +127,83 @@ public boolean isdrawn(Vector4f pixel) {
111127

112128
if (!isVisible())
113129
return false;
114-
130+
115131
return (alphaFunction(pixel) > 0 && betaFunction(pixel) > 0 && gammaFunction(pixel) > 0);
116132
}
117133

118134
public boolean isVisible() {
119135
return (p1.w > 0 && p2.w > 0 && p3.w > 0);
120136
}
121-
137+
122138
/**
123139
*
124140
* @return an array containing the left, right, upper and lower border of the bounding box.
125141
*
126-
* [leftBorder, rightBorder, upperBorder, lowerBorder]
142+
* [leftBorder, rightBorder, upperBorder, lowerBorder]
127143
*/
128-
public float[] getBoundingBox(){
144+
public float[] getBoundingBox() {
129145
float[] values = new float[4];
130-
146+
131147
values[0] = min(p1.x, p2.x, p3.x);
132148
values[1] = max(p1.x, p2.x, p3.x);
133149
values[2] = min(p1.y, p2.y, p3.y);
134150
values[3] = max(p1.y, p2.y, p3.y);
135-
151+
136152
return values;
137153
}
138-
139-
140154

141155
private static float min(float a, float b, float c) {
142-
if (b < a) {
143-
a = b;
144-
}
145-
if (c < a) {
146-
a = c;
147-
}
148-
return a;
149-
}
150-
156+
if (b < a) {
157+
a = b;
158+
}
159+
if (c < a) {
160+
a = c;
161+
}
162+
return a;
163+
}
164+
151165
private float max(float a, float b, float c) {
152-
if (b > a) {
153-
a = b;
154-
}
155-
if (c > a) {
156-
a = c;
157-
}
158-
159-
return a;
166+
if (b > a) {
167+
a = b;
168+
}
169+
if (c > a) {
170+
a = c;
171+
}
172+
173+
return a;
160174
}
161175

162-
163176
/**
164177
*
165178
* @param pixel
166-
* @return calculates the color to the pixel inside the triangle
179+
* @return calculates the color to the pixel p inside the triangle
167180
*/
168-
public int color(Vector4f pixel) {
169-
// TODO Auto-generated method stub
170-
return 0;
181+
public Color colorAt(Vector4f pixel) {
182+
Vector3d tmpColor;
183+
Vector4f tmp = new Vector4f();
184+
185+
tmpColor = new Vector3d(distance(p2, pixel) / distance(p2, p1) * c1.x + distance(pixel, p1) / distance(p2, p1)
186+
* c2.x, distance(p2, pixel) / distance(p2, p1) * c1.y + distance(pixel, p1) / distance(p2, p1) * c2.y,
187+
distance(p2, pixel) / distance(p2, p1) * c1.z + distance(pixel, p1) / distance(p2, p1) * c2.z);
188+
189+
// Project pixel to p2-p1 line
190+
Vector4f projPixelX = new Vector4f();
191+
projPixelX.sub(p2, p1);
192+
tmp = new Vector4f(pixel);
193+
tmp.normalize();
194+
projPixelX.dot(tmp);
195+
projPixelX.dot(tmp);
196+
197+
tmpColor = new Vector3d(distance(p3, pixel) / distance(p3, projPixelX) * tmpColor.x + distance(pixel, projPixelX)
198+
/ distance(p3, projPixelX) * c3.x, distance(p3, pixel) / distance(p3, projPixelX) * tmpColor.y
199+
+ distance(pixel, projPixelX) / distance(p3, projPixelX) * c3.y, distance(p3, pixel)
200+
/ distance(p3, projPixelX) * tmpColor.z + distance(pixel, projPixelX) / distance(p3, projPixelX) * c3.z);
201+
202+
return new Color((float) tmpColor.x, (float) tmpColor.y, (float) tmpColor.z);
203+
}
204+
205+
private float distance(Vector4f pixel, Vector4f tmp) {
206+
tmp.sub(p2, pixel);
207+
return tmp.length();
171208
}
172209
}

0 commit comments

Comments
 (0)