Advertisement
Guest User

Untitled

a guest
Mar 10th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 7.51 KB | None | 0 0
  1. package;
  2. import kha.*;
  3. import kha.graphics4.*;
  4. import kha.math.*;
  5. import kha.input.*;
  6. import khaMath.Matrix4;
  7. import trilateral.tri.Triangle;
  8. import trilateral.tri.TriangleArray;
  9. import testTrilateral.TrilateralTest;
  10.  
  11. class Main {
  12.     public static function main() {
  13.         System.init({ title: "TestKha4", width: 1024, height: 1024, samplesPerPixel: 4 }, function() { new Main(); } );
  14.     }
  15.  
  16.     var pixelLayer:         Image;
  17.     var vectorLayer:        Image;
  18.     var initialized:        Bool = false;
  19.     var xPos:               Float;
  20.     var yPos:               Float;
  21.     var keys:               Array<Bool> = [for(i in 0...4) false];
  22.     var trilateralTest:     TrilateralTest;
  23.     var modelViewProjection:Matrix4;
  24.  
  25.     var pipeline:       PipelineState;
  26.     var vertexBuffer:   VertexBuffer;
  27.     var indexBuffer:    IndexBuffer;
  28.     var mvp:            FastMatrix4;
  29.     var mvpID:          ConstantLocation;
  30.     var z:              Float = -1;
  31.     var structureLength = 6;
  32.     var scale:          Float;
  33.     var stageRadius:    Float;
  34.     var timeSlice: Float = 0;
  35.     var r = 0.0;
  36.     var ibi = 0;
  37.  
  38.     public function new() {
  39.         Assets.loadEverything(loadingFinished);
  40.     }
  41.  
  42.     function loadingFinished() {
  43.         stageRadius = 570;
  44.         scale = 1/(stageRadius);
  45.         trilateralTest =  new TrilateralTest(stageRadius, setMatrix, function() {});
  46.         trilateralTest.setup();
  47.  
  48.         setup3d();
  49.  
  50.         Keyboard.get().notify(keyDown, keyUp);
  51.         Mouse.get().notify(null, null, mouseMove, null);
  52.         System.notifyOnRender(render);
  53.         Scheduler.addTimeTask(update, 0, 1 / 60);
  54.     }
  55.  
  56.     function setup3d() {
  57.         // Define vertex structure
  58.         var structure = new VertexStructure();
  59.         structure.add( "pos", VertexData.Float3 );
  60.         structure.add( "col", VertexData.Float3 );
  61.         // Save length - we store position and color data
  62.  
  63.  
  64.         // Compile pipeline state
  65.         // Shaders are located in 'Sources/Shaders' directory
  66.         // and Kha includes them automatically
  67.         pipeline = new PipelineState();
  68.         pipeline.inputLayout = [structure];
  69.         pipeline.fragmentShader = Shaders.simple_frag;
  70.         pipeline.vertexShader = Shaders.simple_vert;
  71.         // Set depth mode
  72.         pipeline.depthWrite = false;
  73.         pipeline.depthMode = CompareMode.Less;
  74.         pipeline.compile();
  75.  
  76.         // Get a handle for our "MVP" uniform
  77.         mvpID = pipeline.getConstantLocation("MVP");
  78.  
  79.         updateMvp();
  80.  
  81.         // vertexBufferLen = Std.int(vertices.length / 3)
  82.         // Create vertex buffer
  83.         vertexBuffer = new VertexBuffer(
  84.             trilateralTest.triangles.length * 3, // Vertex count - 3 floats per vertex
  85.             structure, // Vertex structure
  86.             Usage.DynamicUsage // Vertex data will stay the same
  87.         );
  88.         // indicesLen = indices.length;
  89.         // Create index buffer
  90.         indexBuffer = new IndexBuffer(
  91.             trilateralTest.triangles.length * 3, // Number of indices for our cube
  92.             Usage.StaticUsage // Index data will stay the same
  93.         );
  94.  
  95.         var iData = indexBuffer.lock();
  96.         for (i in 0...100000) {
  97.             iData[i] = i;
  98.         }
  99.         indexBuffer.unlock();
  100.  
  101.         var s = scale*5.5;//4;
  102.         var offX = 8.0;
  103.         var offY = 5.0;
  104.  
  105.         var triangles = trilateralTest.triangles;
  106.         var gameColors = trilateralTest.appColors;
  107.         var vbData = vertexBuffer.lock();
  108.  
  109.         //var adjScale: Float = 87.1;//200
  110.         for( t in 0...triangles.length ){
  111.             var tri = triangles[ t ];
  112.             var i = t * 18;
  113.             var col = gameColors[tri.colorID];
  114.             var r = _r(col);
  115.             var g = _g(col);
  116.             var b = _b(col);
  117.             vbData[  i] = s * tri.ax - offX;
  118.             vbData[++i] = s * tri.ay - offY;
  119.             vbData[++i] = -z;
  120.             vbData[++i] = r;
  121.             vbData[++i] = g;
  122.             vbData[++i] = b;
  123.             vbData[++i] = s * tri.bx - offX;
  124.             vbData[++i] = s * tri.by - offY;
  125.             vbData[++i] = -z;
  126.             vbData[++i] = r;
  127.             vbData[++i] = g;
  128.             vbData[++i] = b;
  129.             vbData[++i] = s * tri.cx - offX;
  130.             vbData[++i] = s * tri.cy - offY;
  131.             vbData[++i]= -z;
  132.             vbData[++i] = r;
  133.             vbData[++i] = g;
  134.             vbData[++i] = b;
  135.         }
  136.  
  137.         vertexBuffer.unlock();
  138.     }
  139.  
  140.     public inline function setMatrix( matrix4: Matrix4 ) {
  141.         modelViewProjection = matrix4;
  142.     }
  143.  
  144.     public function keyDown(keyCode:Int) {
  145.         switch(keyCode){
  146.             case KeyCode.Left:
  147.                 keys[0] = true;
  148.             case KeyCode.Right:
  149.                 keys[1] = true;
  150.             case KeyCode.Up:
  151.                 keys[2] = true;
  152.             case KeyCode.Down:
  153.                 keys[3] = true;
  154.             default:
  155.         }
  156.     }
  157.  
  158.     public function keyUp(keyCode:Int) {
  159.         switch(keyCode){
  160.             case KeyCode.Left:
  161.                 keys[0] = false;
  162.             case KeyCode.Right:
  163.                 keys[1] = false;
  164.             case KeyCode.Up:
  165.                 keys[2] = false;
  166.             case KeyCode.Down:
  167.                 keys[3] = false;
  168.             default:
  169.         }
  170.     }
  171.  
  172.     function mouseMove(x:Int, y:Int, movementX:Int, movementY:Int) {
  173.         r += movementX < 0 ? -0.01 : 0.01;
  174.         //xPos = x - (ball.width / 2);
  175.        // yPos = y - (ball.height / 2);
  176.  
  177.         updateMvp();
  178.     }
  179.  
  180.     function updateMvp() {
  181.         var aspect = kha.System.windowWidth() / kha.System.windowHeight();
  182.         var projection = FastMatrix4.perspectiveProjection(Math.PI / 4, aspect, 0.1, 1000.0);
  183.  
  184.         var view = FastMatrix4.lookAt(new FastVector3(0, 0, 10), // Camera is at (4, 3, 3), in World Space
  185.                                   new FastVector3(0, 0, 0), // and looks at the origin
  186.                                   new FastVector3(0, 1, 0) // Head is up (set to (0, -1, 0) to look upside-down)
  187.         );
  188.  
  189.         // Model matrix: an identity matrix (model will be at the origin)
  190.         var model = FastMatrix4.rotationZ(r).multmat(FastMatrix4.translation(4, 0, 0));
  191.         // Our ModelViewProjection: multiplication of our 3 matrices
  192.         // Remember, matrix multiplication is the other way around
  193.         mvp = FastMatrix4.identity();
  194.         mvp = mvp.multmat(projection);
  195.         mvp = mvp.multmat(view);
  196.         mvp = mvp.multmat(model);
  197.     }
  198.  
  199.     public function update(): Void {
  200.         if (keys[0])
  201.             xPos -= 3;
  202.         else if (keys[1])
  203.             xPos += 3;
  204.         if (keys[2])
  205.             yPos -= 3;
  206.         else if (keys[3])
  207.             yPos += 3;
  208.     }
  209.  
  210.     public static inline function _r( int: Int ) : Float
  211.         return ((int >> 16) & 255) / 255;
  212.  
  213.     public static inline function _g( int: Int ) : Float
  214.         return ((int >> 8) & 255) / 255;
  215.  
  216.     public static inline function _b( int: Int ) : Float
  217.         return (int & 255) / 255;
  218.  
  219.     public function render(framebuffer:Framebuffer) {
  220.         var w = System.windowWidth() / 2;
  221.         var h = System.windowHeight() / 2;
  222.         var g = framebuffer.g2;
  223.         var g4 = framebuffer.g4;
  224.         g4.begin();
  225.         g4.clear(Color.fromValue(0xff000000));
  226.         g4.setVertexBuffer(vertexBuffer);
  227.         g4.setIndexBuffer(indexBuffer);
  228.         g4.setPipeline(pipeline);
  229.         g4.setMatrix(mvpID, mvp);
  230.         g4.drawIndexedVertices(0, trilateralTest.triangles.length * 3);
  231.         g4.end();
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement