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....

  • 相关阅读:
    OpenCV学习:图片的读取、展示
    Windows下使用Anaconda配置opencv和tensorflow环境
    二叉树的链式存储的实现以及对二叉树的各种操作
    数据结构——树笔记1
    Python学习笔记整理总结【Django】Ajax
    Python学习笔记整理总结【Django】:中间件、CSRF、缓存
    Python学习笔记整理总结【Django】:模板语言、分页、Cookie、Session
    Python学习笔记整理总结【Django】:Model操作(一)
    Python学习笔记整理总结【Django】【MVC/MTV/路由分配系统(URL)/视图函数 (views)/表单交互】
    数据结构与算法(C/C++版)【树与二叉树】
  • 原文地址:https://www.cnblogs.com/kex1n/p/2459582.html
Copyright © 2011-2022 走看看