Ok, I tried to add a point sprite to my game engine and the code works. However, my goal was to put a 'sun' in my world and well it looks more like a moon.
I want to create the star like glow around the sun and then blend it with the surrounding sky color and/or texture. Glow mapping is one idea, but I've not been able to understand exactly how to get it done.
So if anyone understands the Direct3D SetTextureStageState() method for doing this, plz help me. My books are, at best, vague on this topic.
Here is the code this far:
CPointSprite.h
CPointSprite.cppCode:#include <d3dx9.h>
#include <string>
struct PointSpriteVertex
{
PointSpriteVertex(D3DXVECTOR3 pos,D3DCOLOR col,float sz):position(pos),pointsize(sz),color(col) {}
D3DXVECTOR3 position;
float pointsize;
D3DCOLOR color;
static const DWORD FVF;
};
class CPointSprite
{
protected:
IDirect3DDevice9 *_device;
D3DXVECTOR3 _position;
D3DCOLOR _col;
IDirect3DTexture9 *_texture;
IDirect3DVertexBuffer9 *_vb;
D3DXMATRIX _worldpos;
public:
CPointSprite(void) {};
~CPointSprite(void);
//
//Init the point sprite
//
void Init(IDirect3DDevice9 *Device,
std::string File,D3DCOLOR col,float sz,D3DXVECTOR3 pos);
//
//Render the point sprite
//
void Render(D3DXVECTOR3 &cameraPos);
};
Code:#include "CPointSprite.h"
#include "d3dutility.h"
const DWORD PointSpriteVertex::FVF=D3DFVF_XYZ | D3DFVF_DIFFUSE;
CPointSprite::~CPointSprite(void)
{
_device=0;
_texture=0;
_vb=0;
}
void CPointSprite::Init(IDirect3DDevice9 *Device,
std::string File,
D3DCOLOR col,
float size,
D3DXVECTOR3 pos)
{
_device=Device;
_position=pos;
//
//Create the texture
//
D3DXCreateTextureFromFile(Device,File.c_str(),&_texture);
//
//Position point sprite in the world
//
D3DXMatrixTranslation(&_worldpos,pos.x,pos.y,pos.z);
//
//Create Vertex Buffer
//
_device->CreateVertexBuffer(1*sizeof(PointSpriteVertex),
D3DUSAGE_POINTS,
PointSpriteVertex::FVF,
D3DPOOL_DEFAULT,
&_vb,
0);
PointSpriteVertex *psv=0;
//
//Populate vertex buffer with data
//
_vb->Lock(0,0,(void **)&psv,0);
psv[0]=PointSpriteVertex(pos,col,size);
_vb->Unlock();
}
void CPointSprite::Render(D3DXVECTOR3 &cameraPos)
{
//
//Set Translation according to camera
//
D3DXMATRIX w;
D3DXMatrixTranslation
(&w,cameraPos.x,cameraPos.y,cameraPos.z);
//
//Set world transform
//
_device->SetTransform(D3DTS_WORLD,&w);
//
//Enable point sprites
//
_device->SetRenderState(D3DRS_POINTSPRITEENABLE,true);
//
//Do not scale this sprite
//
_device->SetRenderState(D3DRS_POINTSCALEENABLE,false);
//
//Set sprite size in screen pixels
//
_device->SetRenderState(D3DRS_POINTSIZE,d3d::FtoDw(100.0f));
//
//Used for scaling point sprites - currently disabled
//_device->SetRenderState(D3DRS_POINTSCALE_A,d3d::FtoDw(0.0f));
//_device->SetRenderState(D3DRS_POINTSCALE_B,d3d::FtoDw(0.0f));
//_device->SetRenderState(D3DRS_POINTSCALE_C,d3d::FtoDw(1.0f));
//_device->SetRenderState(D3DRS_POINTSIZE_MIN,d3d::FtoDw(0.0f));
//
//
//Turn alpha blending on - currently disabled
//
//_device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
//
//Set texture for point sprite
//
_device->SetTexture(0,_texture);
//
//Setup texture stage states for glow mapping
//
//
//Stage 0
//
_device->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
_device->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
_device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
_device->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
//
//Stage 1 color
//
_device->SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_CURRENT);
_device->SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_TEXTURE);
_device->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_ADD);
//
//Stage 1 alpha
//
_device->SetTextureStageState(1,D3DTSS_ALPHAARG1,D3DTA_CURRENT);
_device->SetTextureStageState(1,D3DTSS_ALPHAARG2,D3DTA_TEXTURE);
_device->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_ADD);
//
//Stage 2 - disable color/alpha blend
//
_device->SetTextureStageState(2,D3DTSS_COLOROP,D3DTOP_DISABLE);
_device->SetTextureStageState(2,D3DTSS_ALPHAOP,D3DTOP_DISABLE);
//
//Turn D3D lighting on
//
_device->SetRenderState(D3DRS_LIGHTING,true);
//
//Set Flexible Vertex Format to ours
//
_device->SetFVF(PointSpriteVertex::FVF);
//
//Set D3D stream source to our vertex buffer
//
_device->SetStreamSource(0,_vb,0,sizeof(PointSpriteVertex));
//
//Set texture 0 to ours
//
_device->SetTexture(0,_texture);
//
//Draw the stupid thing
//
_device->DrawPrimitive(D3DPT_POINTLIST,1,1);
//
//Turn off alpha blending - currently disabled
//
//_device->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
}