Rendering Points

Started by
3 comments, last by unbird 13 years ago
Hi ///// Zen.h #define ZENVERTEX_TYPE (D3DFVF_XYZ | D3DFVF_DIFFUSE) class CZenVertex { public: CZenVertex(); CZenVertex( float x, float y, float z, D3DXCOLOR DiffuseColor); ~CZenVertex(); public: void Set (float x, float y, float z, D3DXCOLOR DiffuseColor); public: D3DXVECTOR3 m_Position; D3DXCOLOR m_DiffuseColor; }; class CZenObject { public: CZenObject(); ~CZenObject(); virtual void SetDevice(LPDIRECT3DDEVICE9 pDevice) { m_pDevice = pDevice; } public: virtual HRESULT Render() = 0; protected: char *m_strName; LPDIRECT3DDEVICE9 m_pDevice; }; class CZenPoint : public CZenObject { public: CZenPoint(); CZenPoint( float x, float y, float z, D3DXCOLOR color, LPDIRECT3DDEVICE9 pDevice ); ~CZenPoint(); public: HRESULT Render(); void SetProps ( float x, float y, float z, D3DXCOLOR Color ); void GetProps ( float &x, float &y, float &z, D3DXCOLOR& color ); protected: CZenVertex m_Vertex; }; ////////////////////////////////////////// Zen.cpp #include "stdafx.h" #include #include "Zen.h" CZenVertex::CZenVertex() { ZeroMemory( &m_Position, sizeof(D3DXVECTOR3)); m_DiffuseColor = D3DXCOLOR(255,255,255,255); } CZenVertex::CZenVertex(float x, float y, float z, D3DXCOLOR DiffuseColor) { m_Position.x = x; m_Position.y = y; m_Position.z = z; m_DiffuseColor = DiffuseColor; } CZenVertex::~CZenVertex() { } void CZenVertex::Set(float x, float y, float z, D3DXCOLOR DiffuseColor) { m_Position.x = x; m_Position.y = y; m_Position.z = z; m_DiffuseColor = DiffuseColor; } CZenObject::CZenObject() { m_strName = 0; } CZenObject::~CZenObject() { if ( m_strName ) delete m_strName; } CZenPoint::CZenPoint() { } CZenPoint::~CZenPoint() { } CZenPoint::CZenPoint( float x, float y, float z, D3DXCOLOR Color, LPDIRECT3DDEVICE9 pDevice ) { m_Vertex.m_Position.x = x; m_Vertex.m_Position.y = y; m_Vertex.m_Position.z = z; m_Vertex.m_DiffuseColor = Color; m_pDevice = pDevice; } void CZenPoint::SetProps ( float x, float y, float z, D3DXCOLOR Color ) { m_Vertex.m_Position.x = x; m_Vertex.m_Position.y = y; m_Vertex.m_Position.z = z; m_Vertex.m_DiffuseColor = Color; } void CZenPoint::GetProps (float &x, float &y, float &z, D3DXCOLOR& color ) { x = m_Vertex.m_Position.x; y = m_Vertex.m_Position.y; z = m_Vertex.m_Position.z; color = m_Vertex.m_DiffuseColor; } HRESULT CZenPoint::Render() { HRESULT r = 0; LPDIRECT3DVERTEXBUFFER9 pVB = 0; r = m_pDevice->CreateVertexBuffer ( sizeof(CZenVertex), D3DUSAGE_WRITEONLY, ZENVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB, NULL); if (FAILED (r)) return E_FAIL; void *pData = 0; r = pVB->Lock (0, 0, (void **)&pData, 0); if (FAILED(r)) { pVB->Release(); return E_FAIL; } CopyMemory ( pData, (void *)&m_Vertex, sizeof(CZenVertex)); pVB->Unlock(); m_pDevice->SetStreamSource ( 0, pVB, 0, sizeof(CZenVertex)); m_pDevice->SetFVF(ZENVERTEX_TYPE); m_pDevice->DrawPrimitive ( D3DPT_POINTLIST, 0, 1 ); pVB->Release(); return S_OK; } ///////////////////////////////////////////CMyView const int g_NumPoints = 300; CZenPoint g_Point[300]; void CMyView::OnInitialUpdate() { for (int i = 0; i < g_NumPoints; i++){ g_Points.SetProps((float) (rand()%5) - rand()%5, (float) (rand()%5) - rand()%5, (float) (rand()%5) - rand()%5, D3DXCOLOR(255,255,255,255)); } void CMyView::Render() { D3DXMATRIX WorldMatrix; D3DXMatrixRotationY(&WorldMatrix, timeGetTime()/1500.0f); m_pDevice->SetTransform(D3DTS_WORLD, &WorldMatrix); for(int i = 0; i < g_NumPoints; i++) { g_pPoints.SetDevice(m_pDevice); g_pPoints.Render(); } } The code was originally written for DX8, now when I used this code in DX9, no points were rendered on the screen. Could anyone kindly check my coding? Thanks Jack
Advertisement
I would advise you to get an updated book, or just take a look at some open source samples. There is actually a particle rendering sample in the DXSDK that you could use as a starting point. If you are interested in DX9, I have heard that the Frank Luna book is good. If you can move past DX9 to DX10, then take a look at our online book (linked below in my signature) or even better would be to pick up a DX11 book when they become available (should be within a couple of months).

Also, as a general information it is typically not possible to switch between API levels without making modifications. There is normally a fairly large change between APIs, making them not directly portable from one to the next.

Hi ///// Zen.h #define ZENVERTEX_TYPE (D3DFVF_XYZ | D3DFVF_DIFFUSE) class CZenVertex { public: CZenVertex(); CZenVertex( float x, float y, float z, D3DXCOLOR DiffuseColor); ~CZenVertex(); public: void Set (float x, float y, float z, D3DXCOLOR DiffuseColor); public: D3DXVECTOR3 m_Position; D3DXCOLOR m_DiffuseColor; }; class CZenObject { public: CZenObject(); ~CZenObject(); virtual void SetDevice(LPDIRECT3DDEVICE9 pDevice) { m_pDevice = pDevice; } public: virtual HRESULT Render() = 0; protected: char *m_strName; LPDIRECT3DDEVICE9 m_pDevice; }; class CZenPoint : public CZenObject { public: CZenPoint(); CZenPoint( float x, float y, float z, D3DXCOLOR color, LPDIRECT3DDEVICE9 pDevice ); ~CZenPoint(); public: HRESULT Render(); void SetProps ( float x, float y, float z, D3DXCOLOR Color ); void GetProps ( float &x, float &y, float &z, D3DXCOLOR& color ); protected: CZenVertex m_Vertex; }; ////////////////////////////////////////// Zen.cpp #include "stdafx.h" #include #include "Zen.h" CZenVertex::CZenVertex() { ZeroMemory( &m_Position, sizeof(D3DXVECTOR3)); m_DiffuseColor = D3DXCOLOR(255,255,255,255); } CZenVertex::CZenVertex(float x, float y, float z, D3DXCOLOR DiffuseColor) { m_Position.x = x; m_Position.y = y; m_Position.z = z; m_DiffuseColor = DiffuseColor; } CZenVertex::~CZenVertex() { } void CZenVertex::Set(float x, float y, float z, D3DXCOLOR DiffuseColor) { m_Position.x = x; m_Position.y = y; m_Position.z = z; m_DiffuseColor = DiffuseColor; } CZenObject::CZenObject() { m_strName = 0; } CZenObject::~CZenObject() { if ( m_strName ) delete m_strName; } CZenPoint::CZenPoint() { } CZenPoint::~CZenPoint() { } CZenPoint::CZenPoint( float x, float y, float z, D3DXCOLOR Color, LPDIRECT3DDEVICE9 pDevice ) { m_Vertex.m_Position.x = x; m_Vertex.m_Position.y = y; m_Vertex.m_Position.z = z; m_Vertex.m_DiffuseColor = Color; m_pDevice = pDevice; } void CZenPoint::SetProps ( float x, float y, float z, D3DXCOLOR Color ) { m_Vertex.m_Position.x = x; m_Vertex.m_Position.y = y; m_Vertex.m_Position.z = z; m_Vertex.m_DiffuseColor = Color; } void CZenPoint::GetProps (float &x, float &y, float &z, D3DXCOLOR& color ) { x = m_Vertex.m_Position.x; y = m_Vertex.m_Position.y; z = m_Vertex.m_Position.z; color = m_Vertex.m_DiffuseColor; } HRESULT CZenPoint::Render() { HRESULT r = 0; LPDIRECT3DVERTEXBUFFER9 pVB = 0; r = m_pDevice->CreateVertexBuffer ( sizeof(CZenVertex), D3DUSAGE_WRITEONLY, ZENVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB, NULL); if (FAILED (r)) return E_FAIL; void *pData = 0; r = pVB->Lock (0, 0, (void **)&pData, 0); if (FAILED(r)) { pVB->Release(); return E_FAIL; } CopyMemory ( pData, (void *)&m_Vertex, sizeof(CZenVertex)); pVB->Unlock(); m_pDevice->SetStreamSource ( 0, pVB, 0, sizeof(CZenVertex)); m_pDevice->SetFVF(ZENVERTEX_TYPE); m_pDevice->DrawPrimitive ( D3DPT_POINTLIST, 0, 1 ); pVB->Release(); return S_OK; } ///////////////////////////////////////////CMyView const int g_NumPoints = 300; CZenPoint g_Point[300]; void CMyView::OnInitialUpdate() { for (int i = 0; i < g_NumPoints; i++){ g_Points.SetProps((float) (rand()%5) - rand()%5, (float) (rand()%5) - rand()%5, (float) (rand()%5) - rand()%5, D3DXCOLOR(255,255,255,255)); } void CMyView::Render() { D3DXMATRIX WorldMatrix; D3DXMatrixRotationY(&WorldMatrix, timeGetTime()/1500.0f); m_pDevice->SetTransform(D3DTS_WORLD, &WorldMatrix); for(int i = 0; i < g_NumPoints; i++) { g_pPoints.SetDevice(m_pDevice); g_pPoints.Render(); } } The code was originally written for DX8, now when I used this code in DX9, no points were rendered on the screen. Could anyone kindly check my coding? Thanks Jack


So, for one, it's reeeeeeeally hard to read your code because there's no newlines in it. And for two, try running your program through PIX and seeing if there's anything strange happening when you render your points. Or fire up the D3D debug runtime and see if you're getting any errors or warnings.
Thanks both of you, will try to look into your suggestions. And Sorry, cos Firefox is blocking javascripts so the post looks real strange... Jack
That editor is really annoying :/. Did you use the WYSIWYdontG editor ? I stopped using it pretty soon (settings in your profile) and used code tags again - for which no JavaScript is needed as it seems.

I could (hopefully) reconstruct your code with the help of this


///// Zen.h
#define ZENVERTEX_TYPE (D3DFVF_XYZ | D3DFVF_DIFFUSE)
class CZenVertex
{
public: CZenVertex();
CZenVertex( float x, float y, float z, D3DXCOLOR DiffuseColor);
~CZenVertex();
public: void Set (float x, float y, float z, D3DXCOLOR DiffuseColor);
public: D3DXVECTOR3 m_Position;
D3DXCOLOR m_DiffuseColor;
};
class CZenObject
{
public: CZenObject();
~CZenObject();
virtual void SetDevice(LPDIRECT3DDEVICE9 pDevice)
{
m_pDevice = pDevice;
}
public: virtual HRESULT Render() = 0;
protected: char *m_strName;
LPDIRECT3DDEVICE9 m_pDevice;
};
class CZenPoint : public CZenObject
{
public: CZenPoint();
CZenPoint( float x, float y, float z, D3DXCOLOR color, LPDIRECT3DDEVICE9 pDevice );
~CZenPoint();
public: HRESULT Render();
void SetProps ( float x, float y, float z, D3DXCOLOR Color );
void GetProps ( float &x, float &y, float &z, D3DXCOLOR& color );
protected: CZenVertex m_Vertex;
};
////////////////////////////////////////// Zen.cpp
#include "stdafx.h"
#include "Zen.h" CZenVertex::CZenVertex()
{
ZeroMemory( &m_Position, sizeof(D3DXVECTOR3));
m_DiffuseColor = D3DXCOLOR(255,255,255,255);
}
CZenVertex::CZenVertex(float x, float y, float z, D3DXCOLOR DiffuseColor)
{
m_Position.x = x;
m_Position.y = y;
m_Position.z = z;
m_DiffuseColor = DiffuseColor;
}
CZenVertex::~CZenVertex()
{
}
void CZenVertex::Set(float x, float y, float z, D3DXCOLOR DiffuseColor)
{
m_Position.x = x;
m_Position.y = y;
m_Position.z = z;
m_DiffuseColor = DiffuseColor;
}
CZenObject::CZenObject()
{
m_strName = 0;
}
CZenObject::~CZenObject()
{
if ( m_strName ) delete m_strName;
}
CZenPoint::CZenPoint()
{
}
CZenPoint::~CZenPoint()
{
}
CZenPoint::CZenPoint( float x, float y, float z, D3DXCOLOR Color, LPDIRECT3DDEVICE9 pDevice )
{
m_Vertex.m_Position.x = x;
m_Vertex.m_Position.y = y;
m_Vertex.m_Position.z = z;
m_Vertex.m_DiffuseColor = Color;
m_pDevice = pDevice;
}
void CZenPoint::SetProps ( float x, float y, float z, D3DXCOLOR Color )
{
m_Vertex.m_Position.x = x;
m_Vertex.m_Position.y = y;
m_Vertex.m_Position.z = z;
m_Vertex.m_DiffuseColor = Color;
}
void CZenPoint::GetProps (float &x, float &y, float &z, D3DXCOLOR& color )
{
x = m_Vertex.m_Position.x;
y = m_Vertex.m_Position.y;
z = m_Vertex.m_Position.z;
color = m_Vertex.m_DiffuseColor;
}
HRESULT CZenPoint::Render()
{
HRESULT r = 0;
LPDIRECT3DVERTEXBUFFER9 pVB = 0;
r = m_pDevice->CreateVertexBuffer ( sizeof(CZenVertex), D3DUSAGE_WRITEONLY, ZENVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB, NULL);
if (FAILED (r)) return E_FAIL;
void *pData = 0;
r = pVB->Lock (0, 0, (void **)&pData, 0);
if (FAILED(r))
{
pVB->Release();
return E_FAIL;
}
CopyMemory ( pData, (void *)&m_Vertex, sizeof(CZenVertex));
pVB->Unlock();
m_pDevice->SetStreamSource ( 0, pVB, 0, sizeof(CZenVertex));
m_pDevice->SetFVF(ZENVERTEX_TYPE);
m_pDevice->DrawPrimitive ( D3DPT_POINTLIST, 0, 1 );
pVB->Release();
return S_OK;
}
///////////////////////////////////////////CMyView
const int g_NumPoints = 300;
CZenPoint g_Point[300];
void CMyView::OnInitialUpdate()
{
for (int i = 0;i < g_NumPoints;i++)
{
g_Points.SetProps((float) (rand()%5) - rand()%5, (float) (rand()%5) - rand()%5, (float) (rand()%5) - rand()%5, D3DXCOLOR(255,255,255,255));
}
void CMyView::Render()
{
D3DXMATRIX WorldMatrix;
D3DXMatrixRotationY(&WorldMatrix, timeGetTime()/1500.0f);
m_pDevice->SetTransform(D3DTS_WORLD, &WorldMatrix);
for(int i = 0;i < g_NumPoints;i++)
{
g_pPoints.SetDevice(m_pDevice);
g_pPoints.Render();
}
}
}


Some things that caught my eye:


  • You check some return codes, but not all of them. Better do. By the way: Did the ones you check fail ?
  • You only set the world transformation, you also need to set a view and projection.
  • You supply a color for each vertex and use fixed function. Disable the lighting and enable the color vertex render states.
  • There are probably other render states which have to be set right (e.g. blending). The defaults might already be ok, but I rather vouch for learning about and explicitly setting them. They get easily screwed or forgotten. PIX is your friend here.
  • Do you clear your background to black ? If some render states are screwed, chances are you render something in black, too, so you don't see anything in spite there is actually something drawn.

When you got your points rendered finally, reconsider your drawing logic (but only then, get it working first). It's not wrong, but it's fatally unperformant. For only 300 points it probably won't matter, but done right you could get at least a thousandfold on decent hardware. Currently you create, update (lock) and destroy a vertex buffer for every single point drawn. Rather create one big vertex buffer at startup and update it once per frame (collecting all or lots of your points) and only destroy it at application shutdown. This is called batching your draw calls and can increase performance considerably.

This topic is closed to new replies.

Advertisement