Permalink
Browse files

FIXED Got multiple textures rendered with one texture atlas

  • Loading branch information...
1 parent b583da3 commit 4830850f4216d495453202a82d7c43e13e8eeb9e @potmo committed Mar 24, 2012
@@ -70,7 +70,7 @@ package com.potmo.p2d.atlas
public function handleContextCreated( context:Context3D ):void
{
- createVertices( context, "center", _sizes, _offsets, _frames, _textureBitmaps, _textureFrameOffsets );
+ createVertices( context, "lefttop", _sizes, _offsets, _frames, _textureBitmaps, _textureFrameOffsets );
uploadTextures( context );
@@ -94,21 +94,37 @@ package com.potmo.p2d.atlas
private function uploadTexture( context:Context3D, textureBitmap:BitmapData ):Texture
{
- var texture:Texture = context.createTexture( textureBitmap.width, textureBitmap.height, Context3DTextureFormat.BGRA, false );
+ var textureWidth:int = getNextPowerOfTwo( textureBitmap.width );
+ var textureHeight:int = getNextPowerOfTwo( textureBitmap.height );
+ var texture:Texture = context.createTexture( textureWidth, textureHeight, Context3DTextureFormat.BGRA, false );
//_texture.uploadFromBitmapData( textureBitmap, 0 );
- uploadBitmapData( texture, textureBitmap, false );
+ uploadBitmapData( texture, textureBitmap, textureWidth, textureHeight, false );
return texture;
}
- private function uploadBitmapData( texture:Texture, bitmapData:BitmapData, generateMipmaps:Boolean ):void
+ private function getNextPowerOfTwo( n:Number ):Number
+ {
+ var out:Number = 2.0;
+
+ while ( out < n )
+ {
+ // pow 2
+ out <<= 1;
+ }
+
+ return out;
+ }
+
+
+ private function uploadBitmapData( texture:Texture, bitmapData:BitmapData, textureWidth:Number, textureHeight:Number, generateMipmaps:Boolean ):void
{
texture.uploadFromBitmapData( bitmapData );
if ( generateMipmaps )
{
- var currentWidth:int = bitmapData.width >> 1;
- var currentHeight:int = bitmapData.height >> 1;
+ var currentWidth:int = textureWidth >> 1;
+ var currentHeight:int = textureHeight >> 1;
var level:int = 1;
var canvas:BitmapData = new BitmapData( currentWidth, currentHeight, true, 0 );
var transform:Matrix = new Matrix( .5, 0, 0, .5 );
@@ -136,7 +152,7 @@ package com.potmo.p2d.atlas
// get the frame offset and the next frame offset
var currentTexture:int = -1;
- var currentTextureLastFrame:uint = 0;
+ var currentTextureLastFrame:int = -1;
var currentTextureWidth:uint;
var currentTextureHeight:uint;
@@ -151,11 +167,11 @@ package com.potmo.p2d.atlas
{
// swap to the next texture
- if ( c == currentTextureLastFrame )
+ if ( c >= currentTextureLastFrame )
{
currentTexture++;
- if ( currentTexture < textureFrameOffsets.length - 1 )
+ if ( currentTexture + 1 < textureFrameOffsets.length )
{
currentTextureLastFrame = textureFrameOffsets[ currentTexture + 1 ];
}
@@ -164,8 +180,8 @@ package com.potmo.p2d.atlas
currentTextureLastFrame = numFrames;
}
- currentTextureWidth = textureBitmaps[ currentTexture ].width;
- currentTextureHeight = textureBitmaps[ currentTexture ].height;
+ currentTextureWidth = getNextPowerOfTwo( textureBitmaps[ currentTexture ].width );
+ currentTextureHeight = getNextPowerOfTwo( textureBitmaps[ currentTexture ].height );
}
var x:Number = frames[ c ].x;
@@ -275,8 +291,6 @@ package com.potmo.p2d.atlas
vd[ v++ ] = currentTexture == 0 ? 1 : 0; //w
vd[ v++ ] = currentTexture == 1 ? 1 : 0; //w
- // texture mask (1 * FLOAT_2)
-
// indices (two triangles times 3 vertices)
id[ i++ ] = ( c * 4 + 0 );
id[ i++ ] = ( c * 4 + 1 );
@@ -7,17 +7,15 @@ package com.potmo.p2d.atlas.animation
private var _sequences:Vector.<P2DSpriteAtlasSequence>;
private var _sequenceCount:uint;
- private var _atlasId:int;
- public function P2DSpriteAtlas( atlasId:int, names:Vector.<String>, frameSizes:Vector.<Point> )
+ public function P2DSpriteAtlas( names:Vector.<String>, frameSizes:Vector.<Point> )
{
_sequences = new Vector.<P2DSpriteAtlasSequence>();
_sequenceCount = 0;
- createSequences( atlasId, names, frameSizes );
- _atlasId = atlasId;
+ createSequences( names, frameSizes );
}
@@ -32,7 +30,7 @@ package com.potmo.p2d.atlas.animation
if ( !sequence )
{
- throw new Error( "No sequence founds called: " + name + " in atlasId: " + _atlasId );
+ throw new Error( "No sequence founds called: " + name );
}
return sequence;
@@ -56,7 +54,7 @@ package com.potmo.p2d.atlas.animation
}
- private function createSequences( atlasId:uint, names:Vector.<String>, frameSizes:Vector.<Point> ):void
+ private function createSequences( names:Vector.<String>, frameSizes:Vector.<Point> ):void
{
var length:int = names.length;
@@ -96,7 +94,7 @@ package com.potmo.p2d.atlas.animation
if ( !sequence )
{
- sequence = createSequence( atlasId, sequenceName );
+ sequence = createSequence( sequenceName );
}
var frameSize:Point = frameSizes[ i ];
@@ -106,9 +104,9 @@ package com.potmo.p2d.atlas.animation
}
- private function createSequence( atlas:int, name:String ):P2DSpriteAtlasSequence
+ private function createSequence( name:String ):P2DSpriteAtlasSequence
{
- var sequence:P2DSpriteAtlasSequence = new P2DSpriteAtlasSequence( atlas, name );
+ var sequence:P2DSpriteAtlasSequence = new P2DSpriteAtlasSequence( name );
_sequences.push( sequence );
_sequenceCount++;
return sequence;
@@ -12,17 +12,15 @@ package com.potmo.p2d.atlas.animation
public class P2DSpriteAtlasSequence implements SpriteAtlasSequence
{
private var _name:String;
- private var _atlasId:int;
private var _sequenceFrameCount:int;
private var _labels:Vector.<String>;
private var _atlasFrames:Vector.<uint>;
private var _sequenceOffsetInAtlas:uint;
private var _frameSize:Vector.<Point>;
- public function P2DSpriteAtlasSequence( atlas:int, name:String )
+ public function P2DSpriteAtlasSequence( name:String )
{
- _atlasId = atlas;
_name = name;
_labels = new Vector.<String>();
_atlasFrames = new Vector.<uint>();
@@ -105,11 +103,5 @@ package com.potmo.p2d.atlas.animation
return _frameSize[ frame ];
}
-
- public function getAtlasId():uint
- {
- return _atlasId;
- }
-
}
}
@@ -9,7 +9,6 @@ package com.potmo.p2d.atlas.animation
function getFrameCount():int;
function getName():String;
function getNextFrame( currentFrame:uint, loop:Boolean, followLabelPointers:Boolean ):uint;
- function getAtlasId():uint;
function getSizeOfFrame( frame:uint ):Point;
}
}
@@ -29,10 +29,11 @@ package com.potmo.p2d.renderer
* vertex shader to batch from multiple textures
*/
private static const VERTEX_SHADER_2_TEXTURE_BATCH:Vector.<String> = new <String>[ "mov vt0.xyzw, va0.xyww\n", // copy two first floats (setVertexBufferAt(0...)
- "m33 vt0.xyz, vt0, vc0\n", // multiply matrix (setProgramContantFromVector)
+ "m33 vt0.xyz, vt0, vc1\n", // multiply matrix (setProgramContantFromVector)
"mov op, vt0\n", // print out
"mov v0, va1\n", // pass uv to fragment shader (setVertexBufferAt(1...)
- "mov v1, va2\n" ]; // pass mask to fragment shader (setVertexBufferAt(1...)
+ "mov v1, va2\n", // pass textureIdMask to fragment shader (setVertexBufferAt(1...)
+ "mov v2, vc0\n" ]; // pass colortransform to fragment shader
/**
* http://blog.flash-core.com/?p=493
*/
@@ -41,7 +42,8 @@ package com.potmo.p2d.renderer
"mul ft0, ft0, v1.xxxx\n", // multiply with mask 1
"tex ft1, v0, fs1 <2d,clamp,mipnone>\n", // sample texture 2
"mul ft1, ft1, v1.yyyy\n", // multiply with mask 2
- "add oc, ft0, ft1\n" ]; // add color 1 and color 2 and copy to output
+ "add ft0, ft0, ft1\n", // add color 1 and color 2
+ "mul oc, ft0, v2" ]; // multiply with colortransform and copy to output
private var _program:Program3D;
@@ -69,13 +69,13 @@ package com.potmo.p2d.renderer
}
- public function clear( r:Number = 0, g:Number = 0, b:Number = 0, a:Number = 0 ):void
+ private function clear( r:Number = 0, g:Number = 0, b:Number = 0, a:Number = 0 ):void
{
_context.clear( r, g, b, a );
}
- public function prepareRender():void
+ private function prepareRender():void
{
_program.setProgram( _context );
@@ -94,7 +94,7 @@ package com.potmo.p2d.renderer
}
- public function draw( atlasId:int, frame:uint, x:Number, y:Number, rotation:Number, scaleX:Number, scaleY:Number ):void
+ public function draw( frame:uint, x:Number, y:Number, rotation:Number, scaleX:Number, scaleY:Number ):void
{
_matrix.createBox( scaleX, scaleY, rotation, x * 2 - _backBufferWidth, _backBufferHeight - y * 2 );
_matrix.scale( _backBufferWidthInv, _backBufferHeightInv );
@@ -103,6 +103,8 @@ package com.potmo.p2d.renderer
//TODO: Append to one matrix vector for later execution (This should be pushed to ProgramConstants and then pulled in vertext shader)
// same for the fragmen shader
+
+ //this matrix vector has a length of 12
_matrixVector[ 0 ] = _matrix.a;
_matrixVector[ 1 ] = _matrix.c;
_matrixVector[ 2 ] = _matrix.tx;
@@ -115,8 +117,9 @@ package com.potmo.p2d.renderer
_transformVector[ 1 ] = 1.0;
_transformVector[ 2 ] = 1.0;
_transformVector[ 3 ] = 1.0; // alpha here
- //_context.setProgramConstantsFromVector( Context3DProgramType.FRAGMENT, 0, _transformVector, 1 ); // this is the colortransform but we dont care about it now
- _context.setProgramConstantsFromVector( Context3DProgramType.VERTEX, 0, _matrixVector, 3 );
+
+ _context.setProgramConstantsFromVector( Context3DProgramType.VERTEX, 0, _transformVector, 1 ); // this is the colortransform but we dont care about it now
+ _context.setProgramConstantsFromVector( Context3DProgramType.VERTEX, 1, _matrixVector, 3 );
//var indexBuffer:IndexBuffer3D = _atlases.getIndexBuffer();
var indexBuffer:IndexBuffer3D = _atlas.getIndexBuffer();
@@ -126,15 +129,20 @@ package com.potmo.p2d.renderer
public function render( displayRoot:Renderable ):void
{
+ this.clear();
+ this.prepareRender();
+
//TODO: Set batch num to 0
displayRoot.render( this );
//TODO: Set program constants from vector and draw the full batch
// remember that we might have to do this more often if the batch is full
+ this.present();
+
}
- public function present():void
+ private function present():void
{
_context.present();
}
@@ -8,7 +8,6 @@ package com.potmo.p2d.renderer
/**
* Render a frame to the canvas
*
- * @param the atlas to get images from
* @param frame the frame in the atlas to be rendered
* @param x position to render
* @param y position to render
@@ -17,7 +16,7 @@ package com.potmo.p2d.renderer
* @param scaleY scale to render frame
*
*/
- function draw( atlasId:int, frame:uint, x:Number, y:Number, rotation:Number, scaleX:Number, scaleY:Number ):void;
+ function draw( frame:uint, x:Number, y:Number, rotation:Number, scaleX:Number, scaleY:Number ):void;
}
}

0 comments on commit 4830850

Please sign in to comment.