最後讓我的模型加載程序工作(基於上一個問題)。將所有值存儲好,運行glDrawElemts正常 - 但它不運行glDrawArrays。OpenGL模型加載程序問題
是的,你首先要說的是'glDrawArrays顯然沒有寫出正確的'。這就是問題 - 我99%確定它是。
代碼如下:
bool modelLoader(string fileName,vector<GLfloat>& vertices, vector<GLushort>& indices)
{
fstream objFile;
vector<GLfloat> localVertices;
string dataLine;
stringstream mystream;
GLfloat x, y, z;
GLushort ind1, ind2, ind3;
char c, d;
vertices.clear();
indices.clear();
for (int i = 0; i < 3; i++)
{
vertices.push_back(0);
}
for (int i = 0; i < 3; i++)
{
localVertices.push_back(0);
}
objFile.open(fileName);
if (!objFile.good())
{
printf("Error with loader");
return false;
}
while (!objFile.eof())
{
getline(objFile, dataLine);
if (dataLine == "")
{
mystream.clear();
continue;
}
mystream.clear();
mystream.str(dataLine);
c = dataLine[0];
d = dataLine[1];
mystream.ignore(2);
switch (c)
{
case 'v':
{
switch (d)
{
case 'n':
{ /* for normals */ break;}
case ' ':
mystream >> x >> y >> z;
localVertices.push_back(x);
localVertices.push_back(y);
localVertices.push_back(z);
printf("\nVertices: %f, %f, %f", x, y, z);
break;
default:
{break;}
}
break;
}
case 'f':
{
//printf("F entered");
mystream >> ind1 >> ind2 >> ind3;
vertices.push_back(localVertices[ind1* 3 + 0]);
vertices.push_back(localVertices[ind1* 3 + 1]);
vertices.push_back(localVertices[ind1* 3 + 2]);
vertices.push_back(localVertices[ind2* 3 + 0]);
vertices.push_back(localVertices[ind2* 3 + 1]);
vertices.push_back(localVertices[ind2* 3 + 2]);
vertices.push_back(localVertices[ind3* 3 + 0]);
vertices.push_back(localVertices[ind3* 3 + 1]);
vertices.push_back(localVertices[ind3* 3 + 2]);
indices.push_back(ind1);
indices.push_back(ind2);
indices.push_back(ind3);
printf("\nIndices: %d, %d, %d", ind1, ind2, ind3);
break;
}
case !'v' || !'f':
{
break;
}
default:
{break;}
}
mystream.clear();
}
objFile.close();
return true;
}
在這裏,我去打電話的主要功能如下:
vector<GLfloat> vertices;
vector<GLushort> indices;
if (!modelLoader("triangles.obj", vertices, indices))
{
cout << "FAILED TO RUN: MODEL LOADER";
return 1;
}
插入一堆其他說大話的有關設置模型視圖矩陣,運行一個循環來更新每個迭代...
int size = vertices.size()-3;
glDrawArrays(GL_TRIANGLES, 3, (GLsizei)size);
哦,並且triangles.obj文件是:
v -10.0 10.0 10.0
v -10.0 -10.0 10.0
v 10.0 -10.0 10.0
v 10.0 10.0 10.0
v 20.0 10.0 10.0
v 20.0 -10.0 10.0
f 1 2 3
f 4 5 6
根本沒有快樂。正如我所說的,使用DrawElements可以做到這一點,但是當我嘗試繪製任何其他.obj文件時會導致異常錯誤。
任何線索,我要去哪裏錯了?
我仍然自己學習OpenGL,但不應該使用* glDrawElements *或* glDrawArrays,而不是兩個? – Zutty
** ['while(!objFile.eof())'wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)* *。什麼學習資源告訴你這樣做? –
我一次只用一個,別擔心!我只是試圖讓兩個人(單獨)工作。 – DerryHolt