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

Commit 9fd7f00

Browse files
committed
add phong shading mode
1 parent 165ea06 commit 9fd7f00

File tree

6 files changed

+356
-23
lines changed

6 files changed

+356
-23
lines changed

desktop/graph3d/com/support/constraintlayout/extlib/graph3d/Graph3dPanel.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void onSizeChanged(ComponentEvent c) {
101101
}
102102
mImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
103103
mImageBuff = ((DataBufferInt) (mImage.getRaster().getDataBuffer())).getData();
104-
mScene3D.setScreenDim(width, height, mImageBuff, 0x00FFEEFF);
104+
mScene3D.setScreenDim(width, height, mImageBuff, 0x00AAAAAA);
105105
}
106106

107107
public void onMouseDown(MouseEvent ev) {
@@ -134,20 +134,28 @@ public void onMouseUP(MouseEvent ev) {
134134
mLastTouchY0 = Float.NaN;
135135
}
136136

137+
<<<<<<< Updated upstream
137138
public void onMouseWheel(MouseEvent ev) {
138139
MouseWheelEvent we = (MouseWheelEvent)ev;
139140
range = range * (float) Math.pow(1.01,we.getWheelRotation());
140141
System.out.println(range);
141142
mSurface.setRange(-range, range, -range, range);
142143
mAxisBox.setRange(-range, range, -range, range,-2,20);
143144
mScene3D.update();
145+
=======
146+
range = range * (float) Math.pow(1.01, ev.getWheelRotation());
147+
mSurface.setArraySize(Math.min(300,(int) (range * 5)));
148+
mSurface.setRange(-range, range, -range, range);
149+
mAxisBox.setRange(-range, range, -range, range, minZ, maxZ);
150+
mScene3D.update();
151+
}
152+
>>>>>>> Stashed changes
144153
repaint();
145154
}
146155
public void paintComponent(Graphics g) {
147156
int w = getWidth();
148157
int h = getHeight();
149158
if (mScene3D.notSetUp()) {
150-
System.out.println("setup");
151159
mScene3D.setUpMatrix(w, h);
152160
}
153161

desktop/graph3d/com/support/constraintlayout/extlib/graph3d/Object3D.java

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
*/
2222
public 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

Comments
 (0)