Skip to content
Browse files

fixed offscreen buffer clipping bug (issue #578)

  • Loading branch information...
1 parent 56849da commit 3851b527052d40a59384812f98c50819ad15348d @patrickmeehan patrickmeehan committed Oct 9, 2012
Showing with 87 additions and 59 deletions.
  1. +16 −1 src/aku/AKU.cpp
  2. +40 −48 src/moaicore/MOAIGfxDevice.cpp
  3. +11 −8 src/moaicore/MOAIGfxDevice.h
  4. +19 −1 src/moaicore/MOAILayer.cpp
  5. +1 −1 src/moaicore/MOAISim.cpp
View
17 src/aku/AKU.cpp
@@ -475,7 +475,22 @@ void AKUSetScreenSize ( int width, int height ) {
//----------------------------------------------------------------//
void AKUSetViewSize ( int width, int height ) {
- MOAIGfxDevice::Get ().SetSize ( width, height );
+ MOAIGfxDevice& device = MOAIGfxDevice::Get ();
+
+ u32 currentWidth = device.GetBufferWidth ();
+ u32 currentHeight = device.GetBufferHeight ();
+
+ if (( currentWidth != ( u32 )width ) || ( currentHeight != ( u32 )height )) {
+
+ MOAIGfxDevice::Get ().SetBufferSize ( width, height );
+
+ MOAILuaStateHandle state = MOAILuaRuntime::Get ().State ();
+ if ( device.PushListener ( MOAIGfxDevice::EVENT_RESIZE, state )) {
+ lua_pushnumber ( state, width );
+ lua_pushnumber ( state, height );
+ state.DebugCall ( 2, 0 );
+ }
+ }
}
//----------------------------------------------------------------//
View
88 src/moaicore/MOAIGfxDevice.cpp
@@ -268,8 +268,8 @@ void MOAIGfxDevice::BeginDrawing () {
//----------------------------------------------------------------//
void MOAIGfxDevice::BeginLayer () {
- float width = ( float )this->mWidth;
- float height = ( float )this->mHeight;
+ float width = ( float )this->mBufferWidth;
+ float height = ( float )this->mBufferHeight;
MOAIViewport viewport;
viewport.Init ( 0.0f, 0.0f, width, height );
@@ -534,7 +534,7 @@ const USMatrix4x4& MOAIGfxDevice::GetBillboardMtx () const {
//----------------------------------------------------------------//
float MOAIGfxDevice::GetDeviceScale () {
- return this->mDeviceScale;
+ return this->mBufferScale;
}
//----------------------------------------------------------------//
@@ -558,7 +558,7 @@ cc8* MOAIGfxDevice::GetErrorString ( int error ) const {
//----------------------------------------------------------------//
u32 MOAIGfxDevice::GetHeight () const {
- return this->mHeight;
+ return this->mBufferHeight;
}
//----------------------------------------------------------------//
@@ -586,8 +586,8 @@ USRect MOAIGfxDevice::GetRect () const {
USRect rect;
rect.mXMin = 0;
rect.mYMin = 0;
- rect.mXMax = ( float )this->mWidth;
- rect.mYMax = ( float )this->mHeight;
+ rect.mXMax = ( float )this->mBufferWidth;
+ rect.mYMax = ( float )this->mBufferHeight;
return rect;
}
@@ -638,7 +638,7 @@ USMatrix4x4 MOAIGfxDevice::GetViewProjMtx () const {
//----------------------------------------------------------------//
u32 MOAIGfxDevice::GetWidth () const {
- return this->mWidth;
+ return this->mBufferWidth;
}
//----------------------------------------------------------------//
@@ -728,8 +728,6 @@ MOAIGfxDevice::MOAIGfxDevice () :
mClearColorNode ( 0 ),
mCpuVertexTransform ( false ),
mCpuUVTransform ( false ),
- mDefaultFrameBuffer ( 0 ),
- mDeviceScale ( 1.0f ),
mHasContext ( false ),
mIsFramebufferSupported ( 0 ),
mIsOpenGLES ( false ),
@@ -756,8 +754,10 @@ MOAIGfxDevice::MOAIGfxDevice () :
mVertexFormatBuffer ( 0 ),
mVertexMtxInput ( VTX_STAGE_MODEL ),
mVertexMtxOutput ( VTX_STAGE_MODEL ),
- mWidth ( 0 ),
- mHeight ( 0 ),
+ mDefaultBufferID ( 0 ),
+ mBufferWidth ( 0 ),
+ mBufferHeight ( 0 ),
+ mBufferScale ( 1.0f ),
mLandscape ( 0 ) {
RTTI_SINGLE ( MOAIGlobalEventSource )
@@ -809,18 +809,18 @@ void MOAIGfxDevice::PushDeleter ( u32 type, GLuint id ) {
//----------------------------------------------------------------//
void MOAIGfxDevice::ReadFrameBuffer ( MOAIImage * img ) {
- unsigned char *buffer = (unsigned char *) malloc ( this->mWidth * this->mHeight * 4 );
+ unsigned char *buffer = (unsigned char *) malloc ( this->mBufferWidth * this->mBufferHeight * 4 );
- glReadPixels( 0, 0, this->mWidth, this->mHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
+ glReadPixels( 0, 0, this->mBufferWidth, this->mBufferHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
//image is flipped vertically, flip it back
int index,indexInvert;
- for ( u32 y = 0; y < ( this->mHeight / 2 ); ++y ) {
- for ( u32 x = 0; x < this->mWidth; ++x ) {
+ for ( u32 y = 0; y < ( this->mBufferHeight / 2 ); ++y ) {
+ for ( u32 x = 0; x < this->mBufferWidth; ++x ) {
for ( u32 i = 0; i < 4; ++i ) {
- index = i + ( x * 4 ) + ( y * this->mWidth * 4 );
- indexInvert = i + ( x * 4 ) + (( this->mHeight - 1 - y ) * this->mWidth * 4 );
+ index = i + ( x * 4 ) + ( y * this->mBufferWidth * 4 );
+ indexInvert = i + ( x * 4 ) + (( this->mBufferHeight - 1 - y ) * this->mBufferWidth * 4 );
unsigned char temp = buffer [ indexInvert ];
buffer [ indexInvert ] = buffer [ index ];
@@ -829,7 +829,7 @@ void MOAIGfxDevice::ReadFrameBuffer ( MOAIImage * img ) {
}
}
- img->Init ( buffer, this->mWidth, this->mHeight, USColor::RGBA_8888 );
+ img->Init ( buffer, this->mBufferWidth, this->mBufferHeight, USColor::RGBA_8888 );
free ( buffer );
}
@@ -1046,6 +1046,21 @@ void MOAIGfxDevice::SetBlendMode ( int srcFactor, int dstFactor ) {
}
//----------------------------------------------------------------//
+void MOAIGfxDevice::SetBufferScale ( float scale ) {
+
+ this->mBufferScale = scale;
+}
+
+//----------------------------------------------------------------//
+void MOAIGfxDevice::SetBufferSize ( u32 width, u32 height ) {
+
+ if (( this->mBufferWidth != width ) || ( this->mBufferHeight != height )) {
+ this->mBufferWidth = width;
+ this->mBufferHeight = height;
+ }
+}
+
+//----------------------------------------------------------------//
void MOAIGfxDevice::SetClearColor ( MOAIColor* color ) {
if ( this->mClearColorNode != color ) {
@@ -1080,15 +1095,9 @@ void MOAIGfxDevice::SetCullFunc ( int cullFunc ) {
}
//----------------------------------------------------------------//
-void MOAIGfxDevice::SetDeviceScale ( float scale ) {
-
- this->mDeviceScale = scale;
-
-}
-//----------------------------------------------------------------//
void MOAIGfxDevice::SetDefaultFrameBuffer ( GLuint frameBuffer ) {
- this->mDefaultFrameBuffer = frameBuffer;
+ this->mDefaultBufferID = frameBuffer;
}
//----------------------------------------------------------------//
@@ -1135,7 +1144,7 @@ void MOAIGfxDevice::SetFrameBuffer ( MOAIFrameBuffer* frameBuffer ) {
}
else {
if ( this->mIsFramebufferSupported ) {
- glBindFramebuffer ( GL_FRAMEBUFFER, this->mDefaultFrameBuffer ); // TODO: crash?
+ glBindFramebuffer ( GL_FRAMEBUFFER, this->mDefaultBufferID ); // TODO: crash?
}
}
}
@@ -1295,23 +1304,6 @@ void MOAIGfxDevice::SetShaderPreset ( u32 preset ) {
}
//----------------------------------------------------------------//
-void MOAIGfxDevice::SetSize ( u32 width, u32 height ) {
-
- if (( this->mWidth != width ) || ( this->mHeight != height )) {
-
- this->mWidth = width;
- this->mHeight = height;
-
- MOAILuaStateHandle state = MOAILuaRuntime::Get ().State ();
- if ( this->PushListener ( EVENT_RESIZE, state )) {
- lua_pushnumber ( state, width );
- lua_pushnumber ( state, height );
- state.DebugCall ( 2, 0 );
- }
- }
-}
-
-//----------------------------------------------------------------//
bool MOAIGfxDevice::SetTexture () {
if ( this->mActiveTextures ) {
@@ -1582,8 +1574,8 @@ void MOAIGfxDevice::SetVertexTransform ( u32 id, const USMatrix4x4& transform )
//----------------------------------------------------------------//
void MOAIGfxDevice::SetViewport () {
- float width = ( float )this->mWidth;
- float height = ( float )this->mHeight;
+ float width = ( float )this->mBufferWidth;
+ float height = ( float )this->mBufferHeight;
MOAIViewport rect;
rect.Init ( 0.0f, 0.0f, width, height );
@@ -1819,7 +1811,7 @@ USRect MOAIGfxDevice::WndRectToDevice ( USRect rect ) const {
if ( this->mLandscape ) {
- float width = ( float )this->mWidth;
+ float width = ( float )this->mBufferWidth;
float xMin = rect.mYMin;
float yMin = width - rect.mXMax;
@@ -1833,7 +1825,7 @@ USRect MOAIGfxDevice::WndRectToDevice ( USRect rect ) const {
}
else {
- float height = ( float )this->mHeight;
+ float height = ( float )this->mBufferHeight;
float xMin = rect.mXMin;
float yMin = height - rect.mYMax;
@@ -1846,7 +1838,7 @@ USRect MOAIGfxDevice::WndRectToDevice ( USRect rect ) const {
rect.mYMax = yMax;
}
- rect.Scale ( this->mDeviceScale, this->mDeviceScale );
+ rect.Scale ( this->mBufferScale, this->mBufferScale );
return rect;
}
View
19 src/moaicore/MOAIGfxDevice.h
@@ -100,9 +100,6 @@ class MOAIGfxDevice :
USMatrix4x4 mCpuVertexTransformCacheMtx [ TOTAL_VTX_TRANSFORMS ]; // composition of VIEW and PROJ matrices via CPU
bool mCpuUVTransform;
-
- GLuint mDefaultFrameBuffer;
- float mDeviceScale;
u32 mDrawCount;
bool mHasContext;
@@ -155,8 +152,10 @@ class MOAIGfxDevice :
USMatrix4x4 mBillboardMtx;
USRect mViewRect;
- u32 mWidth;
- u32 mHeight;
+ GLuint mDefaultBufferID;
+ u32 mBufferWidth;
+ u32 mBufferHeight;
+ float mBufferScale;
bool mLandscape;
USFrustum mViewVolume;
@@ -208,6 +207,10 @@ class MOAIGfxDevice :
GET ( USColorVec, FinalColor, mFinalColor )
GET ( USColorVec, PenColor, mPenColor )
+ GET ( u32, BufferWidth, mBufferWidth )
+ GET ( u32, BufferHeight, mBufferHeight )
+ GET ( float, BufferScale, mBufferScale )
+
GET_SET ( bool, Landscape, mLandscape )
//----------------------------------------------------------------//
@@ -275,6 +278,9 @@ class MOAIGfxDevice :
void SetBlendMode ( const MOAIBlendMode& blendMode );
void SetBlendMode ( int srcFactor, int dstFactor );
+ void SetBufferScale ( float scale );
+ void SetBufferSize ( u32 width, u32 height );
+
void SetClearColor ( MOAIColor* color );
void SetCullFunc ();
@@ -285,8 +291,6 @@ class MOAIGfxDevice :
void SetDepthFunc ();
void SetDepthFunc ( int depthFunc );
void SetDepthMask ( bool depthMask );
-
- void SetDeviceScale ( float scale );
void SetFrameBuffer ( MOAIFrameBuffer* frameBuffer );
bool SetGfxState ( MOAIGfxState* gfxState );
void SetPenColor ( u32 color );
@@ -300,7 +304,6 @@ class MOAIGfxDevice :
void SetScreenSpace ( MOAIViewport& viewport );
void SetShader ( MOAIShader* shader = 0 );
void SetShaderPreset ( u32 preset );
- void SetSize ( u32 width, u32 height );
bool SetTexture ();
bool SetTexture ( MOAITextureBase* texture );
bool SetTexture ( MOAIMultiTexture* multi );
View
20 src/moaicore/MOAILayer.cpp
@@ -788,6 +788,24 @@ void MOAILayer::RegisterLuaFuncs ( MOAILuaState& state ) {
//----------------------------------------------------------------//
void MOAILayer::Render () {
- MOAIGfxDevice::Get ().BeginLayer ();
+ if ( !( this->mFlags & FLAGS_VISIBLE )) return;
+ if ( !this->mViewport ) return;
+
+ MOAIGfxDevice& device = MOAIGfxDevice::Get ();
+
+ u32 currentWidth = device.GetBufferWidth ();
+ u32 currentHeight = device.GetBufferHeight ();
+ float currentScale = device.GetBufferScale ();
+
+ if ( this->IsOffscreen ()) {
+ MOAIViewport& viewport = *this->mViewport;
+ device.SetBufferSize (( u32 )viewport.Width (), ( u32 )viewport.Height ());
+ device.SetBufferScale ( 1.0f );
+ }
+
+ device.BeginLayer ();
this->Draw ( MOAIProp::NO_SUBPRIM_ID );
+
+ device.SetBufferSize ( currentWidth, currentHeight);
+ device.SetBufferScale ( currentScale );
}
View
2 src/moaicore/MOAISim.cpp
@@ -336,7 +336,7 @@ int MOAISim::_openWindow ( lua_State* L ) {
AKUOpenWindowFunc openWindow = AKUGetFunc_OpenWindow ();
if ( openWindow ) {
- MOAIGfxDevice::Get ().SetSize ( width, height );
+ MOAIGfxDevice::Get ().SetBufferSize ( width, height );
openWindow ( title, width, height );
}

0 comments on commit 3851b52

Please sign in to comment.
Something went wrong with that request. Please try again.