Direct3D中的繪制(3)

立方體——只比三角形稍微復雜一點,這個程序渲染一個線框立方體。

這個簡單的繪制和渲染立方體的程序的運行結果如下圖所示:

o_cube_demo.jpg

源程序:

/**************************************************************************************
? Renders a spinning cube in wireframe mode.? Demonstrates vertex and index buffers,
? world and view transformations, render states and drawing commands.
**************************************************************************************/
#include "d3dUtility.h"
#pragma warning(disable : 4100)
const int WIDTH? = 640;
const int HEIGHT = 480;
IDirect3DDevice9*??????? g_d3d_device??? = NULL;
IDirect3DVertexBuffer9*??? g_vertex_buffer = NULL;
IDirect3DIndexBuffer9*??? g_index_buffer??? = NULL;
class cVertex
{
public:
float m_x, m_y, m_z;
??? cVertex() {}
??? cVertex(float x, float y, float z)
??? {
??????? m_x = x;
??????? m_y = y;
??????? m_z = z;
??? }
};
const DWORD VERTEX_FVF = D3DFVF_XYZ;

bool setup()
{???
??? g_d3d_device->CreateVertexBuffer(8 * sizeof(cVertex), D3DUSAGE_WRITEONLY, VERTEX_FVF,
???????????????????????????????????? D3DPOOL_MANAGED, &g_vertex_buffer, NULL);
??? g_d3d_device->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,
??????????????????????????????????? D3DPOOL_MANAGED, &g_index_buffer, NULL);
// fill the buffers with the cube data
??? cVertex* vertices;
??? g_vertex_buffer->Lock(0, 0, (void**)&vertices, 0);
// vertices of a unit cube
??? vertices[0] = cVertex(-1.0f, -1.0f, -1.0f);
??? vertices[1] = cVertex(-1.0f,? 1.0f, -1.0f);
??? vertices[2] = cVertex( 1.0f,? 1.0f, -1.0f);
??? vertices[3] = cVertex( 1.0f, -1.0f, -1.0f);
??? vertices[4] = cVertex(-1.0f, -1.0f,? 1.0f);
??? vertices[5] = cVertex(-1.0f,? 1.0f,? 1.0f);
??? vertices[6] = cVertex( 1.0f,? 1.0f,? 1.0f);
??? vertices[7] = cVertex( 1.0f, -1.0f,? 1.0f);
??? g_vertex_buffer->Unlock();
// define the triangles of the cube
??? WORD* indices = NULL;
??? g_index_buffer->Lock(0, 0, (void**)&indices, 0);
// front side
??? indices[0]? = 0; indices[1]? = 1; indices[2]? = 2;
??? indices[3]? = 0; indices[4]? = 2; indices[5]? = 3;
// back side
??? indices[6]? = 4; indices[7]? = 6; indices[8]? = 5;
??? indices[9]? = 4; indices[10] = 7; indices[11] = 6;
// left side
??? indices[12] = 4; indices[13] = 5; indices[14] = 1;
??? indices[15] = 4; indices[16] = 1; indices[17] = 0;
// right side
??? indices[18] = 3; indices[19] = 2; indices[20] = 6;
??? indices[21] = 3; indices[22] = 6; indices[23] = 7;
// top
??? indices[24] = 1; indices[25] = 5; indices[26] = 6;
??? indices[27] = 1; indices[28] = 6; indices[29] = 2;
// bottom
??? indices[30] = 4; indices[31] = 0; indices[32] = 3;
??? indices[33] = 4; indices[34] = 3; indices[35] = 7;
??? g_index_buffer->Unlock();
// position and aim the camera
??? D3DXVECTOR3 position(0.0f, 0.0f, -5.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);
// set the projection matrix
??? D3DXMATRIX proj;
??? D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)WIDTH/HEIGHT, 1.0f, 1000.0f);
??? g_d3d_device->SetTransform(D3DTS_PROJECTION, &proj);
// set wireframe mode render state
??? g_d3d_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
return true;
}
void cleanup()
{
??? safe_release<IDirect3DVertexBuffer9*>(g_vertex_buffer);
??? safe_release<IDirect3DIndexBuffer9*>(g_index_buffer);
}
bool display(float time_delta)
{
// spin the cube
??? D3DXMATRIX rx, ry;
// rotate 45 degree on x-axis
??? D3DXMatrixRotationX(&rx, 3.14f/4.0f);
// increment y-rotation angle each frame
static float y = 0.0f;
??? D3DXMatrixRotationY(&ry, y);
??? y += time_delta;
// reset angle to zero when angle reaches 2*PI
if(y >= 6.28f)
??????? y = 0.0f;
// combine x and y axis ratation transformations
??? D3DXMATRIX rxy = rx * ry;
??? g_d3d_device->SetTransform(D3DTS_WORLD, &rxy);
// draw the scene
??? g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
??? g_d3d_device->BeginScene();
??? g_d3d_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(cVertex));
??? g_d3d_device->SetIndices(g_index_buffer);
??? g_d3d_device->SetFVF(VERTEX_FVF);
// draw cube
??? g_d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
??? 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;
}

setup函數創建頂點和索引緩存,鎖定它們,把構成立方體的頂點寫入頂點緩存,以及把定義立方體的三角形的索引寫入索引緩存。然后把攝象機向后移動幾個單位以便我們能夠看見在世界坐標系中原點處被渲染的立方體。

display方法有兩個任務;它必須更新場景并且緊接著渲染它。既然想旋轉立方體,那么我們將對每一幀增加一個角度使立方體能在這一幀旋轉。對于這每一幀,立方體將被旋轉一個很小的角度,這樣我們看起來旋轉就會更平滑。接著我們使用IDirect3DDevice9::DrawIndexedPrimitive方法來繪制立方體。

最后,我們釋放使用過的所有內存。這意味著釋放頂點和索引緩存接口。

下載立方體演示程序

轉載于:https://www.cnblogs.com/flying_bat/archive/2008/03/20/1115379.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/380117.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/380117.shtml
英文地址,請注明出處:http://en.pswp.cn/news/380117.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

遠控免殺專題(19)-nps_payload免殺

免殺能力一覽表 幾點說明&#xff1a; 1、上表中標識 √ 說明相應殺毒軟件未檢測出病毒&#xff0c;也就是代表了Bypass。 2、為了更好的對比效果&#xff0c;大部分測試payload均使用msf的windows/meterperter/reverse_tcp模塊生成。 3、由于本機測試時只是安裝了360全家桶…

VS2005中使用WebDeploymentProject的問題

近來做Web項目&#xff0c;VS2005中發布網站時默認發布大批的程序集&#xff0c;這給升級網站時造成很大麻煩&#xff0c;所以偶從MS下載了個WebDeploymentProject的插件&#xff08;下載地址http://download.microsoft.com/download/c/c/b/ccb4877f-55f7-4478-8f16-e41886607a…

操作系統中的多級隊列調度

多級隊列調度 (Multilevel queue scheduling) Every algorithm supports a different class of process but in a generalized system, some process wants to be scheduled using a priority algorithm. While some process wants to remain in the system (interactive proce…

編寫一程序,輸入一個字符串,查找該字符串中是否包含“abc”。

import java.lang.String.*;//這里調用java.long.String.contains()方法&#xff1b; import java.util.Scanner; public class shit {public static void main(String[] args) {Scanner wsq new Scanner(System.in);String str wsq.next();boolean status str.contains(&qu…

顯示消息提示對話框(WebForm)

1: /// <summary>2: /// 顯示消息提示對話框。3: /// Copyright (C) Maticsoft4: /// </summary>5: public class MessageBox6: { 7: private MessageBox()8: { 9: }10: 11: …

借助格式化輸出過canary保護

0x01 canary保護機制 棧溢出保護是一種緩沖區溢出攻擊緩解手段&#xff0c;當函數存在緩沖區溢出攻擊漏洞時&#xff0c;攻擊者可以覆蓋棧上的返回地址來讓shellcode能夠得到執行。當啟用棧保護后&#xff0c;函數開始執行的時候會先往棧里插入cookie信息&#xff0c;當函數真…

什么叫灰度圖

任何顏色都有紅、綠、藍三原色組成&#xff0c;假如原來某點的顏色為RGB(R&#xff0c;G&#xff0c;B)&#xff0c;那么&#xff0c;我們可以通過下面幾種方法&#xff0c;將其轉換為灰度&#xff1a; 1.浮點算法&#xff1a;GrayR*0.3G*0.59B*0.11 2.整數方法&#xff1a;Gra…

各抓包軟件的之間差異_系統軟件和應用程序軟件之間的差異

各抓包軟件的之間差異什么是軟件&#xff1f; (What is Software?) Software is referred to as a set of programs that are designed to perform a well-defined function. A program is a particular sequence of instructions written to solve a particular problem. 軟件…

輸入一字符串,統計其中有多少個單詞(單詞之間用空格分隔)(java)

import java.util.*; class Example3{public static void main(String args[]){Scanner sc new Scanner(System.in);String s sc.nextLine();//這里的sc.nextLine&#xff08;&#xff09;空格也會記數&#xff1b;StringTokenizer st new StringTokenizer(s," ")…

為何苦命干活的人成不了專家?

所謂熟能生巧&#xff0c;但離專家卻有一個巨大的鴻溝&#xff0c;在農田干活的農民怎么也成不了水稻專家&#xff0c;推廣之&#xff0c;那些在本職工作上勤勤懇懇的人&#xff0c;在業務上總有一個不可沖破的瓶頸。 這種現象非常普遍&#xff0c;這就是為什么很多人很勤奮&am…

今天發布一個新網站www.heijidi.com

新網站發布了&#xff0c;歡迎訪問&#xff0c;關于國產機的 網站 www.heijidi.com 轉載于:https://www.cnblogs.com/liugod/archive/2008/03/26/1122753.html

ret2shellcdoe

ret2shellcode的關鍵是找到一個緩沖區&#xff0c;這個緩沖區是可讀寫寫可執行的&#xff0c;我們要想辦法把我們的shellcdoe放到這個緩沖區&#xff0c;然后跳轉到我們的shellcode處執行。 例子&#xff1a; #include <stdio.h> #include <string.h> char str1[…

stl取出字符串中的字符_從C ++ STL中的字符串訪問字符元素

stl取出字符串中的字符字符串作為數據類型 (String as datatype) In C, we know string basically a character array terminated by \0. Thus to operate with the string we define character array. But in C, the standard library gives us the facility to use the strin…

Object類的hashCode()方法

public class day11 {public static void main(String[] args) {Object obj1 new Object();int hashCode obj1.hashCode();System.out.println(hashCode);}} hashCode public int hashCode()返回該對象的哈希碼值。支持此方法是為了提高哈希表&#xff08;例如 java.util.Ha…

調整Tomcat上的參數提高性能[轉]

Webtop Performance Test w/ Tomcat(調整Tomcat上的參數提高性能) Login several users with one second between each login. After the 25th user, the users begin to experience poor performance, to the point where some users are receiving “Page cannot be display…

RecordSet中的open完全的語法

RecordSet中的open完全的語法是:SecordSet.Open Source,ActiveConnection,CursorType,LockType,Options例如&#xff1a; rs.open sql,conn,1,3CursorTypeadOpenForwardOnly 0 默認游標類型, 為打開向前游標, 只能在記錄集中向前移動。adOpenKeyset 1 打開鍵集類型的游標, 可以…

用篩選法求100之內的素數

#include <stdio.h> int main() {int i ,j ,a[100];//定義一個數組存放1~100&#xff1b;for(i2; i<100; i)//由于1既不是素數也不是質素&#xff0c;所以不用考慮1&#xff0c;直接從2開始&#xff1b;{a[i]i;//以次賦值&#xff0c;2~100&#xff1b;for(j2; j<i…

遠控免殺專題(20)-GreatSCT免殺

轉載&#xff1a;https://mp.weixin.qq.com/s/s9DFRIgpvpE-_MneO0B_FQ 免殺能力一覽表 幾點說明&#xff1a; 1、上表中標識 √ 說明相應殺毒軟件未檢測出病毒&#xff0c;也就是代表了Bypass。 2、為了更好的對比效果&#xff0c;大部分測試payload均使用msf的windows/mete…

Java LinkedList對象的get(int index)方法與示例

LinkedList對象的get(int索引)方法 (LinkedList Object get(int index) method) This method is available in package java.util.LinkedList.get(int index). 軟件包java.util.LinkedList.get(int index)中提供了此方法。 This method is used to retrieve an object or eleme…

Sql養成一個好習慣是一筆財富

我們做軟件開發的&#xff0c;大部分人都離不開跟數據庫打交道&#xff0c;特別是erp開發的&#xff0c;跟數據庫打交道更是頻繁&#xff0c;存儲過程動不動就是上千行&#xff0c;如果數據量大&#xff0c;人員流動大&#xff0c;那么我么還能保證下一段時間系統還能流暢的運行…