Example #1
0
void TriGridDemo::buildGeoBuffers()
{
	std::vector<D3DXVECTOR3> verts;
	std::vector<DWORD> indices;

	GenTriGrid(100, 100, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices);

	mNumVertices  = 100*100;
	mNumTriangles = 99*99*2;

	HR(gd3dDevice->CreateVertexBuffer(mNumVertices * sizeof(VertexPos),
		D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mVB, 0));

	VertexPos *v = 0;
	HR(mVB->Lock(0, 0, (void**)&v, 0));
	for (DWORD i = 0; i < mNumVertices; ++i) v[i] = verts[i];
	HR(mVB->Unlock());

	HR(gd3dDevice->CreateIndexBuffer(mNumTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));

	WORD *k = 0;
	HR(mIB->Lock(0, 0, (void**)&k, 0));
	for (DWORD i = 0; i < mNumTriangles*3; ++i) k[i] = (WORD)indices[i];
	HR(mIB->Unlock());
}
Example #2
0
bool CIndexBuffer::Cache(CDisplayDevice *poDisplayDevice) {
#if RENDER == DX9
	IDirect3DIndexBuffer9* IB;

	int len = sizeof(unsigned short) * m_iNumIndices; 
	HRESULT result = poDisplayDevice->m_pDevice->CreateIndexBuffer(
		len, 
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16, 
		D3DPOOL_SYSTEMMEM,
		&IB, 
		0);

	if (result != D3D_OK)
		return false;

	char *buffer;
	IB->Lock(0, 0, (void **)&buffer, 0);
	memcpy(buffer, GetIndexBuffer(), len);
	IB->Unlock();

	m_pCachedData = IB;
#endif

	return true;
}
Example #3
0
void Impl::Unlock(BytesPtr srcBytes, BytesPtr dstBytes)
{
    bytes::read_proc rp(srcBytes);
    ProxyId id;
    rp(id);
    
    IDirect3DIndexBuffer9 *self = procMap_->getPtr<IDirect3DIndexBuffer9>(id);
    
    UINT offset, size;
    DWORD flags;

    rp(offset);
    rp(size);
    rp(flags);
    char *ptr = nullptr;

    HRESULT res;
    
    res = self->Lock(offset, size, reinterpret_cast<void**>(&ptr), flags);
    if (SUCCEEDED(res))
    {
        rp.array(ptr, size);
        res = self->Unlock();
        Assert(SUCCEEDED(res));
    }
    else
    {
        vector<char> dump(size);
        rp.array(dump.data(), size);
        Assert(false);
    }

    bytes::write_proc wp(dstBytes);
    wp(res);
}
Example #4
0
void CubeDemo::buildIndexBuffer()
{
	HR(gd3dDevice->CreateIndexBuffer(36*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));

	WORD *k = 0;
	HR(mIB->Lock(0, 0, (void**)&k, 0));

	// front
	k[0] = 0; k[1] = 1; k[2] = 2;
	k[3] = 0; k[4] = 2; k[5] = 3;

	// back
	k[6] = 4; k[7] = 6; k[8] = 5;
	k[9] = 4; k[10] = 7; k[11] = 6;

	// left
	k[12] = 4; k[13] = 5; k[14] = 1;
	k[15] = 4; k[16] = 1; k[17] = 0;

	// right
	k[18] = 3; k[19] = 2; k[20] = 6;
	k[21] = 3; k[22] = 6; k[23] = 7;

	// top
	k[24] = 1; k[25] = 5; k[26] = 6;
	k[27] = 1; k[28] = 6; k[29] = 2;

	k[30] = 4; k[31] = 0; k[32] = 3;
	k[33] = 4; k[34] = 3; k[35] = 7;

	HR(mIB->Unlock());
}
Example #5
0
void MultiTexDemo::buildGridGeometry()
{
	std::vector<D3DXVECTOR3> verts;
	std::vector<DWORD> indices;

	GenTriGrid(100, 100, 1.0f, 1.0f, 
		D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices);

	// Save vertex count and triangle count for DrawIndexedPrimitive arguments.
	mNumGridVertices  = 100*100;
	mNumGridTriangles = 99*99*2;

	// Obtain a pointer to a new vertex buffer.
	HR(gd3dDevice->CreateVertexBuffer(mNumGridVertices * sizeof(VertexPNT), 
		D3DUSAGE_WRITEONLY,	0, D3DPOOL_MANAGED, &mGridVB, 0));

	// Now lock it to obtain a pointer to its internal data, and write the
	// grid's vertex data.
	VertexPNT* v = 0;
	HR(mGridVB->Lock(0, 0, (void**)&v, 0));

	float w = 99.0f; 
	float d = 99.0f;
	for(int i = 0; i < 100; ++i)
	{
		for(int j = 0; j < 100; ++j)
		{
			DWORD index = i * 100 + j;
			v[index].pos    = verts[index];
			v[index].normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
			v[index].tex0.x = (v[index].pos.x + (0.5f*w)) / w;
			v[index].tex0.y = (v[index].pos.z - (0.5f*d)) / -d;
		}
	}

	HR(mGridVB->Unlock());


	// Obtain a pointer to a new index buffer.
	HR(gd3dDevice->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16, D3DPOOL_MANAGED, &mGridIB, 0));

	// Now lock it to obtain a pointer to its internal data, and write the
	// grid's index data.

	WORD* k = 0;
	HR(mGridIB->Lock(0, 0, (void**)&k, 0));

	for(DWORD i = 0; i < mNumGridTriangles*3; ++i)
		k[i] = (WORD)indices[i];

	HR(mGridIB->Unlock());
}
Example #6
0
MF_API void MFVertex_LockIndexBuffer(MFIndexBuffer *pIndexBuffer, uint16 **ppIndices)
{
	MFDebug_Assert(!pIndexBuffer->bLocked, "Index buffer already locked!");

	IDirect3DIndexBuffer9 *pIB = (IDirect3DIndexBuffer9*)pIndexBuffer->pPlatformData;
	HRESULT hr = pIB->Lock(0, sizeof(uint16)*pIndexBuffer->numIndices, &pIndexBuffer->pLocked, 0);
	MFDebug_Assert(SUCCEEDED(hr), "Failed to lock index buffer");

	if(ppIndices)
		*ppIndices = (uint16*)pIndexBuffer->pLocked;

	pIndexBuffer->bLocked = true;
}
Example #7
0
void SpotlightDemo::buildGeoBuffers()
{
	std::vector<D3DXVECTOR3> verts;
	std::vector<DWORD> indices;

	GenTriGrid(100, 100, 1.0f, 1.0f, 
		D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices);

	// Save vertex count and triangle count for DrawIndexedPrimitive arguments.
	mNumGridVertices  = 100*100;
	mNumGridTriangles = 99*99*2;

	// Obtain a pointer to a new vertex buffer.
	HR(gd3dDevice->CreateVertexBuffer(mNumGridVertices * sizeof(VertexPN), 
		D3DUSAGE_WRITEONLY,	0, D3DPOOL_MANAGED, &mVB, 0));

	// Now lock it to obtain a pointer to its internal data, and write the
	// grid's vertex data.
	VertexPN* v = 0;
	HR(mVB->Lock(0, 0, (void**)&v, 0));

	for(DWORD i = 0; i < mNumGridVertices; ++i)
	{
		v[i].pos = verts[i];
		v[i].normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	}

	HR(mVB->Unlock());


	// Obtain a pointer to a new index buffer.
	HR(gd3dDevice->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));

	// Now lock it to obtain a pointer to its internal data, and write the
	// grid's index data.

	WORD* k = 0;
	HR(mIB->Lock(0, 0, (void**)&k, 0));

	for(DWORD i = 0; i < mNumGridTriangles*3; ++i)
		k[i] = (WORD)indices[i];

	HR(mIB->Unlock());
}
Example #8
0
    IndexBufferLock Gfx_IndexBuffer_Lock(RenderDevice* dev, IndexBufferHandle h, uint32 offset, uint32 size) 
    { 
        IndexBufferLock lock;

        if( h.valid() )
        {
            lock.size = size;
            lock.handle = h;

            uint32 flags = buffer_mode_to_d3d_lock_flags(dev->resources->index_buffers[h].desc.mode);

            IDirect3DIndexBuffer9* ib = dev->resources->index_buffers[h].native;
            HRESULT hr = ib->Lock(offset, size, &lock.data, flags);
            validate_d3d_result(hr);
        }

        return lock;
    };
//----------------------------------------------------------------------------
void PdrIndexBuffer::Disable (Renderer* renderer)
{
	IDirect3DDevice9* device = renderer->mData->mDevice;
	HRESULT hr;
	PX2_UNUSED(hr);

#ifdef PX2_PDR_DEBUG
	// 检测需要被取消激活的数据,是否匹配。
	IDirect3DIndexBuffer9 *activeBuffer = 0;
	hr = device->GetIndices(&activeBuffer);
	assertion(hr == D3D_OK, "Failed to get indices: %s\n",
		DXGetErrorString(hr));
	assertion(activeBuffer == mBuffer, "Mismatched index buffers\n");
	activeBuffer->Release();
#endif

	hr = device->SetIndices(0);
	assertion(hr == D3D_OK, "Failed to set indices: %s\n",
		DXGetErrorString(hr));
}
Example #10
0
MF_API void MFVertex_UnlockIndexBuffer(MFIndexBuffer *pIndexBuffer)
{
	MFDebug_Assert(pIndexBuffer->bLocked, "Index buffer not locked!");

	IDirect3DIndexBuffer9 *pIB = (IDirect3DIndexBuffer9*)pIndexBuffer->pPlatformData;
	pIB->Unlock();

	pIndexBuffer->pLocked = NULL;
	pIndexBuffer->bLocked = false;
/*
	// why on earth did I defer the index locking to a post process???
	IDirect3DIndexBuffer9 *pIB = (IDirect3DIndexBuffer9*)pIndexBuffer->pPlatformData;

	void *pData;
	pIB->Lock(0, 0, &pData, 0);
	MFCopyMemory(pData, pIndexBuffer->pIndices, sizeof(uint16)*pIndexBuffer->numIndices);
	pIB->Unlock();

	pIndexBuffer->bLocked = false;
*/
}
Example #11
0
void Terrain::AlllocateBuffers()
{
	IDirect3DVertexBuffer9* vb;
	d3dContext->CreateVertexBuffer(
		m_uNumVertices*sizeof(TexVertex),
		D3DUSAGE_WRITEONLY,
		TexVertex::FVF_TEX,
		D3DPOOL_MANAGED,
		&vb,
		0);
	m_spVetexBuffer.reset(vb, [](IDirect3DVertexBuffer9* vb) {vb->Release(); });

	IDirect3DIndexBuffer9* ib;
	d3dContext->CreateIndexBuffer(
		m_uNumTriangles * 3 * sizeof(DWORD),
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX32,
		D3DPOOL_MANAGED,
		&ib,
		0);
	m_spIndexBuffer.reset(ib, [](IDirect3DIndexBuffer9* ib) {ib->Release(); });
}
Example #12
0
bool MFVertex_CreateIndexBufferPlatformSpecific(MFIndexBuffer *pIndexBuffer, uint16 *pIndexBufferMemory)
{
	IDirect3DIndexBuffer9 *pIB;
	HRESULT hr = pd3dDevice->CreateIndexBuffer(sizeof(uint16)*pIndexBuffer->numIndices, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &pIB, NULL);
	MFDebug_Assert(SUCCEEDED(hr), "Failed to create index buffer");
	if(FAILED(hr))
		return false;

	if(pIndexBuffer->pName)
		MFRenderer_D3D9_SetDebugName(pIB, pIndexBuffer->pName);
	pIndexBuffer->pPlatformData = pIB;

	if(pIndexBufferMemory)
	{
		void *pData;
		pIB->Lock(0, 0, &pData, 0);
		MFCopyMemory(pData, pIndexBufferMemory, sizeof(uint16)*pIndexBuffer->numIndices);
		pIB->Unlock();
	}

	return true;
}
Example #13
0
void DiffuseCubeDemo::buildIndexBuffer()
{
	// Obtain a pointer to a new index buffer.
	HR(gd3dDevice->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));

	// Now lock it to obtain a pointer to its internal data, and write the
	// cube's index data.

	WORD* k = 0;

	HR(mIB->Lock(0, 0, (void**)&k, 0));

	// fill in the front face index data
	k[0] = 0; k[1] = 1; k[2] = 2;
	k[3] = 0; k[4] = 2; k[5] = 3;

	// fill in the back face index data
	k[6] = 4; k[7]  = 5; k[8]  = 6;
	k[9] = 4; k[10] = 6; k[11] = 7;

	// fill in the top face index data
	k[12] = 8; k[13] =  9; k[14] = 10;
	k[15] = 8; k[16] = 10; k[17] = 11;

	// fill in the bottom face index data
	k[18] = 12; k[19] = 13; k[20] = 14;
	k[21] = 12; k[22] = 14; k[23] = 15;

	// fill in the left face index data
	k[24] = 16; k[25] = 17; k[26] = 18;
	k[27] = 16; k[28] = 18; k[29] = 19;

	// fill in the right face index data
	k[30] = 20; k[31] = 21; k[32] = 22;
	k[33] = 20; k[34] = 22; k[35] = 23;

	HR(mIB->Unlock());
}
Example #14
0
void GateDemo::buildGateGeometry()
{
	// Gate is just a rectangle aligned with the xy-plane.


	// Obtain a pointer to a new vertex buffer.
	HR(gd3dDevice->CreateVertexBuffer(4* sizeof(VertexPNT), 
		D3DUSAGE_WRITEONLY,	0, D3DPOOL_MANAGED, &mGateVB, 0));

	// Now lock it to obtain a pointer to its internal data, and write the
	// grid's vertex data.
	VertexPNT* v = 0;
	HR(mGateVB->Lock(0, 0, (void**)&v, 0));

	// Scale texture coordinates by 4 units in the v-direction for tiling.
	v[0] = VertexPNT(-20.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
	v[1] = VertexPNT(-20.0f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
	v[2] = VertexPNT( 20.0f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 4.0f, 0.0f);
	v[3] = VertexPNT( 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 4.0f, 1.0f);
	HR(mGateVB->Unlock());


	// Obtain a pointer to a new index buffer.
	HR(gd3dDevice->CreateIndexBuffer(6*sizeof(WORD), D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16, D3DPOOL_MANAGED, &mGateIB, 0));

	// Now lock it to obtain a pointer to its internal data, and write the
	// grid's index data.

	WORD* k = 0;
	HR(mGateIB->Lock(0, 0, (void**)&k, 0));

	k[0] = 0;  k[1] = 1;  k[2] = 2; // Triangle 0
	k[3] = 0;  k[4] = 2;  k[5] = 3; // Triangle 1

	HR(mGateIB->Unlock());
}
Example #15
0
void CMesh3D::Release()
{
	if ( m_IndexBuffer )
		m_IndexBuffer->Release();
	if ( m_VertexBuffer )
		m_VertexBuffer->Release();

	if ( m_pMeshMaterial )
		delete[] m_pMeshMaterial;
	if ( m_pMeshTextura )
	{
		for ( int i = 1; i < m_TexturCount; ++i )
		{
			if ( m_pMeshTextura[i] )
				m_pMeshTextura[i] -> Release();
		}
		delete []m_pMeshTextura;
	}
	if ( m_pMesh )
		m_pMesh -> Release();
}
Example #16
0
bool StripifyMeshSubset(ID3DXMesh* mesh,
                        DWORD attribId,
                        ostream& meshfile)
{
    // TODO: Fall back to tri lists if the strip size is too small
    // TODO: Detect when a tri fan should be used instead of a tri list

    // Convert to tri strips
    IDirect3DIndexBuffer9* indices = NULL;
    DWORD numIndices = 0;
    ID3DXBuffer* strips = NULL;
    DWORD numStrips = 0;
    HRESULT hr;
    hr = D3DXConvertMeshSubsetToStrips(mesh,
                                       attribId,
                                       0,
                                       &indices,
                                       &numIndices,
                                       &strips,
                                       &numStrips);
    if (FAILED(hr))
    {
        cout << "Stripify failed\n";
        return false;
    }

    cout << "Converted to " << numStrips << " strips\n";
    cout << "Strip buffer size: " << strips->GetBufferSize() << '\n';
    if (numStrips != strips->GetBufferSize() / 4) 
    {
        cout << "Strip count is incorrect!\n";
        return false;
    }

    bool index32 = false;
    {
        D3DINDEXBUFFER_DESC desc;
        indices->GetDesc(&desc);
        if (desc.Format == D3DFMT_INDEX32)
        {
            index32 = true;
        }
        else if (desc.Format == D3DFMT_INDEX16)
        {
            index32 = false;
        }
        else
        {
            cout << "Bad index format.  Strange.\n";
            return false;
        }
    }

    void* indexData = NULL;
    hr = indices->Lock(0, 0, &indexData, D3DLOCK_READONLY);
    if (FAILED(hr))
    {
        cout << "Failed to lock index buffer: " << D3DErrorString(hr) << '\n';
        return false;
    }

    {
        DWORD* stripLengths = reinterpret_cast<DWORD*>(strips->GetBufferPointer());
        int k = 0;
        for (int i = 0; i < numStrips; i++)
        {
            if (stripLengths[i] == 0)
            {
                cout << "Bad triangle strip (length == 0) in mesh!\n";
                return false;
            }

            if (index32)
            {
                DWORD* indices = reinterpret_cast<DWORD*>(indexData) + k;
                int fanStart = checkForFan(stripLengths[i], indices);
                if (fanStart != 1)
                {
                    DumpTriStrip(stripLengths[i], indices, (int) attribId,
                                 meshfile);
                }
                else
                {
                    DumpTriStripAsFan(stripLengths[i], indices, (int) attribId,
                                      fanStart, meshfile);
                }
            }
            else
            {
                WORD* indices = reinterpret_cast<WORD*>(indexData) + k;
                int fanStart = checkForFan(stripLengths[i], indices);
                if (fanStart != 1)
                {
                    DumpTriStrip(stripLengths[i], indices, (int) attribId,
                                 meshfile);
                }
                else
                {
                    DumpTriStripAsFan(stripLengths[i], indices, (int) attribId,
                                      fanStart, meshfile);
                }
            }

            k += stripLengths[i] + 2;
        }

        cout << "k=" << k << ", numIndices=" << numIndices;
        if (index32)
            cout << ", 32-bit indices\n";
        else
            cout << ", 16-bit indices\n";
    }

    return true;
}
Example #17
0
void Evolution::drawLifeforms()
{
    if (!mbLStart)
    {
        mLVB->Release();
        mLIB->Release();
    }

    int Number = 0;
    int LNumber = 0;

    const D3DXVECTOR3 baseV[4] = {D3DXVECTOR3(-1.00000f, -1.00000f, 0.0f),
                                  D3DXVECTOR3(-1.00000f,  1.00000f, 0.0f),
                                  D3DXVECTOR3( 1.00000f,  1.00000f, 0.0f),
                                  D3DXVECTOR3( 1.00000f, -1.00000f, 0.0f)
                                 };

    const D3DXVECTOR2 baseT[4] = {D3DXVECTOR2(0.0f, 1.0f),
                                  D3DXVECTOR2(0.0f, 0.0f),
                                  D3DXVECTOR2(1.0f, 0.0f),
                                  D3DXVECTOR2(1.0f, 1.0f)
                                 };

    HR(gd3dDevice->CreateVertexBuffer((lifeformList.size()) *4* sizeof(VertexPTL), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mLVB, 0));
    VertexPTL * lv = 0;
    HR(mLVB->Lock(0, 0, (void**)&lv, 0));

    for (std::list<Lifeform>::iterator it = lifeformList.begin(); it != lifeformList.end(); ++it)
    {
        Stat stat = it->getStat();
        float cParts = (float)(stat.carnivoreParts)/10.0f;
        if (stat.carnivoreParts >= 10)
            cParts = 1.0f;

        float sightDist = (stat.sightDistance)/10.0f;
        if (stat.sightDistance >= 10.0f)
            sightDist = 1.0f;
        if (stat.sightDistance >= 10.0f && !(stat.carnivore))
            cParts = 0.0f;



        D3DXVECTOR3 pos = it->getPosition();
        D3DXMATRIX T, R, S, F;
        D3DXMatrixRotationZ(&R, it->getRotation());
        D3DXVECTOR3 V[4];
        D3DXMatrixTranslation(&T, pos.x, pos.y, pos.z);
        D3DXMatrixScaling(&S, 8.0f, 8.0f, 0.0f);
        F = S*(R*T);

        for (int k=0; k<4; ++k)
            D3DXVec3TransformCoord(&V[k], &baseV[k], &F);

        for (int k=0; k<4; ++k)
            lv[Number+k]   = VertexPTL( V[k], baseT[k], cParts, sightDist);

        /*v[Number]   = VertexPTID(-1.0f, -1.0f, 0.0f, 0.0f, 1.0f, F, 2.0f);
        v[Number+1] = VertexPTID(-1.0f,  1.0f, 0.0f, 0.0f, 0.0f, F, 2.0f);
        v[Number+2] = VertexPTID( 1.0f,  1.0f, 0.0f, 1.0f, 0.0f, F, 2.0f);
        v[Number+3] = VertexPTID( 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, F, 2.0f);*/

        //++lv;
        Number+=4;
        ++LNumber;
    }

    HR(mLVB->Unlock());
    //Number = 0;

    HR(gd3dDevice->CreateIndexBuffer(lifeformList.size() *6* sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &mLIB, 0));

    WORD* lind = 0;
    HR(mLIB->Lock(0, 0, (void**)&lind, 0));
    int k=0;
    int g=0;
    for (int n=0; n<LNumber; ++n)
    {
        lind[k] = g;
        lind[k+1] = g+1;
        lind[k+2] = g+2;
        lind[k+3] = g;
        lind[k+4] = g+2;
        lind[k+5] = g+3;
        k+=6;
        g+=4;
    }

    HR(mLIB->Unlock());


    HR(mFX->SetTechnique(mhLTech));
    HR(mFX->SetTexture(mhTex,  mLifeformTex));
    HR(mFX->SetTexture(mhTex2, mCarnTex));
    HR(mFX->SetTexture(mhTex3, mSightTex));

    HR(mFX->SetMatrix(mhWVP, &(mView*mProj)));

    HR(gd3dDevice->SetVertexDeclaration(VertexPTL::Decl));
    HR(gd3dDevice->SetStreamSource(0, mLVB, 0, sizeof(VertexPTL)));
    HR(gd3dDevice->SetIndices(mLIB));

    UINT numPasses = 0;
    HR(mFX->Begin(&numPasses, 0));
    for (UINT i=0; i<numPasses; ++i)
    {
        HR(mFX->BeginPass(i));

        HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4*LNumber, 0, 2*LNumber));

        HR(mFX->EndPass());
    }
    HR(mFX->End());

    if (mbLStart)
        mbLStart = false;
}
Example #18
0
void Evolution::drawEggsFood()
{
    if (!mbEFStart)
    {
        mEFVB->Release();
        mEFIB->Release();
    }

    int Number = 0;
    int FNumber = 0;
    int ENumber = 0;

    const D3DXVECTOR3 baseV[4] = {D3DXVECTOR3(-1.00000f, -1.00000f, 0.0f),
                                  D3DXVECTOR3(-1.00000f,  1.00000f, 0.0f),
                                  D3DXVECTOR3( 1.00000f,  1.00000f, 0.0f),
                                  D3DXVECTOR3( 1.00000f, -1.00000f, 0.0f)
                                 };

    const D3DXVECTOR2 baseT[4] = {D3DXVECTOR2(0.0f, 1.0f),
                                  D3DXVECTOR2(0.0f, 0.0f),
                                  D3DXVECTOR2(1.0f, 0.0f),
                                  D3DXVECTOR2(1.0f, 1.0f)
                                 };


    HR(gd3dDevice->CreateVertexBuffer((foodList.size()+eggList.size()) *4* sizeof(VertexPTEF), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mEFVB, 0));
    VertexPTEF * efv = 0;
    HR(mEFVB->Lock(0, 0, (void**)&efv, 0));

    for (std::list<Food>::iterator it = foodList.begin(); it != foodList.end(); ++it)
    {
        D3DXVECTOR3 pos = it->getPosition();
        D3DXMATRIX T, S, F;
        D3DXVECTOR3 V[4];
        D3DXMatrixTranslation(&T, pos.x, pos.y, pos.z);
        D3DXMatrixScaling(&S, 2.0f, 2.0f, 0.0f);
        F = S*(T);

        for (int k=0; k<4; ++k)
            D3DXVec3TransformCoord(&V[k], &baseV[k], &F);

        for (int k=0; k<4; ++k)
            efv[Number+k]   = VertexPTEF( V[k], baseT[k]);

        //*v = iv;
        //++fv;
        Number+=4;
        ++FNumber;
    }

    /*if (eggList.size() != 0)
    {
    HR(gd3dDevice->CreateVertexBuffer(eggList.size() * sizeof(VertexPTEF), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mEVB, 0));
    VertexPTEF * ev = 0;
    HR(mEVB->Lock(0, 0, (void**)&ev, 0));*/

    ENumber=FNumber;
    for (std::list<Egg>::iterator it = eggList.begin(); it != eggList.end(); ++it)
    {
        D3DXVECTOR3 pos = it->getPosition();
        D3DXMATRIX T, S, F;
        D3DXVECTOR3 V[4];
        D3DXMatrixTranslation(&T, pos.x, pos.y, pos.z);
        D3DXMatrixScaling(&S, 2.0f, 2.0f, 0.0f);
        F = S*(T);

        for (int k=0; k<4; ++k)
            D3DXVec3TransformCoord(&V[k], &baseV[k], &F);

        for (int k=0; k<4; ++k)
            efv[Number+k]   = VertexPTEF( V[k], baseT[k]);

        //*v = iv;
        //++ev;
        Number+=4;
        ++ENumber;
    }

    HR(mEFVB->Unlock());

    Number = 0;

    HR(gd3dDevice->CreateIndexBuffer((foodList.size()+eggList.size()) *6* sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,	D3DPOOL_MANAGED, &mEFIB, 0));

    WORD* find = 0;
    HR(mEFIB->Lock(0, 0, (void**)&find, 0));

    int k=0;
    int m=0;
    int g=0;
    while (m<FNumber)
    {
        find[k] = g;
        find[k+1] = g+1;
        find[k+2] = g+2;
        find[k+3] = g;
        find[k+4] = g+2;
        find[k+5] = g+3;
        k+=6;
        g+=4;
        ++m;
    }

    int j=k;
    int n=m;
    int h=g;
    while (n<ENumber)
    {
        find[j] = h;
        find[j+1] = h+1;
        find[j+2] = h+2;
        find[j+3] = h;
        find[j+4] = h+2;
        find[j+5] = h+3;
        j+=6;
        h+=4;
        ++n;
    }

    HR(mEFIB->Unlock());

    // Set up the geometry data stream

    HR(mFX->SetTechnique(mhEFTech));
    HR(mFX->SetTexture(mhTex,  mFoodTex));
    HR(gd3dDevice->SetStreamSource(0, mEFVB, 0, sizeof(VertexPTEF)));
    HR(gd3dDevice->SetIndices(mEFIB));
    HR(gd3dDevice->SetVertexDeclaration(VertexPTEF::Decl));

    HR(mFX->SetMatrix(mhWVP, &(mView*mProj)));

    UINT numPasses = 0;
    HR(mFX->Begin(&numPasses, 0));
    for (UINT i=0; i<numPasses; ++i)
    {
        HR(mFX->BeginPass(i));

        if (foodList.size() != 0)
            HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4*FNumber, 0, 2*FNumber));

        if (eggList.size() != 0)
        {
            HR(mFX->SetTexture(mhTex,  mEggTex));
            HR(mFX->CommitChanges());
            HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, FNumber*4, 4*(ENumber-FNumber), FNumber*6, 2*(ENumber-FNumber)));
        }
        HR(mFX->EndPass());
    }
    HR(mFX->End());

    if (mbEFStart)
        mbEFStart = false;
}
Example #19
0
Map::Map(std::string RAW_File_Name,
		 int Vertex_Length,
		 int Vertex_Width,
		 int Cell_Spacing,
		 float Height_Scale)
{
	vertex_length = Vertex_Length;
	vertex_width = Vertex_Width;

	total_vertices = vertex_length * vertex_width;				//Total = L * W

	cell_length = vertex_length - 1;
	cell_width = vertex_width - 1;

	cell_spacing = Cell_Spacing;
	height_scale = Height_Scale;

	length = cell_length * cell_spacing;
	width = cell_width * cell_spacing;

	total_triangles = cell_length * cell_width * 2;

	D3DXCreateMeshFVF(cell_length * cell_width * 2,
		vertex_length * vertex_width,
		D3DXMESH_MANAGED,
		Terrain_Vertex::FVF,
		d3ddev,
		&mesh);

	if (!Raw_To_Int_Vector(RAW_File_Name))
	{
		MessageBox(0, "Raw_To_Int_Vector - FAILED", 0, 0);
		PostQuitMessage(0);
	}

	for (int i = 0; i < height_int_vector.size(); i++)
	{
		height_float_vector.push_back((float)height_int_vector[i]);

		height_float_vector[i] = height_float_vector[i] * height_scale;
	}

	if (!Create_Normals(&LightDirectionThree))
	{
		MessageBox(0, "Create_Normals - FAILED", 0, 0);
		PostQuitMessage(0);
	}

	Generate_Vertex_Normals();

	if (!Generate_Vertices())
	{
		MessageBox(0, "Generate_Vertices - FAILED", 0, 0);
		PostQuitMessage(0);
	}

	if (!Generate_Indices())									//Also Generates Mesh
	{
		MessageBox(0, "Generate_Indices - FAILED", 0, 0);
		PostQuitMessage(0);
	}

	adjacency_buffer = new DWORD[cell_width * cell_length * 2 * 3];

	mesh->GenerateAdjacency(0.001f, adjacency_buffer);
	mesh->OptimizeInplace(D3DXMESH_MANAGED |
		D3DXMESHOPT_COMPACT |
		D3DXMESHOPT_ATTRSORT | 
		D3DXMESHOPT_VERTEXCACHE,
		adjacency_buffer,
		adjacency_buffer,
		0,
		0);

	IDirect3DVertexBuffer9* bufvert;
	Terrain_Vertex* tempvert;

	mesh->GetVertexBuffer(&bufvert);
	bufvert->Lock(0, 0, (void**)&tempvert, 0);
	vertexdata = tempvert;
	bufvert->Unlock();

	IDirect3DIndexBuffer9* bufind;
	WORD* tempind;

	mesh->GetIndexBuffer(&bufind);
	bufind->Lock(0, 0, (void**)&tempind, 0);
	indexdata = tempind;
	bufind->Unlock();

	SafeRelease(bufvert);
	SafeRelease(bufind);
}
Example #20
0
    void build_submodel( SubModel& m )
    {
        const std::vector< model_vertex_type >& vsrc = m.vertex_source;
        const std::vector< WORD >&              isrc = m.index_source;

        if( vsrc.empty() || isrc.empty() ) {
            return;
        }

        // triangle vb
        {
            IDirect3DVertexBuffer9* vb = NULL;
            if( FAILED( device_->CreateVertexBuffer(
                            UINT( vsrc.size() ) *
                            sizeof( model_vertex_type ),
                            0,
                            0,
                            D3DPOOL_MANAGED,
                            &vb,
                            NULL ) ) ) {
                onError( "create vertex buffer failed\n" );
                return;
            }
                        
            model_vertex_type* p;
            if( FAILED( vb->Lock( 0, 0, (void**)&p, 0 ) ) ) {
                onError( "Lock vertexbuffer failed\n" );
                vb->Release();
                return;
            }

            int mm = int(vsrc.size());
            for( int j = 0 ; j < mm ; j++ ){
                *p++ = vsrc[j];
            }

            vb->Unlock();
            m.vb = vb;
        }

        // triangle ib
        {
            IDirect3DIndexBuffer9* ib = NULL;
            if( FAILED( device_->CreateIndexBuffer(
                            UINT( isrc.size() ) *
                            sizeof( WORD ),
                            0,
                            D3DFMT_INDEX16,
                            D3DPOOL_MANAGED,
                            &ib,
                            NULL ) ) ) {
                onError( "create index buffer failed\n" );
                m.vb->Release();
                return;
            }
                                
            WORD* p;
            if( FAILED( ib->Lock( 0, 0, (void**)&p, 0 ) ) ) {
                onError( "Lock vertexbuffer failed\n" );
                m.vb->Release();
                ib->Release();
                return;
            }

            int mm = int(isrc.size());
            for( int j = 0 ; j < mm ; j++ ){
                *p++ = isrc[j];
            }

            ib->Unlock();
            m.ib = ib;
        }                        
    }
Example #21
0
void CCampathDrawer::OnPostRenderAllTools()
{
	// Actually we are often called twice per frame due to an engine bug(?), once after 3d skybox
	// and once after world is drawn, maybe we will be even called more times,
	// but we can not care about that for now.
	
	if(!m_Draw)
		return;

	if(!m_VertexShader)
	{
		m_VertexShader = g_AfxShaders.GetVertexShader("afx_line_vs20.fxo");
	}
	IDirect3DVertexShader9 * vertexShader = m_VertexShader->GetVertexShader();

	if(!m_PixelShader)
	{
		m_PixelShader = g_AfxShaders.GetPixelShader("afx_line_ps20.fxo");
	}
	IDirect3DPixelShader9 * pixelShader = m_PixelShader->GetPixelShader();

	if(!(m_Device && vertexShader && m_PixelShader && g_VEngineClient))
	{
		static bool firstError = true;

		if(firstError)
		{
			firstError = false;
			Tier0_Msg(
				"AFXERROR: CCampathDrawer::OnEndScene: Missing required dependencies:%s%s%s%s.\n",
				!m_Device ? " m_Device" : "",
				!vertexShader ? " vertexShader" : "",
				!pixelShader ? " pixelShader" : "",
				!g_VEngineClient ? " g_VEngineClient" : ""
			);
		}

		return;
	}

	// Save device state:

	IDirect3DPixelShader9 * oldPixelShader = 0;
	m_Device->GetPixelShader(&oldPixelShader);
	if(oldPixelShader) oldPixelShader->AddRef();

	IDirect3DVertexShader9 * oldVertexShader = 0;
	m_Device->GetVertexShader(&oldVertexShader);
	if(oldVertexShader) oldVertexShader->AddRef();

	IDirect3DVertexBuffer9 * oldVertexBuffer = 0;
	UINT oldVertexBufferOffset;
	UINT oldVertexBufferStride;
	m_Device->GetStreamSource(0, &oldVertexBuffer, &oldVertexBufferOffset, &oldVertexBufferStride);
	// this is done already according to doc: // if(oldVertexBuffer) oldVertexBuffer->AddRef();

	IDirect3DIndexBuffer9 * oldIndexBuffer = 0;
	m_Device->GetIndices(&oldIndexBuffer);
	// this is done already according to doc: // if(oldIndexBuffer) oldIndexBuffer->AddRef();

	IDirect3DVertexDeclaration9 * oldDeclaration;
	m_Device->GetVertexDeclaration(&oldDeclaration);
	if(oldDeclaration) oldDeclaration->AddRef();

	DWORD oldFVF;
	m_Device->GetFVF(&oldFVF);

	FLOAT oldCViewProj[4][4];
	m_Device->GetVertexShaderConstantF(8, oldCViewProj[0], 4);

	FLOAT oldCScreenInfo[4];
	m_Device->GetVertexShaderConstantF(48, oldCScreenInfo, 1);

	FLOAT oldCPlane0[4];
	m_Device->GetVertexShaderConstantF(49, oldCPlane0, 1);

	FLOAT oldCPlaneN[4];
	m_Device->GetVertexShaderConstantF(50, oldCPlaneN, 1);

	DWORD oldSrgbWriteEnable;
	m_Device->GetRenderState(D3DRS_SRGBWRITEENABLE, &oldSrgbWriteEnable);

	DWORD oldColorWriteEnable;
	m_Device->GetRenderState(D3DRS_COLORWRITEENABLE, &oldColorWriteEnable);

	DWORD oldZEnable;
	m_Device->GetRenderState(D3DRS_ZENABLE, &oldZEnable);

	DWORD oldZWriteEnable;
	m_Device->GetRenderState(D3DRS_ZWRITEENABLE, &oldZWriteEnable);
	
	DWORD oldZFunc;
	m_Device->GetRenderState(D3DRS_ZFUNC, &oldZFunc);

	DWORD oldAlphaTestEnable;
	m_Device->GetRenderState(D3DRS_ALPHATESTENABLE, &oldAlphaTestEnable);

	DWORD oldSeparateAlphaBlendEnable;
	m_Device->GetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, &oldSeparateAlphaBlendEnable);

	DWORD oldAlphaBlendEnable;
	m_Device->GetRenderState(D3DRS_ALPHABLENDENABLE, &oldAlphaBlendEnable);

	DWORD oldBlendOp;
	m_Device->GetRenderState(D3DRS_BLENDOP, &oldBlendOp);

	DWORD oldSrcBlend;
	m_Device->GetRenderState(D3DRS_SRCBLEND, &oldSrcBlend);

	DWORD oldDestBlend;
	m_Device->GetRenderState(D3DRS_DESTBLEND, &oldDestBlend);

	DWORD oldCullMode;
	m_Device->GetRenderState(D3DRS_CULLMODE, &oldCullMode);

	// Draw:
	{
		//Vector3 vvForward, vvUp, vvRight, vvPos;

		double curTime = g_Hook_VClient_RenderView.GetCurTime();
		bool inCampath = 1 <= g_Hook_VClient_RenderView.m_CamPath.GetSize()
			&&	g_Hook_VClient_RenderView.m_CamPath.GetLowerBound() <= curTime
			&& curTime <= g_Hook_VClient_RenderView.m_CamPath.GetUpperBound();
		bool campathCanEval = g_Hook_VClient_RenderView.m_CamPath.CanEval();
		bool campathEnabled = g_Hook_VClient_RenderView.m_CamPath.Enabled_get();
		bool cameraMightBeSelected = false;

		m_Device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE);
		m_Device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED);
		m_Device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
		m_Device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
		m_Device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
		m_Device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
		m_Device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
		m_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
		m_Device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
		m_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
		m_Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
		m_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

		m_Device->SetVertexShader(vertexShader);

		m_WorldToScreenMatrix = g_VEngineClient->WorldToScreenMatrix();
			
		m_Device->SetVertexShaderConstantF(8, m_WorldToScreenMatrix.m[0], 4);

		// Provide view plane info for line clipping:
		{
			double plane0[4]={0,0,0,1};
			double planeN[4]={1,0,0,1};
			//double planeR[4]={0,-1,0,1};
			//double planeU[4]={0,0,1,1};

			unsigned char P[4];
			unsigned char Q[4];

			double L[4][4];
			double U[4][4];

			double M[4][4] = {
				m_WorldToScreenMatrix.m[0][0], m_WorldToScreenMatrix.m[0][1], m_WorldToScreenMatrix.m[0][2], 0,
				m_WorldToScreenMatrix.m[1][0], m_WorldToScreenMatrix.m[1][1], m_WorldToScreenMatrix.m[1][2], 0,
				m_WorldToScreenMatrix.m[2][0], m_WorldToScreenMatrix.m[2][1], m_WorldToScreenMatrix.m[2][2], 0,
				m_WorldToScreenMatrix.m[3][0], m_WorldToScreenMatrix.m[3][1], m_WorldToScreenMatrix.m[3][2], -1,
			};

			double b0[4] = {
				0 -m_WorldToScreenMatrix.m[0][3],
				0 -m_WorldToScreenMatrix.m[1][3],
				0 -m_WorldToScreenMatrix.m[2][3],
				-m_WorldToScreenMatrix.m[3][3],
			};

			double bN[4] = {
				0 -m_WorldToScreenMatrix.m[0][3],
				0 -m_WorldToScreenMatrix.m[1][3],
				1 -m_WorldToScreenMatrix.m[2][3],
				-m_WorldToScreenMatrix.m[3][3],
			};
			/*
			double bR[4] = {
				1 -m_WorldToScreenMatrix.m[0][3],
				0 -m_WorldToScreenMatrix.m[1][3],
				0 -m_WorldToScreenMatrix.m[2][3],
				-m_WorldToScreenMatrix.m[3][3],
			};

			double bU[4] = {
				0 -m_WorldToScreenMatrix.m[0][3],
				1 -m_WorldToScreenMatrix.m[1][3],
				0 -m_WorldToScreenMatrix.m[2][3],
				-m_WorldToScreenMatrix.m[3][3],
			};
			*/
			if(!LUdecomposition(M, P, Q, L, U))
			{
				Tier0_Warning("AFXERROR in CCampathDrawer::OnPostRenderAllTools: LUdecomposition failed\n");
			}
			else
			{
				SolveWithLU(L, U, P, Q, b0, plane0);
				SolveWithLU(L, U, P, Q, bN, planeN);
				
				//SolveWithLU(L, U, P, Q, bR, planeR);
				//SolveWithLU(L, U, P, Q, bU, planeU);
			}

			/*
			vvPos = Vector3(plane0[0], plane0[1], plane0[2]);
			vvForward = Vector3(planeN[0] -vvPos.X, planeN[1] -vvPos.Y, planeN[2]-vvPos.Z);
			vvForward.Normalize();
			vvRight = Vector3(planeR[0] -vvPos.X, planeR[1] -vvPos.Y, planeR[2]-vvPos.Z);
			vvRight.Normalize();
			vvUp = Vector3(planeU[0] -vvPos.X, planeU[1] -vvPos.Y, planeU[2]-vvPos.Z);
			vvUp.Normalize();
			*/

			/*
			Tier0_Msg("CCampathDrawer::OnPostRenderAllTools: curTime = %f\n",curTime);
			Tier0_Msg("M[0]=%f %f %f %f\nM[1]=%f %f %f %f\nM[2]=%f %f %f %f\nM[3]=%f %f %f %f\n", M[0][0],M[0][1],M[0][2],M[0][3], M[1][0],M[1][1],M[1][2],M[1][3], M[2][0],M[2][1],M[2][2],M[2][3], M[3][0],M[3][1],M[3][2],M[3][3]);
			Tier0_Msg("b0[0]=%f %f %f %f\n", b0[0], b0[1], b0[2], b0[3]);
			Tier0_Msg("bN[0]=%f %f %f %f\n", bN[0], bN[1], bN[2], bN[3]);
			Tier0_Msg("plane0=%f %f %f %f\n", plane0[0], plane0[1], plane0[2], plane0[3]);
			Tier0_Msg("planeN=%f %f %f %f\n", planeN[0], planeN[1], planeN[2], planeN[3]);
			*/

			FLOAT vPlane0[4] = {(float)plane0[0], (float)plane0[1], (float)plane0[2], 0.0f};

			Vector3 planeNormal(planeN[0] -plane0[0], planeN[1] -plane0[1], planeN[2] -plane0[2]);
			planeNormal.Normalize();

			FLOAT vPlaneN[4] = {(float)planeNormal.X, (float)planeNormal.Y, (float)planeNormal.Z, 0.0f};

			m_Device->SetVertexShaderConstantF(49, vPlane0, 1);
			m_Device->SetVertexShaderConstantF(50, vPlaneN, 1);
		}

		m_Device->SetPixelShader(pixelShader);

		m_Device->SetFVF(CCampathDrawer_VertexFVF);

		int screenWidth, screenHeight;
		g_VEngineClient->GetScreenSize(screenWidth, screenHeight);
		FLOAT newCScreenInfo[4] = { 0 != screenWidth ? 1.0f / screenWidth : 0.0f, 0 != screenHeight ? 1.0f / screenHeight : 0.0f, 0.0, 0.0f};

		// Draw trajectory:
		if(2 <= g_Hook_VClient_RenderView.m_CamPath.GetSize() && campathCanEval)
		{
			if(m_RebuildDrawing)
			{
				// Rebuild trajectory points.
				// This operation can be quite expensive (up to O(N^2)),
				// so it should be done only when s.th.
				// changed (which is what we do here).

				m_TrajectoryPoints.clear();
				
				CamPathIterator last = g_Hook_VClient_RenderView.m_CamPath.GetBegin();				
				CamPathIterator it = last;

				TempPoint * pts = new TempPoint[c_CameraTrajectoryMaxPointsPerInterval];

				for(++it; it != g_Hook_VClient_RenderView.m_CamPath.GetEnd(); ++it)
				{
					double delta = it.GetTime() -last.GetTime();

					for(size_t i = 0; i<c_CameraTrajectoryMaxPointsPerInterval; i++)
					{
						double t = last.GetTime() + delta*((double)i/(c_CameraTrajectoryMaxPointsPerInterval-1));

						CamPathValue cpv = g_Hook_VClient_RenderView.m_CamPath.Eval(t);

						pts[i].t = t;
						pts[i].y = Vector3(cpv.X, cpv.Y, cpv.Z);
						pts[i].nextPt = i+1 <c_CameraTrajectoryMaxPointsPerInterval ? &(pts[i+1]) : 0;
					}

					RamerDouglasPeucker(&(pts[0]), &(pts[c_CameraTrajectoryMaxPointsPerInterval-1]), c_CameraTrajectoryEpsilon);

					// add all points except the last one (to avoid duplicates):
					for(TempPoint * pt = &(pts[0]); pt && pt->nextPt; pt = pt->nextPt)
					{
						m_TrajectoryPoints.push_back(pt->t);
					}

					last = it;
				}

				// add last point:
				m_TrajectoryPoints.push_back(pts[c_CameraTrajectoryMaxPointsPerInterval-1].t);

				delete pts;

				m_RebuildDrawing = false;
			}

			newCScreenInfo[2] = c_CameraTrajectoryPixelWidth;
			m_Device->SetVertexShaderConstantF(48, newCScreenInfo, 1);

			AutoPolyLineStart();

			std::list<double>::iterator itPts = m_TrajectoryPoints.begin();

			CamPathIterator itKeysLast = g_Hook_VClient_RenderView.m_CamPath.GetBegin();
			CamPathIterator itKeysNext = itKeysLast;
			++itKeysNext;

			bool hasLastPt = false;
			bool hasNextPt = false;
			bool hasCurPt = false;
			
			double lastPtTime;
			CamPathValue lastPtValue;
			double curPtTime;
			CamPathValue curPtValue;
			double nextPtTime;
			CamPathValue nextPtValue;

			do
			{
				if(hasNextPt)
				{
					hasLastPt = true;
					lastPtTime = curPtTime;
					lastPtValue = curPtValue;

					hasCurPt = true;
					curPtTime = nextPtTime;
					curPtValue = nextPtValue;

					hasNextPt = false;
				}
				else
				{
					hasCurPt = true;
					curPtTime = *itPts;
					curPtValue = g_Hook_VClient_RenderView.m_CamPath.Eval(curPtTime);
					++itPts;
				}

				while(itKeysNext.GetTime() < curPtTime)
				{
					itKeysLast = itKeysNext;
					++itKeysNext;
				}

				if(itPts != m_TrajectoryPoints.end())
				{
					hasNextPt = true;
					nextPtTime = *itPts;
					nextPtValue = g_Hook_VClient_RenderView.m_CamPath.Eval(nextPtTime);
					++itPts;
				}
				else
				{
					// current point is last point.
					hasNextPt = false;
					nextPtValue = curPtValue;
				}

				if(!hasLastPt)
				{
					// current point is first point.
					lastPtValue = curPtValue;
				}

				// emit current point:
				{
					double deltaTime = abs(curTime -curPtTime);

					DWORD colour;

					// determine colour:
					if(deltaTime < 1.0)
					{
						double t = (deltaTime -0.0)/1.0;
						colour = D3DCOLOR_RGBA(
							ValToUCCondInv(255.0*t, curPtValue.Selected),
							ValToUCCondInv(255, curPtValue.Selected),
							ValToUCCondInv(0, curPtValue.Selected),
							(unsigned char)(127*(1.0-t))+128
						);
					}
					else
					if(deltaTime < 2.0)
					{
						double t = (deltaTime -1.0)/1.0;
						colour = D3DCOLOR_RGBA(
							ValToUCCondInv(255, curPtValue.Selected),
							ValToUCCondInv(255.0*(1.0-t), curPtValue.Selected),
							ValToUCCondInv(0, curPtValue.Selected),
							(unsigned char)(64*(1.0-t))+64
						);
					}
					else
					{
						colour = D3DCOLOR_RGBA(
							ValToUCCondInv(255, curPtValue.Selected),
							ValToUCCondInv(0, curPtValue.Selected),
							ValToUCCondInv(0, curPtValue.Selected),
							64
						);
					}

					AutoPolyLinePoint(
						Vector3(lastPtValue.X,lastPtValue.Y,lastPtValue.Z)
						, Vector3(curPtValue.X,curPtValue.Y,curPtValue.Z)
						, colour
						, Vector3(nextPtValue.X,nextPtValue.Y,nextPtValue.Z));
				}
			}
			while(hasNextPt);

			AutoPolyLineFlush();
		}

		// Draw keyframes:
		{
			newCScreenInfo[2] = c_CampathCrossPixelWidth;
			m_Device->SetVertexShaderConstantF(48, newCScreenInfo, 1);

			bool lpSelected = false;
			double lpTime;
			
			/*if(0 < g_Hook_VClient_RenderView.m_CamPath.GetSize())
			{
				// Test for not too unlikely hard case:
				CamPathValue cpv = g_Hook_VClient_RenderView.m_CamPath.GetBegin().GetValue();
				Vector3 current(cpv.X+76, cpv.Y+76, cpv.Z+76);
				Vector3 previous(current.X+76, current.Y-1*4, current.Z);
				Vector3 next(current.X+76, current.Y+1*4, current.Z);
				Vector3 next2(current.X, current.Y+2*4, current.Z);
				Vector3 next3(current.X+76, current.Y+3*4, current.Z);
				Vector3 next4(current.X, current.Y+4*4, current.Z);
				Vector3 next5(current.X+76, current.Y+5*4, current.Z);

				AutoPolyLineStart();
				AutoPolyLinePoint(previous, previous, D3DCOLOR_RGBA(255,0,0,255), current);
				AutoPolyLinePoint(previous, current, D3DCOLOR_RGBA(255,0,0,255), next);
				AutoPolyLinePoint(current, next, D3DCOLOR_RGBA(255,0,0,255), next2);
				AutoPolyLinePoint(next, next2, D3DCOLOR_RGBA(255,0,0,255), next3);
				AutoPolyLinePoint(next2, next3, D3DCOLOR_RGBA(255,0,0,255), next4);
				AutoPolyLinePoint(next3, next4, D3DCOLOR_RGBA(255,0,0,255), next5);
				AutoPolyLinePoint(next4, next5, D3DCOLOR_RGBA(255,0,0,255), next5);
				AutoPolyLineFlush();
			}*/
			
			/*if(0 < g_Hook_VClient_RenderView.m_CamPath.GetSize())
			{
				CamPathValue cpv = g_Hook_VClient_RenderView.m_CamPath.GetBegin().GetValue();

				float x = cpv.X * m_WorldToScreenMatrix.m[0][0] + cpv.Y * m_WorldToScreenMatrix.m[0][1] + cpv.Z * m_WorldToScreenMatrix.m[0][2] +m_WorldToScreenMatrix.m[0][3];
				float y = cpv.X * m_WorldToScreenMatrix.m[1][0] + cpv.Y * m_WorldToScreenMatrix.m[1][1] + cpv.Z * m_WorldToScreenMatrix.m[1][2] +m_WorldToScreenMatrix.m[1][3];
				float z = cpv.X * m_WorldToScreenMatrix.m[2][0] + cpv.Y * m_WorldToScreenMatrix.m[2][1] + cpv.Z * m_WorldToScreenMatrix.m[2][2] +m_WorldToScreenMatrix.m[2][3];
				float w = cpv.X * m_WorldToScreenMatrix.m[3][0] + cpv.Y * m_WorldToScreenMatrix.m[3][1] + cpv.Z * m_WorldToScreenMatrix.m[3][2] +m_WorldToScreenMatrix.m[3][3];

				float iw = w ? 1/w : 0;

				Tier0_Msg("pt: %f %f %f %f -> %f %f %f %f\n",x,y,z,w,x*iw,y*iw,z*iw,w*iw);
			}*/
		
			for(CamPathIterator it = g_Hook_VClient_RenderView.m_CamPath.GetBegin(); it != g_Hook_VClient_RenderView.m_CamPath.GetEnd(); ++it)
			{
				double cpT = it.GetTime();
				CamPathValue cpv = it.GetValue();

				cameraMightBeSelected = cameraMightBeSelected || lpSelected && cpv.Selected && lpTime <= curTime && curTime <= cpT;

				lpSelected = cpv.Selected;
				lpTime = cpT;

				double deltaTime = abs(curTime -cpT);

				bool selected = cpv.Selected;

				DWORD colour;

				// determine colour:
				if(deltaTime < 1.0)
				{
					double t = (deltaTime -0.0)/1.0;
					colour = D3DCOLOR_RGBA(
						ValToUCCondInv(255.0*t, selected),
						ValToUCCondInv(255, selected),
						ValToUCCondInv(0, selected),
						(unsigned char)(127*(1.0-t))+128
					);
				}
				else
				if(deltaTime < 2.0)
				{
					double t = (deltaTime -1.0)/1.0;
					colour = D3DCOLOR_RGBA(
						ValToUCCondInv(255, selected),
						ValToUCCondInv(255.0*(1.0-t), selected),
						ValToUCCondInv(0, selected),
						(unsigned char)(64*(1.0-t))+64
					);
				}
				else
				{
					colour = D3DCOLOR_RGBA(
						ValToUCCondInv(255, selected),
						ValToUCCondInv(0, selected),
						ValToUCCondInv(0, selected),
						64
					);
				}

				// x / forward line:

				AutoSingleLine(
					Vector3(cpv.X -c_CampathCrossRadius, cpv.Y, cpv.Z),
					colour,
					Vector3(cpv.X +c_CampathCrossRadius, cpv.Y, cpv.Z),
					colour
				);

				// y / left line:

				AutoSingleLine(
					Vector3(cpv.X, cpv.Y -c_CampathCrossRadius, cpv.Z),
					colour,
					Vector3(cpv.X, cpv.Y +c_CampathCrossRadius, cpv.Z),
					colour
				);

				// z / up line:

				AutoSingleLine(
					Vector3(cpv.X, cpv.Y, cpv.Z -c_CampathCrossRadius),
					colour,
					Vector3(cpv.X, cpv.Y, cpv.Z +c_CampathCrossRadius),
					colour
				);
			}

			AutoSingleLineFlush();
		}

		// Draw wireframe camera:
		if(inCampath && campathCanEval)
		{
			newCScreenInfo[2] = c_CameraPixelWidth;
			m_Device->SetVertexShaderConstantF(48, newCScreenInfo, 1);

			DWORD colourCam = campathEnabled
				? D3DCOLOR_RGBA(
					ValToUCCondInv(255,cameraMightBeSelected),
					ValToUCCondInv(0,cameraMightBeSelected),
					ValToUCCondInv(255,cameraMightBeSelected),
					128)
				: D3DCOLOR_RGBA(
					ValToUCCondInv(255,cameraMightBeSelected),
					ValToUCCondInv(255,cameraMightBeSelected),
					ValToUCCondInv(255,cameraMightBeSelected),
					128);
			DWORD colourCamUp = campathEnabled
				? D3DCOLOR_RGBA(
					ValToUCCondInv(0,cameraMightBeSelected),
					ValToUCCondInv(255,cameraMightBeSelected),
					ValToUCCondInv(0,cameraMightBeSelected),
					128)
				: D3DCOLOR_RGBA(
					ValToUCCondInv(0,cameraMightBeSelected),
					ValToUCCondInv(0,cameraMightBeSelected),
					ValToUCCondInv(0,cameraMightBeSelected),
					128);

			CamPathValue cpv = g_Hook_VClient_RenderView.m_CamPath.Eval(curTime);

			// limit to values as RenderView hook:
			cpv.Fov = max(1,cpv.Fov);
			cpv.Fov = min(179,cpv.Fov);

			double forward[3], right[3], up[3];
			QEulerAngles ang = cpv.R.ToQREulerAngles().ToQEulerAngles();
			MakeVectors(ang.Roll, ang.Pitch, ang.Yaw, forward, right, up);

			Vector3 vCp(cpv.X, cpv.Y, cpv.Z);
			Vector3 vForward(forward);
			Vector3 vUp(up);
			Vector3 vRight(right);

			//Tier0_Msg("----------------",curTime);
			//Tier0_Msg("currenTime = %f",curTime);
			//Tier0_Msg("vCp = %f %f %f\n", vCp.X, vCp.Y, vCp.Z);

			double a = sin(cpv.Fov * M_PI / 360.0) * c_CameraRadius;
			double b = a;

			int screenWidth, screenHeight;
			g_VEngineClient->GetScreenSize(screenWidth, screenHeight);

			double aspectRatio = screenWidth ? (double)screenHeight / (double)screenWidth : 1.0;

			b *= aspectRatio;

			Vector3 vLU = vCp +(double)c_CameraRadius * vForward -a * vRight +b * vUp;
			Vector3 vRU = vCp +(double)c_CameraRadius * vForward +a * vRight +b * vUp;
			Vector3 vLD = vCp +(double)c_CameraRadius * vForward -a * vRight -b * vUp;
			Vector3 vRD = vCp +(double)c_CameraRadius * vForward +a * vRight -b * vUp;
			Vector3 vMU = vLU +(vRU -vLU)/2;
			Vector3 vMUU = vMU +(double)c_CameraRadius * vUp;

			AutoSingleLine(vCp, colourCam, vLD, colourCam);

			AutoSingleLine(vCp, colourCam, vRD, colourCam);

			AutoSingleLine(vCp, colourCam, vLU, colourCam);

			AutoSingleLine(vCp, colourCam, vRU, colourCam);

			AutoSingleLine(vLD, colourCam, vRD, colourCam);

			AutoSingleLine(vRD, colourCam, vRU, colourCam);

			AutoSingleLine(vRU, colourCam, vLU, colourCam);

			AutoSingleLine(vLU, colourCam, vLD, colourCam);

			AutoSingleLine(vMU, colourCam, vMUU, colourCamUp);

			AutoSingleLineFlush();

			//
			/*

			colourCam = D3DCOLOR_RGBA(255, 0, 0, 255);
			colourCamUp = D3DCOLOR_RGBA(255, 255, 0, 255);

			vCp = vvPos;
			vForward = vvForward;
			vUp = vvUp;
			vRight = vvRight;

			//Tier0_Msg("vCp2 = %f %f %f\n", vCp.X, vCp.Y, vCp.Z);

			vLU = vCp +(double)c_CameraRadius * vForward -a * vRight +b * vUp;
			vRU = vCp +(double)c_CameraRadius * vForward +a * vRight +b * vUp;
			vLD = vCp +(double)c_CameraRadius * vForward -a * vRight -b * vUp;
			vRD = vCp +(double)c_CameraRadius * vForward +a * vRight -b * vUp;
			vMU = vLU +(vRU -vLU)/2;
			vMUU = vMU +(double)c_CameraRadius * vUp;

			AutoSingleLine(vCp, colourCam, vLD, colourCam);

			AutoSingleLine(vCp, colourCam, vRD, colourCam);

			AutoSingleLine(vCp, colourCam, vLU, colourCam);

			AutoSingleLine(vCp, colourCam, vRU, colourCam);

			AutoSingleLine(vLD, colourCam, vRD, colourCam);

			AutoSingleLine(vRD, colourCam, vRU, colourCam);

			AutoSingleLine(vRU, colourCam, vLU, colourCam);

			AutoSingleLine(vLU, colourCam, vLD, colourCam);

			AutoSingleLine(vMU, colourCam, vMUU, colourCamUp);

			AutoSingleLineFlush();
			*/
		}
	}

	// Restore device state:

	m_Device->SetPixelShader(oldPixelShader);
	if(oldPixelShader) oldPixelShader->Release();

	m_Device->SetVertexShader(oldVertexShader);
	if(oldVertexShader) oldVertexShader->Release();

	m_Device->SetStreamSource(0, oldVertexBuffer, oldVertexBufferOffset, oldVertexBufferStride);
	if(oldVertexBuffer) oldVertexBuffer->Release();

	m_Device->SetIndices(oldIndexBuffer);
	if(oldIndexBuffer) oldIndexBuffer->Release();

	m_Device->SetFVF(oldFVF);

	m_Device->SetVertexDeclaration(oldDeclaration);
	if(oldDeclaration) oldDeclaration->Release();

	m_Device->SetVertexShaderConstantF(8, oldCViewProj[0], 4);
	m_Device->SetVertexShaderConstantF(48, oldCScreenInfo, 1);
	m_Device->SetVertexShaderConstantF(49, oldCPlane0, 1);
	m_Device->SetVertexShaderConstantF(50, oldCPlaneN, 1);

	m_Device->SetRenderState(D3DRS_CULLMODE, oldCullMode);
	m_Device->SetRenderState(D3DRS_DESTBLEND, oldDestBlend);
	m_Device->SetRenderState(D3DRS_SRCBLEND, oldSrcBlend);
	m_Device->SetRenderState(D3DRS_BLENDOP, oldBlendOp);
	m_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, oldAlphaBlendEnable);
	m_Device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, oldSeparateAlphaBlendEnable);
	m_Device->SetRenderState(D3DRS_ALPHATESTENABLE, oldAlphaTestEnable);
	m_Device->SetRenderState(D3DRS_ZFUNC, oldZFunc);
	m_Device->SetRenderState(D3DRS_ZWRITEENABLE, oldZWriteEnable);
	m_Device->SetRenderState(D3DRS_ZENABLE, oldZEnable);
	m_Device->SetRenderState(D3DRS_COLORWRITEENABLE, oldColorWriteEnable);
	m_Device->SetRenderState(D3DRS_SRGBWRITEENABLE, oldSrgbWriteEnable);
}
Example #22
0
BYTE KScene::Draw(
    IDirect3DDevice9* piDevice,
    KVertexEx* pVertex, int nVertexCount,
    KTriangleEx* pTriangle, int nTriangleCount,
    BOOL bSolid, 
    BOOL bWireframe
)
{
    BOOL                    bResult         = false;
    HRESULT                 hRetCode        = E_FAIL;
    IDirect3DVertexBuffer9* piVertexBuffer  = NULL;
    IDirect3DIndexBuffer9*  piIndexBuffer   = NULL;
    void*                   pvBuffer        = NULL;

    hRetCode = piDevice->CreateVertexBuffer(
        sizeof(KVertex) * nVertexCount, 0, 
        VERTEX_FVF, D3DPOOL_MANAGED, &piVertexBuffer, 0
    );
    KG_PROCESS_ERROR(!FAILED(hRetCode));

    hRetCode = piVertexBuffer->Lock(0, 0, &pvBuffer, D3DLOCK_DISCARD);
    KG_PROCESS_ERROR(!FAILED(hRetCode));

    for (int i = 0; i < nVertexCount; i++)
    {
        KVertex* pV = (KVertex*)pvBuffer + i;
        pV->vPos    = pVertex[i].vPos;
        pV->vNormal = pVertex[i].vNormal;
    }
    piVertexBuffer->Unlock();

    hRetCode = piDevice->CreateIndexBuffer(
        sizeof(KTriangle) * nTriangleCount, 0, 
        D3DFMT_INDEX32, D3DPOOL_MANAGED, &piIndexBuffer, 0
    );
    KG_PROCESS_ERROR(!FAILED(hRetCode));

    hRetCode = piIndexBuffer->Lock(0, 0, &pvBuffer, D3DLOCK_DISCARD);
    KG_PROCESS_ERROR(!FAILED(hRetCode));

    for (int i = 0; i < nTriangleCount; i++)
    {
        KTriangle* pT = (KTriangle*)pvBuffer + i;

        if (!pTriangle[i].pVertexA)
            pT->nA = 0;
        else
            pT->nA = (int)(pTriangle[i].pVertexA - pVertex);

        if (!pTriangle[i].pVertexB)
            pT->nB = 0;
        else
            pT->nB = (int)(pTriangle[i].pVertexB - pVertex);

        if (!pTriangle[i].pVertexC)
            pT->nC = 0;
        else
            pT->nC = (int)(pTriangle[i].pVertexC - pVertex);
    }
    piIndexBuffer->Unlock();

    piDevice->SetStreamSource(0, piVertexBuffer, 0, sizeof(KVertex));
    piDevice->SetIndices(piIndexBuffer);
    piDevice->SetFVF(VERTEX_FVF);

    if (bSolid)
    {
        piDevice->SetRenderState(D3DRS_LIGHTING, true);
        piDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
        piDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nVertexCount, 0, nTriangleCount);
    }

    if (bWireframe)
    {
        piDevice->SetRenderState(D3DRS_LIGHTING, false);
        piDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
        piDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nVertexCount, 0, nTriangleCount);
    }

    bResult = true;
Exit0:
    KG_COM_RELEASE(piIndexBuffer);
    KG_COM_RELEASE(piVertexBuffer);
    return bResult;
}
Example #23
0
void MFVertex_DestroyIndexBufferPlatformSpecific(MFIndexBuffer *pIndexBuffer)
{
	IDirect3DIndexBuffer9 *pIB = (IDirect3DIndexBuffer9*)pIndexBuffer->pPlatformData;
	pIB->Release();
}