• 大小: 18.5MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-07-14
  • 语言: 其他
  • 标签: OpenGL  

资源简介

该压缩包为OpenGL的3D场景绘制源代码,需要进一步学习的同学可翻阅博主的博客进行对照学习,文中有对源代码以及实现过程的详细解释。

资源截图

代码片段和文件信息

#include “loadObj.h“
#include 
using namespace std;
ObjLoader::ObjLoader()
{}

ObjLoader::~ObjLoader()
{}

vector split(string s char delim) {
vector v;
stringstream stringstream1(s);
string tmp;
while (getline(stringstream1 tmp delim)) {
v.push_back(tmp);
}
return v;
}

//读取obj文件
void ObjLoader::loadObjFile(std::string filename)
{
if (filename.empty())
return;
std::ifstream fin;
fin.open(filename);
if (!fin)
{
printf(“文件有误!“);
return;
}
else
{
std::string off;
while (getline(fin off))//每次读取一行
{
//读取法线
if (off[0] == ‘v‘ && off[1] == ‘n‘)
{
istringstream sin(off);
std::string s;
float x y z;
//依次读取vn以及法线的x、y、z坐标
sin >> s >> x >> y >> z;
Point3 point(x y z);
normals.push_back(point);
}
//读取纹理坐标
else if (off[0] == ‘v‘ && off[1] == ‘t‘)
{
istringstream sin(off);
std::string s;
float tmp x y z;
//读取掉前面的vt
sin >> s;
int j = 0;//j用来判断obj文件的纹理坐标是否有z坐标
//依次读取纹理坐标的x、y、z坐标
while (sin >> tmp)
{
if (j == 0)
x = tmp;
else if (j == 1)
y = tmp;
else if (j == 2)
z = tmp;
j++;
}
//当j为2时,表示无z坐标,此时默认z坐标为0
if (j == 2)
{
Point3 point(x y 0.0);
textures.push_back(point);
}
//当j为3时,表示有z坐标
else if (j == 3)
{
Point3 point(x y z);
textures.push_back(point);
}
}
//读取顶点坐标
else if (off[0] == ‘v‘)
{
istringstream sin(off);
std::string s;
float x y z;
//依次读取v以及顶点坐标的x、y、z坐标
sin >> s >> x >> y >> z;
Point3 point(x y z);
vertexs.push_back(point);
}
//读取顶点、UV坐标、法线索引
else if (off[0] == ‘f‘)
{
istringstream sin(off);
std::string s;
sin >> s;//去掉‘f‘ 
//读取3次,每次读取一组顶点、UV坐标、法线索引
int groupNum=0;
while(sin>>s)
{
groupNum++;
//sin >> s;
int index;
//根据‘/‘拆分读取的一组顶点、UV坐标、法线索引
vector v = split(s ‘/‘);
int j = 0;
for (string tmp : v)
{
istringstream cin(tmp);
cin >> index;
if (j == 0)
vertexsList.push_back(index);
else if (j == 1)
texturesList.push_back(index);
else if (j == 2)
normalsList.push_back(index);
j++;
}
}
//当一个面有3组索引时,表示这是一个三角面,将其存在对应三角数组中
if (groupNum == 3)
{
for (int k = 0; k < groupNum; k++)
{
trivertexsList.push_back(vertexsList[k]);
tritexturesList.push_back(texturesList[k]);
trinormalsList.push_back(normalsList[k]);
}
}
//当一个面有3组索引时,表示这是一个四角面,将其存在对应四角数组中
else if (groupNum == 4)
{
for (int k = 0; k < groupNum; k++)
{
quadvertexsList.push_back(vertexsList[k]);
quadtexturesList.push_back(texturesList[k]);
quadnormalsList.push_back(normalsList[k]);
}
}
vertexsList.clear();
texturesList.clear();
n

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1524  2019-12-18 17:32  loadTexture.cpp

     文件        307  2019-12-17 20:15  loadTexture.h

     文件      13126  2020-01-08 20:42  main.cpp

     文件    5647872  2015-03-15 22:48  FreeImage所需文件\FreeImage.dll

     文件      67932  2015-03-15 22:48  FreeImage所需文件\FreeImage.lib

     文件         69  2020-01-08 22:31  FreeImage所需文件\FreeImage导入说明书.txt

     文件       2176  2016-08-19 01:50  include\Angel.h

     文件       1290  2013-04-03 12:41  include\CheckError.h

     文件      18165  2013-04-03 12:42  include\mat.h

     文件       2158  2016-08-19 10:50  include\mesh.h

     文件        785  2016-08-16 19:00  include\Mesh_Painter.h

     文件       1841  2016-10-17 15:45  include\TriMesh.h

     文件      10441  2018-11-02 11:18  include\vec.h

     文件     844529  2018-11-09 16:46  texture\ball.png

     文件     288221  2018-11-09 16:35  texture\ball_low.obj

     文件     726351  2008-11-30 15:51  texture\cartooncastle.jpg

     文件    1051853  2008-11-30 16:11  texture\cartooncastle.obj

     文件    1172841  2019-12-20 00:04  texture\dusk.jpg

     文件     187855  2017-09-01 10:13  texture\jianke.obj

     文件     155452  2019-12-19 19:09  texture\moon.jpeg

     文件   11575264  2016-11-21 15:26  texture\Mountains.jpg

     文件        462  2017-04-07 09:45  texture\Mountains.mtl

     文件    4755602  2017-04-07 09:45  texture\Mountains.obj

     文件    2488142  2019-12-19 21:00  texture\night.jpeg

     文件     284749  2019-12-19 20:52  texture\sky.obj

     文件      71161  2019-12-19 19:02  texture\sun.jpg

     文件      98201  2019-12-19 23:40  texture\sunny.jpeg

     文件      57726  2019-11-28 20:53  FreeImage.h

     文件       6653  2019-12-22 04:10  loadObj.cpp

     文件       1469  2019-12-22 04:10  loadObj.h

............此处省略6个文件信息

评论

共有 条评论