• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lucky6969b

Cannot render the navigation mesh

13 posts in this topic

The following coding cannot render the navigation mesh to the scene.

I suspect there is something wrong with the DrawPrimitive API call.

But I do not have any good references handy currently.

Anyone shed some lights on this?

m_dxDebugFaces is of type CPerfectFace

BTW, I am using both the fixed and dynamic rendering pipelines. Will this work?

Thanks

Jack

#define PERFECTVERTEX_TYPE ( \
	D3DFVF_XYZ | \
	D3DFVF_DIFFUSE \
	)

class CPerfectObject {
public:
	CPerfectObject() { }
	~CPerfectObject() { }

public:
	virtual HRESULT Render() = 0;

public:
	char *m_strName;
};

class CPerfectVertex : public CPerfectObject  {
public:
	CPerfectVertex() { }
	CPerfectVertex(float x, float y, float z,
				D3DCOLOR DiffuseColor);
	~CPerfectVertex() { }

public:
	void Set (float x, float y, float z,
		D3DCOLOR DiffuseColor);

	HRESULT Render() { return S_OK; }

public:
	D3DVECTOR m_Position;
	D3DCOLOR m_DiffuseColor;

};

class CPerfectFace : public CPerfectObject {
public:
	CPerfectFace() { }
	~CPerfectFace() { }

public:
	void SetProps( int Vertex, float x, float y, float z,
		D3DCOLOR DiffuseColor);

	HRESULT Render();

protected:
	std::vector m_Vertices;

};

#include "Renderer.h"
#include "PerfectPrimitive.h"

extern LPDIRECT3DDEVICE9 g_pDevice;

void CPerfectFace::SetProps( int Vertex, float x, float y, float z,
								D3DCOLOR color) {
	
	CPerfectVertex v;
	v.m_Position.x = x;
	v.m_Position.y = y;
	v.m_Position.z = z;

	v.m_DiffuseColor = color;

	m_Vertices.push_back(v);

}

HRESULT CPerfectFace::Render() {
	HRESULT r = 0;

	LPDIRECT3DVERTEXBUFFER9 pVB = 0;

	r = g_pDevice->CreateVertexBuffer( sizeof(CPerfectVertex) * 3, D3DUSAGE_WRITEONLY,
		PERFECTVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB, NULL);

	if  (FAILED(r))
		return E_FAIL;

	BYTE* pData = 0;
	r = pVB->Lock( 0, 0, (void**)&pData, 0 );

	if ( FAILED ( r )) {
		pVB->Release();
		return E_FAIL;
	}

	CopyMemory ( pData, (void*)&m_Vertices, sizeof(CPerfectVertex) * 3);

	pVB->Unlock();

	g_pDevice->SetStreamSource( 0, pVB, 0, sizeof(CPerfectVertex));

	g_pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, m_Vertices.size() / 3 );

	pVB->Release();

	return S_OK;

	 
}
bool NavMesher::buildDebugMesh(rcPolyMeshDetail* dmesh)
{
 
    if(dmesh->nmeshes == 0)
    {
        MessageBox(0,"getMeshDataFromPolyMeshDetail(): dmesh->nmeshes == 0\n", 0,0);
        return false;
    }
    D3DVERTEXELEMENT9 vertexElements[] =
    {
        {0, 0,  D3DDECLTYPE_FLOAT3,  D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},       
        {0, 12, D3DDECLTYPE_FLOAT1,  D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
        D3DDECL_END()
    };

    if(FAILED(D3DXCreateMesh(dmesh->ntris, dmesh->nverts, D3DXMESH_MANAGED, vertexElements, m_d3d9device, &m_dxDebugMesh)))
    {
        MessageBox(0, "Failed to create dx mesh!", 0, 0);
        return false;
    }
 
    HRESULT hr = E_FAIL;
    DWORD numFaces = m_dxDebugMesh->GetNumFaces();
    DWORD* meshAdj = new DWORD[numFaces * 3];
    hr = m_dxDebugMesh->GenerateAdjacency(0.001f, meshAdj);
    if (FAILED(hr))
    {
        MessageBox(NULL, "Failed to build debug mesh", "Debug Navmesh build failed", NULL);
        return false;
    }

    const D3DXVECTOR3 outlineColor(0.0f, 0.0f, 0.0f);
    const D3DXVECTOR3 innerColor(0.85f, 0.85f, 0.85f);

    struct MeshVertex
    {
        D3DXVECTOR3 p;
        //D3DXVECTOR3 n;
        //D3DXVECTOR2 t;
        D3DCOLOR c;
    };
    WORD* pInd;
    MeshVertex* pVert;
    m_dxDebugMesh->LockVertexBuffer(0, (LPVOID*)&pVert);
    m_dxDebugMesh->LockIndexBuffer(0, (LPVOID*)&pInd);

    for (DWORD i = 0; i < numFaces; ++i)
    {
        D3DCOLOR color = 0x0000FF;
        m_dxDebugFaces.SetProps(i, pVert[pInd[i*3+0]].p.x ,pVert[pInd[i*3+0]].p.y,
            pVert[pInd[i*3+0]].p.z, color);

        m_dxDebugFaces.SetProps(i, pVert[pInd[i*3+1]].p.x ,pVert[pInd[i*3+1]].p.y,
            pVert[pInd[i*3+1]].p.z, color);

        m_dxDebugFaces.SetProps(i, pVert[pInd[i*3+2]].p.x ,pVert[pInd[i*3+2]].p.y,
            pVert[pInd[i*3+2]].p.z, color);
        
    }

    m_dxDebugMesh->UnlockVertexBuffer();
    m_dxDebugMesh->UnlockIndexBuffer();

    return true;
}

void NavMesher::RenderDebugMesh()
{
    m_dxDebugFaces.Render();
}
Edited by lucky6969b
0

Share this post


Link to post
Share on other sites

There are so many things wrong in your code, i will try to show some of them:

 

1.  How this can even compile?

std::vector m_Vertices; 

2. You are recreating VB every Render call which is very bad.

 

3. You fill the vector with all mesh vertices but your VB can hold only 1 triangle and then you try to draw all of them:

g_pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, m_Vertices.size() / 3 );

 

4. You need to set FVF or vertex declaration before DrawPrimitive  call.

 

5. Navigation mesh is static, build it once and draw anytime, no need to lock/unlock every render.

 

6. Use dx debug runtime to your advantage.

Edited by belfegor
1

Share this post


Link to post
Share on other sites

Hello belfegor,

I've fixed the errors you mentioned, but not sure how correct they are.

Before showing that to you, I wonder why I am getting

 

m_dxDebugFaces.SetProps(i, pVert[pInd[i*3+0]].p.x ,pVert[pInd[i*3+0]].p.y,
            pVert[pInd[i*3+0]].p.z, color);

For x,y and z, all of them turns out to be 0.

This happens before and after the fixes.

Thanks

Jack

0

Share this post


Link to post
Share on other sites
#define PERFECTVERTEX_TYPE ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX0 )
 
#ifndef _PERFECTPRIMITIVE_H_
#define _PERFECTPRIMITIVE_H_

#include <d3dx9.h>
#include <vector>

class CPerfectObject {
public:
    CPerfectObject() { }
    ~CPerfectObject() { }

public:
    virtual HRESULT Render() = 0;

public:
    char *m_strName;
};

class CPerfectVertex : public CPerfectObject  {
public:
    CPerfectVertex() { }
    CPerfectVertex(float x, float y, float z,
                D3DCOLOR DiffuseColor);
    ~CPerfectVertex() { }

public:
    void Set (float x, float y, float z,
        D3DCOLOR DiffuseColor);

    HRESULT Render() { return S_OK; }

public:
    D3DVECTOR m_Position;
    D3DCOLOR m_DiffuseColor;

};

class CPerfectFace : public CPerfectObject {
public:
    CPerfectFace() { }
    ~CPerfectFace() { }

public:
    void SetProps( float x, float y, float z,
        D3DCOLOR DiffuseColor);

    HRESULT Render();

    HRESULT CreateVB();

protected:
    std::vector<CPerfectVertex> m_Vertices;

};
 

#endif
 
#include "Renderer.h"
#include "PerfectPrimitive.h"

extern LPDIRECT3DDEVICE9 g_pDevice;

void CPerfectFace::SetProps( float x, float y, float z,
                                D3DCOLOR color) {
    
    CPerfectVertex v;
    v.m_Position.x = x;
    v.m_Position.y = y;
    v.m_Position.z = z;

    v.m_DiffuseColor = color;

    m_Vertices.push_back(v);

}

HRESULT CPerfectFace::CreateVB() {
    HRESULT r = 0;

    LPDIRECT3DVERTEXBUFFER9 pVB = 0;

    r = g_pDevice->CreateVertexBuffer( sizeof(CPerfectVertex) * 3 * m_Vertices.size(), D3DUSAGE_WRITEONLY,
        PERFECTVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB, NULL);

    if  (FAILED(r))
        return E_FAIL;

    BYTE* pData = 0;
    r = pVB->Lock( 0, 0, (void**)&pData, 0 );

    if ( FAILED ( r )) {
        pVB->Release();
        return E_FAIL;
    }

    CopyMemory ( pData, (void*)&m_Vertices, sizeof(CPerfectVertex) * 3 * m_Vertices.size());

    pVB->Unlock();

    g_pDevice->SetStreamSource( 0, pVB, 0, sizeof(CPerfectVertex) * 3 * m_Vertices.size());

    
}

HRESULT CPerfectFace::Render() {
    

    g_pDevice->SetFVF(PERFECTVERTEX_TYPE);

    D3DMATERIAL9 mat;
    mat.Diffuse.r = 0x0;
    mat.Diffuse.g = 0x0;
    mat.Diffuse.b = 0xFF;
    mat.Diffuse.a = 0xFF;


    g_pDevice->SetMaterial(&mat);

    g_pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, m_Vertices.size() / 3 );

    

    return S_OK;

    
}
 
bool NavMesher::buildDebugMesh(rcPolyMeshDetail* dmesh)
{
 
    if(dmesh->nmeshes == 0)
    {
        MessageBox(0,"getMeshDataFromPolyMeshDetail(): dmesh->nmeshes == 0\n", 0,0);
        return false;
    }

    D3DVERTEXELEMENT9 vertexElements[] =
    {
        {0, 0,  D3DDECLTYPE_FLOAT3,  D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
        {0, 12, D3DDECLTYPE_FLOAT3,  D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},
        {0, 24, D3DDECLTYPE_FLOAT2,  D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
        D3DDECL_END()
    };

    if(FAILED(D3DXCreateMesh(dmesh->ntris, dmesh->nverts, D3DXMESH_MANAGED, vertexElements, m_d3d9device, &m_dxDebugMesh)))
    {
        MessageBox(0, "Failed to create dx mesh!", 0, 0);
        return false;
    }

 
 
    HRESULT hr = E_FAIL;
    DWORD numFaces = m_dxDebugMesh->GetNumFaces();
    DWORD* meshAdj = new DWORD[numFaces * 3];
    hr = m_dxDebugMesh->GenerateAdjacency(0.001f, meshAdj);
    if (FAILED(hr))
    {
        MessageBox(NULL, "Failed to build debug mesh", "Debug Navmesh build failed", NULL);
        return false;
    }

    const D3DXVECTOR3 outlineColor(0.0f, 0.0f, 0.0f);
    const D3DXVECTOR3 innerColor(0.85f, 0.85f, 0.85f);

    struct MeshVertex
    {
        D3DXVECTOR3 p;
        D3DXVECTOR3 n;
        D3DXVECTOR2 t;
        //D3DCOLOR c;
    };
    WORD* pInd;
    MeshVertex* pVert;
    m_dxDebugMesh->LockVertexBuffer(0, (LPVOID*)&pVert);
    m_dxDebugMesh->LockIndexBuffer(0, (LPVOID*)&pInd);

    for (DWORD i = 0; i < numFaces; ++i)
    {
        D3DCOLOR color = 0x0000FF;
        m_dxDebugFaces.SetProps(pVert[pInd[i*3+0]].p.x ,pVert[pInd[i*3+0]].p.y,
            pVert[pInd[i*3+0]].p.z, color);

        m_dxDebugFaces.SetProps(pVert[pInd[i*3+1]].p.x ,pVert[pInd[i*3+1]].p.y,
            pVert[pInd[i*3+1]].p.z, color);

        m_dxDebugFaces.SetProps(pVert[pInd[i*3+2]].p.x ,pVert[pInd[i*3+2]].p.y,
            pVert[pInd[i*3+2]].p.z, color);
        
    }



    

    m_dxDebugMesh->UnlockVertexBuffer();
    m_dxDebugMesh->UnlockIndexBuffer();


    m_dxDebugFaces.CreateVB();

    return true;
}
Edited by lucky6969b
0

Share this post


Link to post
Share on other sites

I think i read somewhere that in "Debug" mode every variable is intialized with 0, "Release" gives you garbage values.

 

What happend, the mesh is created with D3DXCreateMesh (which then you never use as i can see), but its verticess/indices are not yet assigned and you used them which is wrong!

You need to extract that info from rcPolyMeshDetail and pass that to SetProps

 

 

edit: remove * 3 in CreateVertexBuffer and CopyMemory

edit2:

//CopyMemory ( pData, (void*)&m_Vertices, sizeof(CPerfectVertex) * 3 * m_Vertices.size());
CopyMemory ( pData, (void*)&m_Vertices[0], sizeof(CPerfectVertex) * m_Vertices.size()); // notice [0], it is very different thing!

edit3: You need to call SetStreamSource right before DrawPrimitive (unless this is the only thing you render which i doubt).

Edited by belfegor
2

Share this post


Link to post
Share on other sites

Okay, thanks

First problem solved,

Next on tormorrow, I'll try to write a piece of shader for the navigation mesh.

Because they are on different pipelines right now. I don't think It'll render.

Thanks, you've been great help

Jack

Edited by lucky6969b
0

Share this post


Link to post
Share on other sites

You need to debug further.

How do you build your VB now? rcPolyMeshDetail might be like indexed mesh but you draw your debug mesh without indices so you need to be careful how to extract/fill correct data into it as no vertices are "shared" that way.

Edited by belfegor
0

Share this post


Link to post
Share on other sites

Hello, I wonder how to pass indices to the vertex shader.

For the fixed pipeline,

I used

g_pDevice->SetIndices(m_pIB);

But it still makes a mess.

I am using your original code for building the vertex and index buffers.

Thanks

Jack

Edited by lucky6969b
0

Share this post


Link to post
Share on other sites

I created simple class for you that wrap whole thing in one place

 

http://pastebin.com/SigypwK3

 

and then you can write simple shader to draw with:

 

float4x4 WorldViewProjection;
 
struct VERTEX
{
    float3 Position : POSITION;
    float3 Color    : COLOR;
};

struct V2P
{
    float4 Position : POSITION;
    float3 Color    : COLOR;
};
 
struct P2A
{
    float4 Color : COLOR;
};

void vs(in VERTEX IN, out V2P OUT)
{
    OUT.Position = mul(float4(IN.Position.xyz, 1.0f), WorldViewProjection);
    OUT.Color = IN.Color;
}

void ps(in V2P IN, out P2A OUT)
{
    OUT.Color = float4(IN.Color, 1.0f);
}
 
technique Tech
{
    pass p0
    {
        VertexShader = compile vs_3_0 vs();
        PixelShader = compile ps_3_0 ps();
    }
}
1

Share this post


Link to post
Share on other sites

Hello belfegor, It was a success to render the navmesh nicely.

But one final question,

is how do you add adjacency info into it?

That is to add lines and colors to differentiate between tiles?

Thanks Jack

 

 

Update:

I am following LaMothe's book source code that I own.

here is the modification. You should notice that the end point of the first line is connected to the head of the next line

which is quite messy..... Any ideas why?

#ifndef NavDebugMesh_HEADER_DEFINED
#define NavDebugMesh_HEADER_DEFINED

#include <d3dx9.h>
#include "Recast/Recast.h"
#include "../Camera/CCamera.h"

extern LPDIRECT3DDEVICE9 g_pDevice;
extern LPD3DXEFFECT g_pEffect;
extern CCamera g_CurrCam;

struct Line {
    D3DXVECTOR3 start;
    D3DXVECTOR3 end;
    D3DCOLOR color;
};

class NavDebugMesh
{
private:

    struct Vertex
    {
        D3DXVECTOR3 p;
        D3DXVECTOR3 c;
    };

    LPDIRECT3DDEVICE9            m_device;
    LPDIRECT3DVERTEXDECLARATION9 m_VDecl;
    LPDIRECT3DVERTEXBUFFER9      m_VB;
    LPDIRECT3DINDEXBUFFER9       m_IB;
    CZenLineList ZenLineList;
    //CZenLine  ZenLine;
    LPD3DXMESH   m_dxDebugMesh;

public:

    NavDebugMesh(LPDIRECT3DDEVICE9 device, rcPolyMeshDetail* dmesh)
        : m_device(device), m_VDecl(nullptr), m_VB(nullptr), m_IB(nullptr)
    {
        m_device->AddRef();

        HRESULT hr = 0;

        D3DVERTEXELEMENT9 vertexElements[] =
        {
            { 0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
            { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,    0 },
            D3DDECL_END()
        };

        hr = m_device->CreateVertexDeclaration(vertexElements, &m_VDecl);
        if (FAILED(hr))
        {
            MessageBox(0, "CreateVertexDeclaration failed!", 0, 0);
        }

        hr = m_device->CreateVertexBuffer(sizeof(Vertex) * dmesh->nverts, D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &m_VB, nullptr);
        if (FAILED(hr))
        {
            MessageBox(0, "CreateVertexBuffer failed!", 0, 0);
        }

        hr = m_device->CreateIndexBuffer(sizeof(DWORD) * dmesh->ntris * 3, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_MANAGED, &m_IB, nullptr);
        if (FAILED(hr))
        {
            MessageBox(0, "CreateIndexBuffer failed!", 0, 0);
        }

        Vertex* pVert;
        m_VB->Lock(0, 0, (void**)&pVert, 0);
        const D3DXVECTOR3* verts = (const D3DXVECTOR3*)dmesh->verts;
        for (int i = 0; i < dmesh->nverts; ++i)
        {
            pVert[i].p = verts[i];
            pVert[i].c = D3DXVECTOR3(0.78f, 1.0f, 1.0f);
        }
        

        m_VB->Unlock();

        DWORD* pInd;
        m_IB->Lock(0, 0, (void**)&pInd, 0);
        int cnt = 0;
        int tri_offset = 0;
        int old_nverts = 0;
        for (int p = 0; p < dmesh->nmeshes; ++p)
        {
            unsigned int* m = &(dmesh->meshes[p * 4]);
            const unsigned short bverts = m[0]; // `b' means "beginning of"!!
            const unsigned short nverts = m[1];
            const unsigned short btris = m[2];
            const unsigned short ntris = m[3];
            const unsigned char* tris = &(dmesh->tris[btris * 4]);

            tri_offset += old_nverts;

            for (int n = 0; n < ntris; ++n)
            {
                for (int k = 0; k < 3; ++k)
                {
                    int tri = tris[n * 4 + k] + tri_offset;
                    pInd[cnt] = tri;
                    cnt++;
                }
            }
            old_nverts = nverts;
        }
        m_IB->Unlock();

        if(FAILED(D3DXCreateMesh(dmesh->ntris, dmesh->nverts, D3DXMESH_MANAGED, vertexElements, g_pDevice, &m_dxDebugMesh)))
        {
            MessageBox(0, "Failed to create dx mesh!", 0, 0);
            return;
        }

        DWORD numFaces = m_dxDebugMesh->GetNumFaces();
        DWORD* meshAdj = new DWORD[numFaces * 3];
        hr = m_dxDebugMesh->GenerateAdjacency(0.001f, meshAdj);
        if (FAILED(hr))
        {
            MessageBox(NULL, "Failed to generate adjacency info", "Failed", NULL);
        }
        
        // add line lists
        //const D3DXVECTOR3 outlineColor(0.0f, 0.0f, 0.0f);
        //const D3DXVECTOR3 innerColor(0.85f, 0.85f, 0.85f);
        const D3DCOLOR outlineColor = 0xFFFFFF;
        const D3DCOLOR innerColor = 0xFFFFFF;
            

        for (DWORD i = 0; i < numFaces; ++i)
        {
            if (0xFFFFFFFF == meshAdj[i * 3 + 0])
            {
                Line line;
                line.start = pVert[pInd[i * 3 + 0]].p;
                line.end = pVert[pInd[i * 3 + 1]].p;
                line.color = outlineColor;
                ZenLineList.AddPoint(line.start.x,
                    line.start.y, line.start.z, line.color);

                ZenLineList.AddPoint(line.end.x,
                    line.end.y, line.end.z, line.color);
        /*        ZenLine.SetProps(line.start.x, line.start.y, line.start.z,
                    line.color, line.end.x, line.end.y, line.end.z, line.color);*/
            }
            else
            {
                Line line;
                line.start = pVert[pInd[i * 3 + 0]].p;
                line.end = pVert[pInd[i * 3 + 1]].p;
                line.color = innerColor;
                //lRenderer->addLine(line);
                ZenLineList.AddPoint(line.start.x,
                    line.start.y, line.start.z, line.color);

                ZenLineList.AddPoint(line.end.x,
                    line.end.y, line.end.z, line.color);
                /*ZenLine.SetProps(line.start.x, line.start.y, line.start.z,
                    line.color, line.end.x, line.end.y, line.end.z, line.color);*/
            }

            if (0xFFFFFFFF == meshAdj[i * 3 + 1])
            {
                Line line;
                line.start = pVert[pInd[i * 3 + 1]].p;
                line.end = pVert[pInd[i * 3 + 2]].p;
                line.color = outlineColor;
                //lRenderer->addLine(line);
                ZenLineList.AddPoint(line.start.x,
                    line.start.y, line.start.z, line.color);

                ZenLineList.AddPoint(line.end.x,
                    line.end.y, line.end.z, line.color);
                /*ZenLine.SetProps(line.start.x, line.start.y, line.start.z,
                    line.color, line.end.x, line.end.y, line.end.z, line.color);*/
            }
            else
            {
                Line line;
                line.start = pVert[pInd[i * 3 + 1]].p;
                line.end = pVert[pInd[i * 3 + 2]].p;
                line.color = innerColor;
                //lRenderer->addLine(line);
                ZenLineList.AddPoint(line.start.x,
                    line.start.y, line.start.z, line.color);

                ZenLineList.AddPoint(line.end.x,
                    line.end.y, line.end.z, line.color);
                /*ZenLine.SetProps(line.start.x, line.start.y, line.start.z,
                    line.color, line.end.x, line.end.y, line.end.z, line.color);*/
            }

            if (0xFFFFFFFF == meshAdj[i * 3 + 2])
            {
                Line line;
                line.start = pVert[pInd[i * 3 + 2]].p;
                line.end = pVert[pInd[i * 3 + 0]].p;
                line.color = outlineColor;
                //lRenderer->addLine(line);
                ZenLineList.AddPoint(line.start.x,
                    line.start.y, line.start.z, line.color);

                ZenLineList.AddPoint(line.end.x,
                    line.end.y, line.end.z, line.color);
                /*ZenLine.SetProps(line.start.x, line.start.y, line.start.z,
                    line.color, line.end.x, line.end.y, line.end.z, line.color);*/
            }
            else
            {
                Line line;
                line.start = pVert[pInd[i * 3 + 2]].p;
                line.end = pVert[pInd[i * 3 + 0]].p;
                line.color = innerColor;
                //lRenderer->addLine(line);
                ZenLineList.AddPoint(line.start.x,
                    line.start.y, line.start.z, line.color);

                ZenLineList.AddPoint(line.end.x,
                    line.end.y, line.end.z, line.color);
                /*ZenLine.SetProps(line.start.x, line.start.y, line.start.z,
                    line.color, line.end.x, line.end.y, line.end.z, line.color);*/
            }
        }
        ZenLineList.m_bConnected = FALSE;

    }

    ~NavDebugMesh()
    {
        Release();
    }

    void Release()
    {
        m_device->Release();
        m_VDecl->Release();
        m_VB->Release();
        m_IB->Release();
    }

    void Draw()
    {
        D3DVERTEXBUFFER_DESC vbDesc;
        m_VB->GetDesc(&vbDesc);
        UINT numVerts = vbDesc.Size / sizeof(Vertex);

        D3DINDEXBUFFER_DESC ibDesc;
        m_IB->GetDesc(&ibDesc);
        UINT numTris = ibDesc.Size / sizeof(DWORD) / 3;

        

        m_device->SetVertexDeclaration(m_VDecl);
        m_device->SetStreamSource(0, m_VB, 0, sizeof(Vertex));
        m_device->SetIndices(m_IB);
        
        D3DXHANDLE hTech = g_pEffect->GetTechniqueByName("NavMeshDraw");
        g_pEffect->SetTechnique(hTech);

        

        // get adjacency info and push it onto the Line List and render away
        //ZenLineList.AddPoint(
        

        g_pEffect->Begin(NULL, NULL);
        g_pEffect->BeginPass(0);
        D3DXMATRIX iden;
        D3DXMatrixIdentity(&iden);
        g_pEffect->SetMatrix("WorldViewProjection", &(iden * (*g_CurrCam.GetViewMatrix()) * (*g_CurrCam.GetProjMatrix())));

        
        
        m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVerts, 0, numTris);
        g_pEffect->EndPass();
        g_pEffect->End();

        ZenLineList.Render();
    }
 
};

#endif

CZenLineList::CZenLineList()
{
    m_pSegmentList = 0;
    m_NumPoints = 0;

    m_bConnected = TRUE;
}

CZenLineList::~CZenLineList()
{
    
    CPointSegment* pSegment = m_pSegmentList;
    CPointSegment* pTempSeg = 0;

    while( pSegment )
    {
        pTempSeg = pSegment->m_pNext;
        delete pSegment;

        pSegment = pTempSeg;
    }

}

// Empties the list of segments
void CZenLineList::ClearPoints()
{
    CPointSegment* pSegment = m_pSegmentList;
    CPointSegment* pTempSeg = 0;

    // While there are segments left, delete them
    while( pSegment )
    {
        pTempSeg = pSegment->m_pNext;
        delete pSegment;

        pSegment = pTempSeg;
    }

    // Reset the segment counters
    m_pSegmentList = 0;
    m_NumPoints = 0;
}

// Adds a point to the segment list
HRESULT CZenLineList::AddPoint( float x, float y, float z, D3DCOLOR PointColor )
{
    HRESULT r = 0;

    // If the segment list has not been created yet...
    if( !m_pSegmentList )
    {
        //...Create the list and set this point as the
        // first entry
        m_pSegmentList = new CPointSegment;

        m_pSegmentList->m_Vertex.m_Position.x = x;
        m_pSegmentList->m_Vertex.m_Position.y = y;
        m_pSegmentList->m_Vertex.m_Position.z = z;

        m_pSegmentList->m_Vertex.m_DiffuseColor = PointColor;
    }
    else
    {
        //...or else just add the point to the end of
        // the list
        CPointSegment* pSegment = m_pSegmentList;

        // Find the last entry in the list
        while( pSegment->m_pNext != NULL )
            pSegment = pSegment->m_pNext;

        // Add this new point to the end of the list
        pSegment->m_pNext = new CPointSegment;

        pSegment = pSegment->m_pNext;

        pSegment->m_Vertex.m_Position.x = x;
        pSegment->m_Vertex.m_Position.y = y;
        pSegment->m_Vertex.m_Position.z = z;

        pSegment->m_Vertex.m_DiffuseColor = PointColor;
        
    }

    // Increment the segment counter
    m_NumPoints++;    

    return S_OK;
}

// Renders the line list
HRESULT CZenLineList::Render()
{
    HRESULT r = 0;

    // Exit if no points are in the list
    if( !m_pSegmentList )
        return E_FAIL;
    
    // Pointer for the vertex buffer
    LPDIRECT3DVERTEXBUFFER9 pVB = 0;

    // Create the vertex buffer
    r = g_pDevice->CreateVertexBuffer( sizeof( CZenVertex ) * m_NumPoints,
                D3DUSAGE_WRITEONLY, ZENVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB, NULL );
    if( FAILED( r ) )
        return E_FAIL;

    // Pointer to the vertex buffer data
    BYTE* pData = 0;

    // Lock the vertex buffer
    r = pVB->Lock( 0, 0, (void**)&pData, 0 );
    if( FAILED( r ) )
    {
        pVB->Release();
        return E_FAIL;
    }

    // Get a pointer to the first segment
    CPointSegment* pSegment = m_pSegmentList;

    int Offset = 0;

    // Loop though the segments and copy them into the vertex buffer
    while( pSegment )
    {
        CopyMemory( pData + Offset, &(pSegment->m_Vertex), sizeof( CZenVertex ) );

        pSegment = pSegment->m_pNext;

        Offset += sizeof( CZenVertex );
    }

    // Unlock the vertex buffer
    pVB->Unlock();

    // Connect the vertex buffer to a rendering source
    g_pDevice->SetStreamSource( 1, pVB, 0, sizeof( CZenVertex ) );

    // Render the points as a strip or list
    if( m_bConnected )
        g_pDevice->DrawPrimitive( D3DPT_LINESTRIP, 0, m_NumPoints-1 );
    else
        g_pDevice->DrawPrimitive( D3DPT_LINELIST, 0, m_NumPoints/2 );

    // Release the vertex buffer
    pVB->Release();

    return S_OK;    
}
Edited by lucky6969b
0

Share this post


Link to post
Share on other sites

I come to know that I can implement the DebugDraw interface to draw the outlines of the Navigation Mesh. But How do I convert this code to Direct3D? Sorry, I've been using the high level stuff (D3DX) and didn't understand the low level stuff very well yet.... I am still learning..

Are the any good books to learn about porting between these 2 competing APIs?

#include <math.h>
#include <string.h>
#include <d3dx9.h>
#include "..\include\DebugNavMesh.h"

extern LPDIRECT3DDEVICE9 g_pDevice;

 void DebugDrawDX::depthMask(bool state)
{
	//glDepthMask(state ? GL_TRUE : GL_FALSE);
}

void DebugDrawDX::texture(bool state)
{
	/*if (state)
	{
		glEnable(GL_TEXTURE_2D);
		g_tex.bind();
	}
	else
	{
		glDisable(GL_TEXTURE_2D);
	}*/
}

void DebugDrawDX::begin(duDebugDrawPrimitives prim, float size)
{
	/*
	switch (prim)
	{
		case DU_DRAW_POINTS:
			glPointSize(size);
			glBegin(GL_POINTS);
			break;
		case DU_DRAW_LINES:
			glLineWidth(size);
			glBegin(GL_LINES);
			break;
		case DU_DRAW_TRIS:
			glBegin(GL_TRIANGLES);
			break;
		case DU_DRAW_QUADS:
			glBegin(GL_QUADS);
			break;
	};*/
}

void DebugDrawDX::vertex(const float* pos, unsigned int color)
{
	/*
	glColor4ubv((GLubyte*)&color);
	glVertex3fv(pos);
	*/
}

void DebugDrawDX::vertex(const float x, const float y, const float z, unsigned int color)
{
	/*
	glColor4ubv((GLubyte*)&color);
	glVertex3f(x,y,z);
	*/
}

void DebugDrawDX::vertex(const float* pos, unsigned int color, const float* uv)
{
	/*
	glColor4ubv((GLubyte*)&color);
	glTexCoord2fv(uv);
	glVertex3fv(pos);
	*/
}

void DebugDrawDX::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v)
{
	/*
	glColor4ubv((GLubyte*)&color);
	glTexCoord2f(u,v);
	glVertex3f(x,y,z);
	*/
}

void DebugDrawDX::end()
{
	/*
	glEnd();
	glLineWidth(1.0f);
	glPointSize(1.0f);
	*/
	
}
Edited by lucky6969b
0

Share this post


Link to post
Share on other sites

Hello,

Just wondering...

Where can I find a correspondence table between OpenGL and Direct3D calls?

Are there any good books on this topic?

Thanks

Jack

Edited by lucky6969b
0

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  
Followers 0