JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區 » Image  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:[工具] 3D函數畫圖 [Re:T55555]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-08-17 02:06 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
又有一點時間了. 不把 texture 加進來, 好像有點美中不足.
Below is simple texture mapping demo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.image.TextureLoader;
 
import javax.media.j3d.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.vecmath.TexCoord2f;
import javax.vecmath.Point3f;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
/**
 * Quick draw function:
 * f(x, y) = (x^2 + 3*y^2) * exp(1-(x^2 + y^2))
 *
 * @author T55555
 * @version 2004-08-16
 */
public class MathGeom extends JFrame {
    public static void main(String[] args) {
        new MathGeom().show();
    }
 
    public MathGeom() {
        super("z = (x^2 + 3*y^2) * exp(1-(x^2 + y^2)) ----- By T55555");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(450, 450);
        setLocationRelativeTo(null);
        Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        getContentPane().add(canvas3D);
 
        SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
        simpleU.getViewingPlatform().setNominalViewingTransform();
 
        TextureLoader loader = new TextureLoader("T55555.jpg", this);
        ImageComponent2D image = loader.getImage();
        Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,image.getWidth(), image.getHeight());
        texture.setImage(0, image);        
 
        BranchGroup objRoot = new BranchGroup();
        Appearance appearance = new Appearance();
        appearance.setTexture(texture);
        TextureAttributes textureAttributes = new TextureAttributes();
        textureAttributes.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
        appearance.setTextureAttributes(textureAttributes);
        objRoot.addChild(new Shape3D(createGeometry(), appearance));
        
        objRoot = addMouseBehavior(objRoot);        
        objRoot.compile();
        simpleU.addBranchGraph(objRoot);
    }
 
    private Geometry createGeometry() {
        int nx = (int) ((2.5 * 2) / 0.125 + 1);
        int ny = (int) ((2.5 * 2) / 0.125 + 1);
        Point3f coords[] = new Point3f[nx * ny];
        float z =0f;        
        int count = 0;
        for (float y = -2.5f; y <= 2.5f; y += 0.125f) {
            for (float x = -2.5f; x <= 2.5f; x += 0.125f) {
                float xx = x * x;
                float yy = y * y;
                z = (float) ((xx + 3 * yy) * Math.exp(1 - (xx + yy)));
                coords[count++] = new Point3f(x/2.5f, y/2.5f, z/4f);
            }
        }
        int[] indices = new int[(nx-1) * (ny-1) * 4];        
        count = 0;
        for (int y = 0, endY = ny - 1; y < endY; y++) {
            for (int x = 0, endX = nx - 1; x < endX; x++) {
                int v = nx * y + x;
                indices[count++] = v;
                indices[count++] = v + 1;
                v += nx;
                indices[count++] = v + 1;
                indices[count++] = v;
            }
        }
 
        count = 0;
        TexCoord2f texCoords[] = new TexCoord2f[nx * ny];
        for (int x = 0; x < nx; x++) {
            for (int y = 0; y < ny; y++) {
                texCoords[count++] = new TexCoord2f(((float)x)/(nx-1), ((float)y)/(ny-1));
            }
        }        
        int[] indices2 = new int[(nx-1) * (ny-1) * 4];
        System.arraycopy(indices, 0, indices2, 0, indices.length);            
            
        IndexedQuadArray iqa = new IndexedQuadArray(coords.length, IndexedQuadArray.COORDINATES | IndexedQuadArray.TEXTURE_COORDINATE_2, indices.length);
        iqa.setCoordinates(0, coords);
        iqa.setCoordinateIndices(0, indices);
        iqa.setTextureCoordinates(0, 0, texCoords);
        iqa.setTextureCoordinateIndices(0, 0, indices2);
        return iqa;
    }    
    
    private BranchGroup addMouseBehavior(BranchGroup child) {
        BranchGroup objRoot = new BranchGroup();
        BoundingSphere boundingSphere = new BoundingSphere();
        TransformGroup tg = new TransformGroup();
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        objRoot.addChild(tg);
        tg.addChild(child);
 
        MouseRotate mouseRotate = new MouseRotate();
        mouseRotate.setTransformGroup(tg);
        mouseRotate.setSchedulingBounds(boundingSphere);
        objRoot.addChild(mouseRotate);
 
        MouseTranslate mouseTranslate = new MouseTranslate();
        mouseTranslate.setTransformGroup(tg);
        mouseTranslate.setSchedulingBounds(boundingSphere);
        objRoot.addChild(mouseTranslate);
        
        MouseZoom mouseZoom = new MouseZoom();
        mouseZoom.setTransformGroup(tg);
        mouseZoom.setSchedulingBounds(boundingSphere);
        objRoot.addChild(mouseZoom);
 
        return objRoot;
    }
}

Notes:
1. You could use any image file for texture, but the image size must be 2^n. ( see Java3D API for more info.)
2. Below image show you that we could modify the texture mapping coordination to have different mapping effect.

(縮略圖,點擊圖片鏈接看原圖)


T55555 edited on 2004-08-31 23:29
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
25432 [圖像]3D函數畫圖 T55555 3949 2004-05-06 04:04
21237 Re:函數畫圖 T55555 28 2004-05-06 04:05
20326 Re:[圖像]3D函數畫圖 aixy 1351 2004-08-19 17:01
19812 Re:[圖像]3D函數畫圖 T55555 207 2004-08-19 21:02
19223 Re:[圖像]3D函數畫圖 supergaosong 226 2004-10-20 16:16
17126 Re:[圖像]3D函數畫圖 jc 155 2005-12-27 19:41
16892 Re:[圖像]3D函數畫圖 T55555 410 2005-12-27 22:15
16345 Re:[圖像]3D函數畫圖 一肩风雪 239 2006-03-07 11:47
18055 Re:[圖像]3D函數畫圖 T55555 425 2006-03-07 22:40
12738 Re:[圖像]3D函數畫圖 CasperLiu 1319 2008-01-07 17:14
6133 j3d如何學習 JOJO10098 39 2011-03-28 23:28
6107 Re:j3d如何學習 T55555 394 2011-03-31 00:02
6011 Re:[圖像]3D函數畫圖 JOJO10098 1431 2011-03-30 18:49
20940 Re:[工具]函數畫圖 T55555 783 2004-05-06 05:22
11001 Re:[工具]函數畫圖 paul760509 55 2008-07-05 15:46
11717 Re:[工具]函數畫圖 T55555 4085 2008-07-08 04:23
8171 Re:[工具]函數畫圖 madmadl 56 2009-06-01 11:56
21040 Re:[工具]函數畫圖 T55555 65 2004-05-06 05:07
20702 Re:[工具]函數畫圖 T55555 32 2004-05-06 05:08
22054 Re:[工具] 3D函數畫圖 T55555 4829 2004-05-29 03:30
20192 Re:[工具] 3D函數畫圖 T55555 15 2004-05-29 03:33
20195 Re:[工具] 3D函數畫圖 T55555 20 2004-05-29 03:35
20202 Re:[工具] 3D函數畫圖 T55555 7 2004-05-29 03:41
20615 Re:[工具] 3D函數畫圖 T55555 247 2004-05-29 03:50
20098 Re:[工具] 3D函數畫圖 worookie 0 2004-05-29 03:55
20209 Re:[工具] 3D函數畫圖 T55555 473 2004-05-29 04:16
20713 Re:[工具] 3D函數畫圖 T55555 5308 2004-08-17 02:06
12654 Re:[工具] 3D函數畫圖 CasperLiu 36 2007-12-26 17:31
» JWorld@TW »  Java 程式分享區 » Image

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8