zoukankan      html  css  js  c++  java
  • 第一个顶点找色器程序

    开始错误一大堆。。。

    都是shader里面的语法错误。。。我擦!

    #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 directionLight;

    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);

    directionLight.w = 0.0f;

    inputData.normal.w = 0.0f;

    directionLight = mul(directionLight, 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;

    }

    注意:VS_OUTPUT Main(VS_INPUT inputData)中VS_INPUT inputData不要用VS_INPUT input....

  • 相关阅读:
    C#编程语言详解(第2版) 11.3 类和结构的区别
    .Net程序员如何快速成长之我见
    封装 JMail 4.4 的 POP3 为 .Net 组件 (.dll 程序集),实现 "邮件(附件) 到达" 等 "事件,c#接收邮件
    POJ1163The Triangle
    hdoj_1231&&hdoj_1003最大连续子序列
    数组名++操作导致的_BLOCK_TYPE_IS_VALID
    《windows程序设计》第二章学习心得
    error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
    Windows环境下Unicode编程总结
    已知顺序表L递增有序,写一算法,将X插入到线性表的适当位置,以保持线性表的有序
  • 原文地址:https://www.cnblogs.com/kex1n/p/2459582.html
Copyright © 2011-2022 走看看