【问题标题】:OpenGL Model Loader IssueOpenGL 模型加载器问题
【发布时间】:2013-02-09 07:47:30
【问题描述】:

终于让我的模型加载器工作了(基于上一个问题)。可以正常存储所有值,可以正常运行 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 文件时会导致异常错误。

关于我哪里出错的任何线索?

【问题讨论】:

  • 我自己还在学习 OpenGL,但你不应该使用 either glDrawElements glDrawArrays,而不是两者都使用吗?
  • while (!objFile.eof()) is wrong。什么学习资源告诉你这样做的?
  • 我一次只用一个,别担心!我只是想让两者都工作(单独)。
  • @LightnessRacesinOrbit 我的导师.. 哈!你有什么建议?
  • 什么是“异常错误”?因为老实说,这是最重要的部分。

标签: c++ opengl


【解决方案1】:

你不打算画第一个三角形吗?绘制所有三角形的正确调用应该是glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size()); 在此之前,您还需要启用调用并通过glEnableClientState(GL_VERTEX_ARRAY)glVertexPointer() 设置数组。如果需要,其他属性(如颜色和法线)也是如此。顺便说一句,自 OpenGL 3.0 起,所有这些都已弃用,因此如果您刚刚开始,您可能想学习核心 3.0 API。

【讨论】:

  • 老实说,我不打算在本模块结束后继续为我的学位继续 openGL。明年我们将启动 DirectX,它更有吸引力!我正在使用启用客户端状态和指针,是的。
  • 只是想知道,但为什么 DirectX 对您更有吸引力?我知道没有重大的功能差异。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 2016-12-28
相关资源
最近更新 更多