Last active
August 29, 2015 13:57
-
-
Save joskuijpers/9755025 to your computer and use it in GitHub Desktop.
Pegasus scene node matrix transformations and rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
===== | |
Lib | |
===== | |
function RenderNode() { | |
this.parent = null; | |
this.children = []; | |
this.vertices = []; | |
this.rotation = 0; | |
this.position = {x:0, y:0}; | |
this.scale = {x:0, y:0} | |
this.colorMask = null; | |
this.mask = null; | |
// ... shader, filters... | |
} | |
RenderNode.prototype.addChild(object) { | |
object.parent = this; | |
this.children.push(object); | |
} | |
RenderNode.prototype.getTransformationMatrix() { | |
// Create a matrix using all known properties | |
// How the matrix is represented is platform specific | |
// That is why this script would be in the /system/scripts directory, | |
// delivered with the engine. | |
// One could use 2d object arrays or 1d FloatArray of size m*n. | |
// But use the known properties: rotation, scale, translate, | |
// to make a matrix. | |
// Now I already forgot my linear algebra (I happen to have an exam | |
// of it in a couple of weeks), but there was something difficult | |
// about translations :P. But as its representation of the return value | |
// is internal, it doesn't really matter how it is done. | |
} | |
===== | |
Test | |
===== | |
scene = new Scene(); // is a RenderNode | |
scene.colorMask = new Color(255,0,0); | |
node = new Image('path'); // whatever you want here | |
scene.addChild(node); // add to rootnode | |
textNode = new Text('myFont','Hello World'); | |
textNode.position.x = 10; // 10 offset from parent | |
textNode.scale.y = 2.0; // Do a funny scaling | |
node.addChild(textNode) // add to parent | |
scene.render(); // whatever. Or Renderer.render(scene); (and then Renderer.render(map), i dunnp) | |
====== | |
Native | |
====== | |
DoRender() { | |
RenderNode *rootNode, *node; | |
Queue *renderQueue; | |
rootNode = get root node; | |
renderQueue = new Queue(); | |
node = rootNode | |
while(renderQueue.isEmpty) | |
{ | |
// get transformation matrix, store it privately for use by children | |
node._transformMatrix = node.getTransformationMatrix(); | |
// multiply with the parents matrix | |
if(node.parent) | |
node._transformMatrix = node._transformMatrix * node.parent._transformMatrix; | |
node.render(); // applies matrix to the vertices | |
renderQueue.addAll(node.children) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment