zoukankan      html  css  js  c++  java
  • Flash3D学习计划(三)——学习VB,IB相关,理解三角形顶点顺序;在屏幕上显示2D矩形,并实现缩放,平移,旋转

    VB:顶点缓冲

    IB: 顶点索引缓冲

    三角形的顶点顺序决定了三角形是顺时针还是逆时针,从而决定了三角形在背面剔除的过程中是否会被剔除掉。

    相关理论知识可以在前面的文章中找到更多的说明。

    实现效果

    sfw下载

    主要代码

      1 package
      2 {
      3     import com.adobe.utils.AGALMiniAssembler;
      4     import com.adobe.utils.PerspectiveMatrix3D;
      5     
      6     import flash.display.Sprite;
      7     import flash.display.Stage3D;
      8     import flash.display3D.Context3D;
      9     import flash.display3D.Context3DProgramType;
     10     import flash.display3D.Context3DVertexBufferFormat;
     11     import flash.display3D.IndexBuffer3D;
     12     import flash.display3D.Program3D;
     13     import flash.display3D.VertexBuffer3D;
     14     import flash.events.Event;
     15     import flash.geom.Matrix3D;
     16     import flash.geom.Vector3D;
     17     
     18     /**
     19      * @author smartqi
     20      * @E-mail: 
     21      * 创建时间:2013-6-29 上午9:36:36
     22      * 
     23      */
     24     public class RectangleTest extends Sprite
     25     {
     26         
     27         private var context:Context3D;
     28         private var vertexBuff:VertexBuffer3D;
     29         private var indexBuff:IndexBuffer3D;
     30         private var vertexData:Vector.<Number>;
     31         private var indexData:Vector.<uint>;
     32         private var shaderProgram:Program3D;
     33         private var perspectiveMatrix:PerspectiveMatrix3D;
     34         private var i:int;
     35         private var sign:int = 1;
     36         private const angleGap:Number = 20;
     37         private var angle:Number = 0;
     38         private var modelMatrix:Matrix3D;
     39         private var viewMatrix:Matrix3D;
     40         private var finalMatrix:Matrix3D;
     41         
     42         
     43         public function RectangleTest()
     44         {
     45             var stage3d:Stage3D = stage.stage3Ds[0];
     46             stage3d.addEventListener(Event.CONTEXT3D_CREATE,onContextCreate);
     47             stage3d.requestContext3D();
     48         }
     49         
     50         private function onContextCreate(e:Event):void{
     51             context = (e.target as Stage3D).context3D;
     52             if(context == null) return;
     53             context.enableErrorChecking = true; //允许进行错误检测,release版本应设置
     54             context.configureBackBuffer(500,500,0); //设置显示区域的大小
     55             setupVertexBuff(); //设置顶点缓冲
     56             setupShaderProgram(); //设置shander
     57             setupPerspectiveMatrix(); //设置投影矩阵
     58             initMatrix();
     59             addEventListener(Event.ENTER_FRAME,onEnterFrame);
     60         }
     61         
     62         private function setupVertexBuff():void{
     63             vertexData = Vector.<Number>([
     64                 // x    y     z     r     g     b
     65                 40,        40,        -40,    1,    0,    0,
     66                 40,        -40,    -40,    0,    1,    0,
     67                 -40,    -40,    -40,    0,    0,    1,
     68                 -40,    40,        -40,    1,    1,    1,
     69             ]);
     70             
     71             indexData = Vector.<uint>([0,1,2,0,2,3]);
     72             vertexBuff = context.createVertexBuffer(4,vertexData.length/4);
     73             vertexBuff.uploadFromVector(vertexData,0,4);
     74             indexBuff = context.createIndexBuffer(6);
     75             indexBuff.uploadFromVector(indexData,0,6);
     76             context.setVertexBufferAt(0,vertexBuff,0,Context3DVertexBufferFormat.FLOAT_3);
     77             context.setVertexBufferAt(1,vertexBuff,3,Context3DVertexBufferFormat.FLOAT_3);
     78         }
     79         
     80         private function setupShaderProgram():void{
     81             var vertexProgram:AGALMiniAssembler =  new AGALMiniAssembler();
     82             vertexProgram.assemble(Context3DProgramType.VERTEX,
     83                 "m44 op,va0,vc0
    " +
     84                 "mov v1,va1
    ");
     85             var fragmentProgram:AGALMiniAssembler = new AGALMiniAssembler();
     86             fragmentProgram.assemble(Context3DProgramType.FRAGMENT,
     87                 "mov oc,v1");
     88             shaderProgram = context.createProgram();
     89             shaderProgram.upload(vertexProgram.agalcode,fragmentProgram.agalcode);
     90             context.setProgram(shaderProgram);
     91         }
     92         
     93         private function setupPerspectiveMatrix():void{
     94             perspectiveMatrix = new PerspectiveMatrix3D();
     95             perspectiveMatrix.perspectiveFieldOfViewRH(Math.PI*90/180,1,1,1000);  //注意这里的角度使用的是弧度
     96         }
     97         
     98         private function initMatrix():void{
     99             modelMatrix = new Matrix3D();
    100             viewMatrix = new Matrix3D();
    101             finalMatrix = new Matrix3D();
    102         }
    103         
    104         private function onEnterFrame(e:Event):void{
    105             context.clear(0,0,0);
    106             angle += angleGap;
    107             modelMatrix.identity();
    108             modelMatrix.prependRotation(angle,Vector3D.Z_AXIS);  //绕着Z轴旋转物体,注意这里的角度使用的是角度
    109             if(i>30){
    110                 sign = -1;
    111             }
    112             if(i<0){
    113                 sign = 1;
    114             }
    115             i += sign;
    116             viewMatrix.identity();
    117             viewMatrix.prependTranslation(0,0,-30 + i); //将相机向后移,使物体看起来变小了,将相机向前移,使物体看起来变大
    118             finalMatrix.identity();
    119             finalMatrix.append(modelMatrix);
    120             finalMatrix.append(viewMatrix);
    121             finalMatrix.append(perspectiveMatrix);
    122             context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX,0,finalMatrix,true); //上传最终的顶点转换矩阵,注意这里最后一个参数为true
    123             context.drawTriangles(indexBuff,0,2);
    124             context.present();
    125         }
    126     }
    127 }
  • 相关阅读:
    Mac下java环境jdk、maven环境安装
    Pandas基本操作
    python-numpy入门
    深度学习-Pytorch基础
    深度学习-手写数字识别代码
    机器学习-梯度下降算法案例
    机器学习-EM算法
    机器学习-特征选择
    机器学习-聚类
    机器学习-朴素贝叶斯
  • 原文地址:https://www.cnblogs.com/hisiqi/p/3162151.html
Copyright © 2011-2022 走看看