我是是个程序设计的爱好者,大学期间是学计算机的后来去当服务员了,我把一段C++代码转成了C#希望能给需要朋友点提示,希望大家留言指出我的问题。我会继续发表文章,关于3D。这个程序只是简单的现实两个多边行,锥形沿着Y轴旋转,正矩形沿着X轴旋转。C++directx8例子出自《Visual C++ OpenGL DirectX 三维动画编程宝典》,不错的一本书。
C#代码 directx9 managed
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D=Microsoft.DirectX.Direct3D;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
Device device=null;
VertexBuffer VB=null;
PresentParameters presentpara=new PresentParameters();
bool pause = false;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
public void OnResetDevice(object obj,EventArgs e)
{
Device dev = (Device)obj;
device.RenderState.CullMode=Cull.None;
device.RenderState.ZBufferEnable = true;
device.RenderState.Lighting = false;
}
public void OnCreateDevice(object obj,EventArgs e)
{
Device dev=(Device)obj;
VB=new VertexBuffer(typeof(CustomVertex.PositionColored), 42, dev, Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
VB.Created+=new EventHandler(this.OnCreateVertexBuffer);
this.OnCreateVertexBuffer(VB,null);
}
public void OnCreateVertexBuffer(object obj, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)obj;
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vb.Lock(0,0);
verts[0].Position = new Vector3(0,1,0);
verts[0].Color=System.Drawing.Color.DarkGoldenrod.ToArgb();//System.Drawing.Color.FromArgb(0,255,255,0);
//{ 0.0f, 1.0f, 0.0f, System.Drawing.Color.DarkGoldenrod.ToArgb(); },
verts[1].Position = new Vector3(1,-1,-1);
verts[1].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
//{ 1.0f, -1.0f,-1.0f, 0x0000FF00 },
verts[2].Position = new Vector3(-1,-1,-1);
verts[2].Color=System.Drawing.Color.Cornsilk.ToArgb();;//new System.Drawing.Color.FromArgb(0,0,0,255);
//{ -1.0f, -1.0f,-1.0f, System.Drawing.Color.Cornsilk.ToArgb(); },
verts[3].Position = new Vector3(-1,-1,1);
verts[3].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
//{ -1.0f, -1.0f, 1.0f, 0x0000FF00 },
verts[4].Position = new Vector3(1,-1,1);
verts[4].Color=System.Drawing.Color.Cornsilk.ToArgb();;//new System.Drawing.Color.FromArgb(0,0,0,255);
// { 1.0f, -1.0f, 1.0f, System.Drawing.Color.Cornsilk.ToArgb(); },
verts[5].Position = new Vector3(1,-1,-1);
verts[5].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
//{ 1.0f, -1.0f,-1.0f, 0x0000FF00 },
verts[6].Position = new Vector3(-1,-1,-1);
verts[6].Color=System.Drawing.Color.Cornsilk.ToArgb();;//new System.Drawing.Color.FromArgb(0,0,0,255);
//{ -1.0f, -1.0f, -1.0f, System.Drawing.Color.Cornsilk.ToArgb(); },
verts[7].Position = new Vector3(-1,1,-1);
verts[7].Color=System.Drawing.Color.Cornsilk.ToArgb();;//new System.Drawing.Color.FromArgb(0,0,0,255);
//{ -1.0f, 1.0f, -1.0f, System.Drawing.Color.Cornsilk.ToArgb(); },
verts[8].Position = new Vector3(1,1,-1);
verts[8].Color=System.Drawing.Color.Cornsilk.ToArgb();;//new System.Drawing.Color.FromArgb(0,0,0,255);
//{ 1.0f, 1.0f, -1.0f, System.Drawing.Color.Cornsilk.ToArgb(); },
verts[9].Position = new Vector3(1,1,-1);
verts[9].Color=System.Drawing.Color.Cornsilk.ToArgb();;//new System.Drawing.Color.FromArgb(0,0,0,255);
// { 1.0f, 1.0f, -1.0f, System.Drawing.Color.Cornsilk.ToArgb(); },
verts[10].Position = new Vector3(1,-1,-1);
verts[10].Color=System.Drawing.Color.Cornsilk.ToArgb();;//new System.Drawing.Color.FromArgb(0,0,0,255);
//{ 1.0f, -1.0f, -1.0f, System.Drawing.Color.Cornsilk.ToArgb(); },
verts[11].Position = new Vector3(-1,-1,-1);
verts[11].Color=System.Drawing.Color.Cornsilk.ToArgb();;//new System.Drawing.Color.FromArgb(0,0,0,255);
//{ -1.0f, -1.0f, -1.0f, System.Drawing.Color.Cornsilk.ToArgb(); },
verts[12].Position = new Vector3(1,-1,1);
verts[12].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
//{ 1.0f, -1.0f, 1.0f, 0x0000FF00 },
verts[13].Position = new Vector3(1,1,1);
verts[13].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
// { 1.0f, 1.0f, 1.0f, 0x0000FF00 },
verts[14].Position = new Vector3(-1,1,1);
verts[14].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
//{ -1.0f, 1.0f, 1.0f, 0x0000FF00 },
verts[15].Position = new Vector3(-1,1,1);
verts[15].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
//{ -1.0f, 1.0f, 1.0f, 0x0000FF00 },
verts[16].Position = new Vector3(-1,-1,1);
verts[16].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
// { -1.0f, -1.0f, 1.0f, 0x0000FF00 },
verts[17].Position = new Vector3(1,-1,1);
verts[17].Color=System.Drawing.Color.MediumOrchid.ToArgb();//new System.Drawing.Color.FromArgb(0,0,255,0);
//{ 1.0f, -1.0f, 1.0f, 0x0000FF00 },
verts[18].Position = new Vector3(-1,1,-1);
verts[18].Color=System.Drawing.Color.DarkGoldenrod.ToArgb();;//new System.Drawing.Color.FromArgb(0,255,0,0);
// { -1.0f, 1.0f, -1.0f, System.Drawing.Color.DarkGoldenrod.ToArgb(); },
verts[19].Position = new Vector3(-1,1,1);
verts[19].Color=System.Drawing.Color.DarkGoldenrod.ToArgb();;//new System.Drawing.Color.FromArgb(0,255,0,0);
//{ -1.0f, 1.0f, 1.0f, System.Drawing.Color.DarkGoldenrod.ToArgb(); },
verts[20].Position = new Vector3(1,1,1);
verts[20].Color=System.Drawing.Color.DarkGoldenrod.ToArgb();;//new System.Drawing.Color.FromArgb(0,255,0,0);
//{ 1.0f, 1.0f, 1.0f, System.Drawing.Color.DarkGoldenrod.ToArgb(); },
verts[21].Position = new Vector3(1,1,1);
verts[21].Color=System.Drawing.Color.DarkGoldenrod.ToArgb();;//new System.Drawing.Color.FromArgb(0,255,0,0);
//{ 1.0f, 1.0f, 1.0f, System.Drawing.Color.DarkGoldenrod.ToArgb(); },
verts[22].Position = new Vector3(1,1,-1);
verts[22].Color=System.Drawing.Color.DarkGoldenrod.ToArgb();;//new System.Drawing.Color.FromArgb(0,255,0,0);
// { 1.0f, 1.0f, -1.0f, System.Drawing.Color.DarkGoldenrod.ToArgb(); },
verts[23].Position = new Vector3(-1,1,-1);
verts[23].Color=System.Drawing.Color.DarkGoldenrod.ToArgb();;//new System.Drawing.Color.FromArgb(0,255,0,0);
//{ -1.0f, 1.0f, -1.0f, System.Drawing.Color.DarkGoldenrod.ToArgb(); },
verts[24].Position = new Vector3(1,-1,-1);
verts[24].Color=0x00FFFF00;//new System.Drawing.Color.FromArgb(0,255,255,0);
//{ 1.0f, -1.0f, -1.0f, 0x00FFFF00 },
verts[25].Position = new Vector3(1,-1,1);
verts[25].Color=0x00FFFF00;//new System.Drawing.Color.FromArgb(0,255,255,0);
// { 1.0f, -1.0f, 1.0f, 0x00FFFF00 },
verts[26].Position = new Vector3(-1,-1,1);
verts[26].Color=0x00FFFF00;//new System.Drawing.Color.FromArgb(0,255,255,0);
// { -1.0f, -1.0f, 1.0f, 0x00FFFF00 },
verts[27].Position = new Vector3(-1,-1,1);
verts[27].Color=0x00FFFF00;//new System.Drawing.Color.FromArgb(0,255,255,0);
// { -1.0f, -1.0f, 1.0f, 0x00FFFF00 },
verts[28].Position = new Vector3(-1,-1,-1);
verts[28].Color=0x00FFFF00;//new System.Drawing.Color.FromArgb(0,255,255,0);
//{ -1.0f, -1.0f, -1.0f, 0x00FFFF00 },
verts[29].Position = new Vector3(1,-1,-1);
verts[29].Color=0x00FFFF00;//new System.Drawing.Color.FromArgb(0,255,255,0);
// { 1.0f, -1.0f, -1.0f, 0x00FFFF00 },
verts[30].Position = new Vector3(-1,-1,1);
verts[30].Color=0x00FF00FF;//new System.Drawing.Color.FromArgb(0,255,0,255);
//{ -1.0f, -1.0f, 1.0f, 0x00FF00FF },
verts[31].Position = new Vector3(-1,1,1);
verts[31].Color=0x00FF00FF;//new System.Drawing.Color.FromArgb(0,255,0,255);
//{ -1.0f, 1.0f, 1.0f, 0x00FF00FF },
verts[32].Position = new Vector3(-1,1,-1);
verts[32].Color=0x00FF00FF;//new System.Drawing.Color.FromArgb(0,255,0,255);
//{ -1.0f, 1.0f, -1.0f, 0x00FF00FF },
verts[33].Position = new Vector3(-1,1,-1);
verts[33].Color=0x00FF00FF;//new System.Drawing.Color.FromArgb(0,255,0,255);
//{ -1.0f, 1.0f, -1.0f, 0x00FF00FF },
verts[34].Position = new Vector3(-1,-1,-1);
verts[34].Color=0x00FF00FF;//new System.Drawing.Color.FromArgb(0,255,0,255);
//{ -1.0f, -1.0f, -1.0f, 0x00FF00FF },
verts[35].Position = new Vector3(-1,-1,1);
verts[35].Color=0x00FF00FF;//new System.Drawing.Color.FromArgb(0,255,0,255);
//{ -1.0f, -1.0f, 1.0f, 0x00FF00FF },
verts[36].Position = new Vector3(1,-1,-1);
verts[36].Color=0x0000FFFF; //new System.Drawing.Color.FromArgb(0,0,255,255);
// { 1.0f, -1.0f, -1.0f, 0x0000FFFF },
verts[37].Position = new Vector3(1,1,-1);
verts[37].Color=0x0000FFFF;//new System.Drawing.Color.FromArgb(0,0,255,255);
// { 1.0f, 1.0f, -1.0f, 0x0000FFFF },
verts[38].Position = new Vector3(1,1,1);
verts[38].Color=0x0000FFFF;//new System.Drawing.Color.FromArgb(0,0,255,255);
//{ 1.0f, 1.0f, 1.0f, 0x0000FFFF },
verts[39].Position = new Vector3(1,1,1);
verts[39].Color=0x0000FFFF;//new System.Drawing.Color.FromArgb(0,0,255,255);
//{ 1.0f, 1.0f, 1.0f, 0x0000FFFF },
verts[40].Position = new Vector3(1,-1,1);
verts[40].Color=0x0000FFFF;//new System.Drawing.Color.FromArgb(0,0,255,255);
//{ 1.0f, -1.0f, 1.0f, 0x0000FFFF },
verts[41].Position = new Vector3(1,-1,-1);
verts[41].Color=0x0000FFFF;//new System.Drawing.Color.FromArgb(0,0,255,255);
// { 1.0f, -1.0f, -1.0f, 0x0000FFFF },
VB.Unlock();
}
public void screne()
{
if(pause)
return;
device.Clear(ClearFlags.Target|ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
Matrix sp_matrix=new Matrix();
Matrix rot_matrix=new Matrix();
Matrix trans_matrix=new Matrix();
Matrix transp_matrix=new Matrix();
///////////////////////
//static float rot_triangle=0;
//static float rot_square=0;
//rot_triangle+=0.004f;
//rot_square+=0.007f;
////////////////////////
device.BeginScene();
device.SetStreamSource(0,VB,0);
device.VertexFormat=CustomVertex.PositionColored.Format;
rot_matrix.RotateY( Environment.TickCount / 500.0f);
trans_matrix.Translate(-1.5f,0f,1f);
rot_matrix.Multiply(trans_matrix);
device.Transform.World=rot_matrix;
device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 4);
//device.Transform.World=Matrix.RotationAxis(new Vector3(0,1,0), Environment.TickCount / 3000.0f);
//device.Transform.World=Matrix.RotationX(0.04f);
sp_matrix.RotateX( 300.0f);//Environment.TickCount / 300.0f);
transp_matrix.Translate(1.5f,0f,1f);
sp_matrix.Multiply(transp_matrix);
device.Transform.World=sp_matrix;
device.DrawPrimitives(PrimitiveType.TriangleList, 6,16);
device.Transform.View = Matrix.LookAtLH( new Vector3( 0, 0,-9f ), new Vector3( 0, 0, 0 ), new Vector3( 0.0f, 1.0f, 0.0f ) );
device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4.0f, 1.0f, 1.0f, 100.0f );
device.EndScene();
device.Present();
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "my 3D vertex";
}
#endregion
public bool InitializeGraphics()
{
try
{
presentpara.Windowed=true;
presentpara.SwapEffect=SwapEffect.Discard;
presentpara.EnableAutoDepthStencil=true;
presentpara.AutoDepthStencilFormat=DepthFormat.D16;
device=new Device(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,presentpara);
device.DeviceReset+=new System.EventHandler(this.OnResetDevice);
this.OnCreateDevice(device,null);
this.OnResetDevice(device,null);
pause=false;
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.screne(); // Render on painting
}
protected override void OnResize(System.EventArgs e)
{
pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
}
static void Main()
{
using (Form1 frm = new Form1())
{
if (!frm.InitializeGraphics()) // Initialize Direct3D
{
MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.");
return;
}
MessageBox.Show((Environment.TickCount/30000f).ToString());
frm.Show();
// While the form is still valid, render and process messages
while(frm.Created)
{
frm.screne();
Application.DoEvents();
}
}
//Application.Run(new Form1());
}
}
}
C++ directx8
#include <windows.h>
#include <D3DX8.h>
HWND hWnd;
HDC hDC;
HINSTANCE hInstance; //得到程序的例子
LPDIRECT3D8 P_D3D;
LPDIRECT3DDEVICE8 p_D3DDevice;
RECT rect;
int sw = 640;
int sh = 480;
bool fullscreen = 1;
//声名一个结构,控制一个顶点的所有信息
struct my_vertex{
FLOAT x, y, z; //顶点的非变换位置
DWORD color; //顶点的颜色.
};
//一个用来定义顶点的宏'macro'
#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
//三角形和矩形的顶点坐标及颜色值
my_vertex g_vertices[] ={
{ 0.0f, 1.0f, 0.0f, 0x00FF0000 },
{ 1.0f, -1.0f,-1.0f, 0x0000FF00 },
{ -1.0f, -1.0f,-1.0f, 0x000000FF },
{ -1.0f, -1.0f, 1.0f, 0x0000FF00 },
{ 1.0f, -1.0f, 1.0f, 0x000000FF },
{ 1.0f, -1.0f,-1.0f, 0x0000FF00 },
{ -1.0f, -1.0f, -1.0f, 0x000000FF },
{ -1.0f, 1.0f, -1.0f, 0x000000FF },
{ 1.0f, 1.0f, -1.0f, 0x000000FF },
{ 1.0f, 1.0f, -1.0f, 0x000000FF },
{ 1.0f, -1.0f, -1.0f, 0x000000FF },
{ -1.0f, -1.0f, -1.0f, 0x000000FF },
{ 1.0f, -1.0f, 1.0f, 0x0000FF00 },
{ 1.0f, 1.0f, 1.0f, 0x0000FF00 },
{ -1.0f, 1.0f, 1.0f, 0x0000FF00 },
{ -1.0f, 1.0f, 1.0f, 0x0000FF00 },
{ -1.0f, -1.0f, 1.0f, 0x0000FF00 },
{ 1.0f, -1.0f, 1.0f, 0x0000FF00 },
{ -1.0f, 1.0f, -1.0f, 0x00FF0000 },
{ -1.0f, 1.0f, 1.0f, 0x00FF0000 },
{ 1.0f, 1.0f, 1.0f, 0x00FF0000 },
{ 1.0f, 1.0f, 1.0f, 0x00FF0000 },
{ 1.0f, 1.0f, -1.0f, 0x00FF0000 },
{ -1.0f, 1.0f, -1.0f, 0x00FF0000 },
{ 1.0f, -1.0f, -1.0f, 0x00FFFF00 },
{ 1.0f, -1.0f, 1.0f, 0x00FFFF00 },
{ -1.0f, -1.0f, 1.0f, 0x00FFFF00 },
{ -1.0f, -1.0f, 1.0f, 0x00FFFF00 },
{ -1.0f, -1.0f, -1.0f, 0x00FFFF00 },
{ 1.0f, -1.0f, -1.0f, 0x00FFFF00 },
{ -1.0f, -1.0f, 1.0f, 0x00FF00FF },
{ -1.0f, 1.0f, 1.0f, 0x00FF00FF },
{ -1.0f, 1.0f, -1.0f, 0x00FF00FF },
{ -1.0f, 1.0f, -1.0f, 0x00FF00FF },
{ -1.0f, -1.0f, -1.0f, 0x00FF00FF },
{ -1.0f, -1.0f, 1.0f, 0x00FF00FF },
{ 1.0f, -1.0f, -1.0f, 0x0000FFFF },
{ 1.0f, 1.0f, -1.0f, 0x0000FFFF },
{ 1.0f, 1.0f, 1.0f, 0x0000FFFF },
{ 1.0f, 1.0f, 1.0f, 0x0000FFFF },
{ 1.0f, -1.0f, 1.0f, 0x0000FFFF },
{ 1.0f, -1.0f, -1.0f, 0x0000FFFF },
};
#define NUM_VERTICES (sizeof(g_vertices)/sizeof(my_vertex))
//顶点缓冲区是用优化方法渲染存储的顶点的一种方法
IDirect3DVertexBuffer8 *g_vb=NULL;
// 程序链接时加入d3d8.lib
#pragma comment(lib,"d3d8.lib")
#pragma comment(lib,"d3dx8.lib")
void D3DInit()
{
HRESULT hr;
unsigned char *vb_vertices;
D3DXMATRIX view_matrix;
D3DXMATRIX matProj;
//由于要提供顶点颜色,所以要关闭D3D光源
p_D3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
// 开启z缓冲区。
p_D3DDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
//顶点缓冲区是顶点的优化的存储方法。这里建立一个可足以容纳7个顶点
//的顶点缓冲区。并允许Direct3D确定其在内存中的存放位置
hr=p_D3DDevice->CreateVertexBuffer(NUM_VERTICES*sizeof(my_vertex),
D3DUSAGE_WRITEONLY,
D3D8T_CUSTOMVERTEX,
D3DPOOL_MANAGED,
&g_vb);
//锁定Lock顶点缓冲区,使Direct3D知道要进行数据修改。完成数据拷贝后由解锁命令Unlock告诉Direct3D修改任务已完成
hr=g_vb->Lock(0, //偏移量,从缓冲区起始位置开始
0, //锁定的尺寸,0代表锁定整个缓冲区
&vb_vertices, //如果成功,将指向顶点缓冲区中的数据
0); //标志,未定义
//vb_vertices指向了顶点缓冲区里的顶点,为了填充顶点缓冲区,
//将数据拷贝到vb_vertices
memcpy(vb_vertices, g_vertices, sizeof(g_vertices) );
//解锁使Direct3D知道操作已完成
g_vb->Unlock();
//建立可视矩阵
D3DXMatrixLookAtLH(&view_matrix,&D3DXVECTOR3( 0.0f, 0.0f,-8.0f ),
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ));
//设定相机'camera'
p_D3DDevice->SetTransform(D3DTS_VIEW,&view_matrix);
D3DXMatrixPerspectiveFovLH(&matProj, //结果矩阵
D3DX_PI/4,//可视弧度范围
((float)sw / (float)sh), //屏幕高宽比
1.0f, //接近观测面
100.0f ); //远离观测面
//透视矩阵不做改变
p_D3DDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
D3DFORMAT find_16bit_mode(void)
{
HRESULT hr;
//首先测试(16位色模式)R5G6B5
hr=P_D3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_R5G6B5,D3DFMT_R5G6B5,FALSE);
if(SUCCEEDED(hr)){
OutputDebugString("D3DFMT_R5G6B5/n");
return D3DFMT_R5G6B5;
}
//接下来测试X1R5G5B5
hr=P_D3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X1R5G5B5,D3DFMT_X1R5G5B5,FALSE);
if(SUCCEEDED(hr)){
OutputDebugString("D3DFMT_X1R5G5B5/n");
return D3DFMT_X1R5G5B5;
}
return (D3DFORMAT)NULL;
}
void D3DEnable()
{
HRESULT hr;
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE display_mode;
//创建Drect3D8,这是在任何D3D8程序里必须做的事情
//返回D3D版本值(D3D_SDK_VERSION)
P_D3D = Direct3DCreate8( D3D_SDK_VERSION );
//仅运行在窗口模式,功能是得到当前(桌面)显示模式
hr=P_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&display_mode);
//将D3DPRESENT_PARAMETERS结构清零
ZeroMemory(&d3dpp,sizeof(d3dpp));
//在全屏和窗口方式下均相同
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //由于不再有用,所以要释放以前的帧。
d3dpp.hDeviceWindow = hWnd; //这是主窗口(唯一的)
d3dpp.BackBufferCount= 1; //仅需要一个备分缓冲区
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
////在全屏模式下要设置后备缓冲区的宽/高.
if(fullscreen){
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = sw;
d3dpp.BackBufferHeight = sh;
d3dpp.BackBufferFormat = find_16bit_mode();
}
else
{
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = display_mode.Format;
}
hr=P_D3D->CreateDevice(D3DADAPTER_DEFAULT, //在多显示器系统中默认的接口
//可以超过一个。尽可能地选择硬件加速而不是软件渲染
D3DDEVTYPE_HAL,
//当前的窗口
hWnd,
//用软件处理矢量,要比硬件方式慢,但兼容所有图形卡
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
// D3DPRESENT_PARAMETERS结构
&d3dpp,
//指向新的设备
&p_D3DDevice);
}
void D3DDisable()
{
if(p_D3DDevice){
p_D3DDevice->Release();
p_D3DDevice=NULL;
}
if(P_D3D){
P_D3D->Release();
P_D3D=NULL;
}
if(g_vb){
g_vb->Release();
g_vb=NULL;
}
}
void D3DSceneShow()
{
D3DXMATRIX matWorld;
D3DXMATRIX rot_matrix;
D3DXMATRIX trans_matrix;
static float rot_triangle=0;
static float rot_square=0;
rot_triangle+=0.004f;
rot_square+=0.007f;
//清除缓冲区并置为新的颜色
p_D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER , D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
//向设备提示开始进行渲染
if(SUCCEEDED(p_D3DDevice->BeginScene())){
//使用普通的顶点格式
p_D3DDevice->SetVertexShader(D3D8T_CUSTOMVERTEX);
//告诉D3D为三角形建立的顶点缓冲区是渲染功能要读取的数据流
p_D3DDevice->SetStreamSource(0,g_vb,sizeof(my_vertex));
//为三角形设置旋转矩阵。
D3DXMatrixRotationY(&rot_matrix,rot_triangle);
//向左移动1.5个单元,向屏幕内移动1个单元
D3DXMatrixTranslation(&trans_matrix,-1.5,0.0f,1.0f);
//合并2矩阵得到最终的世界矩阵
D3DXMatrixMultiply(&matWorld,&rot_matrix,&trans_matrix);
//设置世界矩阵
p_D3DDevice->SetTransform(D3DTS_WORLD,&matWorld );
//绘制三角形
p_D3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,4);
//为正方形设置旋转矩阵。
D3DXMatrixRotationX(&rot_matrix,rot_square);
//为正方形设置世界矩阵。
D3DXMatrixTranslation(&trans_matrix,1.5,0.0f,1.0f);
//合并2矩阵得到最终的世界矩阵
D3DXMatrixMultiply(&matWorld,&rot_matrix,&trans_matrix);
p_D3DDevice->SetTransform( D3DTS_WORLD, &matWorld );
//跳过开始6个顶点(属于金字塔的),绘制剩下的12个三角形。
p_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,6,12);
//向设备提示渲染完成
p_D3DDevice->EndScene();
}
//显示结果
p_D3DDevice->Present( NULL, NULL, NULL, NULL );
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
case WM_CREATE:
GetWindowRect(hWnd, &rect);
sw = rect.right - rect.left;
sh = rect.bottom - rect.top;
// SceneResizeViewport(sw, sh);
return 0;
case WM_SIZE:
return 0;
case WM_CLOSE:
ShowWindow (hWnd, SW_HIDE);
PostQuitMessage( 0 );
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch( wParam )
{
case VK_ESCAPE:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
return 0;
default:
return DefWindowProc( hWnd,message, wParam, lParam );
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
MSG msg;
bool bQuit = false;
if (MessageBox(NULL,"是否选择全屏显示模式?", "全屏方式运行?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=0; //窗口模式
}
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = "Name";
RegisterClass( &wc );
if(fullscreen)
{
hWnd = CreateWindow(
"Name",
"Lesson1",
WS_POPUP | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0,
GetSystemMetrics( SM_CXSCREEN ),
GetSystemMetrics( SM_CYSCREEN ),
NULL, NULL,
hInstance,
NULL );
}else
{
hWnd = CreateWindow(
"Name",
"Lesson1",
WS_TILEDWINDOW | WS_VISIBLE,
GetSystemMetrics( SM_CXSCREEN )/2-sw/2,
GetSystemMetrics( SM_CYSCREEN )/2-sh/2,
sw,
sh,
NULL, NULL,
hInstance,
NULL );
ChangeDisplaySettings(NULL, 0);
}
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
//初始化
D3DEnable();
D3DInit();
if(!fullscreen)
{
GetWindowRect(hWnd, &rect);
sw = rect.right - rect.left;
sh = rect.bottom - rect.top;
}
while ( !bQuit )
{
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
if ( msg.message == WM_QUIT )
bQuit = true;
else
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
D3DSceneShow();
}
}
//关闭,退出程序
D3DDisable();
ShowWindow (hWnd, SW_HIDE);
DestroyWindow( hWnd );
ChangeDisplaySettings(NULL, 0);
return msg.wParam;
return 0;
}