熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java3D中平行光投影的實現

2022-06-13   來源: Java核心技術 

  理論根據:
假設一個光的方向是( 投影到XZ平面

  一個是直線方程一個是平面方程求交
而且平面方程還比較特殊經過原點法向量是
簡化後就簡單了 假定v是直線的方向

  x vertexx       y vertexy        zvertexz
  = =          直線方程
     vx               vy                 vz

  平面方程 y =

  帶入就得到了
x = vertexx + vx / vy * (vertexy)
z = vertexz + vx / vz * (vertexz)

  源程序:
import javaappletApplet;
import javaawtBorderLayout;
import javaawtGraphicsConfiguration;
import javaawtLabel;
import comsunjdutilsappletMainFrame;
import comsunjdutilsuniverse*;
import comsunjdutilsgeometry*;
import diajd*;
import javaxvecmath*;
public class SimpleShadow extends Applet{
 //三角平面類
 public class TriPlane extends ShapeD{
  TriPlane(Pointf APointf BPointf C){
   thissetGeometry(thiscreateGeometry(ABC));
   thissetAppearance(thiscreateAppearance());
  }
  //建立三角平面
  
  Geometry createGeometry(Pointf APointf BPointf C){
   TriangleArray plane=new TriangleArray(GeometryArrayCOORDINATES|GeometryArrayNORMALS);
   //設置平面個頂點的坐標
   planesetCoordinate(A);
   planesetCoordinate(B);
   planesetCoordinate(C);
   
   //計算平面法向量
   Vectorf a=new Vectorf(AxBxAyByAzBz);
   Vectorf b=new Vectorf(CxBxCyByCzBz);
   Vectorf n=new Vectorf();
   ncross(ba);
   //法向量單位化
   nnormalize();
   //設置平面個頂點的法向量
   planesetNormal(n);
   planesetNormal(n);
      planesetNormal(n);
   return plane;
  }
  
  //創建Material不為空的外觀
  
  Appearance createAppearance(){
   Appearance appear=new Appearance();
   Material material=new Material();
   appearsetMaterial(material);
   return appear;
  }
  
 }
 
 
 //四邊平面類
 public class QuadPlane extends ShapeD{
  
  QuadPlane(Pointf APointf BPointf CPointf D){
   thissetGeometry(thiscreateGeometry(ABCD));
   thissetAppearance(thiscreateAppearance());
  }
  
  //創建四邊性平面
  Geometry createGeometry(Pointf APointf BPointf CPointf D){
   QuadArray plane=new QuadArray(GeometryArrayCOORDINATES|GeometryArrayNORMALS);
   //設置平面個頂點的坐標
   planesetCoordinate(A);
   planesetCoordinate(B);
   planesetCoordinate(C);
   planesetCoordinate(D);
   //計算平面法向量
   Vectorf a=new Vectorf(AxBxAyByAzBz);
   Vectorf b=new Vectorf(CxBxCyByCzBz);
   Vectorf n=new Vectorf();
   ncross(ba);
   //法向量單位化
   nnormalize();
   //設置平面個頂點的法向量
   planesetNormal(n);
   planesetNormal(n);
   planesetNormal(n);
   planesetNormal(n);
   return plane;
  }
  
 

  //創建Material不為空的外觀
  
  Appearance createAppearance(){
   Appearance appear=new Appearance();
   Material material=new Material();
   appearsetMaterial(material);
   return appear;
  }
 }
 
 
 //陰影類
 public class ShadowD extends ShapeD{
  ShadowD(GeometryArray geomVectorf directionColorf colfloat height){
   int vCount=geomgetVertexCount();
   TriangleArray poly=new TriangleArray(vCountGeometryArrayCOORDINATES|GeometryArrayCOLOR_);
   int v;
   Pointf vertex=new Pointf();
   Pointf shadow=new Pointf();
   
   for(v=;v<vCount;v++){
    //計算可見面頂點在投影面上的投影坐標
    geomgetCoordinate(vvertex);
    Systemoutprintln(vertexyheight);
          shadowset(vertexx(vertexyheight)height+fvertexz(vertexyheight));
          polysetCoordinate(vshadow);
          polysetColor(vcol);

  }
   thissetGeometry(poly);
   
  }
 }
 
 
 public SimpleShadow(){
  thissetLayout(new BorderLayout());
   //GraphicsConfiguration config =SimpleUniversegetPreferredConfiguration();
  CanvasD c=new CanvasD(null);
  thisadd(Centerc);
  //創建分支節點
  BranchGroup scene=new BranchGroup();
  //創建三角形可見平面
  ShapeD plane=new TriPlane(new Pointf(fff)new Pointf(fff)new Pointf(fff));
  //添加到根分支節點
  sceneaddChild(plane);
  //創建四邊形投影平面
  ShapeD floor=new QuadPlane(new Pointf(fff)new Pointf(fff)new Pointf(fff)new Pointf(fff));
  //添加到根分支節點
  sceneaddChild(floor);
  //添加到環境光
  AmbientLight lightA=new AmbientLight();
  lightAsetInfluencingBounds(new BoundingSphere());
  sceneaddChild(lightA);
  //添加平行光
  DirectionalLight lightD=new DirectionalLight();
  lightDsetInfluencingBounds(new BoundingSphere());
  Vectorf direction=new Vectorf(fff);
  //方向矢量單位化
   directionnormalize();
  lightDsetDirection(direction);
  sceneaddChild(lightD);
  //創建陰影物體
  ShapeD shadow=new ShadowD((GeometryArray)planegetGeometry()directionnew Colorf(fff)f);
  sceneaddChild(shadow);
  SimpleUniverse u=new SimpleUniverse(c);
  ugetViewingPlatform()setNominalViewingTransform();
  uaddBranchGraph(scene);
 }
 
 public static void main(String argv[]){
  new MainFrame(new SimpleShadow());
 }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26962.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.