Quote:Original post by mhagain
Look at the tutorials in the DirectX SDK, particularly D3D9 Tutorial 2 - Rendering Vertices. This should tell you almost everything you need to know.
The one thing it won't tell you is about DrawPrimitiveUP, which lets you use arrays of structs in system memory as a source for rendering from. It's use is generally discouraged as it's slower than using a vertex buffer, but for simple beginning code it's quite fine, and will save you the complication of having to do everything the right way from the outset (especially if you're porting code from OpenGL) (although VBs in D3D are not really that difficult).
Hello mhagain,
I wonder the normal of (ROOT2D2, -ROOT2D2,0) is a vertex normal or surface normal.
I ask because if it is a vertex normal, i have to duplicate it 3 times in the vertex buffer, seems redundant. It looks like a surface normal, but I am not sure how to add it to the vertex buffer?
#define JVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX1)class CJVertex{public: CJVertex(); CJVertex( float x, float y, float z, float nx, float ny, float nz, D3DXCOLOR DiffuseColor, D3DXCOLOR SpecularColor, float tu, float tv); ~CJVertex();// Functionspublic: void Set( float x, float y, float z, float nx, float ny, float nz, D3DXCOLOR DiffuseColor, D3DXCOLOR SpecularColor, float tu, float tv);protected:// Variablespublic: D3DXVECTOR3 m_Position; D3DXVECTOR3 m_Normal; D3DXCOLOR m_DiffuseColor; D3DXCOLOR m_SpecularColor; float m_tu, m_tv;protected:};// Untested Jacky Luk 21/7/2010void drawPyramid(LPDIRECT3DDEVICE9 pDevice,DXfloat x, DXfloat y, DXfloat z, DXfloat height, DXfloat width){ std::vector<CJVertex> v_vertex; CJVertex jv; D3DXVECTOR3 vertex1(x,y,z-height); D3DXVECTOR3 vertex2(x-width, y-width, z); D3DXVECTOR3 vertex3(x-width, y+width, z); D3DXVECTOR3 edge1 = vertex2 - vertex1; D3DXVECTOR3 edge2 = vertex3 - vertex2; D3DXVECTOR3 vec3; D3DXVECTOR3 nVec; D3DXVec3Cross(&vec3, &edge1, &edge2); D3DXVec3Normalize(&nVec, &vec3); jv.Set(x,y,z-height,-ROOT2D2, ROOT2D2,0, D3DXCOLOR(255,255,255,255), D3DXCOLOR(127,127,127,255), 1.0f, 1.0f); v_vertex.push_back(jv); jv.Set(x-width, y-width, z, -ROOT2D2, ROOT2D2, 0, D3DXCOLOR(255,255,255,255), D3DXCOLOR(127,127,127,255), 1.0f, 1.0f); v_vertex.push_back(jv); jv.Set(x-width, y+width, z, -ROOT2D2, ROOT2D2, 0, D3DXCOLOR(255,255,255,255), D3DXCOLOR(127,127,127,255), 1.0f, 1.0f); v_vertex.push_back(jv);////..... add the rest of them HRESULT r = 0; // New vertex buffer LPDIRECT3DVERTEXBUFFER9 pVB = 0; r = pDevice->CreateVertexBuffer( sizeof( CJVertex ), D3DUSAGE_WRITEONLY, JVERTEX, D3DPOOL_DEFAULT, &pVB , NULL); if( FAILED( r ) ) return;// E_FAIL; // Pointer to vertex buffer data void* pData = 0; // Lock the vertex buffer r = pVB->Lock( 0, 0, /*(BYTE**)*/&pData, 0 ); if( FAILED( r ) ) { pVB->Release(); return; } for (int i = 0; i < v_vertex.size(); i++) { // Copy the vertex for the point into the vertex buffer CopyMemory( (PBYTE)pData+i*sizeof(CJVertex), (PBYTE)&v_vertex, sizeof( CJVertex ) ); } // Unlock the vertex buffer pVB->Unlock(); // Attach the vertex buffer to a rendering stream pDevice->SetStreamSource( 0, pVB, 0, sizeof( CJVertex ) ); // Draw the point pDevice->DrawPrimitive( D3DPT_POINTLIST, 0, 1 ); // Release the vertex buffer pVB->Release();
Thanks in advance
Jack