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

DirectX基础学习系列4 颜色和光照的更多相关文章

  1. DirectX 基础学习系列5 纹理映射

    1 纹理坐标 类似BMP图像坐标系,左上为原点 纹理坐标为了规范化,范围限定在[0,1]之间,使用纹理的时候,需要修改顶点结构 struct ColorVetex { float x, y,z; fl ...

  2. DirectX基础学习系列2

    补充第一章矩阵内容 向量 1 3D空间向量,包含浮点数类型坐标 D3DXVECTOR-->D3DXVECTOR3 2向量的长度 D3DXVector3Length(const D3DXVECTO ...

  3. DirectX基础学习系列8 渐进网格以及外接体

    1 IUnknown--> ID3DXBUFFER D3D泛型接口: GetBufferPointer Retrieves a pointer to the data in the buffer ...

  4. DirectX基础学习系列5 融合技术

    7.1融合方程 1概念 融合技术将当前光栅化像素的颜色与以前已光栅化并处于同一个位置的像素颜色进行合成,即将当前要进行光栅化的三角形单元与已写入后台的像素进行融合 2需要遵循的原则: (1)先绘制不需 ...

  5. DirectX基础学习系列1

    1.3 基础 1.3.1表面 表面接口:     IDirect3DSurface9 获得表面信息:GetDesc(D3DSURFACE_DESC) 获得表面接口指针 :LockRect( D3DLO ...

  6. directX基础学习系列7 网格(自己创建)

    D3DXMesh 以及 D3DXPMesh都是从ID3DXBaseMesh类中集成,mesh基本是对三角单元进行操作 ID3DXBaseMesh主要函数: HRESULT DrawSubset( DW ...

  7. DirectX 基础学习系列6 字体

    DIRECTX9自带ID3DXFONT类 内部调用GDI的接口,效率一般,但能够处理一些复杂的字体 HRESULT D3DXCreateFontIndirect( LPDIRECT3DDEVICE9 ...

  8. Linux基础学习系列目录导航

    Linux基础学习-通过VM安装RHEL7.4 Linux基础学习-命令行与图形界面切换 Linux基础学习-基本命令 Linux基础学习-RHEL7.4之YUM更换CentOS源 Linux基础学习 ...

  9. Bootstrap基础学习 ---- 系列文章

    [Bootstrap基础学习]05 Bootstrap学习总结 [Bootstrap基础学习]04 Bootstrap的HTML和CSS编码规范 [Bootstrap基础学习]03 Bootstrap ...

随机推荐

  1. 【Python】【解决】UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)

    1.问题描述 今天在升级Ubuntu到14.04,使用命令行启动软件更新器,进行版本升级,结果开始升级就异常退出了,具体打印如下: $update-manager -d 正在检查新版 Ubuntu 使 ...

  2. jquery之别踩白块游戏的实现

    转载请注明出处http://www.cnblogs.com/Wxtrkbc/p/5687112.html 前端学习要告一段落了,也没机会写什么像样的东西,然后无意中想起某人以前给我玩了一下别踩白块的游 ...

  3. javascript二叉树基本功能实现

    都是常用的功能. 删除是最复杂的.. <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  4. android 搭建环境工具

    ---恢复内容开始--- 一.下载jdk 1.JDK可以去sun的官方网站下载Java SE Development Kit 6  http://java.sun.com/javase/downloa ...

  5. hdu 4831

    写了一个小时题目看错了,艹 自己百度吧

  6. Struts2标签实现for循环

    感悟:但是不建议使用这种方法,按照MVC框架的思想 ,应该把业务更多放在后台.前台尽量只进行数据展示. 转自:http://blog.csdn.net/guandajian/article/detai ...

  7. Android快捷键

    Android快捷键ALT+/ :在布局文件中,提示输入的内容Shift + Ctrl +  / :注释Shift + Ctrl +  \ :解除注释

  8. Codeforces Gym 100203I I - I WIN 网络流最大流

    I - I WINTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...

  9. 在MSSQL中对ACCESS文件操作方式汇总

    CSDN朋友问题回复: 有个ACCESS数据库文件在本机,机器上的OFFICE套件已经卸载,ACCESS没有用户名和密码,如何用MSSQLServer来查询和修改其文件内容? 比如ACCESS物理文件 ...

  10. 最新IP地址数据库

    2016年12月1日 最新发行版 265051条数据 基于:国内基于省市区以及运营商 国外基于国家 版本:全球旗舰版  国内精华版 国外拓展版 英文版 掩码版 字段:大洲 国家 省份 城市 县区 运营 ...