Init mouse events data on first render

This commit is contained in:
Yaski
2012-05-23 15:42:32 +06:00
parent 100bc66453
commit a5b98b0b6c

View File

@@ -63,7 +63,7 @@ package alternativa.engine3d.core {
private static var properties:Dictionary = new Dictionary(true); private static var properties:Dictionary = new Dictionary(true);
private var cachedContext3D:Context3D; private var cachedContext3D:Context3D;
private var context3DViewProperties:Context3DViewProperties; private var context3DProperties:Context3DViewProperties;
static private var drawDistanceFragment:Linker; static private var drawDistanceFragment:Linker;
static private var drawDistanceVertexProcedure:Procedure; static private var drawDistanceVertexProcedure:Procedure;
@@ -108,12 +108,7 @@ package alternativa.engine3d.core {
* @private * @private
*/ */
alternativa3d var _height:int; alternativa3d var _height:int;
private var backBufferContext3D:Context3D;
private var backBufferWidth:int = -1;
private var backBufferHeight:int = -1;
private var backBufferAntiAlias:int = -1;
/** /**
* @private * @private
*/ */
@@ -495,11 +490,47 @@ package alternativa.engine3d.core {
createRenderBitmap(); createRenderBitmap();
} }
} }
if (_width != backBufferWidth || _height != backBufferHeight || antiAlias != backBufferAntiAlias || context != backBufferContext3D) { if (context != cachedContext3D) {
backBufferWidth = _width; // Get properties.
backBufferHeight = _height; cachedContext3D = context;
backBufferAntiAlias = antiAlias; context3DProperties = properties[cachedContext3D];
backBufferContext3D = context; if (context3DProperties == null) {
context3DProperties = new Context3DViewProperties();
// Inititalize data for mouse events
var rectGeometry:Geometry = new Geometry(4);
rectGeometry.addVertexStream([VertexAttributes.POSITION, VertexAttributes.POSITION, VertexAttributes.POSITION, VertexAttributes.TEXCOORDS[0], VertexAttributes.TEXCOORDS[0]]);
rectGeometry.setAttributeValues(VertexAttributes.POSITION, Vector.<Number>([0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1]));
rectGeometry.setAttributeValues(VertexAttributes.TEXCOORDS[0], Vector.<Number>([0, 0, 0, 1, 1, 1, 1, 0]));
rectGeometry.indices = Vector.<uint>([0, 1, 3, 2, 3, 1]);
rectGeometry.upload(context);
var vLinker:Linker = new Linker(Context3DProgramType.VERTEX);
vLinker.addProcedure(Procedure.compileFromArray([
"#a0=a0",
"#c0=c0",
"mul t0.x, a0.x, c0.x",
"mul t0.y, a0.y, c0.y",
"add o0.x, t0.x, c0.z",
"add o0.y, t0.y, c0.w",
"mov o0.z, a0.z",
"mov o0.w, a0.z",
]));
var fLinker:Linker = new Linker(Context3DProgramType.FRAGMENT);
fLinker.addProcedure(Procedure.compileFromArray([
"#c0=c0",
"mov o0, c0",
]));
var coloredRectProgram:ShaderProgram = new ShaderProgram(vLinker, fLinker);
coloredRectProgram.upload(context);
context3DProperties.drawRectGeometry = rectGeometry;
context3DProperties.drawColoredRectProgram = coloredRectProgram;
properties[cachedContext3D] = context3DProperties;
}
}
if (_width != context3DProperties.backBufferWidth || _height != context3DProperties.backBufferHeight || antiAlias != context3DProperties.backBufferAntiAlias) {
context3DProperties.backBufferWidth = _width;
context3DProperties.backBufferHeight = _height;
context3DProperties.backBufferAntiAlias = antiAlias;
context.configureBackBuffer(_width, _height, antiAlias); context.configureBackBuffer(_width, _height, antiAlias);
} }
var r:Number = ((backgroundColor >> 16) & 0xff)/0xff; var r:Number = ((backgroundColor >> 16) & 0xff)/0xff;
@@ -647,45 +678,8 @@ package alternativa.engine3d.core {
context.setVertexBufferAt(6, null); context.setVertexBufferAt(6, null);
context.setVertexBufferAt(7, null); context.setVertexBufferAt(7, null);
if (context != cachedContext3D) { var drawRectGeometry:Geometry = context3DProperties.drawRectGeometry;
// Get properties. var drawColoredRectProgram:ShaderProgram = context3DProperties.drawColoredRectProgram;
cachedContext3D = context;
context3DViewProperties = properties[cachedContext3D];
if (context3DViewProperties == null) {
// TODO: create programs on first render
context3DViewProperties = new Context3DViewProperties();
var rectGeometry:Geometry = new Geometry(4);
rectGeometry.addVertexStream([VertexAttributes.POSITION, VertexAttributes.POSITION, VertexAttributes.POSITION, VertexAttributes.TEXCOORDS[0], VertexAttributes.TEXCOORDS[0]]);
rectGeometry.setAttributeValues(VertexAttributes.POSITION, Vector.<Number>([0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1]));
rectGeometry.setAttributeValues(VertexAttributes.TEXCOORDS[0], Vector.<Number>([0, 0, 0, 1, 1, 1, 1, 0]));
rectGeometry.indices = Vector.<uint>([0, 1, 3, 2, 3, 1]);
rectGeometry.upload(context);
vLinker = new Linker(Context3DProgramType.VERTEX);
vLinker.addProcedure(Procedure.compileFromArray([
"#a0=a0",
"#c0=c0",
"mul t0.x, a0.x, c0.x",
"mul t0.y, a0.y, c0.y",
"add o0.x, t0.x, c0.z",
"add o0.y, t0.y, c0.w",
"mov o0.z, a0.z",
"mov o0.w, a0.z",
]));
fLinker = new Linker(Context3DProgramType.FRAGMENT);
fLinker.addProcedure(Procedure.compileFromArray([
"#c0=c0",
"mov o0, c0",
]));
var coloredRectProgram:ShaderProgram = new ShaderProgram(vLinker, fLinker);
coloredRectProgram.upload(context);
context3DViewProperties.drawRectGeometry = rectGeometry;
context3DViewProperties.drawColoredRectProgram = coloredRectProgram;
properties[cachedContext3D] = context3DViewProperties;
}
}
var drawRectGeometry:Geometry = context3DViewProperties.drawRectGeometry;
var drawColoredRectProgram:ShaderProgram = context3DViewProperties.drawColoredRectProgram;
// Rectangle // Rectangle
var vLinker:Linker, fLinker:Linker; var vLinker:Linker, fLinker:Linker;
@@ -791,7 +785,7 @@ package alternativa.engine3d.core {
var procedure:Procedure = procedures[index]; var procedure:Procedure = procedures[index];
var object:Object3D = surface.object; var object:Object3D = surface.object;
// Program // Program
var drawDistanceProgram:ShaderProgram = context3DViewProperties.drawDistancePrograms[procedure]; var drawDistanceProgram:ShaderProgram = context3DProperties.drawDistancePrograms[procedure];
if (drawDistanceProgram == null) { if (drawDistanceProgram == null) {
// Assembling the vertex shader // Assembling the vertex shader
var vertex:Linker = new Linker(Context3DProgramType.VERTEX); var vertex:Linker = new Linker(Context3DProgramType.VERTEX);
@@ -810,7 +804,7 @@ package alternativa.engine3d.core {
drawDistanceProgram = new ShaderProgram(vertex, drawDistanceFragment); drawDistanceProgram = new ShaderProgram(vertex, drawDistanceFragment);
drawDistanceProgram.fragmentShader.varyings = drawDistanceProgram.vertexShader.varyings; drawDistanceProgram.fragmentShader.varyings = drawDistanceProgram.vertexShader.varyings;
drawDistanceProgram.upload(context); drawDistanceProgram.upload(context);
context3DViewProperties.drawDistancePrograms[procedure] = drawDistanceProgram; context3DProperties.drawDistancePrograms[procedure] = drawDistanceProgram;
} }
var buffer:VertexBuffer3D = geometry.getVertexBuffer(VertexAttributes.POSITION); var buffer:VertexBuffer3D = geometry.getVertexBuffer(VertexAttributes.POSITION);
if (buffer == null) return; if (buffer == null) return;
@@ -1423,6 +1417,12 @@ class Logo extends Sprite {
} }
class Context3DViewProperties { class Context3DViewProperties {
public var backBufferWidth:int = -1;
public var backBufferHeight:int = -1;
public var backBufferAntiAlias:int = -1;
// Mouse events
// Key - vertex program of object, value - program. // Key - vertex program of object, value - program.
public var drawDistancePrograms:Dictionary = new Dictionary(); public var drawDistancePrograms:Dictionary = new Dictionary();
public var drawColoredRectProgram:ShaderProgram; public var drawColoredRectProgram:ShaderProgram;