Example #1
0
GLenum Dx9BackEnd::setupAttributesPreDraw(const TranslatedAttribute *attributes)
{
    HRESULT hr;

    D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS+1];

    D3DVERTEXELEMENT9 *nextElement = &elements[0];

    for (BYTE i = 0; i < MAX_VERTEX_ATTRIBS; i++)
    {
        if (attributes[i].enabled)
        {
            nextElement->Stream = i + 1;    // Stream 0 is skipped because D3D does not permit it to be an instanced stream.
            nextElement->Offset = 0;
            nextElement->Type = static_cast<BYTE>(mapAttributeType(attributes[i].type, attributes[i].size, attributes[i].normalized));
            nextElement->Method = D3DDECLMETHOD_DEFAULT;
            nextElement->Usage = D3DDECLUSAGE_TEXCOORD;
            nextElement->UsageIndex = attributes[i].semanticIndex;
            nextElement++;
        }
    }

    static const D3DVERTEXELEMENT9 end = D3DDECL_END();
    *nextElement = end;

    IDirect3DVertexDeclaration9* vertexDeclaration;
    hr = mDevice->CreateVertexDeclaration(elements, &vertexDeclaration);
    mDevice->SetVertexDeclaration(vertexDeclaration);
    vertexDeclaration->Release();

    mDevice->SetStreamSource(0, NULL, 0, 0);

    bool nonArrayAttributes = false;

    for (size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++)
    {
        if (attributes[i].enabled)
        {
            if (attributes[i].nonArray) nonArrayAttributes = true;

            mDevice->SetStreamSource(i + 1, getDxBuffer(attributes[i].buffer), attributes[i].offset, attributes[i].stride);
            if (!mAppliedAttribEnabled[i])
            {
                mAppliedAttribEnabled[i] = true;
            }
        }
        else
        {
            if (mAppliedAttribEnabled[i])
            {
                mDevice->SetStreamSource(i + 1, 0, 0, 0);
                mAppliedAttribEnabled[i] = false;
            }
        }
    }

    if (mUseInstancingForStrideZero)
    {
        // When there are no stride zero attributes, we disable instancing so that DrawPrimitive can be used.

        if (nonArrayAttributes)
        {
            if (mStreamFrequency[0] != STREAM_FREQUENCY_INDEXED)
            {
                mStreamFrequency[0] = STREAM_FREQUENCY_INDEXED;
                mDevice->SetStreamSourceFreq(0, D3DSTREAMSOURCE_INDEXEDDATA | 1);
            }

            for (size_t i = 0; i < MAX_VERTEX_ATTRIBS; i++)
            {
                if (attributes[i].enabled)
                {
                    if (attributes[i].nonArray)
                    {
                        if (mStreamFrequency[i+1] != STREAM_FREQUENCY_INSTANCED)
                        {
                            mStreamFrequency[i+1] = STREAM_FREQUENCY_INSTANCED;
                            mDevice->SetStreamSourceFreq(i + 1, D3DSTREAMSOURCE_INSTANCEDATA | 1);
                        }
                    }
                    else
                    {
                        if (mStreamFrequency[i+1] != STREAM_FREQUENCY_INDEXED)
                        {
                            mStreamFrequency[i+1] = STREAM_FREQUENCY_INDEXED;
                            mDevice->SetStreamSourceFreq(i + 1, D3DSTREAMSOURCE_INDEXEDDATA | 1);
                        }
                    }
                }
            }
        }
        else
        {
            for (size_t i = 0; i < MAX_VERTEX_ATTRIBS + 1; i++)
            {
                if (mStreamFrequency[i] != STREAM_FREQUENCY_UNINSTANCED)
                {
                    mStreamFrequency[i] = STREAM_FREQUENCY_UNINSTANCED;

                    // This should not be needed, but otherwise there is a buggy driver that will leave instancing
                    // enabled for the first draw after it has been turned off.
                    mDevice->SetStreamSourceFreq(i, D3DSTREAMSOURCE_INDEXEDDATA | 1);

                    mDevice->SetStreamSourceFreq(i, 1);
                }
            }
        }
    }

    return GL_NO_ERROR;
}
Example #2
0
bool Engine::init(int width, int height, HINSTANCE hInstance)
{
	WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, ::WindowProc, 0, 0, hInstance, 0, 0, 0, 0, "CG Task3", 0};
	if (0 == RegisterClassEx(&wc))
	{
		return false;
	}
	m_window = CreateWindow("CG Task3", "CG Task3: Cylinder", (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX), 0, 0, width, height, GetDesktopWindow(), 0, hInstance, 0);
	if(NULL == m_window)
	{
		return false;
	}

	m_d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if(!m_d3d)
	{
	    return false;
	}

	D3DPRESENT_PARAMETERS d3dpp = {0};
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	
	HRESULT hr = m_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_window, 
		D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_d3dDevice);
	
	if(FAILED(hr) || !m_d3dDevice)
	{
		return false;
	}

	m_d3dDevice->SetRenderState(D3DRS_AMBIENT, AMBIENT_COLOR);
	m_d3dDevice->SetRenderState(D3DRS_LIGHTING, false);
	m_d3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	
	m_d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	m_d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    m_d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    //m_d3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);

	D3DVERTEXELEMENT9 vertexDeclaration[] = {
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 3 * sizeof(float), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
		D3DDECL_END()};

	IDirect3DVertexDeclaration9 *decl;
	if(FAILED(m_d3dDevice->CreateVertexDeclaration(vertexDeclaration, &decl)))
	{
	    return false;
	}
	if(FAILED(m_d3dDevice->SetVertexDeclaration(decl)))
	{
	    return false;
	}
	decl->Release();

	Vertex *vertices;
	uint16_t *indices;
	generateCylinder(CYLINDER_STACKS, CYLINDER_SLICES, CYLINDER_RADIUS, CYLINDER_HEIGHT, 0x80000000, true, &indices, &vertices, m_indexCount, m_vertexCount);

	if((0 == vertices) || (0 == indices))
	{
		return false;
	}

	if(FAILED(m_d3dDevice->CreateVertexBuffer(m_vertexCount * sizeof(Vertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &m_vertexBuffer, 0)))
	{
        return false;
	}

	void *vertexPointer;
	if(FAILED(m_vertexBuffer->Lock(0, m_vertexCount * sizeof(Vertex), (void**)&vertexPointer, 0)))
	{
        return false;
	}
	memcpy(vertexPointer, vertices, m_vertexCount * sizeof(Vertex));
	if(FAILED(m_vertexBuffer->Unlock()))
	{
	    return false;
	}

	if(FAILED(m_d3dDevice->CreateIndexBuffer(m_indexCount * sizeof(uint16_t), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_indexBuffer, 0)))
	{
		return false;
	}

	void *indexPointer;
	if(FAILED(m_indexBuffer->Lock(0, m_indexCount * sizeof(uint16_t), (void**)&indexPointer, 0)))
    {
		return false;
	}
    memcpy(indexPointer, indices, m_indexCount * sizeof(uint16_t));
	if(FAILED(m_indexBuffer->Unlock()))
	{
	    return false;
	}
    
#include "shader.h"
	if (FAILED(m_d3dDevice->CreateVertexShader(g_vs11_main, &m_shader)))
	{
		return false;
	}

    m_mainCamera.setDirection(DEFAULT_CAMERA_LOOK, DEFAULT_CAMERA_UP);
	D3DXVECTOR3 look = m_mainCamera.look();
    m_mainCamera.setPosition(*D3DXVec3Scale(&look, &look, -m_radius));
	
	D3DXMatrixIdentity(&m_worldMatrix);

	resize(width, height);

	m_bones = new D3DXMATRIX[2]; 
	D3DXMatrixIdentity(&m_bones[0]);
	D3DXMatrixIdentity(&m_bones[1]);

	D3DXMatrixTranspose(&m_bones[0], &m_bones[0]);
	D3DXMatrixTranspose(&m_bones[1], &m_bones[1]);
	m_d3dDevice->SetVertexShader(m_shader);

	if(FAILED(m_d3dDevice->SetVertexShaderConstantF(4, &(m_bones[0].m[0][0]), 4)))
	{
		return false;
	}
	if(FAILED(m_d3dDevice->SetVertexShaderConstantF(8, &(m_bones[1].m[0][0]), 4)))
	{
		return false;
	}
	float sizeConst[4];
	sizeConst[0] = 1 / CYLINDER_HEIGHT; 
	if(FAILED(m_d3dDevice->SetVertexShaderConstantF(12, sizeConst, 1)))
	{
		return false;
	}

    updateMatrices();

	return true;
}
Example #3
0
void MFVertex_DestroyVertexDeclarationPlatformSpecific(MFVertexDeclaration *pDeclaration)
{
	IDirect3DVertexDeclaration9 *pDecl = (IDirect3DVertexDeclaration9*)pDeclaration->pPlatformData;
	pDecl->Release();
}