OpenGL模型加载器问题
OpenGL Model Loader Issue
终于让我的模型加载器工作起来了(基于前面的问题)。存储所有值都很好,运行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;
}
插入一堆关于设置模型视图矩阵、运行循环以更新每次迭代的其他陈词滥调。。。
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文件时会导致异常错误。
关于我哪里出了问题,有什么线索吗?
你打算不画第一个三角形吗?绘制所有三角形的正确调用应该是glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size());
。在此之前,您还需要启用调用并按glEnableClientState(GL_VERTEX_ARRAY)
和glVertexPointer()
设置数组。如果需要,其他属性(如颜色和法线)也是如此。顺便说一句,自从OpenGL 3.0以后,所有这些都不推荐使用,所以如果你刚刚开始,你可能想学习核心3.0 API。
相关文章:
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- Qt Visual Studio 2015 加载项编译问题
- 使用 sv hup SIGHUP 信号重新加载配置时出现问题
- 从文件问题加载纹理 Android NDK 和 OpenGL
- 将文件从 iOS 应用程序加载到 C++ 对象中/<iostream>iOS 上的问题
- 加载带有stb_image的图像时出现问题
- 我如何加载多个脚本而没有变量问题
- 问题在具有依赖性时从其他路径上加载DLL的DLL
- 关于加载COM Tlb文件的一些问题
- 问题:Windows 10:QMYSQL驱动程序未加载
- 部署时QT SQLITE问题:未加载驱动程序
- 从中加载字符串时出现 sscanf 问题
- 协议缓冲区;将数据保存到磁盘并重新加载问题
- 断开命名管道(在加载表上)上的 SybaseIQ 问题
- 问题4:如何加载()和存储()
- 与OsX相比,Windows上的GUI加载问题
- MFC扩展dll资源加载问题
- DLL 无法加载:0x8007007E找不到模块。不确定导致此依赖项问题的原因
- 将简单网格加载到 OpenGL 中时遇到问题
- 我的glsl着色器加载代码有什么问题吗