Lines演示程序
#include "stdafx.h"
#include "d3d9.h"
#include "d3dx9.h"
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#define WINDOW_CLASS "UGPDX" //窗口类名称
#define WINDOW_NAME "Template" //窗口类标题
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
bool InitializeD3D(HWND hWnd,bool fullscreen);//用于在程序中设置和创建Direct3D
bool InitializeObjects(); //用于创建显示程序中要绘制在屏幕上的物体
void RenderScene();//用于在屏幕上渲染已经绘制好的图形
void Shutdown(); //用于在程序退出时进行一些销毁工作
//direct3D object and device
LPDIRECT3D9 g_D3D=NULL;
LPDIRECT3DDEVICE9 g_D3DDevice=NULL;
//Matrices
D3DXMATRIX g_projection;
D3DXMATRIX g_ViewMatrix;
D3DXMATRIX g_WorldMatrix;
//vertex buffer to hold the geometry
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer=NULL;//定义了一个顶点缓存对象
//A structure for our custom vertex type
//定义场景中单个3D点的结构
struct stD3Dvertex
{
float x,y,z,rhw;//点的x,y,z坐标值
unsigned long color;//点的颜色
};
//our custom FVF,which describes our custom vertex structure
//定义顶点格式标示符,即灵活顶点格式
#define D3DFVF_VERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
//窗口过程函数(系统自动调用,即回调函数)
LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
if(wParam==VK_ESCAPE)
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE prevhInst,LPSTR cmdLine,int
show)
{
//Register the window class
WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,
GetModuleHandle(NULL),NULL,NULL,NULL,NULL,
WINDOW_CLASS,NULL};
RegisterClassEx(&wc);
//create the application's window
HWND hWnd=CreateWindow(WINDOW_CLASS,WINDOW_NAME,WS_OVERLAPPEDWINDOW,
100,100,640,480,GetDesktopWindow(),NULL,
wc.hInstance,NULL);
//initialize Direct3D
if (InitializeD3D(hWnd,false))
{
ShowWindow(hWnd,SW_SHOWDEFAULT);
UpdateWindow(hWnd);
//enter the message loop
MSG msg;
ZeroMemory(&msg,sizeof(msg));//宏用0来填充一块内存区域
while(msg.message!=WM_QUIT)
{
if (PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))//从消息队列中获取下一条消息
{
TranslateMessage(&msg);//对相关消息进行一些转换
DispatchMessage(&msg);//将转换后的消息发送给消息过程函数
}
else
//处理向屏幕绘制图像的代码部分
RenderScene();
}
}
//release any and all resources
Shutdown();
UnregisterClass("AppClass",wc.hInstance);//取消对窗口类的注册
return 0;
}
//设置写入要绘制的场景数据
bool InitializeObjects()
{
unsigned long col=D3DCOLOR_XRGB(255,255,255);//红,绿,蓝
//Fill in our structure to draw an object
//x,y,z,rhw,color
//objData结构体数组:设置所有点的数据
stD3Dvertex objData[]=
{
{420.0f,150.0f,0.5f,1.0f,col,},//第一个点的坐标及颜色
{420.0f,350.0f,0.5f,1.0f,col,},//第二个点的坐标及颜色
{220.0f,150.0f,0.5f,1.0f,col,},//第三个点的坐标及颜色
{220.0f,350.0f,0.5f,1.0f,col,},//第四个点的坐标及颜色
};
//create the vertex buffer
//创建顶点缓存
if (FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData),0,
D3DFVF_VERTEX,D3DPOOL_DEFAULT,&g_VertexBuffer,
NULL)))
return false;
//fill the vertex buffer
void *ptr;
//锁定顶点缓存,以进行读写操作
if(FAILED(g_VertexBuffer->Lock(0,sizeof(objData),
(void**)&ptr,0)))
return false;
//将数据复制到该缓存中
memcpy(ptr,objData,sizeof(objData));
//对顶点缓存进行解锁
g_VertexBuffer->Unlock();
return true;
}
//绘制场景
void RenderScene()
{
//clear the back buffer
g_D3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,0),1.0f,0);
//begin the scene start rendering
g_D3DDevice->BeginScene();
//render object
//设置顶点数据流的输入源
g_D3DDevice->SetStreamSource(0,g_VertexBuffer,0,
sizeof(stD3Dvertex));
g_D3DDevice->SetFVF(D3DFVF_VERTEX);//设置顶点格式
g_D3DDevice->DrawPrimitive(D3DPT_LINELIST,0,2);//进行绘制
//end the scene.stop rendering
g_D3DDevice->EndScene();
//display the scene
//向显示器显示绘制的结果
g_D3DDevice->Present(NULL,NULL,NULL,NULL);
}
void Shutdown()
{
if(g_D3DDevice!=NULL) g_D3DDevice->Release();
if(g_D3D!=NULL) g_D3D->Release();
if(g_VertexBuffer!=NULL) g_VertexBuffer->Release();
g_D3DDevice=NULL;
g_D3D=NULL;
g_VertexBuffer=NULL;
}
bool InitializeD3D(HWND hWnd,bool fullscreen)
{
D3DDISPLAYMODE displayMode;
//create the D3D object
g_D3D=Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D==NULL) return false;
//get the desktop display mode
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
&displayMode)))
return false;
//set up the structure used to create the d3ddevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
if (fullscreen)
{
d3dpp.Windowed=FALSE;
d3dpp.BackBufferWidth=640;
d3dpp.BackBufferHeight=480;
}
else
d3dpp.Windowed=TRUE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat=displayMode.Format;
//create the D3Device
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,&g_D3DDevice)))
return false;
//initialize any objects we will be displaying
if(!InitializeObjects()) return false;
return true;
}
Lines演示程序的更多相关文章
- Kafka Streams演示程序
本文从以下六个方面详细介绍Kafka Streams的演示程序: Step 1: 下载代码 Step 2: 启动kafka服务 Step 3: 准备输入topic并启动Kafka生产者 Step 4: ...
- 图形学基础教程02--顶点数据和SHADER寄存器 (带演示程序)
本文系原创,欢迎转载,请标明链接 http://www.cnblogs.com/luming1979 有问题欢迎加qq群讨论:366239605
- extracting lines bases a list using awk
extracting lines bases a list using awk awk 'NR==FNR{a[$1]=$0; next}($1 in a){print a[$1]"\n&qu ...
- Enum:Game of Lines(POJ 3668)
画直线 题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条 数据量比较少,直接暴力枚举,然后放到set查找即可 #include <iostream> #include < ...
- 我的常用mixin 之 lines
/** * 最多显示 $lineCount 行 * lines * * example: * @include lines; * @include lines(3); */ @mixin lines( ...
- Direct3D11学习:(五)演示程序框架
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 在此系列最开始的文章Direct3D11学习:(一)开发环境配置中,我们运行了一个例子BoxDemo,看过这个例 ...
- uva 1471 defence lines——yhx
After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...
- POJ 1269 Intersecting Lines --计算几何
题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...
- POJ 1269 Intersecting Lines【判断直线相交】
题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...
随机推荐
- ios8中的UIScreen
let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation pri ...
- sharepoint 2010 找不到搜索不到ad里的用户
前提条件: 1.这个用户是在ad中存在的. 2.这个用户也同步到了userprofile中. 问题现象: 在sharepoint的人员选择器中,搜索不到已经添加的用户. 可能原因: 1.有人说需要将 ...
- 31.DDR2问题3_waring?
Warning: (vlog-2275) 'ddr2_controller' already exists and will be overwritte. 出现这个waring,是因为xxx_bb.v ...
- Junit单元测试中优先使用AssertThat
主要的优点: 1. 易读性 2. 错误信息更方便 推荐阅读:https://objectpartners.com/2013/09/18/the-benefits-of-using-assertthat ...
- 小学生四则运算C/C++编程设计思想
题目: 1.题目避免重复: 2.可定制(数量(打印方式)): 3.可控制下列参数:是否有乘除法.是否有括号.数值范围.加减有无负数. 除法有无余数.是否支持分 ...
- 如何用pdfbox-app-1.8.10.jar批处理将pdf文档转换成text文档
1.首先下载pdfbox-app-1.8.10.jar(下载地址:http://pdfbox.apache.org/download.html) 2.将pdfbox-app-1.8.10.jar加载到 ...
- JS 学习笔记--13---原型
练习中使用的是IE11,如果有错误之处,还请各位朋友多多指教.本文关于原型难以描述,故多用代码展示 原型是JS中一个很重要的概念,也是JS中一个难点,语言上难以描述,原型对象的属性和方法叫做原型属性和 ...
- iOS 开发中的问题
错误提示: Your build settings specify a provisioning profile with the UUID “39642B69-0278-4265-8392-4B28 ...
- 【bzoj1085】[SCOI2005]骑士精神
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1757 Solved: 961[Submit][Statu ...
- DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
这个类型的resource 后面多个SRGB 表示SRGB空间是指gamma 矫正系数为2.2的色彩空间,这个问题我没有问别人,是我自己是上网查的,我觉得我不是个女人了......我又level up ...