这节需要把顶点布局写在文件里面,为了方便,因为一大串非常抽象的坐标放在CPP和程序混在一起非常的不方便。
下面全为c++知识,读取文件里面的特定格式的数据:
Vertex Count: 36 Data: -1.0 1.0 -1.0 0.0 0.0 0.0 0.0 -1.0 1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0 -1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0 -1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0 1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0 1.0 -1.0 -1.0 1.0 1.0 0.0 0.0 -1.0 1.0 1.0 -1.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0 1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0 1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0 1.0 -1.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0 -1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0 1.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.0 1.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.0 -1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0 -1.0 -1.0 1.0 1.0 1.0 0.0 0.0 1.0 -1.0 1.0 1.0 0.0 0.0 -1.0 0.0 0.0 -1.0 1.0 -1.0 1.0 0.0 -1.0 0.0 0.0 -1.0 -1.0 1.0 0.0 1.0 -1.0 0.0 0.0 -1.0 -1.0 1.0 0.0 1.0 -1.0 0.0 0.0 -1.0 1.0 -1.0 1.0 0.0 -1.0 0.0 0.0 -1.0 -1.0 -1.0 1.0 1.0 -1.0 0.0 0.0 -1.0 1.0 1.0 0.0 0.0 0.0 1.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 0.0 -1.0 1.0 -1.0 0.0 1.0 0.0 1.0 0.0 -1.0 1.0 -1.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 0.0 1.0 1.0 -1.0 1.0 1.0 0.0 1.0 0.0 -1.0 -1.0 -1.0 0.0 0.0 0.0 -1.0 0.0 1.0 -1.0 -1.0 1.0 0.0 0.0 -1.0 0.0 -1.0 -1.0 1.0 0.0 1.0 0.0 -1.0 0.0 -1.0 -1.0 1.0 0.0 1.0 0.0 -1.0 0.0 1.0 -1.0 -1.0 1.0 0.0 0.0 -1.0 0.0 1.0 -1.0 1.0 1.0 1.0 0.0 -1.0 0.0
#include <iostream> #include <fstream> using namespace std; int main() { ifstream in; in.open("cube.txt"); if(!in) { cout<<"打开失败"<<endl; }else { cout<<"打开成功"<<endl; } char ch; int count; float f[8]; while(ch=in.get(),ch!=':');//跳过"Vertex Count:" in>>count; cout<<count<<endl; while(ch=in.get(),ch!=':');//跳过"Data:" while(!in.eof()) { for(int i=0;i<8;i++) in>>f[i]; for(int i=0;i<8;i++) cout<<f[i]<<" "; cout<<endl; } in.close(); in.clear(); getchar(); }
本文转载请注明:Younfor的博客笔记,http://blog.csdn.net/cq361106306/article/details/40300205
如果套用呢。只需要在模型文件里改就行了;
#pragma once #include "XComm.h" #include <fstream> #include <iostream> class CubeModel { protected: struct SimpleVertex { XMFLOAT3 Pos; XMFLOAT3 Normal; }; struct ModelType { float x, y, z; float tu, tv; float nx, ny, nz; }; ModelType* m_model; //模型顶点坐标数据结构 public://顶点缓冲和顶点索引缓冲 ID3D11Buffer *m_vertexBuffer, *m_indexBuffer; int m_vertexCount, m_indexCount; public: CubeModel():m_vertexCount(0),m_indexCount(0){}; bool init(ID3D11Device*); void close(); void render(ID3D11DeviceContext*); bool loadModel(char * filename); };上面头文件中 多了一个struct ModelType 这个主要是获取数据然后存储到内存的格式。
然后多了一个loadModel()函数 这个里面就写上面的纯c++代码了
下面修改初始化模型 init() 函数里面的前面一部分
unsigned long* indices; SimpleVertex *vertices; D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc; D3D11_SUBRESOURCE_DATA vertexData, indexData; //导入模型数据 if(!loadModel("cube.txt")) { return false; } // 创建顶点临时缓冲. vertices = new SimpleVertex[m_vertexCount]; // 创建索引缓冲. indices = new unsigned long[m_indexCount]; for(int i=0; i<m_vertexCount; i++) { vertices[i].Pos = XMFLOAT3(m_model[i].x, m_model[i].y, m_model[i].z); vertices[i].Normal = XMFLOAT3(m_model[i].nx, m_model[i].ny, m_model[i].nz); indices[i] = i; }
下面这个代码是之前的赋值。
//首先,我们创建2个临时缓冲存放顶点和索引数据,以便后面使用。. // 设置顶点缓冲大小为3,一个三角形. m_vertexCount = 8; // 设置索引缓冲大小. m_indexCount = 36; //6面*2三角形*3个点 // 创建顶点临时缓冲. SimpleVertex vertices[] = { {XMFLOAT3(-1.0f, -1.0f, -1.0f),WHITE}, {XMFLOAT3(-1.0f, 1.0f, -1.0f),BLACK}, {XMFLOAT3(1.0f, 1.0f, -1.0f),RED}, {XMFLOAT3(1.0f, -1.0f, -1.0f),GREEN}, {XMFLOAT3(-1.0f, -1.0f, 1.0f),BLUE}, {XMFLOAT3(-1.0f, 1.0f, 1.0f),YELLOW}, {XMFLOAT3(1.0f, 1.0f, 1.0f),CYAN}, {XMFLOAT3(1.0f, -1.0f, 1.0f),MAGENTA}, }; //右移一段距离 for(int i=0;i<8;i++) vertices[i].Pos.x+=6.0f; // 创建索引缓冲. indices = new unsigned long[m_indexCount]; // 设置索引缓冲数据. 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;
明显要麻烦许多对不对。
然后初始化里面我调用了一个loadModel函数。 我把里面的参数"cube.txt"放到了工程目录下面和.fx效果文件平级的地方
bool CubeModel::loadModel(char * filename) { std::ifstream in; in.open(filename); if(!in) { std::cout<<"打开失败"<<std::endl; }else { std::cout<<"打开成功"<<std::endl; } char ch; int count; while(ch=in.get(),ch!=':');//跳过"Vertex Count:" in>>count; m_model = new ModelType[count]; m_vertexCount=count; m_indexCount=count; while(ch=in.get(),ch!=':');//跳过"Data:" for(int i=0;i<count;i++) { in>>m_model[i].x>>m_model[i].y>>m_model[i].z; in>>m_model[i].tu>>m_model[i].tv; in>>m_model[i].nx>>m_model[i].ny>>m_model[i].nz; } in.close(); in.clear(); return true; }这里就是之前文章开头写的一段c++代码核心。
然后再close写一下释放内存就行了。
void CubeModel::close() { //释放模型 if(m_model) { delete []m_model; m_model=0; } // 释放顶点缓冲. if(m_indexBuffer) { m_inde.......