Guest User

Untitled

a guest
May 24th, 2018
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package;
  2.  
  3. import lime.graphics.opengl.GLBuffer;
  4. import lime.graphics.opengl.GLProgram;
  5. import lime.graphics.opengl.GLShader;
  6. import lime.graphics.GLRenderContext;
  7. import lime.utils.Float32Array;
  8. import lime.utils.GLUtils;
  9. import openfl.display.OpenGLRenderer;
  10. import openfl.display.Sprite;
  11. import openfl.events.Event;
  12. import openfl.events.RenderEvent;
  13.  
  14. class GLSprite extends Sprite {
  15.  
  16.     private var positionAttributeLocation:Int;
  17.     private var positionBuffer:GLBuffer;
  18.     private var glCurrentProgram:GLProgram;
  19.  
  20.     private var vertexShader:GLShader;
  21.     private var fragmentShader:GLShader;
  22.  
  23.     private var inited:Bool = false;
  24.  
  25.     var tri:Float32Array;
  26.  
  27.     var vertexSource = "
  28.         attribute vec2 a_position;
  29.        
  30.         void main (void) {
  31.             gl_Position = vec4 (a_position, 0.0, 1.0);
  32.         }
  33.        
  34.     ";
  35.  
  36.     var fragmentSource =
  37.     #if (!desktop || rpi)
  38.     "precision mediump float;"+
  39.     #end
  40.     "
  41.         void main (void) {
  42.             gl_FragColor = vec4 (1, 0, 0.5, 1);            
  43.         }
  44.     ";
  45.  
  46.     public function new () {
  47.         super ();
  48.         addEventListener (RenderEvent.RENDER_OPENGL, render);
  49.         addEventListener (Event.ENTER_FRAME, enterFrame);
  50.     }
  51.  
  52.     /* INIT */
  53.  
  54.     private function initGL(gl:GLRenderContext) {
  55.         //create shader
  56.         glCurrentProgram = GLUtils.createProgram(vertexSource, fragmentSource);
  57.         gl.useProgram(glCurrentProgram);
  58.  
  59.         positionAttributeLocation = gl.getAttribLocation(glCurrentProgram, "a_position");
  60.  
  61.         //create buffer
  62.         positionBuffer = gl.createBuffer();
  63.  
  64.         gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  65.  
  66.         //tri = new Float32Array([-1.0,-1.0, 1.0,-1.0, 1.0,1.0]);
  67.         tri = new Float32Array([100,100, 200,100, 200,100]);
  68.  
  69.         gl.bufferData(gl.ARRAY_BUFFER, Float32Array.BYTES_PER_ELEMENT*tri.length, tri, gl.STATIC_DRAW);
  70.  
  71.         gl.bindBuffer(gl.ARRAY_BUFFER, null);
  72.     }
  73.  
  74.     /* UPDATE */
  75.  
  76.     public function enterFrame(event:Event):Void {
  77.         invalidate();
  78.     }
  79.  
  80.     public function render(event):Void {
  81.         var renderer:OpenGLRenderer = cast event.renderer;
  82.         var gl:GLRenderContext = renderer.gl;
  83.        
  84.         if (!inited) {
  85.             inited = true;
  86.             initGL(gl);
  87.         }
  88.  
  89.         gl.useProgram(glCurrentProgram);
  90.  
  91.         gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  92.  
  93.         gl.enableVertexAttribArray(positionAttributeLocation);
  94.         gl.vertexAttribPointer(positionAttributeLocation, 2, gl.FLOAT, false, 0, 0);
  95.  
  96.         gl.blendFunc (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  97.         gl.enable (gl.BLEND);
  98.  
  99.         gl.drawArrays(gl.TRIANGLES, 0, 1);
  100.        
  101.         gl.bindBuffer(gl.ARRAY_BUFFER, null);
  102.     }
  103. }
RAW Paste Data Copied