Skip to content

Instantly share code, notes, and snippets.

@jgranick

jgranick/Main.hx

Last active May 10, 2017
Embed
What would you like to do?
Native Render
package;
import lime.app.Application;
import lime.graphics.cairo.*;
import lime.graphics.opengl.*;
import lime.graphics.Image;
import lime.graphics.Renderer;
import lime.math.Matrix4;
import lime.utils.Float32Array;
import lime.utils.GLUtils;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.NativeSprite;
import openfl.display.Sprite;
import openfl.events.NativeRenderEvent;
import openfl.Assets;
class Main extends Sprite {
private var bitmapData:BitmapData;
private var cairoSurface:CairoSurface;
private var glBuffer:GLBuffer;
private var glMatrixUniform:GLUniformLocation;
private var glProgram:GLProgram;
private var glTexture:GLTexture;
private var glTextureAttribute:Int;
private var glVertexAttribute:Int;
private var nativeSprite:NativeSprite;
public function new () {
super ();
nativeSprite = new NativeSprite ();
nativeSprite.addEventListener (NativeRenderEvent.NATIVE_RENDER, render);
addChild (nativeSprite);
nativeSprite.x = 200;
nativeSprite.rotation = 20;
}
private function render (event:NativeRenderEvent):Void {
if (bitmapData == null) {
bitmapData = Assets.getBitmapData ("assets/lime.png");
switch (nativeSprite.context) {
case CAIRO (cairo):
cairoSurface = CairoImageSurface.fromImage (bitmapData.image);
case DOM (div):
div.appendChild (bitmapData.image.src);
case FLASH (sprite):
var bitmap = new Bitmap (bitmapData);
sprite.addChild (bitmap);
case OPENGL (gl):
var gl:WebGLContext = gl;
var vertexSource =
"attribute vec4 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
uniform mat4 uMatrix;
void main(void) {
vTexCoord = aTexCoord;
gl_Position = uMatrix * aPosition;
}";
var fragmentSource =
#if !desktop
"precision mediump float;" +
#end
"varying vec2 vTexCoord;
uniform sampler2D uImage0;
void main(void)
{
gl_FragColor = texture2D (uImage0, vTexCoord);
}";
glProgram = GLUtils.createProgram (vertexSource, fragmentSource);
gl.useProgram (glProgram);
glVertexAttribute = gl.getAttribLocation (glProgram, "aPosition");
glTextureAttribute = gl.getAttribLocation (glProgram, "aTexCoord");
glMatrixUniform = gl.getUniformLocation (glProgram, "uMatrix");
var imageUniform = gl.getUniformLocation (glProgram, "uImage0");
gl.enableVertexAttribArray (glVertexAttribute);
gl.enableVertexAttribArray (glTextureAttribute);
gl.uniform1i (imageUniform, 0);
gl.blendFunc (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enable (gl.BLEND);
var data = [
bitmapData.width, bitmapData.height, 0, 1, 1,
0, bitmapData.height, 0, 0, 1,
bitmapData.width, 0, 0, 1, 0,
0, 0, 0, 0, 0
];
glBuffer = gl.createBuffer ();
gl.bindBuffer (gl.ARRAY_BUFFER, glBuffer);
gl.bufferData (gl.ARRAY_BUFFER, new Float32Array (data), gl.STATIC_DRAW);
gl.bindBuffer (gl.ARRAY_BUFFER, null);
glTexture = gl.createTexture ();
gl.bindTexture (gl.TEXTURE_2D, glTexture);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
#if js
gl.texImage2D (gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmapData.image.src);
#else
gl.texImage2D (gl.TEXTURE_2D, 0, gl.RGBA, bitmapData.image.buffer.width, bitmapData.image.buffer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, bitmapData.image.data);
#end
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.bindTexture (gl.TEXTURE_2D, null);
default:
}
}
switch (nativeSprite.context) {
case CAIRO (cairo):
cairo.setSourceSurface (cairoSurface, 0, 0);
cairo.paint ();
case CANVAS (context):
context.drawImage (bitmapData.image.src, 0, 0, bitmapData.width, bitmapData.height);
case OPENGL (gl):
var gl:WebGLContext = gl;
//gl.viewport (0, 0, stage.stageWidth, stage.stageHeight);
if (bitmapData != null) {
var matrix = Matrix4.createOrtho (0, stage.stageWidth, stage.stageHeight, 0, -1000, 1000);
gl.uniformMatrix4fv (glMatrixUniform, false, matrix);
gl.activeTexture (gl.TEXTURE0);
gl.bindTexture (gl.TEXTURE_2D, glTexture);
#if desktop
gl.enable (gl.TEXTURE_2D);
#end
gl.bindBuffer (gl.ARRAY_BUFFER, glBuffer);
gl.vertexAttribPointer (glVertexAttribute, 3, gl.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 0);
gl.vertexAttribPointer (glTextureAttribute, 2, gl.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 3 * Float32Array.BYTES_PER_ELEMENT);
gl.drawArrays (gl.TRIANGLE_STRIP, 0, 4);
}
default:
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment