Color finder program for the first Vertex

Source: Internet
Author: User
Tags timedelta

Starting with a lot of errors...

Are all syntax errors in the shader... Wipe it!

#include "d3dUtility.h"const DWORD width = 640;const DWORD height = 480;//// Globals//IDirect3DDevice9* Device = 0; IDirect3DVertexShader9* pShader = 0;ID3DXConstantTable*     pConstTable = 0;ID3DXMesh*          teaPot = 0;D3DXHANDLE              viewMatHandle;D3DXHANDLE              viewProjHandle;D3DXHANDLE              ambientMtrlHandle;D3DXHANDLE              diffuseMtrlHandle;D3DXHANDLE              directionLightHandle;D3DXMATRIX              proj;//// Framework Functions//bool Setup(){    // Nothing to setup in this sample.    HRESULT hr = 0;    D3DXCreateTeapot(Device, &teaPot, 0);    ID3DXBuffer* errorBuf;    ID3DXBuffer* shaderBuf;    hr = D3DXCompileShaderFromFile("diffuse.txt", 0, 0,        "Main", "vs_2_0", D3DXSHADER_DEBUG, &shaderBuf, &errorBuf, &pConstTable);    if (errorBuf)    {        ::MessageBox(0, (char*)errorBuf->GetBufferPointer(), 0, 0);        d3d::Release<ID3DXBuffer*>(errorBuf);    }    if (FAILED(hr))    {        return false;    }    hr = Device->CreateVertexShader((DWORD*)shaderBuf->GetBufferPointer(),        &pShader);    if (FAILED(hr))    {        return false;    }    d3d::Release<ID3DXBuffer*>(shaderBuf);    pConstTable->SetDefaults(Device);    viewMatHandle = pConstTable->GetConstantByName(0, "viewMatrix");    viewProjHandle = pConstTable->GetConstantByName(0, "viewProjMatrix");    ambientMtrlHandle = pConstTable->GetConstantByName(0, "ambientMtrl");    diffuseMtrlHandle = pConstTable->GetConstantByName(0, "diffuseMtrl");    directionLightHandle = pConstTable->GetConstantByName(0, "directionLight");    D3DXVECTOR4 directionLight(-0.57f, -0.57f, -0.57f, 0.0f);    pConstTable->SetVector(Device, directionLightHandle, &directionLight);    D3DXVECTOR4 ambientMtrl(0.0f, 0.0f, 1.0f, 0.0f);    D3DXVECTOR4 diffuseMtrl(0.0f, 0.0f, 1.0f, 0.0f);    pConstTable->SetVector(Device, ambientMtrlHandle, &ambientMtrl);    pConstTable->SetVector(Device, diffuseMtrlHandle, &diffuseMtrl);    D3DXMatrixPerspectiveFovLH(        &proj, D3DX_PI * 0.25f, (float)width / (float)height, 1.0f, 1000.0f        );    return true;}void Cleanup(){    // Nothing to cleanup in this sample.}bool Display(float timeDelta){    if( Device ) // Only use Device methods if we have a valid device.    {        static float angle = (3.0f * D3DX_PI) / 2.0f;        static float height = 3.0f;        if (::GetAsyncKeyState(VK_LEFT) & 0x8000f)        {            angle -= 0.5f * timeDelta;        }        if (::GetAsyncKeyState(VK_RIGHT) & 0x8000f)        {            angle += 0.5f * timeDelta;        }        if (::GetAsyncKeyState(VK_UP) & 0x8000f)        {            height += 5.0f * timeDelta;        }        if (::GetAsyncKeyState(VK_DOWN) & 0x8000f)        {            height -= 5.0f * timeDelta;        }        D3DXVECTOR3 position(cosf(angle) * 7.0f, height, sinf(angle) * 7.0f);        D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);        D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);        D3DXMATRIX v;        D3DXMatrixLookAtLH(&v, &position, &target, &up);        pConstTable->SetMatrix(Device, viewMatHandle, &v);        D3DXMATRIX viewProjMat = v * proj;        pConstTable->SetMatrix(Device, viewProjHandle, &viewProjMat);        // Instruct the device to set each pixel on the back buffer black -        // D3DCLEAR_TARGET: 0x00000000 (black) - and to set each pixel on        // the depth buffer to a value of 1.0 - D3DCLEAR_ZBUFFER: 1.0f.        Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);        Device->BeginScene();        Device->SetVertexShader(pShader);        teaPot->DrawSubset(0);        Device->EndScene();        // Swap the back and front buffers.        Device->Present(0, 0, 0, 0);    }    return true;}//// WndProc//LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){    switch( msg )    {    case WM_DESTROY:        ::PostQuitMessage(0);        break;            case WM_KEYDOWN:        if( wParam == VK_ESCAPE )            ::DestroyWindow(hwnd);        break;    }    return ::DefWindowProc(hwnd, msg, wParam, lParam);}//// WinMain//int WINAPI WinMain(HINSTANCE hinstance,                   HINSTANCE prevInstance,                    PSTR cmdLine,                   int showCmd){    if(!d3d::InitD3D(hinstance,        640, 480, true, D3DDEVTYPE_HAL, &Device))    {        ::MessageBox(0, "InitD3D() - FAILED", 0, 0);        return 0;    }            if(!Setup())    {        ::MessageBox(0, "Setup() - FAILED", 0, 0);        return 0;    }    d3d::EnterMsgLoop( Display );    Cleanup();    Device->Release();    return 0;}

 

 

Matrix viewMatrix;

Matrix viewProjMatrix;

Vector ambientMtrl;

Vector diffuseMtrl;

Vector direlight light;

 

Vector diffuseLightIntensify = {0.0f, 0.0f, 1.0f, 1.0f };

Vector ambientLightIntensify = {0.0f, 0.0f, 0.2f, 1.0f };

 

Struct VS_INPUT

{

Vector position: POSITION;

Vector normal: NORMAL;

};

 

Struct VS_OUTPUT

{

Vector position: POSITION;

Vector diffuse: COLOR;

};

 

 

VS_OUTPUT Main (VS_INPUT inputData)

{

VS_OUTPUT output = (VS_OUTPUT) 0;

Output. position = mul (inputData. position, viewProjMatrix );

 

Direlight light. w = 0.0f;

InputData. normal. w = 0.0f;

Direlight light = mul (direlight light, viewMatrix );

InputData. normal = mul (inputData. normal, viewMatrix );

 

Float s = dot (directionLight, inputData. normal );

If (s <0.0f)

S = 0.0f;

 

Output. diffuse = ambientLightIntensify * ambientMtrl +

S * diffuseLightIntensify * diffuseMtrl;

Return output;

}

 

Note: Do not use VS_INPUT input... In VS_OUTPUT Main (VS_INPUT inputData ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.