Rendering merupakan salah satu sub topik utama dalam 3D computer graphics. Pada prakteknya, topik ini selalu berhubungan dengan aspek-aspek yang lain, seperti graphic pipeline, yang merupakan tahapan terakhir,
memberikan tampilan akhir pada model dan animasi. Banyak teknik rendering yang telah dikembangkan untuk memperoleh gambar akhir, diantaranya scanline rendering and rasterisation, ray casting, radiosity, dan ray
tracing.
Sekarang kita lihat contoh yang bisa memanfaatkan Rendering dalam paket JAVA 3D.
1> Teks 3D dengan WIREFRAME
API Java 3D tidak hanya menyediakan objek untuk pembuatan bangun 3D, namun juga menawarkan objek pembentuk teks 3D. Pada prinsipnya, tahap pembuatan teks 3D dan geometri sama persis. Pembuatan objek teks 3D dilakukan melalui kelas Text3D yang terletak didalam paket javax.media.j3d. Kelas yang menyediakan lima jenis konstruktor ini akan merepresentasikan masukan teks argumen ke dalam bentuk 3D.
---------code--------
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.vp.*;
import javax.swing.JFrame;
public class DText3D extends JFrame {
public DText3D() {
// Menciptakan kanvas
Canvas3D canvas = new Canvas3D(
SimpleUniverse.getPreferredConfiguration());
// Menciptakan SimpleUniverse dan mengatur posisi
SimpleUniverse univ = new SimpleUniverse(canvas);
univ.getViewingPlatform().setNominalViewingTransform();
// Menciptakan scene.
createSceneGraph(univ);
// Navigasi melalui mouse
OrbitBehavior ob = new OrbitBehavior(canvas);
ob.setSchedulingBounds(new BoundingSphere());
univ.getViewingPlatform().setViewPlatformBehavior(ob);
// Mengatur frame window
setTitle("Demo 3D Text");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,300);
getContentPane().add("Center", canvas);
setVisible(true);
}
public void createSceneGraph(SimpleUniverse univ) {
float r = 0.3f;
BranchGroup group = new BranchGroup();
Appearance ap = new Appearance();
ColoringAttributes textColor = new ColoringAttributes();
textColor.setColor(1.0f, 0.0f, 0.0f);
ap.setColoringAttributes(textColor);
ap.setMaterial(new Material());
// Menciptakan teks 3D
Font3D font3D = new Font3D(new Font("Helvetica",
Font.BOLD, 1), new FontExtrusion());
Text3D txt3D = new Text3D(font3D, "3D Text");
txt3D.setAlignment(Text3D.ALIGN_CENTER);
Shape3D textShape = new Shape3D();
textShape.setGeometry(txt3D);
textShape.setAppearance(ap);
group.addChild(textShape);
BoundingSphere bounds = new BoundingSphere();
// Mengatur mode wireframe
PolygonAttributes pa = new PolygonAttributes();
pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
ap.setPolygonAttributes(pa);
// Menciptakan background putih.
Background bg = new Background(new Color3f(Color.white));
bg.setApplicationBounds(bounds);
group.addChild(bg);
Color3f col = new Color3f(Color.green);
Vector3f vek = new Vector3f(-2.0f, -2.0f, -2.0f);
DirectionalLight light = new DirectionalLight(col, vek);
light.setInfluencingBounds(bounds);
group.addChild(light);
group.compile();
// Menambahkan scene ke universe
univ.addBranchGraph(group);
}
public static void main(String[] args) {
new DText3D();
}
}
-------Akhir Kelas-------eksekusi dan menjalankan program
javac DText3D.java
java DText3D
Tampilan program DText3D.java
2> SPERE
sphere yang memiliki kemampuan untuk berpindah dari mode wireframe ke rendered secara dinamis dengan memanfaatkan radio button sebagai kendali pilihan.
--------Code-------
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.vp.*;
import javax.swing.*;
public class ProblemWire extends JFrame {
private PolygonAttributes pa = new PolygonAttributes();
public ProblemWire() {
JPanel panel = new JPanel();
panel.add(new JLabel("Mode:"));
// Menciptakan radio button pilihan
JRadioButton fillBtn = new JRadioButton("Fill");
fillBtn.setSelected(true);
fillBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
pa.setPolygonMode(PolygonAttributes.POLYGON_FILL);
}
});
// Aksi pemilihan radio
JRadioButton lineBtn=new JRadioButton("Line");
lineBtn.setSelected(true);
lineBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
}
});
// Aksi pemilihan radio
ButtonGroup bg=new ButtonGroup();
bg.add(fillBtn);
bg.add(lineBtn);
panel.add(fillBtn);
panel.add(lineBtn);
add(panel,BorderLayout.SOUTH);
// Menciptakan kanvas
Canvas3D canvas = new Canvas3D(
SimpleUniverse.getPreferredConfiguration());
// Menciptakan SimpleUniverse dan mengatur posisi
SimpleUniverse univ = new SimpleUniverse(canvas);
univ.getViewingPlatform().setNominalViewingTransform();
// Menciptakan scene.
createSceneGraph(univ);
// Navigasi melalui mouse
OrbitBehavior ob = new OrbitBehavior(canvas);
ob.setSchedulingBounds(new BoundingSphere());
univ.getViewingPlatform().setViewPlatformBehavior(ob);
// Mengatur frame window
setTitle("Problem Solving");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,300);
add("Center", canvas);
setVisible(true);
}
public void createSceneGraph(SimpleUniverse univ) {
float r = 0.5f;
BranchGroup group = new BranchGroup();
// Menciptakan bola
Sphere ball = new Sphere(r, Sphere.GENERATE_NORMALS, 70);
Transform3D tf1 = new Transform3D();
tf1.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
TransformGroup tg1 = new TransformGroup(tf1);
pa.setCapability(PolygonAttributes.ALLOW_MODE_WRITE);
// Mengatur mode wireframe (LENGKAPI)
Appearance ap = ball.getAppearance();
ap.setPolygonAttributes(pa);
tg1.addChild(ball);
group.addChild(tg1);
BoundingSphere bounds = new BoundingSphere();
// Menciptakan background putih.
Background bg = new Background(new Color3f(Color.white));
bg.setApplicationBounds(bounds);
group.addChild(bg);
Color3f col = new Color3f(Color.green);
Vector3f vek = new Vector3f(-5.0f, -5.0f, -5.0f);
DirectionalLight light = new DirectionalLight(col, vek);
light.setInfluencingBounds(bounds);
group.addChild(light);
group.compile();
// Menambahkan scene ke universe
univ.addBranchGraph(group);
}
public static void main(String[] args) {
new ProblemWire();
}
}
--------Akhir Kelas-------import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.vp.*;
import javax.swing.*;
public class ProblemWire extends JFrame {
private PolygonAttributes pa = new PolygonAttributes();
public ProblemWire() {
JPanel panel = new JPanel();
panel.add(new JLabel("Mode:"));
// Menciptakan radio button pilihan
JRadioButton fillBtn = new JRadioButton("Fill");
fillBtn.setSelected(true);
fillBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
pa.setPolygonMode(PolygonAttributes.POLYGON_FILL);
}
});
// Aksi pemilihan radio
JRadioButton lineBtn=new JRadioButton("Line");
lineBtn.setSelected(true);
lineBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
}
});
// Aksi pemilihan radio
ButtonGroup bg=new ButtonGroup();
bg.add(fillBtn);
bg.add(lineBtn);
panel.add(fillBtn);
panel.add(lineBtn);
add(panel,BorderLayout.SOUTH);
// Menciptakan kanvas
Canvas3D canvas = new Canvas3D(
SimpleUniverse.getPreferredConfiguration());
// Menciptakan SimpleUniverse dan mengatur posisi
SimpleUniverse univ = new SimpleUniverse(canvas);
univ.getViewingPlatform().setNominalViewingTransform();
// Menciptakan scene.
createSceneGraph(univ);
// Navigasi melalui mouse
OrbitBehavior ob = new OrbitBehavior(canvas);
ob.setSchedulingBounds(new BoundingSphere());
univ.getViewingPlatform().setViewPlatformBehavior(ob);
// Mengatur frame window
setTitle("Problem Solving");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,300);
add("Center", canvas);
setVisible(true);
}
public void createSceneGraph(SimpleUniverse univ) {
float r = 0.5f;
BranchGroup group = new BranchGroup();
// Menciptakan bola
Sphere ball = new Sphere(r, Sphere.GENERATE_NORMALS, 70);
Transform3D tf1 = new Transform3D();
tf1.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
TransformGroup tg1 = new TransformGroup(tf1);
pa.setCapability(PolygonAttributes.ALLOW_MODE_WRITE);
// Mengatur mode wireframe (LENGKAPI)
Appearance ap = ball.getAppearance();
ap.setPolygonAttributes(pa);
tg1.addChild(ball);
group.addChild(tg1);
BoundingSphere bounds = new BoundingSphere();
// Menciptakan background putih.
Background bg = new Background(new Color3f(Color.white));
bg.setApplicationBounds(bounds);
group.addChild(bg);
Color3f col = new Color3f(Color.green);
Vector3f vek = new Vector3f(-5.0f, -5.0f, -5.0f);
DirectionalLight light = new DirectionalLight(col, vek);
light.setInfluencingBounds(bounds);
group.addChild(light);
group.compile();
// Menambahkan scene ke universe
univ.addBranchGraph(group);
}
public static void main(String[] args) {
new ProblemWire();
}
}
eksekusi dan menjalankan program:
javac ProblemWire.java
java ProblemWire
Tampilan program ProblemWire.java
Tidak ada komentar:
Posting Komentar