Code conversion assistance please
by Ronald J Nelson · in Torque Game Engine Advanced · 04/23/2008 (12:38 am) · 15 replies
I am trying to convert some of projectile resource code that created wrapping decals over to TGEA. I have a section I am having and issue with and I wanted to see if someone could tell me where I am making a mistake. This is in the decalManager::renderDecal function:
Code removed. See below.
Since the decals are not rendering at all, its pretty obvious I am missing something here. I could really use a little help here. I will update my resource after it works.
Thank you in advance.
Code removed. See below.
Since the decals are not rendering at all, its pretty obvious I am missing something here. I could really use a little help here. I will update my resource after it works.
Thank you in advance.
#2
04/23/2008 (5:19 am)
OK like the post said though, its for wrapped decals. It works quite a bit differently. Also this is still1.03.
#3
First here is the function:
Code removed. See below.
If you download my resource and replace the function with that, enable wrapping decals in the onFire script, it will start making wrapping decals.
Next the problems...
04/23/2008 (7:36 am)
Well I got it to work with only a couple of problems I am hoping someone can shed some light on for me.First here is the function:
Code removed. See below.
If you download my resource and replace the function with that, enable wrapping decals in the onFire script, it will start making wrapping decals.
Next the problems...
#4
First I shoot wrapping decals:

Let them fade and take a couple of steps forward and backward with the player and this happens:

Additionally, the decals flicker a little now and then when walking around them.
Last, and I have only seen it do this once when I shot at the terrain, one of the decal verts was somewhere in the sky making a really long decal that reached up into the air.
04/23/2008 (7:40 am)
OK here is the first problem. Now this is TGEA 1.03 and I have also reenabled the footstep decals.First I shoot wrapping decals:

Let them fade and take a couple of steps forward and backward with the player and this happens:

Additionally, the decals flicker a little now and then when walking around them.
Last, and I have only seen it do this once when I shot at the terrain, one of the decal verts was somewhere in the sky making a really long decal that reached up into the air.
#5
1. Your still using PrimBuild::color4f() while creating a VB and using drawPrimitive. PrimBuilder is a helper which internally creates and manages its own VB and DrawPrim calls. In general you don't mix PrimBuilder calls with your own DrawPrim calls.
2. Not sure where your calling this code from, but it seems like your creating a new VB every time you render. You should be creating the VB, storing it, and just updating the data when it needs to change.
3. I see you calling selectionVB.lock(), but you write no data to the VB and never unlock the VB.
From what i can tell your trying to convert GL code over to GFX line by line... you can't do that. GFX is very different from the old GL interface.
The right thing to do would be to learn to use VBs and GFX properly.
The simplest thing to do is to use the PrimBuilder. Although it is not as efficient as VBs in some cases, it is very similar to the old GL interface and examples of its use are all over the TGEA code base.
Again the TGEA codebase is the best example to follow. Look at stuff like ParticleEmitter, DecalManager, and GroundCover for examples.
04/23/2008 (7:51 am)
@Ron - I see a few issues in that code snippet (your original one... not the one you posted while i was writing this):1. Your still using PrimBuild::color4f() while creating a VB and using drawPrimitive. PrimBuilder is a helper which internally creates and manages its own VB and DrawPrim calls. In general you don't mix PrimBuilder calls with your own DrawPrim calls.
2. Not sure where your calling this code from, but it seems like your creating a new VB every time you render. You should be creating the VB, storing it, and just updating the data when it needs to change.
3. I see you calling selectionVB.lock(), but you write no data to the VB and never unlock the VB.
From what i can tell your trying to convert GL code over to GFX line by line... you can't do that. GFX is very different from the old GL interface.
The right thing to do would be to learn to use VBs and GFX properly.
The simplest thing to do is to use the PrimBuilder. Although it is not as efficient as VBs in some cases, it is very similar to the old GL interface and examples of its use are all over the TGEA code base.
Again the TGEA codebase is the best example to follow. Look at stuff like ParticleEmitter, DecalManager, and GroundCover for examples.
#6
As you can see I pretty much just gave up on converting line by line and went with something I knew already did what I wanted it to. Still have those problems though.
04/23/2008 (10:25 am)
Thanks Tom, but as you can see in this latest version there is very little difference between stock TGEA and mine. Even then it just does PrimBuild a bit differently which is actually a rip off of stock TGEA shadow code.As you can see I pretty much just gave up on converting line by line and went with something I knew already did what I wanted it to. Still have those problems though.
#7
I am referencing this post:
www.garagegames.com/mg/forums/result.thread.php?qt=9319
The black spoting after the decal clears is it actually using the decal texture of the different decal like the footprint. It seems it is not properly clearing from the queue.
04/23/2008 (10:25 pm)
Well I have been trying to figure this out and I have read there used to be an issue with layered decals flickering and tearing. These are the same as I am getting right now. I am referencing this post:
www.garagegames.com/mg/forums/result.thread.php?qt=9319
The black spoting after the decal clears is it actually using the decal texture of the different decal like the footprint. It seems it is not properly clearing from the queue.
#8
I did get that other bug with the vertice that shoots up into the sky bug and a screenshot of it.

I could really use a little help here. Even if it is just to help be get rid of just one of the bugs.
04/24/2008 (7:50 pm)
I am surprised no one knows this one, I know there are others out there far better versed in this than I am. So far I have been trying a number of things with less that satisfactory results. I did get that other bug with the vertice that shoots up into the sky bug and a screenshot of it.

I could really use a little help here. Even if it is just to help be get rid of just one of the bugs.
#9
Links removed. See below
When you look at it you will be able to see that the wrapDecal code is largely developed from the shadow code which interestingly enough, also used to have the same issues I am having.
So the problems I am having are these.
1. Flickering or tearing decals at certain angles, my newest version decreased that quite a bit, but it still happens.
2. Wrapping decals not properly clearing out of the queue.
3. Finally, the occasional vertice in the sky issue.
As I have said before I would really appreciate a bit of help on this one.
Thank you in advance.
04/24/2008 (10:56 pm)
OK, Here is my decalManager code:Links removed. See below
When you look at it you will be able to see that the wrapDecal code is largely developed from the shadow code which interestingly enough, also used to have the same issues I am having.
So the problems I am having are these.
1. Flickering or tearing decals at certain angles, my newest version decreased that quite a bit, but it still happens.
2. Wrapping decals not properly clearing out of the queue.
3. Finally, the occasional vertice in the sky issue.
As I have said before I would really appreciate a bit of help on this one.
Thank you in advance.
#10
In void DecalManager::freeDecalInstance(DecalInstance* trash) if I add
trash->exData->mPartition.clear();
to the end of the function to clear the Vector mPartition, it gets rid of the clearing bug.
However I get a crash after shooting a few bullets. Now my testsing includes interchanging between a weapon using wrapping decals and the other using regular decals.
Any idea what I should do to get rid of the crash or if there is a different method to achieve what I did with the clear()?
Now I had thought it might have to do with the rendering function so I tried adding
if(exData->mPartition.size() > 0)
before
for (U32 i = 0; i < exData->mPartition.size(); i++)
and included the whole "for" code within that check. It didn't help.
Thanks in advance.
04/26/2008 (2:31 am)
Well I have found why it is creting the issue with not properly clearing the cache, but my fix causes a crash after a bit. Perhaps you can tell me why.In void DecalManager::freeDecalInstance(DecalInstance* trash) if I add
trash->exData->mPartition.clear();
to the end of the function to clear the Vector
However I get a crash after shooting a few bullets. Now my testsing includes interchanging between a weapon using wrapping decals and the other using regular decals.
Any idea what I should do to get rid of the crash or if there is a different method to achieve what I did with the clear()?
Now I had thought it might have to do with the rendering function so I tried adding
if(exData->mPartition.size() > 0)
before
for (U32 i = 0; i < exData->mPartition.size(); i++)
and included the whole "for" code within that check. It didn't help.
Thanks in advance.
#11
mPartition.
My solution is really quite simple, give them their own queue and queue management functions. I have even completely separated the rendering code from the two types so I can isolate the flicker and tearing bugs without needing to make changes to the already working decal code.
I pulled the rendering code here:
Then I added it to its appropriate places in the renderDecal and renderWrapDecal functions. It works very nicely. Now what I need to figure out is why I am getting the flickering and tearing. Since the renderWrapDecal function is based off of the shadow code you would think that setting similar rendering states would work. Not had a lot of success and I am hoping someone can look at this for me to tell me what might be doing it.
Thanks in advance.
04/26/2008 (9:12 pm)
Well it came to me and it works really well. The problem of the wrapping decals not clearing the queue properly was due to them using the VectorMy solution is really quite simple, give them their own queue and queue management functions. I have even completely separated the rendering code from the two types so I can isolate the flicker and tearing bugs without needing to make changes to the already working decal code.
I pulled the rendering code here:
void DecalManager::renderObject(SceneState* state, RenderInst *)
{
if (!smDecalsOn) return;
renderDecal();
renderWrapDecal();
}Then I added it to its appropriate places in the renderDecal and renderWrapDecal functions. It works very nicely. Now what I need to figure out is why I am getting the flickering and tearing. Since the renderWrapDecal function is based off of the shadow code you would think that setting similar rendering states would work. Not had a lot of success and I am hoping someone can look at this for me to tell me what might be doing it.
void DecalManager::renderWrapDecal()
{
MatrixF projection = GFX->getProjectionMatrix();
RectI viewport = GFX->getViewport();
GFX->pushWorldMatrix();
GFX->disableShaders();
GFX->setCullMode(GFXCullNone);
GFX->setLightingEnable(false);
GFX->setAlphaBlendEnable(true);
GFX->setZEnable(true);
GFX->setZFunc(GFXCmpLessEqual);
GFX->setZWriteEnable(false);
GFX->setSrcBlend(GFXBlendSrcAlpha);
GFX->setDestBlend(GFXBlendInvSrcAlpha);
GFX->setAlphaTestEnable(true);
GFX->setAlphaFunc(GFXCmpGreater);
GFX->setAlphaRef(0);
GFX->setTextureStageColorOp(0, GFXTOPModulate);
GFX->setTextureStageColorOp(1, GFXTOPDisable);
sgThisIsSelfIlluminated = false;
sgLastWasSelfIlluminated = false;
F32 depthbias = -0.00002f;
F32 slopebias = -0.06f;
GFX->setZBias(*((U32 *)&depthbias));
GFX->setSlopeScaleDepthBias(*((U32 *)&slopebias));
DecalData* pLastData = NULL;
for(S32 x=0; x<mWrapDecalQueue.size(); x++)
{
if(mWrapDecalQueue[x]->decalData != pLastData)
{
GFX->setTexture(0, mWrapDecalQueue[x]->decalData->textureHandle);
pLastData = mWrapDecalQueue[x]->decalData;
}
sgThisIsSelfIlluminated = mWrapDecalQueue[x]->decalData->selfIlluminated;
if(sgThisIsSelfIlluminated != sgLastWasSelfIlluminated)
{
if(sgThisIsSelfIlluminated)
{
GFX->setSrcBlend(GFXBlendSrcAlpha);
GFX->setDestBlend(GFXBlendOne);
}
else
{
GFX->setSrcBlend(GFXBlendSrcAlpha);
GFX->setDestBlend(GFXBlendInvSrcAlpha);
}
sgLastWasSelfIlluminated = sgThisIsSelfIlluminated;
}
GFX->pushWorldMatrix();
DecalExData *exData = mWrapDecalQueue[x]->exData;
GFX->multWorld(exData->mTransform);
PrimBuild::color4f(1,1,1,mWrapDecalQueue[x]->fade);
for (U32 i = 0; i < exData->mPartition.size(); i++)
{
PrimBuild::begin( GFXTriangleFan, 4 );
for (U32 j = 0; j < exData->mPartition[i].vertexCount; j++)
{
PrimBuild::texCoord2f( exData->mPartitionTVerts[exData->mPartition[i].vertexStart + j].x,
exData->mPartitionTVerts[exData->mPartition[i].vertexStart + j].y );
PrimBuild::vertex3f( exData->mPartitionVerts[exData->mPartition[i].vertexStart + j].x,
exData->mPartitionVerts[exData->mPartition[i].vertexStart + j].y,
exData->mPartitionVerts[exData->mPartition[i].vertexStart + j].z );
}
PrimBuild::end();
}
GFX->popWorldMatrix();
}
GFX->setAlphaBlendEnable(false);
GFX->setZEnable(true);
GFX->setZWriteEnable(true);
GFX->setAlphaTestEnable(false);
GFX->setTextureStageColorOp(0, GFXTOPDisable);
GFX->setZBias(0);
GFX->setSlopeScaleDepthBias(0);
GFX->setTextureStageColorOp(0, GFXTOPDisable);
GFX->setTextureStageColorOp(1, GFXTOPDisable);
GFX->popWorldMatrix();
GFX->setViewport(viewport);
GFX->setProjectionMatrix(projection);
}Thanks in advance.
#12
I could really use a point in the right direction here.
04/28/2008 (6:33 pm)
Seriously? No one has a clue why it is flickering? I have managed to minimize the random vertice bug, but the flickering is driving me mad and I have been trying everything I can think of.I could really use a point in the right direction here.
#13
The first thing is to make sure that you are running in 32-bit mode since this increases the precision of the depth buffer a lot and can help make this sort of thing go away.
The other thing to do is to try offsetting the decal polys slightly upward off the surface so that they aren't exactly coplanar.
Finally, there is also a way to do a z offset bias through most graphics API's (like DirectX, OpenGL, and GFX) that allows one coplanar poly "win" over the other in the depth tests. You can see a use of that as GFX->setZBias() in BlobShadow::render().
04/29/2008 (3:31 pm)
"Flickering" usually is caused by "z-fighting" which is when two polys are exactly coplanar and the video card can't figure out which one it should actually render (they both have the same z/depth values).The first thing is to make sure that you are running in 32-bit mode since this increases the precision of the depth buffer a lot and can help make this sort of thing go away.
The other thing to do is to try offsetting the decal polys slightly upward off the surface so that they aren't exactly coplanar.
Finally, there is also a way to do a z offset bias through most graphics API's (like DirectX, OpenGL, and GFX) that allows one coplanar poly "win" over the other in the depth tests. You can see a use of that as GFX->setZBias() in BlobShadow::render().
#14
F32 depthbias = -0.00002f;
to
F32 depthbias = -0.00006f;
And it works beautifully.
04/29/2008 (3:43 pm)
Thanks Matt. That was the fix. I just changed this lineF32 depthbias = -0.00002f;
to
F32 depthbias = -0.00006f;
And it works beautifully.
#15
04/30/2008 (2:33 pm)
Glad I could help =)
Torque Owner Stefan Lundmark
Player footsteps are decals too, don't they render in stock 1.7.0?