資源簡介
實現(xiàn)讀取obj文件并創(chuàng)建顯示列表繪制圖像,鍵盤可控制旋轉(zhuǎn)、縮放圖像功能

代碼片段和文件信息
//?lesson12_suchunyin.cpp?:?此文件包含?“main“?函數(shù)。程序執(zhí)行將在此處開始并結(jié)束。
//
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
int?n?=?30;????????????//每一次轉(zhuǎn)動30°
GLdouble?view[2]?=?{?08?};??//設(shè)置旋轉(zhuǎn)觀察點初始位置?
int?i?=?0;?????????????//旋轉(zhuǎn)次數(shù)控制
GLdouble?r?=?view[1];??//觀察點旋轉(zhuǎn)半徑
float?t?=?0.5;?????????//拉近拉遠的距離
vector>vSets;??//存放頂點(xyz)坐標(biāo)
vector>fSets;????//存放面的三個頂點索引
vector>lSets;????//存放線的頂點索引
vector>cSets;??//存放顏色
vector?name;??//存放每一部分名稱
GLint?part?=?-1;???????//每一部分的編號
string?filePath?=?“D://計算機圖形學(xué)//Squirtle.obj“;
void?ObjLoader(string?filename)
{
ifstream?file(filename);
string?line;
while?(getline(file?line))
{
if?(line.substr(0?1)?==?“o“)
{
string?str;
istringstream?on(line.substr(2));
on?>>?str;
name.push_back(str);
part++;
}
else?if?(line.substr(0?1)?==?“c“)
{
vector?color;???//記錄面對應(yīng)的顏色
GLfloat?R?G?B?A;
istringstream?cl(line.substr(2));
cl?>>?R;?cl?>>?G;?cl?>>?B;?cl?>>?A;
color.push_back(R);
color.push_back(G);
color.push_back(B);
color.push_back(A);
cSets.push_back(color);
}
else?if?(line.substr(0?1)?==?“v“)
{
vector?Point;
GLfloat?x?y?z;
istringstream?s(line.substr(2));
s?>>?x;?s?>>?y;?s?>>?z;
Point.push_back(x);
Point.push_back(y);
Point.push_back(z);
vSets.push_back(Point);
}
else?if?(line.substr(0?1)?==?“f“)
{
vector?vIndexSets(4);
GLint?u;
GLint?v;
GLint?w;
istringstream?vtns(line.substr(2));
vtns?>>?u;?
vtns?>>?v;?
vtns?>>?w;
vIndexSets[0]?=?u?-?1;
vIndexSets[1]?=?v?-?1;
vIndexSets[2]?=?w?-?1;
vIndexSets[3]?=?part;
fSets.push_back(vIndexSets);
}
else?if?(line.substr(0?1)?==?“l(fā)“)
{
vector?lIndexSets;
GLint?s?t;
istringstream?vt(line.substr(2));
vt?>>?s;?vt?>>?t;
lIndexSets.push_back(s?-?1);
lIndexSets.push_back(t?-?1);
lSets.push_back(lIndexSets);
}
}
file.close();
}
GLint?Squirtle?=?1;
GLint?MatrixMode?=?2;
GLint?VIEW?=?3;
void?createList()?{
glNewList(Squirtle?GL_COMPILE);??//杰尼龜?shù)娘@示列表
glBegin(GL_TRIANGLES);????//開始繪制面
for?(int?i?=?0;?i? GLfloat?VN[3];
//三個頂點
GLfloat?SV1[3];
GLfloat?SV2[3];
GLfloat?SV3[3];
if?((fSets[i]).size()?!=?4)?{
cout?<“面頂點數(shù)組有誤!“?< }
else?{
//配置顏色:殼一個顏色,身體和尾巴一個顏色
glColor4f(cSets[fSets[i][3]][0]?cSets[fSets[i][3]][1]?cSets[fSets[i][3]][2]?cSets[fSets[i][3]][3]);
GLint?firstVertexIndex?=?(fSets[i])[0];//取出頂點索引
GLint?secondVertexIndex?=?(fSets[i])[1];
GLint?thirdVertexIndex?=?(fSets[i])[2];
SV1[0]?=?(vSets[firstVertexIndex])[0];//第一個頂點
SV1[1]?=?(vSets[firstVertexIndex])[1];
SV1[2]?=?(vSets[firstVertexIndex])[2];
SV2[0]?=?(vSets[secondVertexIndex])[0];?//第二個
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????156861??2020-05-11?00:02??ziyuan2\Squirtle.obj
?????文件????????7791??2020-06-17?11:27??ziyuan2\ziyuan2.cpp
?????目錄???????????0??2020-06-17?11:29??ziyuan2\
評論
共有 條評論