Sign in to follow this  
lucky6969b

Rendering Points

Recommended Posts

lucky6969b    1330
Hi [code] ///// 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[i].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[i].SetDevice(m_pDevice); g_pPoints[i].Render(); } } [/code] 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

Share this post


Link to post
Share on other sites
Jason Z    6436
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.

Share this post


Link to post
Share on other sites
DragonGeo2    100
[quote name='lucky6969b' timestamp='1302530273' post='4797120']
Hi [code] ///// 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[i].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[i].SetDevice(m_pDevice); g_pPoints[i].Render(); } } [/code] 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
[/quote]

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.

Share this post


Link to post
Share on other sites
unbird    8338
That editor is really annoying :/. Did you use the WYSIWY[i]dont[/i]G 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 [url="http://couponmeister.com/beautify.aspx"]this[/url]

[code]
///// 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[i].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[i].SetDevice(m_pDevice);
g_pPoints[i].Render();
}
}
}[/code]

Some things that caught my eye:

[list]
[*]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.
[/list]
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 [i]at least a thousandfold[/i] 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this