IDX9.cpp

本文展示了一个使用IDX9类进行简单3D图形绘制的例子。该类实现了初始化Direct3D设备、提交多边形列表、刷新及绘制后缓冲区等功能。通过对顶点和索引的处理,展示了基本的3D图形渲染流程。
#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
src/step2/LinkList.cpp:12:32: error: stray ‘#’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:34: error: stray ‘\350’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:35: error: stray ‘\241’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:36: error: stray ‘\250’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:37: error: stray ‘\345’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:38: error: stray ‘\244’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:39: error: stray ‘\264’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:40: error: stray ‘\346’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:41: error: stray ‘\214’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:42: error: stray ‘\207’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:43: error: stray ‘\351’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:44: error: stray ‘\222’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:12:45: error: stray ‘\210’ in program self.front = None # 表头指针 ^ src/step2/LinkList.cpp:13:32: error: stray ‘#’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:34: error: stray ‘\350’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:35: error: stray ‘\241’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:36: error: stray ‘\250’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:37: error: stray ‘\345’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:38: error: stray ‘\260’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:39: error: stray ‘\276’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:40: error: stray ‘\346’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:41: error: stray ‘\214’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:42: error: stray ‘\207’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:43: error: stray ‘\351’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:44: error: stray ‘\222’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:13:45: error: stray ‘\210’ in program self.rear = None # 表尾指针 ^ src/step2/LinkList.cpp:14:32: error: stray ‘#’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:34: error: stray ‘\345’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:35: error: stray ‘\275’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:36: error: stray ‘\223’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:37: error: stray ‘\345’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:38: error: stray ‘\211’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:39: error: stray ‘\215’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:40: error: stray ‘\350’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:41: error: stray ‘\212’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:42: error: stray ‘\202’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:43: error: stray ‘\347’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:44: error: stray ‘\202’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:45: error: stray ‘\271’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:46: error: stray ‘\347’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:47: error: stray ‘\232’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:48: error: stray ‘\204’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:49: error: stray ‘\345’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:50: error: stray ‘\211’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:51: error: stray ‘\215’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:52: error: stray ‘\351’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:53: error: stray ‘\251’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:14:54: error: stray ‘\261’ in program self.pre = None # 当前节点的前驱 ^ src/step2/LinkList.cpp:15:32: error: stray ‘#’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:34: error: stray ‘\345’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:35: error: stray ‘\275’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:36: error: stray ‘\223’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:37: error: stray ‘\345’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:38: error: stray ‘\211’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:39: error: stray ‘\215’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:40: error: stray ‘\350’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:41: error: stray ‘\212’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:42: error: stray ‘\202’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:43: error: stray ‘\347’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:44: error: stray ‘\202’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:15:45: error: stray ‘\271’ in program self.curr = None # 当前节点 ^ src/step2/LinkList.cpp:16:32: error: stray ‘#’ in program self.position = 0 # 当前位置索引(从0开始) ^ src/step2/LinkList.cpp:16:34: error: stray ‘\345’ in program self.position = 0 # 当前位置索引(从0开始) ^ src/step2/LinkList....
最新发布
10-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值