資源簡(jiǎn)介
該壓縮包為OpenGL的3D場(chǎng)景繪制源代碼,需要進(jìn)一步學(xué)習(xí)的同學(xué)可翻閱博主的博客進(jìn)行對(duì)照學(xué)習(xí),文中有對(duì)源代碼以及實(shí)現(xiàn)過(guò)程的詳細(xì)解釋。
代碼片段和文件信息
#include?“l(fā)oadObj.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坐標(biāo)
sin?>>?s?>>?x?>>?y?>>?z;
Point3?point(x?y?z);
normals.push_back(point);
}
//讀取紋理坐標(biāo)
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用來(lái)判斷obj文件的紋理坐標(biāo)是否有z坐標(biāo)
//依次讀取紋理坐標(biāo)的x、y、z坐標(biāo)
while?(sin?>>?tmp)
{
if?(j?==?0)
x?=?tmp;
else?if?(j?==?1)
y?=?tmp;
else?if?(j?==?2)
z?=?tmp;
j++;
}
//當(dāng)j為2時(shí),表示無(wú)z坐標(biāo),此時(shí)默認(rèn)z坐標(biāo)為0
if?(j?==?2)
{
Point3?point(x?y?0.0);
textures.push_back(point);
}
//當(dāng)j為3時(shí),表示有z坐標(biāo)
else?if?(j?==?3)
{
Point3?point(x?y?z);
textures.push_back(point);
}
}
//讀取頂點(diǎn)坐標(biāo)
else?if?(off[0]?==?‘v‘)
{
istringstream?sin(off);
std::string?s;
float?x?y?z;
//依次讀取v以及頂點(diǎn)坐標(biāo)的x、y、z坐標(biāo)
sin?>>?s?>>?x?>>?y?>>?z;
Point3?point(x?y?z);
vertexs.push_back(point);
}
//讀取頂點(diǎn)、UV坐標(biāo)、法線索引
else?if?(off[0]?==?‘f‘)
{
istringstream?sin(off);
std::string?s;
sin?>>?s;//去掉‘f‘?
//讀取3次,每次讀取一組頂點(diǎn)、UV坐標(biāo)、法線索引
int?groupNum=0;
while(sin>>s)
{
groupNum++;
//sin?>>?s;
int?index;
//根據(jù)‘/‘拆分讀取的一組頂點(diǎn)、UV坐標(biāo)、法線索引
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++;
}
}
//當(dāng)一個(gè)面有3組索引時(shí),表示這是一個(gè)三角面,將其存在對(duì)應(yīng)三角數(shù)組中
if?(groupNum?==?3)
{
for?(int?k?=?0;?k? {
trivertexsList.push_back(vertexsList[k]);
tritexturesList.push_back(texturesList[k]);
trinormalsList.push_back(normalsList[k]);
}
}
//當(dāng)一個(gè)面有3組索引時(shí),表示這是一個(gè)四角面,將其存在對(duì)應(yīng)四角數(shù)組中
else?if?(groupNum?==?4)
{
for?(int?k?=?0;?k? {
quadvertexsList.push_back(vertexsList[k]);
quadtexturesList.push_back(texturesList[k]);
quadnormalsList.push_back(normalsList[k]);
}
}
vertexsList.clear();
texturesList.clear();
n
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????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導(dǎo)入說(shuō)明書(shū).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個(gè)文件信息
評(píng)論
共有 條評(píng)論