⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cuboid.cpp

📁 DirectX 8 教程 第六章 源码下载:可运行
💻 CPP
字号:
// Cuboid.cpp: implementation of the CCuboid class.
//
//////////////////////////////////////////////////////////////////////

#include "Cuboid.h"

//构造立方体对象
CCuboid::CCuboid(LPDIRECT3DDEVICE8 pD3DDevice)
{
	m_pD3DDevice = pD3DDevice;
	m_pVertexBuffer = NULL;
	m_pTexture = NULL;

	//Set a default size and position
	//设置立方体默认大小和位置
	m_rWidth = 10.0;
	m_rHeight = 10.0;
	m_rDepth = 10.0;
	m_rX = 0.0;
	m_rY = 0.0;
	m_rZ = 0.0;

	//Initialize Vertex Buffer
	//初始化顶点缓冲
    if(SUCCEEDED(CreateVertexBuffer()))
	{
		UpdateVertices();
	}
}

//析构函数
CCuboid::~CCuboid()
{
	//释放纹理
	SafeRelease(m_pTexture);
	//释放顶点缓冲
	SafeRelease(m_pVertexBuffer);
}

//渲染函数
bool CCuboid::Render()
{
	//设置数据流
	m_pD3DDevice->SetStreamSource(0,                    //设置Direct3D数据流的源  
								  m_pVertexBuffer,      //将被“绑定”到数据源上的顶点缓冲
								  sizeof(CUBIOD_CUSTOMVERTEX));//每一个顶点数据的大小
	//设置顶点光影模式
    m_pD3DDevice->SetVertexShader(CUBIOD_D3DFVF_CUSTOMVERTEX);

	//Set how the texture should be rendered.
	//设置渲染纹理
	if(m_pTexture != NULL)
	{
		//A texture has been set. We don't want to blend our texture with
		//the colours of our vertices, so use D3DTOP_SELECTARG1
		//设置用于渲染得纹理,取消色彩渲染
        m_pD3DDevice->SetTexture(0, m_pTexture);
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
    }
	else
	{
		//No texture has been set. So we will disable texture rendering.
		//没有设置渲染纹理,取消纹理渲染方式
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
	}

    m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);		//Top  渲染顶部
	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8);		//Sides  渲染侧面
	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2);	//Bottom  渲染底面

	return true;
}

//创建顶点缓冲
HRESULT CCuboid::CreateVertexBuffer()
{
    //Create the vertex buffer from our device.
	//从当前设备创建顶点缓冲
    if(FAILED(m_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUBIOD_CUSTOMVERTEX),   //缓冲大小
                                               0,                                  //定点缓冲用途
											   CUBIOD_D3DFVF_CUSTOMVERTEX,         //向量格式
                                               D3DPOOL_DEFAULT,                    //D3DPOOL_DEFAULT 标志告诉Direct3D在最恰当的内存位置中申请缓存的空间 
											   &m_pVertexBuffer)))                 //即将建立的顶点缓冲对象的地址。


    {
        return E_FAIL;
    }

    return S_OK;
}

//设置顶点对象
bool CCuboid::UpdateVertices()
{
	VOID* pVertices;

    //Store each point of the cube together with it's colour and texture coordinates
	//Make sure that the points of a polygon are specified in a clockwise direction,
	//this is because anti-clockwise faces will be culled
	//We will use a three triangle strips to render these polygons (Top, Sides, Bottom).
	//设置组成立方体的每一个三角形顶点
    CUBIOD_CUSTOMVERTEX cvVertices[] =
    {	
		//Top Face
		{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 1.0f,},		//Vertex 0 - Blue 
        {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 0.0f, 0.0f,},		//Vertex 1 - Red 
        {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 1.0f,},		//Vertex 2 - Red 
		{m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0.0f,},		//Vertex 3 - Green 

		//Face 1
		{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 0.0f, 1.0f,},		//Vertex 4 - Red 
        {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,},		//Vertex 5 - Blue 
        {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f,},		//Vertex 6 - Green 
		{m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0.0f,},		//Vertex 7 - Red 

		//Face 2
		{m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 1.0f,},		//Vertex 8 - Blue 
        {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 0.0f, 0.0f,},		//Vertex 9 - Green
		
		//Face 3
        {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f,},		//Vertex 10 - Green 
		{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0.0f,},		//Vertex 11 - Red 

		//Face 4
        {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 0.0f, 1.0f,},		//Vertex 12 - Red 
        {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,},		//Vertex 13 - Blue

		//Bottom Face
		{m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 0.0f, 1.0f,},		//Vertex 14 - Green 
        {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,},		//Vertex 15 - Blue 
        {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 1.0f,},		//Vertex 16 - Red 
		{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0.0f,},		//Vertex 17 - Green
    };

	//Get a pointer to the vertex buffer vertices and lock the vertex buffer
	//申请顶点缓冲并锁定顶点缓冲
    if(FAILED(m_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
    {
        return false;
    }

    //Copy our stored vertices values into the vertex buffer
	//复制顶点数据到定点缓冲
    memcpy(pVertices, cvVertices, sizeof(cvVertices));

    //Unlock the vertex buffer
	//解锁顶点缓冲
    m_pVertexBuffer->Unlock();

	return true;
}

//设置立方体大小
bool CCuboid::SetSize(float rWidth, float rHeight, float rDepth)
{
	m_rWidth = rWidth;
	m_rHeight = rHeight;
	m_rDepth = rDepth;
    //更新立方体顶点数据
	UpdateVertices();

	return true;
}

//设置立方体空间位置
bool CCuboid::SetPosition(float x, float y, float z)
{
	m_rX = x;
	m_rY = y;
	m_rZ = z;
    //更新立方体顶点数据
	UpdateVertices();

	return true;
}

//设置立方体纹理
bool CCuboid::SetTexture(const char *szTextureFilePath)//指向纹理的路径指针
{
	//设置立方体渲染纹理
	if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice, szTextureFilePath, &m_pTexture)))
	{
		//设置纹理失败返回false
		return false;
	}

	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -