A Wrapper class for a Vertex Buffer

Started by
1 comment, last by Armadon 19 years, 11 months ago
Hi Everyone, this is probally not the most efficient way to encapsulate this but might be helpfull to beginners. Might the experts express their opinion about this class for me? CTriangle.h:

#ifndef _CTriangle_H_
#define _CTriangle_H_
#include "CDirect3d.h"
#include <d3d9.h>
#include <d3dx9.h>
class CTriangle
{
public:
        CTriangle::CTriangle(IDirect3DVertexBuffer9* vertexBuffer);
        CTriangle::CTriangle();
        CTriangle::~CTriangle();

        void create(IDirect3DDevice9* pDevice);
        void render(IDirect3DDevice9* pDevice);
private:
        IDirect3DVertexBuffer9* vb;
        const DWORD VERTEX_FVF;
        struct VERTEX
        {
        float x,y,z;
        DWORD dwColor;
        };
};
#endif
CTriangle.cpp:

#include "CTriangle.h"
#include <d3d9.h>
#include <d3dx9.h>

CTriangle::CTriangle(IDirect3DVertexBuffer9* vertexBuffer):
VERTEX_FVF(D3DFVF_XYZ | D3DFVF_DIFFUSE)
{
this->vb = vertexBuffer;
}

CTriangle::CTriangle():
VERTEX_FVF(D3DFVF_XYZ | D3DFVF_DIFFUSE)
{
}

CTriangle::~CTriangle()
{
if(vb)
  vb->Release();
}

void CTriangle::create(IDirect3DDevice9* pDevice)
{
VERTEX* triangle;
VERTEX data[] = {{-1.0f, 0.0f, 0.0f, 0xFF0000FF},
                 {-1.0f, 2.0f, 0.0f, 0xFF00FFFF},
                 { 1.0f, 0.0f, 0.0f, 0xFFFF00FF},
                 { 1.0f, 2.0f, 0.0f, 0xFF00FFFF}};
                 
pDevice->CreateVertexBuffer(sizeof(VERTEX) * 4, D3DUSAGE_WRITEONLY, VERTEX_FVF, D3DPOOL_MANAGED, &vb, NULL);
vb->Lock(0, 0, (void**)&triangle, 0);
memcpy(triangle, data, sizeof(VERTEX) * 4);
vb->Unlock();
}

void CTriangle::render(IDirect3DDevice9* pDevice)
{
pDevice->SetFVF(VERTEX_FVF);
pDevice->SetStreamSource(0, vb, 0, sizeof(VERTEX));
pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
}
---DirectXSA--- Lachdannan Corp. www.lachdannan.za.net
Advertisement
Other than the fact that creating a vertex buffer for every triangle in the scene is a really bad idea, there isn''t enough to talk about.

John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
You should use one large DYNAMIC vertex buffer, and lock it with NOOVERWRITE and an ever-increasing write pointer. Then draw the parts that are locked/written for each frame. When you get to the end, issue the data that was written this frame, and lock with DISCARD; this allows the driver to double-buffer, and the geometry management of a single buffer is much better than what you''re trying to do.

Also, calling it "triangle" when it''s actually a strip of two triangles, forming a quad, is rather confusing.

If you actually want to draw a quad in D3D, the typical way to do it is with a triangle FAN, btw, because then the vertex order is the same as for a single quad.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement