Ejemplo n.º 1
0
StaticPlane* StaticPlane::init() {
	LPDIRECT3DDEVICE9 device = ShaderDevise::device();

	device->CreateVertexBuffer(
		sizeof(CUSTOMVERTEX)*4,
		D3DUSAGE_WRITEONLY,
		D3DFVF_CUSTOMVERTEX,
		D3DPOOL_MANAGED,
		&vtx,
		NULL
		);

	float size = 10;
	CUSTOMVERTEX vtx[] = {
		CUSTOMVERTEX(D3DXVECTOR3(-size, 0,  size), D3DXVECTOR3(0, 1, 0), 0xffffffff, D3DXVECTOR2(0, 0)),
		CUSTOMVERTEX(D3DXVECTOR3( size, 0,  size), D3DXVECTOR3(0, 1, 0), 0xffffffff, D3DXVECTOR2(1, 0)),
		CUSTOMVERTEX(D3DXVECTOR3(-size, 0, -size), D3DXVECTOR3(0, 1, 0), 0xffffffff, D3DXVECTOR2(0, 1)),
		CUSTOMVERTEX(D3DXVECTOR3( size, 0, -size), D3DXVECTOR3(0, 1, 0), 0xffffffff, D3DXVECTOR2(1, 1))
	};

	D3DXVec3Cross(&vtx[0].nor, &(vtx[1].pos - vtx[0].pos), &(vtx[2].pos - vtx[0].pos));
	D3DXVec3Cross(&vtx[3].nor, &(vtx[2].pos - vtx[3].pos), &(vtx[1].pos - vtx[3].pos));
	D3DXVec3Normalize(&vtx[0].nor, &vtx[0].nor);
	D3DXVec3Normalize(&vtx[3].nor, &vtx[3].nor);

	void *data;
	this->vtx->Lock(0, sizeof(CUSTOMVERTEX)*4, &data, 0);
	memcpy(data, vtx, sizeof(CUSTOMVERTEX)*4);
	this->vtx->Unlock();
	return this;

}
Ejemplo n.º 2
0
void Arrow::updateBuffer() {
	CUSTOMVERTEX* data = (CUSTOMVERTEX*)Common::getVtxDataWithLock(vbuf);
	data[0] = CUSTOMVERTEX(_start, Common::vec3zero, 0xffff0000, Common::vec2zero);
	data[1] = CUSTOMVERTEX(_end, Common::vec3zero, 0xff0000ff, Common::vec2zero);
	D3DXVECTOR3 dir(_end - _start);
	D3DXVec3Normalize(&dir, &dir);
	D3DXVECTOR3 rev_dir(dir*-1);
	data[2] = CUSTOMVERTEX(_end + rev_dir + D3DXVECTOR3(0, 1, 0), Common::vec3zero, 0xff0000ff, Common::vec2zero);
	data[3] = CUSTOMVERTEX(_end, Common::vec3zero, 0xff0000ff, Common::vec2zero);
	vbuf->Unlock();
}
Ejemplo n.º 3
0
Plane* Plane::init(LPDIRECT3DDEVICE9 device) {

	CUSTOMVERTEX vtx[] = {
		CUSTOMVERTEX(D3DXVECTOR3(-1, 0,  1), D3DXVECTOR3(0, 1, 0), 0x00ffffff, D3DXVECTOR2(0, 0)),
		CUSTOMVERTEX(D3DXVECTOR3( 1, 0,  1), D3DXVECTOR3(0, 1, 0), 0x00ffffff, D3DXVECTOR2(1, 0)),
		CUSTOMVERTEX(D3DXVECTOR3(-1, 0, -1), D3DXVECTOR3(0, 1, 0), 0xffffffff, D3DXVECTOR2(0, 1)),
		CUSTOMVERTEX(D3DXVECTOR3( 1, 0, -1), D3DXVECTOR3(0, 1, 0), 0xffffffff, D3DXVECTOR2(1, 1)),
	};
	setVertices(vtx, 4);

	LPD3DXBUFFER code;
	LPD3DXBUFFER error;

	D3DXCompileShaderFromFile("shader.hlsl", NULL, NULL,
								"vertexShaderTexture", "vs_2_0", 0,
								&code, &error, &_vs_constant_table);

	if(error) {
		OutputDebugString((LPCSTR)error->GetBufferPointer());
		vbuf->Release();
		error->Release();
		return 0;
	}

	device->CreateVertexShader((DWORD*)code->GetBufferPointer(), &_vs);
	code->Release();

	D3DXCompileShaderFromFile("shader.hlsl", NULL, NULL,
								"pixelShaderTexture", "ps_2_0", 0,
								&code, &error, &_ps_constant_table);

	if(error) {
		OutputDebugString((LPCSTR)error->GetBufferPointer());
		vbuf->Release();
		_vs_constant_table->Release();
		_vs->Release();
		error->Release();
		return 0;
	}

	device->CreatePixelShader((DWORD*)code->GetBufferPointer(), &_ps);
	code->Release();

	D3DXCreateTextureFromFile(device, "textures/ground.png", &texture);

	angle = (D3DX_PI / 180.0f * 2);
	//D3DXMATRIX rot;
	//D3DXMatrixRotationAxis(&rot, &D3DXVECTOR3(1, 0, 0), (D3DX_PI / 180.0f * -90));
	//world *= rot;

	return this;
}
Ejemplo n.º 4
0
WallClass::WallClass()
{
	// Creating a quad by pushing the vertices to the draw stack.
	vertices.push_back(CUSTOMVERTEX(-1.0f, 1.0f, 0.0f, 0xff111111, 0.0f, 0.0f));
	vertices.push_back(CUSTOMVERTEX(1.0f, 1.0f, 0.0f, 0xff111111, 0.0f, 0.0f));
	vertices.push_back(CUSTOMVERTEX(-1.0f, -1.0f, 0.0f, 0xff999999, 0.0f, 0.0f));
	vertices.push_back(CUSTOMVERTEX(1.0f, -1.0f, 0.0f, 0xff9f9f9f, 0.0f, 0.0f));

	mPosition.y = -0.5f;
	mPosition.x = 1.0f;		// Need a non-zero value for the multiplication part of tiling.
	mPosition.z = 1.0f;		// Need a non-zero value for the multiplication part of tiling.
	mPosition.y = -19.9f;
	mScale.x = 5.0f;
	mScale.y = 20.0f;
	mScale.z = 20.0f;
}
Ejemplo n.º 5
0
Grid* Grid::createGrid() {
	if(block_size == D3DXVECTOR2(0, 0) || size == 0) return this;
	SAFE_RELEASE(vtx);

	LPDIRECT3DDEVICE9 device = ShaderDevise::device();

	// 使いまわすので保存
	int v_num = size + 1;

	device->CreateVertexBuffer(
		sizeof(CUSTOMVERTEX)*v_num*4,
		D3DUSAGE_WRITEONLY,
		D3DFVF_CUSTOMVERTEX,
		D3DPOOL_MANAGED,
		&vtx,
		NULL
	);

	CUSTOMVERTEX *data;
	vtx->Lock(0, 0, (void**)&data, 0);
	for(int i = 0, len = v_num*2; i < len; i+=2) {
		int ofs = i / 2;
		D3DXVECTOR3 p(-size*block_size.x/2+ofs*block_size.x, 0, -size*block_size.y/2);
		D3DXVECTOR2 uv((float)ofs / (float)size, 0);
		D3DCOLOR color = (i != v_num-1)?0xffffffff:0xffff0000;
		data[i] = CUSTOMVERTEX(p, D3DXVECTOR3(0, 0, -1), color, uv);
		p.z *= -1;
		uv.y = 1;
		data[i+1] = CUSTOMVERTEX(p, D3DXVECTOR3(0, 0, -1), color, uv);
	}
	for(int i = 0, len = v_num*2; i < len; i+=2) {
		int ofs = i / 2;
		D3DXVECTOR3 p(-size*block_size.x/2, 0, -size*block_size.y/2+ofs*block_size.y);
		D3DXVECTOR2 uv(0, (float)ofs / (float)size);
		D3DCOLOR color = (i != v_num-1)?0xffffffff:0xff0000ff;
		data[v_num*2+i] = CUSTOMVERTEX(p, D3DXVECTOR3(0, 1, 0), color, uv);
		p.x *= -1;
		uv.x = 1;
		data[v_num*2+i+1] = CUSTOMVERTEX(p, D3DXVECTOR3(0, 1, 0), color, uv);
	}
	vtx->Unlock();
	return this;
}
void CreateVertexBuffer()
{
	//顶点缓存(Vertex Buffer)是Direct3D用来保存顶点数据的内存空间,可以位于系统内存和图形卡的显存中。
	//当Direct3D绘制图形时,将根据这些顶点结构创建一个三角形列表来描述物体的形状和轮廓
	/*
	在Direct3D中,顶点缓存由IDirect3DVertexBuffer9接口对象表示。在使用顶点缓存之前,需要定义描述顶点的顶点结构,然后可以通过IDirect3DDevice9接口的CreateVertexBuffer方法创建顶点缓存

	HRESULT IDirect3DDevice9::CreateVertexBuffer(
	UINT       Length,
	DWORD    Usage, //指定使用缓存的一些附加属性
	DWORD    FVF, //将要存储在顶点缓存中的灵活顶点格式
	D3DPOOL   Pool, //一个D3DPOOL枚举类型,用于指定存储顶点缓存或索引缓冲的内存位置,在默认情况下时位于显存中
	IDirect3DVertexBuffer9**  ppVertexBuffer, //IDirect3DVertexBuffer9接口类型的变量的指针,分别用于接收创建的顶点缓存和索引缓存的指针
	HANDLE*   pSharedHandle
	);

	*/
	//创建顶点缓存
	g_pd3dDevice->CreateVertexBuffer(6*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVertexBuf,NULL);

	//填充顶点数据
	CUSTOMVERTEX*pVertices=NULL;

	/*
	在取得IDirect3DVertexBuffer9接口对象的指针后,就可以通过这该接口的Lock方法获取指向顶点缓存的存储区的指针,并通过该指针访问缓存中的数据

	HRESULT IDirect3DVertexBuffer9::Lock(
	UINT     OffsetToLock, //在存储区中加锁的起始位置
	UINT     SizeToLock, //以字节为单位的加锁的存储区大小,值为0表示整个缓存存储区
	BYTE  **ppbData, //用于接收被锁定的存储区起始位置的指针,该参数指向的存储区的读写方式由Flags参数指定
	DWORD  Flags
	);

	*/

	//锁定缓存区
	g_pVertexBuf->Lock(0,0,(void**)&pVertices,0);
	pVertices[0]=CUSTOMVERTEX(220.0f, 120.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0));//V0
	pVertices[1]=CUSTOMVERTEX(420.0f, 120.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,255,0));//V1
	pVertices[2]=CUSTOMVERTEX(220.0f, 320.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,255,0));//V2

	pVertices[3]=CUSTOMVERTEX(420.0f, 120.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,255,0));//V1
	pVertices[4]=CUSTOMVERTEX(420.0f, 320.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255));//V3
	pVertices[5]=CUSTOMVERTEX(220.0f, 320.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,255,0));//V2

	//当使用Lock方法对应缓存区进行加锁并完成数据的访问后,需要调用IDirect3DVertexBuffer9接口的Unlock方法对缓存进行解锁
	g_pVertexBuf->Unlock();
}
Ejemplo n.º 7
0
HRESULT Direct3D9Video::CreateVertices ()
{
    HRESULT hr = 0;

    if (!m_pd3dDevice)
        return D3DERR_INVALIDDEVICE;

    if (m_pVertexBuffer) m_pVertexBuffer->Release(), m_pVertexBuffer = NULL;

    if (FAILED(hr = m_pd3dDevice->CreateVertexBuffer(NUM_VERTICES*sizeof(CUSTOMVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVertexBuffer, NULL)))
        return hr;

    DWORD dwWidth = Frame::GetWidth();
    DWORD dwHeight = Frame::GetHeight();

    UINT uWidthView = m_d3dpp.BackBufferWidth;
    UINT uHeightView = m_d3dpp.BackBufferHeight;

    UINT uWidth = dwWidth, uHeight = dwHeight;
    if (GetOption(ratio5_4)) uWidth = uWidth * 5/4;


    UINT uWidthFit = MulDiv(uWidth, uHeightView, uHeight);
    UINT uHeightFit = MulDiv(uHeight, uWidthView, uWidth);

    if (uWidthFit <= uWidthView)
    {
        uWidth = uWidthFit;
        uHeight = uHeightView;
    }
    else if (uHeightFit <= uHeightView)
    {
        uWidth = uWidthView;
        uHeight = uHeightFit;
    }

    RECT r = { 0, 0, uWidth, uHeight };
    OffsetRect(&r, (uWidthView - r.right)/2, (uHeightView - r.bottom)/2);
    m_rTarget = r;

    CUSTOMVERTEX *pVertices;
    hr = m_pVertexBuffer->Lock(0, NUM_VERTICES*sizeof(CUSTOMVERTEX), (void**)&pVertices, 0);


    float tW = float(dwWidth) / float(TEXTURE_SIZE);
    float tH = float(dwHeight) / float(TEXTURE_SIZE);

    // Main display, also used for scanlines
    pVertices[0] = CUSTOMVERTEX((float)r.left  - 0.5f, (float)r.bottom - 0.5f, 0.0f, 1.0f,  0.0f,   tH);
    pVertices[1] = CUSTOMVERTEX((float)r.left  - 0.5f, (float)r.top    - 0.5f, 0.0f, 1.0f,  0.0f,   0.0f);
    pVertices[2] = CUSTOMVERTEX((float)r.right - 0.5f, (float)r.bottom - 0.5f, 0.0f, 1.0f,    tW,   tH);
    pVertices[3] = CUSTOMVERTEX((float)r.right - 0.5f, (float)r.top    - 0.5f, 0.0f, 1.0f,    tW,   0.0f);

    // Main display with height doubled
    pVertices[4] = pVertices[0]; pVertices[4].tv /= 2.0f;
    pVertices[5] = pVertices[1];
    pVertices[6] = pVertices[2]; pVertices[6].tv /= 2.0f;
    pVertices[7] = pVertices[3];

    tW = float(uWidthView) / float(TEXTURE_SIZE);
    tH = float(uHeightView) / float(TEXTURE_SIZE);

    // Matching display mode for native scanlines
    pVertices[8]  = CUSTOMVERTEX(0.0f              - 0.5f, float(uHeightView) - 0.5f, 0.0f, 1.0f,  0.0f,   tH);
    pVertices[9]  = CUSTOMVERTEX(0.0f              - 0.5f, 0.0f               - 0.5f, 0.0f, 1.0f,  0.0f, 0.0f);
    pVertices[10] = CUSTOMVERTEX(float(uWidthView) - 0.5f, float(uHeightView) - 0.5f, 0.0f, 1.0f,    tW,   tH);
    pVertices[11] = CUSTOMVERTEX(float(uWidthView) - 0.5f, 0.0f               - 0.5f, 0.0f, 1.0f,    tW, 0.0f);

    m_pVertexBuffer->Unlock();

    return D3D_OK;
}
Ejemplo n.º 8
0
//--------------------------------------
//NAME : CreateVertexBuffer()
//DESC : 创建图形顶点缓存
//--------------------------------------
void CreateVertexBuffer()
{
	//顶点缓存(Vertex Buffer)是Direct3D用来保存顶点数据的内存空间,可以位于系统内存和图形卡的显存中。
	//当Direct3D绘制图形时,将根据这些顶点结构创建一个三角形列表来描述物体的形状和轮廓
	/*
	在Direct3D中,顶点缓存由IDirect3DVertexBuffer9接口对象表示。在使用顶点缓存之前,需要定义描述顶点的顶点结构,然后可以通过IDirect3DDevice9接口的CreateVertexBuffer方法创建顶点缓存

	HRESULT IDirect3DDevice9::CreateVertexBuffer(
	UINT       Length,
	DWORD    Usage, //指定使用缓存的一些附加属性
	DWORD    FVF, //将要存储在顶点缓存中的灵活顶点格式
	D3DPOOL   Pool, //一个D3DPOOL枚举类型,用于指定存储顶点缓存或索引缓冲的内存位置,在默认情况下时位于显存中
	IDirect3DVertexBuffer9**  ppVertexBuffer, //IDirect3DVertexBuffer9接口类型的变量的指针,分别用于接收创建的顶点缓存和索引缓存的指针
	HANDLE*   pSharedHandle
	);

	*/
	//创建顶点缓存
	g_pd3dDevice->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVertexBuf,NULL);

	//填充顶点数据
	CUSTOMVERTEX*pVertices=NULL;

	/*
	在取得IDirect3DVertexBuffer9接口对象的指针后,就可以通过这该接口的Lock方法获取指向顶点缓存的存储区的指针,并通过该指针访问缓存中的数据

	HRESULT IDirect3DVertexBuffer9::Lock(
	UINT     OffsetToLock, //在存储区中加锁的起始位置
	UINT     SizeToLock, //以字节为单位的加锁的存储区大小,值为0表示整个缓存存储区
	BYTE  **ppbData, //用于接收被锁定的存储区起始位置的指针,该参数指向的存储区的读写方式由Flags参数指定
	DWORD  Flags
	);

	*/

	//锁定缓存区
	g_pVertexBuf->Lock(0,0,(void**)&pVertices,0);
	pVertices[0]=CUSTOMVERTEX(-5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0));
	pVertices[1]=CUSTOMVERTEX(-5.0f,5.0f,5.0f,D3DCOLOR_XRGB(0,255,0));
	pVertices[2]=CUSTOMVERTEX(5.0f,5.0f,5.0f,D3DCOLOR_XRGB(0,0,255));
	pVertices[3]=CUSTOMVERTEX(5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,255,0));
	pVertices[4]=CUSTOMVERTEX(-5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(0,0,255));
	pVertices[5]=CUSTOMVERTEX(-5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(255,255,0));
	pVertices[6]=CUSTOMVERTEX(5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(255,0,0));
	pVertices[7]=CUSTOMVERTEX(5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(0,255,0));

	//当使用Lock方法对应缓存区进行加锁并完成数据的访问后,需要调用IDirect3DVertexBuffer9接口的Unlock方法对缓存进行解锁
	g_pVertexBuf->Unlock();

	//创建索引缓存
	g_pd3dDevice->CreateIndexBuffer(36*sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&g_pIndexBuf,NULL);

	//填充索引数据
	WORD*pIndices=NULL;
	g_pIndexBuf->Lock(0,0,(void**)&pIndices,0);

	//顶面由两个三角形组成
	pIndices[0]=0,pIndices[1]=1,pIndices[2]=2;
	pIndices[3]=0,pIndices[4]=2,pIndices[5]=3;

	//正面
	pIndices[6]=0,pIndices[7]=3,pIndices[8]=7;
	pIndices[9]=0,pIndices[10]=7,pIndices[11]=4;

	//左侧面
	pIndices[12]=0,pIndices[13]=4,pIndices[14]=5;
	pIndices[15]=0,pIndices[16]=5,pIndices[17]=1;

	//右侧面
	pIndices[18]=2,pIndices[19]=6,pIndices[20]=7;
	pIndices[21]=2,pIndices[22]=7,pIndices[23]=3;

	//背面
	pIndices[24]=2,pIndices[25]=5,pIndices[26]=6;
	pIndices[27]=2,pIndices[28]=1,pIndices[29]=5;

	//底面
	pIndices[30]=4,pIndices[31]=6,pIndices[32]=5;
	pIndices[33]=4,pIndices[34]=7,pIndices[35]=6;
	g_pIndexBuf->Unlock();
}
Ejemplo n.º 9
0
void object::CreateSphere(FLOAT fRadius, WORD wNumRings, WORD wNumSections)
{
    FLOAT x, y, z, v, rsintheta; // Temporary variables
    WORD  i, j, n, m;            // counters
    D3DXVECTOR3 vPoint;

//	SAFE_RELEASE(m_pSphereVertexBuffer);
//	SAFE_RELEASE(m_pSphereIndexBuffer);

    //Generate space for the required triangles and vertices.
    WORD       wNumTriangles = (wNumRings + 1) * wNumSections * 2;
    DWORD      dwNumIndices   = wNumTriangles*3;
    DWORD      dwNumVertices  = (wNumRings + 1) * wNumSections + 2;

//	m_dwNumVertices = dwNumVertices;
//	m_dwNumIndices = wNumTriangles * 3;
//	m_dwNumFaces = wNumTriangles;	

	d3ddevice->CreateVertexBuffer(dwNumVertices * sizeof(CUSTOMVERTEX), 0, 0, D3DPOOL_MANAGED, &vertex_buffer);
	d3ddevice->CreateIndexBuffer(3 * wNumTriangles * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &index_buffer);
	size = dwNumVertices;
	size2 = wNumTriangles;

	CUSTOMVERTEX* pVertices;
	WORD* pIndices;
	
	vertex_buffer->Lock(0, 0, (BYTE**)&pVertices, 0);
	index_buffer->Lock(0, 0, (BYTE**)&pIndices, 0);

    // Generate vertices at the top and bottom points.
    D3DXVECTOR3 vTopPoint  = D3DXVECTOR3( 0.0f, +fRadius, 0.0f);
    D3DXVECTOR3 vBotPoint  = D3DXVECTOR3( 0.0f, -fRadius, 0.0f);
    D3DXVECTOR3 vNormal = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );

    pVertices[0]               = CUSTOMVERTEX(vTopPoint.x, vTopPoint.y, vTopPoint.z,vNormal.x, vNormal.y, vNormal.z,0.0f, 0.0f );
    pVertices[dwNumVertices-1] = CUSTOMVERTEX(vBotPoint.x, vBotPoint.y, vBotPoint.z,-vNormal.x, -vNormal.y, -vNormal.z,0.0f, 0.0f );

    // Generate vertex points for rings
    FLOAT dtheta = (float)(D3DX_PI / (wNumRings + 2));     //Angle between each ring
    FLOAT dphi   = (float)(2*D3DX_PI / (wNumSections)); //Angle between each section
    FLOAT theta  = dtheta;
    n = 1; //vertex being generated, begins at 1 to skip top point

    for( i = 0; i < (wNumRings+1); i++ )
    {
        y = fRadius * (float)cos(theta); // y is the same for each ring
        v = theta / D3DX_PI;     // v is the same for each ring
        rsintheta = fRadius * (float)sin(theta);
        FLOAT phi = 0.0f;

        for( j = 0; j < wNumSections; j++ )
        {
            x = rsintheta * (float)sin(phi);
            z = rsintheta * (float)cos(phi);
        
            FLOAT u = 1.0f - (FLOAT)(phi / (2 * D3DX_PI) );
            vPoint        = D3DXVECTOR3( x, y, z );
            vNormal       = D3DXVECTOR3( x/fRadius, y/fRadius, z/fRadius );
			D3DXVec3Normalize(&vNormal, &vNormal);
			pVertices[n] = CUSTOMVERTEX( vPoint.x, vPoint.y, vPoint.z, vNormal.x, vNormal.y, vNormal.z, u, v );

            phi += dphi;
            ++n;
        }
        theta += dtheta;
    }

    // Generate triangles for top and bottom caps.
    for( i = 0; i < wNumSections; i++ )
    {
        pIndices[3*i+0] = 0;
        pIndices[3*i+1] = i + 1;
        pIndices[3*i+2] = 1 + ((i + 1) % wNumSections);

        pIndices[3*(wNumTriangles - wNumSections + i)+0] = (WORD)( dwNumVertices - 1 );
        pIndices[3*(wNumTriangles - wNumSections + i)+1] = (WORD)( dwNumVertices - 2 - i );
        pIndices[3*(wNumTriangles - wNumSections + i)+2] = (WORD)( dwNumVertices - 2 - 
                ((1 + i) % wNumSections) );
    }

    // Generate triangles for the rings
   m = 1;            // first vertex in current ring,begins at 1 to skip top point
    n = wNumSections; // triangle being generated, skip the top cap 
        
    for( i = 0; i < wNumRings; i++ )
    {
        for( j = 0; j < (wNumSections); j++ )
        {
            pIndices[3*n+0] = m + j;
            pIndices[3*n+1] = m + wNumSections + j;
            pIndices[3*n+2] = m + wNumSections + ((j + 1) % wNumSections);
            
            pIndices[3*(n+1)+0] = pIndices[3*n+0];
            pIndices[3*(n+1)+1] = pIndices[3*n+2];
            pIndices[3*(n+1)+2] = m + ((j + 1) % wNumSections);
            
            n += 2;
        }
      
        m += wNumSections;
    }

	vertex_buffer->Unlock();
	index_buffer->Unlock();

}
Ejemplo n.º 10
0
HRESULT Objects_Init()
{
	//创建字体
	D3DXCreateFont(g_pd3dDevice, 36, 0, 0, 1000, false, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("Calibri"), &g_pTextFPS);
	D3DXCreateFont(g_pd3dDevice, 20, 0, 1000, 0, false, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, L"华文中宋", &g_pTextAdaperName);
	D3DXCreateFont(g_pd3dDevice, 23, 0, 1000, 0, false, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, L"微软雅黑", &g_pTextHelper);
	D3DXCreateFont(g_pd3dDevice, 26, 0, 1000, 0, false, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, L"黑体", &g_pTextInfor);

	//--------------------------------------------------------------------------------------
	// 创建顶点缓存和索引缓存
	//--------------------------------------------------------------------------------------
	//创建顶点缓存
	if (FAILED(g_pd3dDevice->CreateVertexBuffer(24 * sizeof(CUSTOMVERTEX),
		0, D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL)))
	{
		return E_FAIL;
	}
	// 创建索引缓存
	if (FAILED(g_pd3dDevice->CreateIndexBuffer(36 * sizeof(WORD), 0,
		D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIndexBuffer, NULL)))
	{
		return E_FAIL;

	}
	//--------------------------------------------------------------------------------------
	// 【纹理绘制四步曲之二】:顶点的访问	
	//--------------------------------------------------------------------------------------
	//填充顶点缓存
	CUSTOMVERTEX* pVertices;
	if (FAILED(g_pVertexBuffer->Lock(0, 24 * sizeof(CUSTOMVERTEX), (void**)&pVertices, 0)))
		return E_FAIL;

	// 正面顶点数据
	pVertices[0] = CUSTOMVERTEX(-10.0f, 10.0f, -10.0f, 0.0f, 0.0f);
	pVertices[1] = CUSTOMVERTEX(10.0f, 10.0f, -10.0f, 1.0f, 0.0f);
	pVertices[2] = CUSTOMVERTEX(10.0f, -10.0f, -10.0f, 1.0f, 1.0f);
	pVertices[3] = CUSTOMVERTEX(-10.0f, -10.0f, -10.0f, 0.0f, 1.0f);

	// 背面顶点数据
	pVertices[4] = CUSTOMVERTEX(10.0f, 10.0f, 10.0f, 0.0f, 0.0f);
	pVertices[5] = CUSTOMVERTEX(-10.0f, 10.0f, 10.0f, 1.0f, 0.0f);
	pVertices[6] = CUSTOMVERTEX(-10.0f, -10.0f, 10.0f, 1.0f, 1.0f);
	pVertices[7] = CUSTOMVERTEX(10.0f, -10.0f, 10.0f, 0.0f, 1.0f);

	// 顶面顶点数据
	pVertices[8] = CUSTOMVERTEX(-10.0f, 10.0f, 10.0f, 0.0f, 0.0f);
	pVertices[9] = CUSTOMVERTEX(10.0f, 10.0f, 10.0f, 1.0f, 0.0f);
	pVertices[10] = CUSTOMVERTEX(10.0f, 10.0f, -10.0f, 1.0f, 1.0f);
	pVertices[11] = CUSTOMVERTEX(-10.0f, 10.0f, -10.0f, 0.0f, 1.0f);

	// 底面顶点数据
	pVertices[12] = CUSTOMVERTEX(-10.0f, -10.0f, -10.0f, 0.0f, 0.0f);
	pVertices[13] = CUSTOMVERTEX(10.0f, -10.0f, -10.0f, 1.0f, 0.0f);
	pVertices[14] = CUSTOMVERTEX(10.0f, -10.0f, 10.0f, 1.0f, 1.0f);
	pVertices[15] = CUSTOMVERTEX(-10.0f, -10.0f, 10.0f, 0.0f, 1.0f);

	// 左侧面顶点数据
	pVertices[16] = CUSTOMVERTEX(-10.0f, 10.0f, 10.0f, 0.0f, 0.0f);
	pVertices[17] = CUSTOMVERTEX(-10.0f, 10.0f, -10.0f, 1.0f, 0.0f);
	pVertices[18] = CUSTOMVERTEX(-10.0f, -10.0f, -10.0f, 1.0f, 1.0f);
	pVertices[19] = CUSTOMVERTEX(-10.0f, -10.0f, 10.0f, 0.0f, 1.0f);

	// 右侧面顶点数据
	pVertices[20] = CUSTOMVERTEX(10.0f, 10.0f, -10.0f, 0.0f, 0.0f);
	pVertices[21] = CUSTOMVERTEX(10.0f, 10.0f, 10.0f, 1.0f, 0.0f);
	pVertices[22] = CUSTOMVERTEX(10.0f, -10.0f, 10.0f, 1.0f, 1.0f);
	pVertices[23] = CUSTOMVERTEX(10.0f, -10.0f, -10.0f, 0.0f, 1.0f);

	g_pVertexBuffer->Unlock();

	// 填充索引数据
	WORD *pIndices = NULL;
	g_pIndexBuffer->Lock(0, 0, (void**)&pIndices, 0);

	// 正面索引数据
	pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
	pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;

	// 背面索引数据
	pIndices[6] = 4; pIndices[7] = 5; pIndices[8] = 6;
	pIndices[9] = 4; pIndices[10] = 6; pIndices[11] = 7;

	// 顶面索引数据
	pIndices[12] = 8; pIndices[13] = 9; pIndices[14] = 10;
	pIndices[15] = 8; pIndices[16] = 10; pIndices[17] = 11;

	// 底面索引数据
	pIndices[18] = 12; pIndices[19] = 13; pIndices[20] = 14;
	pIndices[21] = 12; pIndices[22] = 14; pIndices[23] = 15;

	// 左侧面索引数据
	pIndices[24] = 16; pIndices[25] = 17; pIndices[26] = 18;
	pIndices[27] = 16; pIndices[28] = 18; pIndices[29] = 19;

	// 右侧面索引数据
	pIndices[30] = 20; pIndices[31] = 21; pIndices[32] = 22;
	pIndices[33] = 20; pIndices[34] = 22; pIndices[35] = 23;

	g_pIndexBuffer->Unlock();

	//--------------------------------------------------------------------------------------
	// 【纹理绘制四步曲之三】:纹理的创建
	//--------------------------------------------------------------------------------------
	D3DXCreateTextureFromFile(g_pd3dDevice, L"pal5q.jpg", &g_pTexture);

	// 设置材质
	D3DMATERIAL9 mtrl;
	::ZeroMemory(&mtrl, sizeof(mtrl));
	mtrl.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	mtrl.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	mtrl.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	g_pd3dDevice->SetMaterial(&mtrl);

	D3DLIGHT9 light;
	::ZeroMemory(&light, sizeof(light));
	light.Type = D3DLIGHT_DIRECTIONAL;
	light.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	light.Specular = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
	light.Direction = D3DXVECTOR3(1.0f, 1.0f, 0.0f);
	g_pd3dDevice->SetLight(0, &light); //设置光源  
	g_pd3dDevice->LightEnable(0, true);//启用光照  

	// 设置渲染状态
	g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);   //初始化顶点法线
	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);   //开启背面消隐
	g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(36, 36, 36));   //设置一下环境光 

	return S_OK;
}
//-----------------------------------【Object_Init( )函数】--------------------------------------
//	描述:渲染资源初始化函数,在此函数中进行要被渲染的物体的资源的初始化
//--------------------------------------------------------------------------------------------------
HRESULT Objects_Init(HWND hwnd)
{
	//创建字体
	if(FAILED(D3DXCreateFont(g_pd3dDevice, 36, 0, 0, 1000, false, DEFAULT_CHARSET, 
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("Calibri"), &g_pTextFPS)))
		return E_FAIL;
	if (FAILED(D3DXCreateFont(g_pd3dDevice, 20, 0, 1000, 0, false, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("华文中宋"), &g_pTextAdaperName)))
		return E_FAIL;
	if (FAILED(D3DXCreateFont(g_pd3dDevice, 23, 0, 1000, 0, false, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("微软雅黑"), &g_pTextHelper)))
		return E_FAIL;
	if (FAILED(D3DXCreateFont(g_pd3dDevice, 26, 0, 1000, 0, false, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("黑体"), &g_pTextInfo)))
		return E_FAIL;

	LPD3DXBUFFER pAdjBuffer = NULL;
	LPD3DXBUFFER pMtrlBuffer = NULL;

	if (FAILED(D3DXLoadMeshFromX(L"Warden.X", D3DXMESH_MANAGED, g_pd3dDevice, &pAdjBuffer, &pMtrlBuffer, NULL, &g_dwNumMtrls, &g_pMesh)))
	//if (FAILED(D3DXLoadMeshFromX(L"WYJ.X", D3DXMESH_MANAGED, g_pd3dDevice, &pAdjBuffer, &pMtrlBuffer, NULL, &g_dwNumMtrls, &g_pMesh)))
		return E_FAIL;

	D3DXMATERIAL *pMtrls = (D3DXMATERIAL *)pMtrlBuffer->GetBufferPointer();
	g_pMaterials = new D3DMATERIAL9[g_dwNumMtrls];
	g_pTextures = new LPDIRECT3DTEXTURE9[g_dwNumMtrls];

	for (DWORD i = 0; i < g_dwNumMtrls; i++)
	{
		g_pMaterials[i] = pMtrls[i].MatD3D;
		g_pMaterials[i].Ambient = g_pMaterials[i].Diffuse;

		g_pTextures[i] = NULL;
		D3DXCreateTextureFromFileA(g_pd3dDevice, pMtrls[i].pTextureFilename, &g_pTextures[i]);
	}

	SAFE_RELEASE(pAdjBuffer);
	SAFE_RELEASE(pMtrlBuffer);

	g_pd3dDevice->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &g_pVertexBuffer, 0);
	CUSTOMVERTEX *pVertices = NULL;
	g_pVertexBuffer->Lock(0, 0, (void **)&pVertices, 0);
	pVertices[0] = CUSTOMVERTEX(-500.0f, 0.0f, -500.0f, 0.0f, 1.0f, 0.0f, 0.0f, 50.0f);
	pVertices[1] = CUSTOMVERTEX(-500.0f, 0.0f, 500.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
	pVertices[2] = CUSTOMVERTEX(500.0f, 0.0f, -500.0f, 0.0f, 1.0f, 0.0f, 50.0f, 50.0f);
	pVertices[3] = CUSTOMVERTEX(500.0f, 0.0f, 500.0f, 0.0f, 1.0f, 0.0f, 50.0f, 0.0f);
	g_pVertexBuffer->Unlock();

	D3DXCreateTextureFromFile(g_pd3dDevice, L"grass.jpg", &g_pTexture);

	D3DXCreateCylinder(g_pd3dDevice, 10.0f, 10.0f, 500.0f, 60, 60, &g_cylinder, 0);

	g_MaterialCylinder.Ambient = D3DXCOLOR(0.9f, 0.0f, 0.8f, 1.0f);
	g_MaterialCylinder.Diffuse = D3DXCOLOR(0.9f, 0.0f, 0.8f, 1.0f);
	g_MaterialCylinder.Specular = D3DXCOLOR(0.9f, 0.2f, 0.9f, 0.9f);
	g_MaterialCylinder.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.9f, 1.0f);

	D3DLIGHT9 light;
	ZeroMemory(&light, sizeof(light));
	light.Type = D3DLIGHT_DIRECTIONAL;
	light.Ambient = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f);
	light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	light.Specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
	light.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
	g_pd3dDevice->SetLight(0, &light);
	g_pd3dDevice->LightEnable(0, true);
	g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
	g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);

	g_pCamera = new CameraClass(g_pd3dDevice);
	g_pCamera->SetCameraPosition(&D3DXVECTOR3(0.0f, 200.0f, -300.0f));
	g_pCamera->SetTargetPosition(&D3DXVECTOR3(0.0f, 300.0f, 0.0f));
	g_pCamera->SetViewMatrix();
	g_pCamera->SetProjMatrix();

	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);


	//g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

	return S_OK;
}