这节须要把顶点布局写在文件中面,为了方便。由于一大串很抽象的坐标放在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. ubuntu16.04安装chrome谷歌浏览器

    按下 Ctrl + Alt + t 键盘组合键,启动终端. 输入以下命令: sudo wget http://www.linuxidc.com/files/repo/google-chrome.lis ...

  2. 【BZOJ 2351】 Matrix

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2351 [算法] 哈希 [代码] #include<bits/stdc++.h& ...

  3. 2017-3-12 leetcode 167 209 216

    ---恢复内容开始--- 对于每次开机avast喊出的“已经检测到危害”实在忍无可忍了(它只能检测到不能根除很气..)于是重装了系统,回到了win10感觉不赖. =================== ...

  4. Spring Boot:Exception parsing document: template="index", line 7 - column 3

    转自:https://blog.csdn.net/u010429286/article/details/75447561

  5. mvc:view-controller直接转发页面

    在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面, 不想通过xxx.jsp直接访问页面,可以通过springmvc.xml配置文件中的 ...

  6. 关于Html基础语法学习

    晚上做完初赛,好像有点颓,就来学了学html,毕竟博客里面会用到嘛. 首先贴出我所学习的教程 http://www.w3school.com.cn/html/index.asp 我觉得吧,可能以我的记 ...

  7. 大数字运算——2、BigDecimal

    package com.wh.BigInteger; import java.math.BigDecimal; import java.util.Arrays; /** * @author 王恒 * ...

  8. 控件——DataGridview

    控件:DataGridview    用来显示数据,      可以显示和编辑来自多种不同类型的数据源的表格数据. 一.两种显示数据的方式:手动,后台代码 主要通过后台代码:先建立三大类   然后绑定 ...

  9. 【MFC】模态、非模态对话框

    MFC 点击按钮,弹出另一个对话框 方法一:模态对话框 资源视图–Dialog–右键–添加资源–新建–对话框-,然后在已经生成的对话框中(解决资源视图中的dialog下的新生成的那个)右键–添加类.例 ...

  10. hdu 2485 Destroying the bus stations 最小费用最大流

    题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...