Implementation of parallel optical projection in Java3D

Source: Internet
Author: User
Tags addchild

Implementation of parallel optical projection in Java3D

Jackjoesh Column

Theoretical Basis:
Assume that the direction of a light is (-1,-1,-1) and is projected to the xz plane.

One is a linear equation and the other is a plane equation.
Furthermore, the plane equation is quite special. After the origin, the normal vector is 0 1 0.
It is simplified. Assume that V is the direction of a straight line.

X-vertex. x y-vertex. y z-vertex.z
---------------- = --------------- = -------------- Linear Equation
V. X v. Y v. Z

Plane equation Y = 0

Bring in.
X = vertex. x + v. X/V. y * (-vertex. Y)
Z = vertex. Z + v. X/V. z * (-vertex. Z)

Source program:
Import java. Applet. Applet;
Import java. AWT. borderlayout;
Import java. AWT. graphicsconfiguration;
Import java. AWT. label;
Import com. Sun. j3d. utils. Applet. Mainframe;
Import com. Sun. j3d. utils. Universe .*;
Import com. Sun. j3d. utils. Geometry .*;
Import javax. Media. j3d .*;
Import javax. vecmath .*;
Public class simpleshadow extends applet {
// Triangular plane
Public class triplane extends shape3d {
Triplane (point3f A, point3f B, point3f c ){
This. setgeometry (this. creategeometry3 (a, B, c ));
This. setappearance (this. createappearance ());
}
// Create a Triangle Plane

Geometry creategeometry3 (point3f A, point3f B, point3f c ){
Trianglearray plane = new trianglearray (3, geometryarray. Coordinates | geometryarray. normals );
// Set the coordinates of the three vertices in the plane
Plane. setcoordinate (0, );
Plane. setcoordinate (1, B );
Plane. setcoordinate (2, c );

// Calculate the flat Vector
Vector3f A = new vector3f (A. x-B.x, A. y-B.y, A. z-B.z );
Vector3f B = new vector3f (C. x-B.x, C. y-B.y, C. z-B.z );
Vector3f n = new vector3f ();
N. Cross (B, );
// Method vector unitization
N. normalize ();
// Set the normal vector of three vertices in the plane
Plane. setnormal (0, N );
Plane. setnormal (1, N );
Plane. setnormal (2, N );
Return plane;
}

// Create a material appearance that is not empty

Appearance createappearance (){
Appearance appear = new appearance ();
Material material = new material ();
Appear. setmaterial (material );
Return appear;
}

}
 
 
// Four-sided plane
Public class quadplane extends shape3d {

Quadplane (point3f A, point3f B, point3f C, point3f d ){
This. setgeometry (this. creategeometry4 (A, B, C, D ));
This. setappearance (this. createappearance ());
}

// Create a four-sided plane
Geometry creategeometry4 (point3f A, point3f B, point3f C, point3f d ){
Quadarray plane = new quadarray (4, geometryarray. Coordinates | geometryarray. normals );
// Set the coordinates of the three vertices in the plane
Plane. setcoordinate (0, );
Plane. setcoordinate (1, B );
Plane. setcoordinate (2, c );
Plane. setcoordinate (3, D );
// Calculate the flat Vector
Vector3f A = new vector3f (A. x-B.x, A. y-B.y, A. z-B.z );
Vector3f B = new vector3f (C. x-B.x, C. y-B.y, C. z-B.z );
Vector3f n = new vector3f ();
N. Cross (B, );
// Method vector unitization
N. normalize ();
// Set the normal vector of four vertices in the plane
Plane. setnormal (0, N );
Plane. setnormal (1, N );
Plane. setnormal (2, N );
Plane. setnormal (3, N );
Return plane;
}

// Create a material appearance that is not empty

Appearance createappearance (){
Appearance appear = new appearance ();
Material material = new material ();
Appear. setmaterial (material );
Return appear;
}
}
 
 
// Shadow class
Public class shadow3d extends shape3d {
Shadow3d (geometryarray Geom, vector3f ction, color3f Col, float height ){
Int vcount = Geom. getvertexcount ();
Trianglearray poly = new trianglearray (vcount, geometryarray. Coordinates | geometryarray. color_3 );
Int V;
Point3f vertex = new point3f ();
Point3f shadow = new point3f ();

For (V = 0; v <vcount; V ++ ){
// Calculate the projection coordinate of the Meeting Vertex on the projection plane
Geom. getcoordinate (v, vertex );
System. Out. println (vertex. Y-height );
Shadow. Set (vertex. X-(vertex. Y-height), height + 0.0001f, vertex. Z-(vertex. Y-height ));
Poly. setcoordinate (v, shadow );
Poly. setcolor (v, col );

}
This. setgeometry (poly );

}
}
 
 
Public simpleshadow (){
This. setlayout (New borderlayout ());
// Graphicsconfiguration Config = simpleuniverse. getpreferredconfiguration ();
Canvas3d c = new canvas3d (null );
This. Add ("center", C );
// Create a branch node
Branchgroup scene = new branchgroup ();
// Create a triangle visible plane
Shape3d plane = new triplane (New point3f (0.0f, 0.6f,-0.2f), new point3f (-0.3f,-0.3f, 0.2f), new point3f (0.6f,-0.3f, 0.2f ));
// Add to the root branch node
Scene. addchild (plane );
// Create a quadrilateral projection plane
Shape3d floor = new quadplane (New point3f (-1.0f,-0.5f,-1.0f), new point3f (-1.0f,-0.5f, 1.0f), new point3f (1.0f,-0.5f, 1.0f), new point3f (1.0f,-0.5f,-1.0f ));
// Add to the root branch node
Scene. addchild (floor );
// Add to ambient light
Ambientlight lighta = new ambientlight ();
Lighta. setinfluencingbounds (New boundingsphere ());
Scene. addchild (lighta );
// Add parallel light
Directionallight lightd1 = new directionallight ();
Lightd1.setinfluencingbounds (New boundingsphere ());
Vector3f direction = new vector3f (-1.0f,-1.0f,-1.0f );
// Vector unit Orientation
Direction. normalize ();
Lightd1.setdirection (Direction );
Scene. addchild (lightd1 );
// Create a shadow object
Shape3d shadow = new shadow3d (geometryarray) plane. getgeometry (), direction, new color3f (0.2f, 0.2f, 0.2f),-0.5f );
Scene. addchild (Shadow );
Simpleuniverse u = new simpleuniverse (C );
U. getviewingplatform (). setnominalviewingtransform ();
U. addbranchgraph (scene );
}
 
Public static void main (string argv []) {
New mainframe (New simpleshadow (), 256,256 );
}

}

Details:

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 627858

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.