IDX9.cpp

本文展示了一个使用IDX9类进行简单3D图形绘制的例子。该类实现了初始化Direct3D设备、提交多边形列表、刷新及绘制后缓冲区等功能。通过对顶点和索引的处理,展示了基本的3D图形渲染流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "IDX9.h"
#include "Math.h"

IDX9 ::IDX9()
{
	g_pD3D = NULL ;
	g_pd3dDevice = NULL ;
	pVertexBuffer = NULL ;
	backVertexBuffer = new ScreenVertex[BVBSize] ;
	backIndexBuffer = new int[BIBSize] ;
	BVBCursor = 0 ; 
	BIBCursor = 0 ;
}

IDX9 ::~IDX9()
{
	CleanUp() ;
}

bool IDX9 ::SetUp(HWND hwnd)
{
	if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
		return false;

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory( &d3dpp, sizeof( d3dpp ) );
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

	if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
										D3DCREATE_SOFTWARE_VERTEXPROCESSING,
										&d3dpp, &g_pd3dDevice ) ) )
	{
		return false;
	}

	return true;
}

void IDX9 ::RefreshBackBuffer()
{
	BVBCursor = BIBCursor = 0 ;
}

bool IDX9 ::SubmitPolygonList(PolySelfContained* polyList, int count)
{
	if (count * 3 >= BVBSize)
		return false ;

	for (int i = 0; i < count; ++i)
	{
		if (polyList[i].removed)
			continue ;

		backVertexBuffer[BVBCursor].x = polyList[i].vertexList[0].x ;
		backVertexBuffer[BVBCursor].y = polyList[i].vertexList[0].y ;
		backVertexBuffer[BVBCursor].z = polyList[i].vertexList[0].z ;
		backVertexBuffer[BVBCursor].rhw = 1 / polyList[i].vertexList[0].w ;
		BIBCursor = BVBCursor * 2 ;
		backIndexBuffer[BIBCursor] = BVBCursor ;
		backIndexBuffer[BIBCursor + 1] = BVBCursor + 1 ;
		++BVBCursor ;

		backVertexBuffer[BVBCursor].x = polyList[i].vertexList[1].x ;
		backVertexBuffer[BVBCursor].y = polyList[i].vertexList[1].y ;
		backVertexBuffer[BVBCursor].z = polyList[i].vertexList[1].z ;
		backVertexBuffer[BVBCursor].rhw = 1 / polyList[i].vertexList[1].w ;
		BIBCursor = BVBCursor * 2 ;
		backIndexBuffer[BIBCursor] = BVBCursor ;
		backIndexBuffer[BIBCursor + 1] = BVBCursor + 1 ;
		++BVBCursor ;

		backVertexBuffer[BVBCursor].x = polyList[i].vertexList[2].x ;
		backVertexBuffer[BVBCursor ].y = polyList[i].vertexList[2].y ;
		backVertexBuffer[BVBCursor].z = polyList[i].vertexList[2].z ;
		backVertexBuffer[BVBCursor].rhw = 1 / polyList[i].vertexList[2].w ;
		BIBCursor = BVBCursor * 2 ;
		backIndexBuffer[BIBCursor] = BVBCursor ;
		backIndexBuffer[BIBCursor + 1] = BVBCursor - 2 ;
		++BVBCursor ;
	}

	return true ;
}

bool IDX9 ::DrawBackBuffer()
{	
	if( FAILED( g_pd3dDevice->CreateVertexBuffer(
		(BVBCursor) * sizeof( ScreenVertex ),
		0,
		SCREEN_VERTEX_FVF,
		D3DPOOL_DEFAULT,
		&pVertexBuffer,
		NULL)))
	{
		return false;
	}

	void* pVertices;
	if( FAILED( pVertexBuffer->Lock( 0, (BVBCursor) * sizeof( ScreenVertex ), ( void** )&pVertices, 0 ) ) )
		return false;
	
	memcpy(pVertices, backVertexBuffer, (BVBCursor) * sizeof( ScreenVertex ));
	
	pVertexBuffer->Unlock();

	if(FAILED( g_pd3dDevice->CreateIndexBuffer(
		(BIBCursor + 2) * sizeof(int),
		0,
		D3DFMT_INDEX32,
		D3DPOOL_DEFAULT,
		&pIndexBuffer,
		NULL)))
	{
		return false ;
	}

	void * pIndices ;
	if (FAILED(pIndexBuffer ->Lock(0, (BIBCursor + 2) * sizeof(int), (void **)&pIndices, 0)))
		return false ;

	memcpy(pIndices, backIndexBuffer, (BIBCursor + 2) * sizeof(int)) ;

	pIndexBuffer ->Unlock() ;

	return true ;
}

void IDX9 ::Render()
{
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );

    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        g_pd3dDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof( ScreenVertex ) );
        g_pd3dDevice->SetFVF( SCREEN_VERTEX_FVF );

		g_pd3dDevice ->SetIndices(pIndexBuffer) ;

		g_pd3dDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, (BIBCursor + 2), 0, (BIBCursor + 2) / 2) ;

        g_pd3dDevice->EndScene();

    }

    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

void IDX9 ::CleanUp()
{
	if (pVertexBuffer != NULL)
		pVertexBuffer ->Release() ;

	if (pIndexBuffer != NULL)
		pIndexBuffer ->Release() ;

	if( g_pd3dDevice != NULL )
		g_pd3dDevice->Release();

	if( g_pD3D != NULL )
		g_pD3D->Release();
}

0/2 /tmp/ccvhPFNh.o: In function `InitQueue(SqQueue&)&#39;: sqqueue.cpp:(.text+0x0): multiple definition of `InitQueue(SqQueue&)&#39; /tmp/ccTCvRuh.o:main.cpp:(.text+0x192): first defined here /tmp/ccvhPFNh.o: In function `QueueEmpty(SqQueue)&#39;: sqqueue.cpp:(.text+0xc4): multiple definition of `QueueEmpty(SqQueue)&#39; /tmp/ccTCvRuh.o:main.cpp:(.text+0x2f3): first defined here /tmp/ccvhPFNh.o: In function `EnQueue(SqQueue&, int)&#39;: sqqueue.cpp:(.text+0x187): multiple definition of `EnQueue(SqQueue&, int)&#39; /tmp/ccTCvRuh.o:main.cpp:(.text+0x1e8): first defined here /tmp/ccvhPFNh.o: In function `DeQueue(SqQueue&, int&)&#39;: sqqueue.cpp:(.text+0x224): multiple definition of `DeQueue(SqQueue&, int&)&#39; /tmp/ccTCvRuh.o:main.cpp:(.text+0x27b): first defined here /tmp/ccdwiTdi.o: In function `InitStack(SqStack&)&#39;: sqstack.cpp:(.text+0x0): multiple definition of `InitStack(SqStack&)&#39; /tmp/ccTCvRuh.o:main.cpp:(.text+0x0): first defined here /tmp/ccdwiTdi.o: In function `StackEmpty(SqStack)&#39;: sqstack.cpp:(.text+0xb2): multiple definition of `StackEmpty(SqStack)&#39; /tmp/ccTCvRuh.o:main.cpp:(.text+0x17b): first defined here /tmp/ccdwiTdi.o: In function `Push(SqStack&, int)&#39;: sqstack.cpp:(.text+0x11c): multiple definition of `Push(SqStack&, int)&#39; /tmp/ccTCvRuh.o:main.cpp:(.text+0x57): first defined here /tmp/ccdwiTdi.o: In function `Pop(SqStack&, int&)&#39;: sqstack.cpp:(.text+0x1ea): multiple definition of `Pop(SqStack&, int&)&#39; /tmp/ccTCvRuh.o:main.cpp:(.text+0x129): first defined here collect2: error: ld returned 1 exit status
最新发布
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值