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;
}
DirectX基础学习系列4 颜色和光照的更多相关文章
- DirectX 基础学习系列5 纹理映射
1 纹理坐标 类似BMP图像坐标系,左上为原点 纹理坐标为了规范化,范围限定在[0,1]之间,使用纹理的时候,需要修改顶点结构 struct ColorVetex { float x, y,z; fl ...
- DirectX基础学习系列2
补充第一章矩阵内容 向量 1 3D空间向量,包含浮点数类型坐标 D3DXVECTOR-->D3DXVECTOR3 2向量的长度 D3DXVector3Length(const D3DXVECTO ...
- DirectX基础学习系列8 渐进网格以及外接体
1 IUnknown--> ID3DXBUFFER D3D泛型接口: GetBufferPointer Retrieves a pointer to the data in the buffer ...
- DirectX基础学习系列5 融合技术
7.1融合方程 1概念 融合技术将当前光栅化像素的颜色与以前已光栅化并处于同一个位置的像素颜色进行合成,即将当前要进行光栅化的三角形单元与已写入后台的像素进行融合 2需要遵循的原则: (1)先绘制不需 ...
- DirectX基础学习系列1
1.3 基础 1.3.1表面 表面接口: IDirect3DSurface9 获得表面信息:GetDesc(D3DSURFACE_DESC) 获得表面接口指针 :LockRect( D3DLO ...
- directX基础学习系列7 网格(自己创建)
D3DXMesh 以及 D3DXPMesh都是从ID3DXBaseMesh类中集成,mesh基本是对三角单元进行操作 ID3DXBaseMesh主要函数: HRESULT DrawSubset( DW ...
- DirectX 基础学习系列6 字体
DIRECTX9自带ID3DXFONT类 内部调用GDI的接口,效率一般,但能够处理一些复杂的字体 HRESULT D3DXCreateFontIndirect( LPDIRECT3DDEVICE9 ...
- Linux基础学习系列目录导航
Linux基础学习-通过VM安装RHEL7.4 Linux基础学习-命令行与图形界面切换 Linux基础学习-基本命令 Linux基础学习-RHEL7.4之YUM更换CentOS源 Linux基础学习 ...
- Bootstrap基础学习 ---- 系列文章
[Bootstrap基础学习]05 Bootstrap学习总结 [Bootstrap基础学习]04 Bootstrap的HTML和CSS编码规范 [Bootstrap基础学习]03 Bootstrap ...
随机推荐
- Java Hour 34 Weather ( 7 ) struts2 – validate
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 34 Form Validation 一般Form 提交都有验证的, ...
- 基于JQuery实现滚动到页面底端时自动加载更多信息
基于JQuery实现滚动到页面底端时自动加载更多信息 关键代码: 代码如下: var stop=true; $(window).scroll(function(){ totalheight = par ...
- WordPress主题制作函数
WordPress基本模板文件 一套完整的WordPress模板应至少具有如下文件: style.css: CSS(样式表)文件 index.php: 主页模板 archive.php: Archiv ...
- javaWeb项目中web.xml的xsd( XML Schemas Definition)文件
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://w ...
- 获取表信息(MSSQL)
涉及到的系统表汇总 sys.databases sys.objects sys.indexes sys.tables sys.columns sys.data_spaces sys.partition ...
- Web测试Selenium:如何选取元素
Web测试工具Selenium:如何选取元素 2009-02-17 23:23 by 敏捷的水, 5372 阅读, 22 评论, 收藏, 编辑 selenium是一个非常棒的Web测试工具,他对Aja ...
- Quartz.Net 配置模板范例
1.App.config <?xml version="1.0" encoding="utf-8"?> <configuration& ...
- html加强
<html> <head><title>hello</title></head> <body> <p>段落</ ...
- Cat VS Dog
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- cocos3 singleton
class TestSingleton : public CCLayer { public: static TestSingleton* getInstance();//创建一个全局访问点,例如我们常 ...