文档章节

D3D中的光照(3)

rise-worlds
 rise-worlds
发布于 2016/06/20 13:34
字数 598
阅读 2
收藏 0

平行光示例:

The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.

Syntax

SHORT GetAsyncKeyState(      
    int vKey
);

Parameters

vKey
[in] Specifies one of 256 possible virtual-key codes. For more information, see Virtual-Key Codes.

Return Value

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

截图:

源代码:

/**************************************************************************************
  Demonstrates using a directional light with D3DX objects. 
  You can orbit the scene using the left and right arrow keys. 
  In addition you can elevate the camera with the up and down arrow keys. 
**************************************************************************************/
#include "d3dUtility.h"
#pragma warning(disable : 4100)
#define MESH_TEAPOT        0
#define MESH_SPHERE        1
#define MESH_TORUS        2
#define MESH_CYLINDER    3
#define NUM_MESH        4
const int WIDTH  = 640;
const int HEIGHT = 480;
IDirect3DDevice9*        g_d3d_device  = NULL;
ID3DXMesh*                g_object_meshes[NUM_MESH];
D3DXMATRIX                g_world_matrices[NUM_MESH];
D3DMATERIAL9            g_materials[NUM_MESH];
////////////////////////////////////////////////////////////////////////////////////////////////////
bool setup()
{   
// create objects
    D3DXCreateTeapot(g_d3d_device, &g_object_meshes[MESH_TEAPOT], NULL);
    D3DXCreateSphere(g_d3d_device, 1.0f, 20, 20, &g_object_meshes[MESH_SPHERE], NULL);
    D3DXCreateTorus(g_d3d_device, 0.5f, 1.0f, 20, 20, &g_object_meshes[MESH_TORUS], NULL);
    D3DXCreateCylinder(g_d3d_device, 0.5f, 1.0f, 2.0f, 20, 20, &g_object_meshes[MESH_CYLINDER], NULL);
// build world matrices - position the objects in world space
    D3DXMatrixTranslation(&g_world_matrices[MESH_TEAPOT],     0.0f,  2.0f, 0.0f);
    D3DXMatrixTranslation(&g_world_matrices[MESH_SPHERE],     0.0f, -2.0f, 0.0f);
    D3DXMatrixTranslation(&g_world_matrices[MESH_TORUS],    -3.0f,  0.0f, 0.0f);
    D3DXMatrixTranslation(&g_world_matrices[MESH_CYLINDER],     3.0f,  0.0f, 0.0f);
// setup the object's materials
    g_materials[MESH_TEAPOT]   = RED_MATERIAL;
    g_materials[MESH_SPHERE]   = BLUE_MATERIAL;
    g_materials[MESH_TORUS]    = GREEN_MATERIAL;
    g_materials[MESH_CYLINDER] = YELLOW_MATERIAL;
// setup a directional light
    D3DXVECTOR3 light_direction(1.0f, -0.0f, 0.25f);
    D3DXCOLOR   color = WHITE;
    D3DLIGHT9   dir_light = init_directional_light(&light_direction, &color);
// set and enable the light
    g_d3d_device->SetLight(0, &dir_light);
    g_d3d_device->LightEnable(0, TRUE);
// turn off specular lighting and instruct Direct3D to renormalize normals
    g_d3d_device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
    g_d3d_device->SetRenderState(D3DRS_SPECULARENABLE, FALSE);
// set the projection matrix
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.25f, (float)WIDTH/HEIGHT, 1.0f, 1000.0f);
    g_d3d_device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void cleanup()
{
for(int i = 0; i < NUM_MESH; i++)
        safe_release<ID3DXMesh*>(g_object_meshes[i]);
}
bool display(float time_delta)
{
// update the scene: update camera position
static float angle = (3.0f * D3DX_PI) / 2.0f;
static float height = 5.0f;
if(GetAsyncKeyState(VK_LEFT) & 0x8000f)
        angle -= 0.5f * time_delta;
if(GetAsyncKeyState(VK_RIGHT) & 0x8000f)
        angle += 0.5f * time_delta;
if(GetAsyncKeyState(VK_UP) & 0x8000f)
        height += 5.0f * time_delta;
if(GetAsyncKeyState(VK_DOWN) & 0x8000f)
        height -= 5.0f * time_delta;
    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 view_matrix;
    D3DXMatrixLookAtLH(&view_matrix, &position, &target, &up);
    g_d3d_device->SetTransform(D3DTS_VIEW, &view_matrix);
// draw the scene
    g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
    g_d3d_device->BeginScene();
for(int i = 0; i < NUM_MESH; i++)
    {
// set material and world matrix for ith object, then render the ith object.
        g_d3d_device->SetMaterial(&g_materials[i]);
        g_d3d_device->SetTransform(D3DTS_WORLD, &g_world_matrices[i]);
        g_object_meshes[i]->DrawSubset(0);
    }
    g_d3d_device->EndScene();
    g_d3d_device->Present(NULL, NULL, NULL, NULL);
return true;
}
LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
{
switch(msg)
    {
case WM_DESTROY:
        PostQuitMessage(0);
break;
case WM_KEYDOWN:
if(word_param == VK_ESCAPE)
            DestroyWindow(hwnd);
break;
    }
return DefWindowProc(hwnd, msg, word_param, long_param);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, PSTR cmd_line, int cmd_show)
{
if(! init_d3d(inst, WIDTH, HEIGHT, true, D3DDEVTYPE_HAL, &g_d3d_device))
    {
        MessageBox(NULL, "init_d3d() - failed.", 0, MB_OK);
return 0;
    }
if(! setup())
    {
        MessageBox(NULL, "Steup() - failed.", 0, MB_OK);
return 0;
    }
    enter_msg_loop(display);
    cleanup();
    g_d3d_device->Release();
return 0;
}

下载源程序

本文转载自:http://www.cnblogs.com/flying_bat/archive/2008/03/20/1115374.html

rise-worlds

rise-worlds

粉丝 2
博文 1755
码字总数 0
作品 0
深圳
程序员
私信 提问
如何使用DXUT框架

DXUT是什么? DXUT即DirectX Utility Library,它是微软为DirectX Samples写的一个框架,有了这个框架,Sample的构建就方便多了,这个框架实际上抽取了构建Sample的公共代码,比如处理窗口消...

吞吞吐吐的
2017/10/12
0
0
D3D9 Device类型

D3D9主要有两种类型的Device,一种是HAL Device(Hardware Abstraction Layer,硬件抽象层)。另一种是REF Device(References Device)。 HAL Device 这是主要的设备类型,该类型支持硬件加...

吞吞吐吐的
2017/09/11
0
0
SDL2源代码分析6:复制到渲染器(SDL_RenderCopy())

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leixiaohua1020/article/details/40895593 ===================================================== SDL源代码......

雷霄骅
2014/11/08
0
0
如何学好游戏3D引擎编程

注:本文是网上看到的一篇文章,感觉写的很好,因此收藏了下来 《如何学好游戏3D引擎编程》 此篇文章献给那些为了游戏编程不怕困难的热血青年,它的神秘要我永远不间断的去挑战自我,超越自我...

loving_forever_
2016/07/28
0
0
关于DIRECTX 的一个小程序,运行之后有窗口无图像。

代码如下: #include #include #include #include #pragma comment(lib,"dxerr.lib") #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "d3dx9.lib") #pragma comment(lib, "dxguid.l......

zkhy
2012/11/02
635
0

没有更多内容

加载失败,请刷新页面

加载更多

代码持续自动发布

需求: 自动更新私人gitbook项目代码 如果代码没更新,不做务必要的构建操作 思路 定时任务加shell脚本,无论更新没更新都执行git pull。 优点:能满足需求 资源浪费,代码仓库不更新也执行p...

阿dai学长
31分钟前
6
0
sparkstreaming原理

Spark Streaming是Spark生态系统当中一个重要的框架,它建立在Spark Core之上,下面这幅图也可以看出Sparking Streaming在Spark生态系统中地位。 Spark Streaming是Spark Core的扩展应用,它...

七旬少女
50分钟前
1
0
springmvc集成cas,并解决前后端分离情况

1.最近项目需要集成已经存在的cas系统。 但是目前已集成的系统都是jsp。而我们项目是前后端分离开发(伪),没有分开部署。 2.cas原理就不介绍了 网上例子很多。基本都是使用302重定向实现的...

起名字什么的太麻烦了
今天
4
0
HDFS-原理

1. 写操作 客户端要向HDFS写数据,首先要跟Namenode通信以确认可以写文件并获得接收文件block的Datanode,然后,客户端按顺序将文件逐个block传递给相应Datanode,并由接收到block的Datanod...

叶枫啦啦
今天
3
0
聊聊elasticsearch的MembershipAction

序 本文主要研究一下elasticsearch的MembershipAction MembershipAction elasticsearch-6.7.1/server/src/main/java/org/elasticsearch/discovery/zen/MembershipAction.java public class M......

go4it
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部