2013-02-07 73 views
1

最後讓我的模型加載程序工作(基於上一個問題)。將所有值存儲好,運行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文件時會導致異常錯誤。

任何線索,我要去哪裏錯了?

+2

我仍然自己學習OpenGL,但不應該使用* glDrawElements *或* glDrawArrays,而不是兩個? – Zutty

+2

** ['while(!objFile.eof())'wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)* *。什麼學習資源告訴你這樣做? –

+0

我一次只用一個,別擔心!我只是試圖讓兩個人(單獨)工作。 – DerryHolt

回答

0

你打算不畫第一個三角形嗎?繪製所有三角形的適當調用應該是glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size());在此之前,您還需要啓用調用並將陣列向上設置glEnableClientState(GL_VERTEX_ARRAY)glVertexPointer()。如果你想要它們,其他屬性(如顏色和法線)也是如此。順便說一下,自從OpenGL 3.0開始,所有這些都被棄用了,所以如果你剛剛開始,你可能想學習核心3.0 API。

+0

誠實地說,我不打算在本課程結束時繼續使用OpenGL。我們明年啓動DirectX,這更吸引人!我正在使用啓用客戶端狀態和指針,是的。 – DerryHolt

+0

只是想知道,但爲什麼DirectX更吸引你?我知道沒有主要的功能差異。 – Joe