State Manager Additions and Fixes.
by Mike Kuklinski · in Torque Game Engine Advanced · 10/12/2005 (1:05 pm) · 0 replies
I have been going thru the state manager, adding support code for Point Sprites, and fixing some of the incomplete stuff. Here goes:
Point Sprites
In gfx/gfxdevice.h:
After void setSrcBlend( GFXBlend blend ); , insert
After the function inline void GFXDevice::setSpecularMaterialSource( GFXMaterialColorSource src ), insert
As you see in setPointSize, I need to set the value, which is stored as a U32, from a F32. I converted it bitwise, when converted back, the float is still intact. These functions, and their declarations, set up the states in the state cache.
We do not need to convert it back with a custom function, because D3D requires an input in DWORD/U32 already, and that is what we are supplying it with :)
Now, we must declare a new kind of Vertex Buffer for use with point sprites. It must include size.
In gfx/gfxStructs.h, after the declaration for GFXVertexPT, insert this:
Now we have the Point-Color-Size buffer added :)
Now this is how you use point sprites:
Set up your buffer for PCS normally. Size should be declared in unit size relative to the camera.
However, if you want setPointSize to take effect, IE, dynamic size, do NOT use PCS, use PC. It only takes effect if size is not declared.
To render the buffer as Point Sprites:
GFX->setPointSprite(bool (1)); // This initializes it as a Point Sprite array... it sets the proper states. //mk
GFX->setPointScale(bool (0)); // This -should- enable the (flawed) scaling of the sprites by distance. //mk
GFX->setPointSize(float (1.f)); // This is only used if size is not in the buffer. //mk
GFX->drawPrimitive(GFXPointList,x,i); // You -must- use GFXPointList. The other two arguments are default.
GFX->setPointSprite(bool (0)); // Don't forget to disable Point Sprites after being rendered :) //mk
Other small fixes
The color state settings were setting a default of 42, for some reason. Direct3D takes a D3DCOLOR value, and the comment was that no case could properly be found? D3DCOLOR is a DWORD, a 32 bit integer. ColorI.getABGRPack() returns a U32 that was the equivalent of a similarly made D3DCOLOR. Hence how these fixes work... I didn't get a chance to test them, but they -should- work, they use the same methods as the D3D ones...:
All of these are in gfx/gfxDevice.h
inline void GFXDevice::setTextureStageBorderColor( U32 stage, ColorI color )
inline void GFXDevice::setAmbientLightColor( ColorI color )
inline void GFXDevice::setTextureFactor( ColorI color )
--EDIT-- fixed some punctuation errors that were messing up copy/paste.
--EDIT-- fixed a bug in the buffer ordering.
--EDIT-- fixed point sprite size bug. That should be all of it.
Point Sprites
In gfx/gfxdevice.h:
After void setSrcBlend( GFXBlend blend ); , insert
void setPointSprite( bool enable ); void setPointScale( bool enable ); void setPointSize( F32 size );
After the function inline void GFXDevice::setSpecularMaterialSource( GFXMaterialColorSource src ), insert
inline void GFXDevice::setPointSprite( bool enable )
{
trackRenderState( GFXRSPointSpriteEnable, enable );
}
inline void GFXDevice::setPointScale( bool enable )
{
trackRenderState( GFXRSPointScaleEnable, enable );
}
inline void GFXDevice::setPointSize( F32 size )
{
trackRenderState( GFXRSPointSize, *(U32*)&size );
}As you see in setPointSize, I need to set the value, which is stored as a U32, from a F32. I converted it bitwise, when converted back, the float is still intact. These functions, and their declarations, set up the states in the state cache.
We do not need to convert it back with a custom function, because D3D requires an input in DWORD/U32 already, and that is what we are supplying it with :)
Now, we must declare a new kind of Vertex Buffer for use with point sprites. It must include size.
In gfx/gfxStructs.h, after the declaration for GFXVertexPT, insert this:
DEFINE_VERT(GFXVertexPCS,GFXVertexFlagXYZ|GFXVertexFlagPointSize|GFXVertexFlagDiffuse)
{
Point3F point;
F32 size;
GFXVertexColor color;
};Now we have the Point-Color-Size buffer added :)
Now this is how you use point sprites:
Set up your buffer for PCS normally. Size should be declared in unit size relative to the camera.
However, if you want setPointSize to take effect, IE, dynamic size, do NOT use PCS, use PC. It only takes effect if size is not declared.
To render the buffer as Point Sprites:
GFX->setPointSprite(bool (1)); // This initializes it as a Point Sprite array... it sets the proper states. //mk
GFX->setPointScale(bool (0)); // This -should- enable the (flawed) scaling of the sprites by distance. //mk
GFX->setPointSize(float (1.f)); // This is only used if size is not in the buffer. //mk
GFX->drawPrimitive(GFXPointList,x,i); // You -must- use GFXPointList. The other two arguments are default.
GFX->setPointSprite(bool (0)); // Don't forget to disable Point Sprites after being rendered :) //mk
Other small fixes
The color state settings were setting a default of 42, for some reason. Direct3D takes a D3DCOLOR value, and the comment was that no case could properly be found? D3DCOLOR is a DWORD, a 32 bit integer. ColorI.getABGRPack() returns a U32 that was the equivalent of a similarly made D3DCOLOR. Hence how these fixes work... I didn't get a chance to test them, but they -should- work, they use the same methods as the D3D ones...:
All of these are in gfx/gfxDevice.h
inline void GFXDevice::setTextureStageBorderColor( U32 stage, ColorI color )
trackSamplerState( stage, GFXSAMPBorderColor, color.getABGRPack() );
inline void GFXDevice::setAmbientLightColor( ColorI color )
trackRenderState( GFXRSAmbient, color.getABGRPack() );
inline void GFXDevice::setTextureFactor( ColorI color )
trackRenderState( GFXRSTextureFactor, color.getABGRPack() );
--EDIT-- fixed some punctuation errors that were messing up copy/paste.
--EDIT-- fixed a bug in the buffer ordering.
--EDIT-- fixed point sprite size bug. That should be all of it.
About the author
http://dev.stackheap.com/