DirectX基础学习系列4 颜色和光照

4.1颜色表示

RGB颜色:D3DCOLOR  可以用宏D3DCOLOR_ARGB(a,r,g,b)  D3DCOLOR_XRGB(255,r,g,b)

另外一种浮点表示:D3DCOLORVALUE,浮点类型,最小为0 最大为1

4.2顶点颜色

struct ColorVetex

{

    float x, y,z;

    D3DCOLOR color;

    static const DWORD FVF;

}

const DWORD ColorVetex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE ;

4.3着色

两种着色方式:shading mode

1flat shading 平面着色:每个图元的像素都被赋予该图元的第一个顶点的颜色

2gourand shading :各像素的颜色由着色的三个顶点颜色插值决定、

设置着色模式:Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);

5 光照

5.1光照的组成

1环境光 

2漫射光:特定方向,达到表面后均匀反射

3镜面光 :特定方向,达到表面后严格向另外一个方向反射,形成在一定范围内可看的高亮区域,计算量很大

                可以控制开关:Device->SetRenderState(D3DRS_SPECULARENABLE, TRUE);

5.2材质

材质允许定义对各种颜色光的反射比

typedef struct D3DMATERIAL9 {
    D3DCOLORVALUE Diffuse;
    D3DCOLORVALUE Ambient;
    D3DCOLORVALUE Specular;
    D3DCOLORVALUE Emissive;
    float Power;
} D3DMATERIAL9, *LPD3DMATERIAL9;

设置材质属性:HRESULT SetMaterial( CONST D3DMATERIAL9 * pMaterial );

5.3顶点法线

struct ColorVetex

{

    float x, y,z;

    float _nx,_ny,_nz ;

    static const DWORD FVF;

}

const DWORD ColorVetex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL;

注意顶点向量的规范化:Device->SetRenderState(D3DRS_NORMALIZEENABLE, TRUE);

5.4 光源

DX支持的三种光源:点光源,方向光,聚光灯

typedef struct D3DLIGHT9 {
    D3DLIGHTTYPE Type;      //D3DLIGHT_POINT  D3DLIGHT_SPOT  D3DLIGHT_DIRECTIONAL 
    D3DCOLORVALUE Diffuse;  //漫反射光颜色
    D3DCOLORVALUE Specular; //镜面反射光颜色
    D3DCOLORVALUE Ambient;  //环境光颜色
    D3DVECTOR Position;     //光源位置,方向光该参数无意义
    D3DVECTOR Direction;    //方向,点光源无意义
    float Range;    //最大光程,方向光无意义
    float Falloff;  //聚光灯从内到外的衰减程度
    float Attenuation0;
    float Attenuation1;
    float Attenuation2; //点光源和聚光灯随距离光强的衰减方式
    float Theta;   // 聚光灯内角
    float Phi;    // 聚光灯外角
} D3DLIGHT9, *LPD3DLIGHT;

光源设置完之后 需要注册,dx维护了一个光源列表

device->SetLight(0,&light);

注册完之后 可以进行控制

device->LightEnable();

5.5场景添加光源的方法:

1启用光照

2创建材质,设置材质

3创建光源,打开光源

4启用其余光源

 

代码:

 
 
#include "d3dUtility.h"
//
// Globals
//
 
IDirect3DDevice9* Device = 0; 
 
const int Width  = 640;
const int Height = 480;
 
IDirect3DVertexBuffer9* Pyramid = 0;
 
//
// Classes and Structures
//
struct Vertex
{
    Vertex(){}
 
    Vertex(float x, float y, float z, float nx, float ny, float nz)
    {
        _x  = x;  _y  = y;    _z  = z;
        _nx = nx; _ny = ny; _nz = nz;
    }
    float  _x,  _y,  _z;
    float _nx, _ny, _nz;
 
    static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL;
 
//
// Framework Functions
//
bool Setup()
{
    //
    // Turn on lighting.
    //
    Device->SetRenderState(D3DRS_LIGHTING, true);
 
    //
    // Create the vertex buffer for the pyramid.
    //
 
    Device->CreateVertexBuffer(
        12 * sizeof(Vertex), 
        D3DUSAGE_WRITEONLY,
        Vertex::FVF,
        D3DPOOL_MANAGED,
        &Pyramid,
        0);
 
    //
    // Fill the vertex buffer with pyramid data.
    //
 
    Vertex* v;
    Pyramid->Lock(0, 0, (void**)&v, 0);
 
    // front face
    v[0] = Vertex(-1.0f, 0.0f, -1.0f, 0.0f, 0.707f, -0.707f);
    v[1] = Vertex( 0.0f, 1.0f,  0.0f, 0.0f, 0.707f, -0.707f);
    v[2] = Vertex( 1.0f, 0.0f, -1.0f, 0.0f, 0.707f, -0.707f);
 
    // left face
    v[3] = Vertex(-1.0f, 0.0f,  1.0f, -0.707f, 0.707f, 0.0f);
    v[4] = Vertex( 0.0f, 1.0f,  0.0f, -0.707f, 0.707f, 0.0f);
    v[5] = Vertex(-1.0f, 0.0f, -1.0f, -0.707f, 0.707f, 0.0f);
 
    // right face
    v[6] = Vertex( 1.0f, 0.0f, -1.0f, 0.707f, 0.707f, 0.0f);
    v[7] = Vertex( 0.0f, 1.0f,  0.0f, 0.707f, 0.707f, 0.0f);
    v[8] = Vertex( 1.0f, 0.0f,  1.0f, 0.707f, 0.707f, 0.0f);
 
    // back face
    v[9]  = Vertex( 1.0f, 0.0f,  1.0f, 0.0f, 0.707f, 0.707f);
    v[10] = Vertex( 0.0f, 1.0f,  0.0f, 0.0f, 0.707f, 0.707f);
    v[11] = Vertex(-1.0f, 0.0f,  1.0f, 0.0f, 0.707f, 0.707f);
 
    Pyramid->Unlock();
 
    //
    // Create and set the material.
    //
 
    D3DMATERIAL9 mtrl;
    mtrl.Ambient  = d3d::WHITE;
    mtrl.Diffuse  = d3d::WHITE;
    mtrl.Specular = d3d::WHITE;
    mtrl.Emissive = d3d::BLACK;
    mtrl.Power    = 5.0f;
 
    Device->SetMaterial(&mtrl);
 
    //
    // Setup a directional light.
    //
 
    D3DLIGHT9 dir;
    ::ZeroMemory(&dir, sizeof(dir));
    dir.Type      = D3DLIGHT_DIRECTIONAL;
    dir.Diffuse   = d3d::RED;
    dir.Specular  = d3d::WHITE * 0.3f;
    dir.Ambient   = d3d::WHITE * 0.6f;
    dir.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
 
    //
    // Set and Enable the light.
    //
 
    Device->SetLight(0, &dir);
    Device->LightEnable(0, true);
 
    //
    // Turn on specular lighting and instruct Direct3D
    // to renormalize normals.
    //
 
    Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
    Device->SetRenderState(D3DRS_SPECULARENABLE, false);
 
    //
    // Position and aim the camera.
    //
 
    D3DXVECTOR3 pos(0.0f, 1.0f, -3.0f);
    D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
    D3DXMatrixLookAtLH(&V, &pos, &target, &up);
    Device->SetTransform(D3DTS_VIEW, &V);
 
    //
    // Set the projection matrix.
    //
 
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(
            &proj,
            D3DX_PI * 0.5f, // 90 - degree
            (float)Width / (float)Height,
            1.0f,
            1000.0f);
    Device->SetTransform(D3DTS_PROJECTION, &proj);
 
    return true;
}
 
void Cleanup()
{
    d3d::Release<IDirect3DVertexBuffer9*>(Pyramid);
}
 
bool Display(float timeDelta)
{
    if( Device )
    {
        // 
        // Update the scene: Rotate the pyramid.
        //
 
        D3DXMATRIX yRot;
 
        static float y = 0.0f;
 
        D3DXMatrixRotationY(&yRot, y);
        y += timeDelta;
 
        if( y >= 6.28f )
            y = 0.0f;
 
        Device->SetTransform(D3DTS_WORLD, &yRot);
 
        //
        // Draw the scene:
        //
 
        Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
        Device->BeginScene();
 
        Device->SetStreamSource(0, Pyramid, 0, sizeof(Vertex));
        Device->SetFVF(Vertex::FVF);
        Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);
 
        Device->EndScene();
        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,
        Width, Height, 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;
}
posted @ 2013-10-23 00:10  RubbyZhang  阅读(638)  评论(0编辑  收藏  举报