最后让我的模型加载器工作(基于前一个问题)。将所有值存储得很好,运行glDrawElemts很好 - 但它不运行glDrawArrays。
是的,首先你要说的是'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;
}
插入一堆关于设置模型视图矩阵的其他malarky,运行循环来更新每次迭代......
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文件时会导致异常错误。
关于我哪里出错的任何线索?
答案 0 :(得分:0)
你打算不画第一个三角形吗?绘制所有三角形的正确调用应为glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size());
在此之前,您还需要启用调用并按glEnableClientState(GL_VERTEX_ARRAY)
和glVertexPointer()
设置数组。如果你想要它们,其他属性如颜色和法线也是如此。顺便说一句,从OpenGL 3.0开始,所有这些都被弃用了,所以如果你刚刚开始,你可能想要学习核心3.0 API。