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

Commit e260d13

Browse files
committed
improved lighting (phong) animation etc.)
1 parent 8e5cf91 commit e260d13

File tree

8 files changed

+159
-22
lines changed

8 files changed

+159
-22
lines changed

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

+66
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class Graph3dPanel extends JPanel {
4444
float minZ = -10;
4545
float maxZ = 10;
4646
float mZoomFactor = 1;
47+
boolean animated = false;
4748

4849
public void buildSurface() {
4950

@@ -70,6 +71,13 @@ public void componentResized(ComponentEvent e) {
7071
}
7172

7273
});
74+
setFocusable(true);
75+
addKeyListener(new KeyAdapter() {
76+
@Override
77+
public void keyTyped(KeyEvent e) {
78+
onKeyTyped(e);
79+
}
80+
});
7381
MouseAdapter mouseAdapter = new MouseAdapter() {
7482
@Override
7583
public void mouseReleased(MouseEvent e) {
@@ -78,6 +86,7 @@ public void mouseReleased(MouseEvent e) {
7886

7987
@Override
8088
public void mousePressed(MouseEvent e) {
89+
requestFocus();
8190
onMouseDown(e);
8291
}
8392

@@ -96,6 +105,54 @@ public void mouseWheelMoved(MouseWheelEvent e) {
96105
addMouseMotionListener(mouseAdapter);
97106
}
98107

108+
public void onKeyTyped(KeyEvent e) {
109+
char c= e.getKeyChar();
110+
System.out.println(c);
111+
switch (c) {
112+
case ' ':
113+
toggleAnimation();
114+
}
115+
}
116+
Timer animationTimer;
117+
long nanoTime;
118+
float time = 0;
119+
void toggleAnimation() {
120+
animated = !animated;
121+
122+
123+
if (!animated) {
124+
animationTimer.stop();
125+
animationTimer = null;
126+
return;
127+
}
128+
129+
mSurface = new Surface3D((x, y) -> {
130+
float d = (float) Math.sqrt(x * x + y * y);
131+
float d2 = (float) Math.pow(x * x + y * y,0.125);
132+
float angle = (float) Math.atan2(y,x);
133+
float s = (float) Math.sin(d+angle-time*5);
134+
float s2 = (float) Math.sin(time);
135+
float c = (float) Math.cos(d+angle-time*5);
136+
return (s2*s2+0.1f)*d2*5*(s+c)/(1+d*d/20);
137+
// return (float) (s*s+0.1) * (float) (Math.cos(d-time*5) *(y*y-x*x) /(1+d*d));
138+
});
139+
nanoTime = System.nanoTime();
140+
mScene3D.setObject(mSurface);
141+
mSurface.setRange(-range, range, -range, range,minZ,maxZ);
142+
animationTimer = new Timer(7, new AbstractAction() {
143+
@Override
144+
public void actionPerformed(ActionEvent e) {
145+
long now = System.nanoTime();
146+
time += (now-nanoTime)*1E-9f;
147+
nanoTime = now;
148+
mSurface.calcSurface(false);
149+
mScene3D.update();
150+
repaint();
151+
}
152+
});
153+
animationTimer.start();
154+
}
155+
99156
public void onSizeChanged(ComponentEvent c) {
100157
int width = getWidth();
101158
int height = getHeight();
@@ -155,6 +212,8 @@ public void onMouseWheel(MouseWheelEvent ev) {
155212
repaint();
156213
}
157214

215+
long previous = System.nanoTime();
216+
int count = 0;
158217
public void paintComponent(Graphics g) {
159218
int w = getWidth();
160219
int h = getHeight();
@@ -167,6 +226,13 @@ public void paintComponent(Graphics g) {
167226
return;
168227
}
169228
g.drawImage(mImage, 0, 0, null);
229+
count++;
230+
long now = System.nanoTime();
231+
if (now -previous > 1000000000) {
232+
// System.out.println(time+" fps "+count/((now-previous)*1E-9f));
233+
count = 0;
234+
previous = now;
235+
}
170236
}
171237

172238

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,38 @@
1717
package com.support.constraintlayout.extlib.graph3d;
1818

1919
import javax.swing.*;
20+
import java.awt.*;
2021

2122
/**
2223
* Simple driver for the Graph3dPanel
2324
*/
2425
public class Main {
26+
static class Controls extends JPanel {
27+
Graph3dPanel gp;
28+
Controls(Graph3dPanel gp) {
29+
this.gp = gp;
30+
JCheckBox cb1 = new JCheckBox("camera Light",false);
31+
cb1.addActionListener((e)->{
32+
gp.mScene3D.mLightMovesWithCamera = cb1.isSelected();
33+
System.out.println(gp.mScene3D.mLightMovesWithCamera);
34+
});
35+
add(cb1);
36+
JSlider sl1 = new JSlider();
37+
sl1.getModel().addChangeListener((e)->{
38+
gp.mSurface.mSaturation = sl1.getValue()/100f;
39+
gp.mScene3D.update();
40+
gp.repaint();
41+
});
42+
add(sl1);
43+
}
44+
}
45+
2546
public static void main(String[] args) {
2647
JFrame frame = new JFrame("3d Plot");
27-
Graph3dPanel p = new Graph3dPanel();
48+
JPanel p = new JPanel(new BorderLayout());
49+
Graph3dPanel gp = new Graph3dPanel();
50+
p.add(gp);
51+
p.add(new Controls(gp),BorderLayout.SOUTH);
2852
frame.setContentPane(p);
2953
frame.setBounds(100,100,500,500);
3054
frame.setVisible(true);

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

+10
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public void mult3v(float[]src,float [] dest){
115115
dest[i] =(float) sum;
116116
}
117117
}
118+
public void mult3v(float[]src,int off,float [] dest){
119+
for (int i = 0; i < 3; i++) {
120+
int col = i*4;
121+
double sum = 0;
122+
for (int j = 0; j < 3; j++) {
123+
sum += m[col+j]*src[off+j];
124+
}
125+
dest[i] =(float) sum;
126+
}
127+
}
118128

119129
public void mult4(double[]src,double [] dest){
120130
for (int i = 0; i < 4; i++) {

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public class Object3D {
2727
protected float mMinX, mMaxX, mMinY, mMaxY, mMinZ, mMaxZ; // bounds in x,y & z
2828
protected int mType = 4;
2929

30+
float mAmbient = 0.3f;
31+
float mDefuse = 0.7f;
32+
public float mSaturation = 0.6f;
33+
3034
public int getType() {
3135
return mType;
3236
}
@@ -110,9 +114,6 @@ void raster_height(Scene3D s, float[] zbuff, int[] img, int w, int h) {
110114
}
111115
}
112116

113-
float mAmbient = 0.2f;
114-
float mDefuse = 0.82f;
115-
116117
// float mSpec = 0.2f;
117118
void raster_color(Scene3D s, float[] zbuff, int[] img, int w, int h) {
118119
for (int i = 0; i < index.length; i += 3) {
@@ -150,6 +151,10 @@ private float bright(float bright) {
150151
return Math.min(1, Math.max(0, bright));
151152
}
152153

154+
private float defuse(float[] normals, int off, float[] light) {
155+
// s.mMatrix.mult3v(normal,off,s.tmpVec);
156+
return Math.abs(VectorUtil.dot(normal, off, light));
157+
}
153158

154159
void raster_phong(Scene3D s, float[] zbuff, int[] img, int w, int h) {
155160
for (int i = 0; i < index.length; i += 3) {
@@ -160,9 +165,12 @@ void raster_phong(Scene3D s, float[] zbuff, int[] img, int w, int h) {
160165
// VectorUtil.triangleNormal(tVert, p1, p2, p3, s.tmpVec);
161166

162167

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);
168+
// float defuse1 = VectorUtil.dot(normal, p1, s.mTransformedLight);
169+
// float defuse2 = VectorUtil.dot(normal, p2, s.mTransformedLight);
170+
// float defuse3 = VectorUtil.dot(normal, p3, s.mTransformedLight);
171+
float defuse1 = defuse(normal, p1, s.mTransformedLight);
172+
float defuse2 = defuse(normal, p2, s.mTransformedLight);
173+
float defuse3 = defuse(normal, p3, s.mTransformedLight);
166174
float col1_hue = hue((vert[p1 + 2] - mMinZ) / (mMaxZ - mMinZ));
167175
float col2_hue = hue((vert[p2 + 2] - mMinZ) / (mMaxZ - mMinZ));
168176
float col3_hue = hue((vert[p3 + 2] - mMinZ) / (mMaxZ - mMinZ));
@@ -174,6 +182,7 @@ void raster_phong(Scene3D s, float[] zbuff, int[] img, int w, int h) {
174182
col1_hue, col1_bright,
175183
col2_hue, col2_bright,
176184
col3_hue, col3_bright,
185+
mSaturation,
177186
w, h,
178187
tVert[p1], tVert[p1 + 1], tVert[p1 + 2],
179188
tVert[p2], tVert[p2 + 1], tVert[p2 + 2],

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public class Scene3D {
3232
float[] zBuff;
3333
int[] img;
3434

35-
private float[] light = {0, -0.3f, 1}; // The direction of the light source
36-
public float[] mTransformedLight = {0, -1, -1}; // The direction of the light source
35+
private float[] light = {0, 0, 1}; // The direction of the light source
36+
public float[] mTransformedLight = {0, 1, 1}; // The direction of the light source
37+
public boolean mLightMovesWithCamera = false;
3738

3839
int width, height;
3940
public float[] tmpVec = new float[3];
@@ -43,7 +44,6 @@ public class Scene3D {
4344
private Function mFunction;
4445
private float mZoomZ = 1;
4546
int background;
46-
private boolean mLightMovesWithCamera = true;
4747

4848
public int getLineColor() {
4949
return lineColor;
@@ -95,7 +95,7 @@ public void transformTriangles() {
9595
public void transform() {
9696
Matrix m = mInverse;
9797
if (mLightMovesWithCamera) {
98-
m.mult3v(light, mTransformedLight);
98+
mMatrix.mult3v(light, mTransformedLight);
9999
VectorUtil.normalize(mTransformedLight);
100100
} else {
101101
System.arraycopy(light, 0, mTransformedLight, 0, 3);
@@ -451,6 +451,7 @@ public static void trianglePhong(float[] zbuff, int[] img,
451451
float h3, float b3,
452452
float h2, float b2,
453453
float h1, float b1,
454+
float sat,
454455
int w, int h,
455456
float fx3, float fy3, float fz3,
456457
float fx2, float fy2, float fz2,
@@ -583,7 +584,7 @@ public static void trianglePhong(float[] zbuff, int[] img,
583584
float bright = pb + dbx * x;
584585
if (zbuff[point] > zval) {
585586
zbuff[point] = zval;
586-
img[point] = Scene3D.hsvToRgb(hue, 0.8f, bright);;
587+
img[point] = Scene3D.hsvToRgb(hue, sat, bright);;
587588
}
588589
}
589590
CX1 -= FDY12;

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

+20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.support.constraintlayout.extlib.graph3d;
1717

18+
import java.text.DecimalFormat;
19+
1820
/**
1921
* A few utilities for vector calculations.
2022
*/
@@ -104,4 +106,22 @@ public static void cross(float a0, float a1, float a2, float b0, float b1, float
104106
out[1] = out1;
105107
out[2] = out2;
106108
}
109+
110+
private static String trim(String s){
111+
return s.substring(s.length()-7);
112+
}
113+
114+
public static String vecToString(float[] light) {
115+
DecimalFormat df =new DecimalFormat(" ##0.000");
116+
String str = "[";
117+
for (int i = 0; i < 3; i++) {
118+
if (Float.isNaN(light[i])) {
119+
str+=(((i == 0) ? "" : " , ") + trim(" NAN"));
120+
continue;
121+
}
122+
str+=(((i == 0) ? "" : " , ") + trim(df.format(light[i])));
123+
}
124+
return str+ "]";
125+
126+
}
107127
}

desktop/graph3d/com/support/constraintlayout/extlib/graph3d/objects/AxisBox.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Draws box along the axis
2424
*/
2525
public class AxisBox extends Object3D {
26-
int color = 0xFFFF3233;
26+
int color = 0xFF1010FF;
2727
public AxisBox() {
2828
mType = 1;
2929
}
@@ -91,11 +91,19 @@ public void render(Scene3D s, float[] zbuff, int[] img, int w, int h) {
9191
int p2 = index[i + 1];
9292
int p3 = index[i + 2];
9393

94-
boolean back = Scene3D.isBackface(
94+
boolean front = Scene3D.isBackface(
9595
tVert[p1], tVert[p1 + 1], tVert[p1 + 2],
9696
tVert[p2], tVert[p2 + 1], tVert[p2 + 2],
9797
tVert[p3], tVert[p3 + 1], tVert[p3 + 2]);
98-
if (back) continue;
98+
if (front) {
99+
Scene3D.drawline(zbuff, img, color, w, h,
100+
tVert[p1], tVert[p1 + 1], tVert[p1 + 2] - 0.01f,
101+
tVert[p2], tVert[p2 + 1], tVert[p2 + 2] - 0.01f);
102+
Scene3D.drawline(zbuff, img, color, w, h,
103+
tVert[p1], tVert[p1 + 1], tVert[p1 + 2] - 0.01f,
104+
tVert[p3], tVert[p3 + 1], tVert[p3 + 2] - 0.01f);
105+
continue;
106+
}
99107

100108
Scene3D.drawline(zbuff, img, color, w, h,
101109
tVert[p1], tVert[p1 + 1], tVert[p1 + 2] - 0.01f,
@@ -113,9 +121,6 @@ public void render(Scene3D s, float[] zbuff, int[] img, int w, int h) {
113121
tVert[p3], tVert[p3 + 1], tVert[p3 + 2] - 0.01f,
114122
tVert[p2]-tVert[p1], tVert[p2 + 1]-tVert[p1+1], tVert[p2 + 2] -tVert[p1+2]);
115123

116-
// Scene3D.drawline(zbuff, img,0x000000, w, h,
117-
// tVert[p2], tVert[p2 + 1], tVert[p2 + 2] - 0.01f,
118-
// tVert[p3], tVert[p3 + 1], tVert[p3 + 2] - 0.01f);
119124
}
120125
}
121126
public static void drawTicks(float[] zbuff, int[] img, int color, int w, int h,
@@ -155,11 +160,11 @@ void raster_color(Scene3D s, float[] zbuff, int[] img, int w, int h) {
155160
VectorUtil.triangleNormal(tVert, p1, p3, p2, s.tmpVec);
156161
float ss = VectorUtil.dot(s.tmpVec, screen);
157162
float defuse = VectorUtil.dot(s.tmpVec, s.mTransformedLight);
158-
float ambient = 0.3f;
163+
float ambient = 0.5f;
159164

160165
float bright = Math.min(Math.max(0, defuse + ambient), 1);
161-
float hue = 0.2f;
162-
float sat = 0.5f;
166+
float hue = 0.4f;
167+
float sat = 0.1f;
163168

164169
int col = Scene3D.hsvToRgb(hue, sat, bright);
165170
Scene3D.triangle(zbuff, img, col, w, h, tVert[p1], tVert[p1 + 1],

desktop/graph3d/com/support/constraintlayout/extlib/graph3d/objects/Surface3D.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public void computeSurface(boolean resetZ) {
5353
int n = (mSize + 1) * (mSize + 1);
5454
makeVert(n);
5555
makeIndexes(mSize * mSize * 2);
56-
System.err.println("index "+index.length);
56+
calcSurface(resetZ);
57+
}
58+
public void calcSurface(boolean resetZ) {
5759
float min_x = mMinX;
5860
float max_x = mMaxX;
5961
float min_y = mMinY;

0 commit comments

Comments
 (0)