绘制立方体

//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: d3dinit.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Demonstrates how to initialize Direct3D, how to use the book's framework
//       functions, and how to clear the screen to black.  Note that the Direct3D
//       initialization code is in the d3dUtility.h/.cpp files.
//
//////////////////////////////////////////////////////////////////////////////////////////////////

#include "d3dUtility.h"
#pragma  comment(lib, "d3d9.lib")
#pragma  comment(lib, "d3dx9.lib")
#pragma  comment(lib, "winmm.lib")
//
// Globals
//

IDirect3DDevice9* Device = 0;

//
// Framework Functions
//
const int Width=640;
const int Height=480;

IDirect3DVertexBuffer9* VB=0;
IDirect3DIndexBuffer9* IB=0;
//定义三角形的顶点结构体

struct Vertex
{
    Vertex(){}
    Vertex(float x,float y,float z)
    {
    _x=x;_y=y;_z=z;
    }
    float _x,_y,_z;
    static const DWORD FVF;
};

const DWORD Vertex::FVF=D3DFVF_XYZ;
bool Setup()
{
   Device->CreateVertexBuffer(
       8*sizeof(Vertex),
       D3DUSAGE_WRITEONLY,
        Vertex::FVF,
        D3DPOOL_MANAGED,
        &VB,
        0);
   Vertex*  vertices;
   VB->Lock(0,0,(void**)&vertices,0);
   vertices[0]=Vertex(-1.0f,-1.0f,-1.0f);
   vertices[1]=Vertex(-1.0f, 1.0f,-1.0f);
   vertices[2]=Vertex( 1.0f, 1.0f,-1.0f);
       vertices[3]=Vertex( 1.0f,-1.0f,-1.0f);
    vertices[4]=Vertex(-1.0f,-1.0f, 1.0f);
    vertices[5]=Vertex(-1.0f, 1.0f, 1.0f);
    vertices[6]=Vertex( 1.0f, 1.0f, 1.0f);
    vertices[7]=Vertex( 1.0f,-1.0f, 1.0f);
   VB->Unlock();

   Device->CreateIndexBuffer(
       36*sizeof(WORD),
       D3DUSAGE_WRITEONLY,
       D3DFMT_INDEX16,
       D3DPOOL_MANAGED,
       &IB,
       0);
       WORD* indices=0;
    IB->Lock(0,0,(void**)&indices,0);
        //前面
    indices[0]=0;
    indices[1]=1;
    indices[2]=2;

    indices[3]=0;
    indices[4]=2;
    indices[5]=3;
    //后面
    indices[6]=4;
    indices[7]=6;
    indices[8]=5;

    indices[9]=4;
    indices[10]=7;
    indices[11]=6;
        //左面
    indices[12]=4;
    indices[13]=5;
    indices[14]=1;

    indices[15]=4;
    indices[16]=1;
    indices[17]=0;

    //右面
    indices[18]=3;
    indices[19]=2;
    indices[20]=6;

    indices[21]=3;
    indices[22]=6;
    indices[23]=7;

        //上面
    indices[24]=1;
    indices[25]=5;
    indices[26]=6;

    indices[27]=1;
    indices[28]=6;
    indices[29]=2;

        //下面
    indices[30]=4;
    indices[31]=0;
    indices[32]=3;

    indices[33]=4;
    indices[34]=3;
    indices[35]=7;
    IB->Unlock();
        //摄像机的位置和方向
    D3DXVECTOR3 position(0.0f,0.0f,-5.0f);//摄像机默认位置为(0,0,0)
    D3DXVECTOR3 target(0.0f,0.0f,0.0f);//摄像机指向的点为坐标原点
    D3DXVECTOR3 up(0.0f,1.0f,0.0f);//摄像机的向上方向为这个方向
    D3DXMATRIX V;//定义一个矩阵
    D3DXMatrixLookAtLH(&V,&position,&target,&up);//观察坐标系转换矩阵
    Device->SetTransform(D3DTS_VIEW,&V);//观察坐标系转换

    D3DXMATRIX proj;//投影变换用的矩阵
    D3DXMatrixPerspectiveFovLH(&proj,
        D3DX_PI*0.5f,
        (float)Width/(float)Height,
        1.0f,
        1000.0f);

    Device->SetTransform(D3DTS_PROJECTION,&proj);
    Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
    return true;
}

void Cleanup()
{

}

bool Display(float timeDelta)
{
    if( Device ) // Only use Device methods if we have a valid device.
    {
        
    


        
        // Instruct the device to set each pixel on the back buffer black -
        // D3DCLEAR_TARGET: 0x00000000 (black) - and to set each pixel on
        // the depth buffer to a value of 1.0 - D3DCLEAR_ZBUFFER: 1.0f.
        //Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
        Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
        // Swap the back and front buffers.
        Device->BeginScene();
        Device->SetStreamSource(0,VB,0,sizeof(Vertex));
        Device->SetIndices(IB);
        Device->SetFVF(Vertex::FVF);
        Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12);
        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,
        640, 480, true, D3DDEVTYPE_HAL, &Device))
    {
        ::MessageBox(0, L"InitD3D() - FAILED", 0, 0);
        return 0;
    }

    if(!Setup())
    {
        ::MessageBox(0, L"Setup() - FAILED", 0, 0);
        return 0;
    }

    d3d::EnterMsgLoop( Display );

    Cleanup();

    Device->Release();

    return 0;
}

 

 

posted @ 2022-04-14 11:30  szmtjs10  阅读(181)  评论(0编辑  收藏  举报