zoukankan      html  css  js  c++  java
  • DirectX11 学习笔记10

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


  • 相关阅读:
    Jser 设计模式系列之面向对象
    jQuery 2.0.3 源码分析 回溯魔法 end()和pushStack()
    jQuery 2.0.3 源码分析 数据缓存
    jQuery 2.0.3 源码分析 Deferred(最细的实现剖析,带图)
    jQuery 2.0.3 源码分析 Deferred概念
    jQuery 2.0.3 源码分析 回调对象
    试试看 ? 离奇古怪的javascript题目
    jQuery 2.0.3 源码分析Sizzle引擎
    设计模式之美:Memento(备忘录)
    设计模式之美:Mediator(中介者)
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7080863.html
Copyright © 2011-2022 走看看