这节须要把顶点布局写在文件中面,为了方便。由于一大串很抽象的坐标放在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();
}

上面是纯c++代码,仅仅是一个读取的列子。下节再更新怎样在模型初始化里面套用。

本文转载请注明: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;
}

这个事实上就是把内存中的那个m_model[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"放到了project文件夹以下和.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.......

DirectX11 学习笔记10 - 用文件存储顶点布局的更多相关文章

  1. CUBRID学习笔记 10 数据库文件的类型和含义

    demodb contains the database data; demodb_lgar000, 001 and so forth are log archives used for point ...

  2. Directx11学习笔记【二十二】 用高度图实现地形

    本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5827714.html 在前面我们曾经实现过简单的地形(Direct ...

  3. 《C++ Primer Plus》学习笔记10

    <C++ Primer Plus>学习笔记10 <<<<<<<<<<<<<<<<<&l ...

  4. SQL反模式学习笔记10 取整错误

    目标:使用小数取代整数 反模式:使用Float类型 根据IEEE754标识,float类型使用二进制格式编码实数数据. 缺点:(1)舍入的必要性: 并不是所有的十进制中描述的信息都能使用二进制存储,处 ...

  5. Windows phone 8 学习笔记(2) 数据文件操作

    原文:Windows phone 8 学习笔记(2) 数据文件操作 Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方 ...

  6. Directx11学习笔记【二】 将HelloWin封装成类

    我们把上一个教程的代码封装到一个类中来方便以后的使用. 首先新建一个空工程叫做MyHelloWin,添加一个main.cpp文件,然后新建一个类叫做MyWindow,将于窗体有关的操作封装到里面 My ...

  7. Directx11学习笔记【一】 最简单的windows程序HelloWin

    声明:本系列教程代码有部分来自dx11龙书及dx11游戏编程入门两本书,后面不再说明 首先,在vs2013中创建一个空的解决方案Dx11Demo,以后的工程都会放在这个解决方案下面.然后创建一个win ...

  8. thinkphp学习笔记10—看不懂的路由规则

    原文:thinkphp学习笔记10-看不懂的路由规则 路由这部分貌似在实际工作中没有怎么设计过,只是在用默认的设置,在手册里面看到部分,艰涩难懂. 1.路由定义 要使用路由功能需要支持PATH_INF ...

  9. golang学习笔记10 beego api 用jwt验证auth2 token 获取解码信息

    golang学习笔记10 beego api 用jwt验证auth2 token 获取解码信息 Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放 ...

随机推荐

  1. 【撸码caffe 一】syncedmen.hpp

    SyncedMemory类主要负责在主机(CPU)和设备(GPU)之间管理内存分配和数据同步工作,封装了CPU和GPU之间的数据交互操作. 补充一点GPU的相关知识: 对CUDA架构而言,主机端的内存 ...

  2. B1012 [JSOI2008]最大数maxnumber 分块||RMQ

    这个题有毒,卡最大值...我开1 << 30爆零让我以为我分块错了...gg,然后去写RMQ,但是这个题st表是真简单啊.后来刘胜与巨佬一眼看出来我最大值不够大...然后1LL<&l ...

  3. 院校-国外-美国:斯坦福大学( Stanford)

    ylbtech-院校-国外-美国:斯坦福大学( Stanford) 斯坦福大学(Stanford University),全名小利兰·斯坦福大学(Leland Stanford Junior Univ ...

  4. linux git保存用户名密码(避免每次push输用户名密码)

    Linux/Unix/Mac 系统 新建一个 ~/.netrc 文件, 将 git 服务器, 用户名以及密码记录在这个文件, 如下所示:   machine your-git-server   log ...

  5. springboot与mybatis集成

    1)添加依赖 <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId& ...

  6. 基于scrapy-redis组件的分布式爬虫

    scrapy-redis组件安装 分布式实现流程 scrapy-redis组件安装 - 下载scrapy-redis组件:pip install scrapy-redis - 更改redis配置文件: ...

  7. mysql在第一次查询的时候很慢,第二次查询就比较快的原因?

    1.默认 query_cache 是打开的 你使用 show global variables like '%query_cache%'; 确认一下有没有打开,如果打开了.第一次查询读数据文件,第二次 ...

  8. Javascript中数组重排序方法详解

    在数组中有两个可以用来直接排序的方法,分别是reverse()和sort().下面通过本文给大家详细介绍,对js 数组重排序相关知识感兴趣的朋友一起看看吧. 1.数组中已存在两个可直接用来重排序的方法 ...

  9. [Offer收割]编程练习赛42

    对局匹配 直接贪心 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #i ...

  10. javascript中的构造函数和原型及原型链

    纯属个人理解,有错误的地方希望大牛指出,以免误人子弟 1.构造函数: 构造函数的作用 : 初始化由new创建出来的对象    new 的作用: 创建对象(空对象) new 后面跟的是函数调用,使用ne ...