2015-12-22 15 views
0

我正在嘗試做一個OBJ loder程序,但無論我多麼努力地嘗試,它只是無法正常工作。有幾個面孔缺失。我打開三角形面,並在Blender中保持頂點順序,但我無法使其工作OpenGL吸取了很多丟失的面孔

enter image description here

這裏是我的OBJLoader代碼:

#include "OBJLoader.h" 

void OBJLoader::Load(string file){ 
int lines = 0; 
int indexsize = 0; 
int offset = 0; 

vector<string> datalines; 
vector<vec3> position; 
vector<vec3> normal; 
vector<vec2> texturecoord; 

bool progress[] = { false, false, false, false}; 

bool succes = IOManager::readFiletoLines(file, datalines, lines); 
if (!succes){ 
    char* tmp = ""; 
    cin >> tmp; 
    exit(-1); 
} 
int facecount = 0; 
string::size_type sz; 
for (int i = 0; i < lines-1; i++){ 
    string line = datalines[i]; 
    if (line.at(0) == 'v'){ 
     if (line.at(1) == ' '){ 
      if (!progress[0]){ 
       cout << "Loading positions..." << endl; 
       progress[0] = true; 
      } 
      offset = 2; 
      float x = stof(line.substr(offset), &sz); 
      offset += sz+1; 
      float y = stof(line.substr(offset), &sz); 
      offset += sz+1; 
      float z = stof(line.substr(offset), &sz); 
      position.push_back(vec3(x, y, z)); 
      continue; 
     } 
     else if (line.at(1) == 't'){ 
      if (!progress[1]){ 
       cout << "Loading texture coordinates..." << endl; 
       progress[1] = true; 
      } 
      offset = 3; 
      float u = stof(line.substr(offset), &sz); 
      offset += sz+1; 
      float v = stof(line.substr(offset), &sz); 
      texturecoord.push_back(vec2(u, v)); 
      continue; 
     }else if (line.at(1) == 'n'){ 
      if (!progress[2]){ 
       cout << "Loading normals..." << endl; 
       progress[2] = true; 
      } 
      offset = 3; 
      float x = stof(line.substr(offset), &sz); 
      offset += sz+1; 
      float y = stof(line.substr(offset), &sz); 
      offset += sz+1; 
      float z = stof(line.substr(offset), &sz); 
      normal.push_back(vec3(x, y, z)); 
      cout << "a"; 
      continue; 
     } 
    }else if (line.at(0) == 'f'){ 
     if (!progress[3]){ 
      cout << "Loading faces..." << endl; 
      data.resize(position.size()); 
      progress[3] = true; 
     } 
     facecount++; 
     cout << facecount << endl; 
     Vertex vertex; 
     Vertex vertex2; 
     Vertex vertex3; 
     //First corner 
     offset = 2; 
     int pos1=stoi(line.substr(offset), &sz); 
     offset += sz+1; 
     int tex1=stoi(line.substr(offset), &sz); 
     offset += sz+1; 
     int norm1=stoi(line.substr(offset), &sz); 

     //Second corner 
     offset += sz+1; 
     int pos2 = stoi(line.substr(offset), &sz); 
     offset += sz + 1; 
     int tex2 = stoi(line.substr(offset), &sz); 
     offset += sz + 1; 
     int norm2 = stoi(line.substr(offset), &sz); 

     //Third corner 
     offset += sz+1; 
     int pos3 = stoi(line.substr(offset), &sz); 
     offset += sz + 1; 
     int tex3 = stoi(line.substr(offset), &sz); 
     offset += sz + 1; 
     int norm3 = stoi(line.substr(offset), &sz); 

     //put the values into the data vector 

     vertex.postion(position[pos1 - 1].x, position[pos1 - 1].y, position[pos1 - 1].z); 
     vertex.textureUV(texturecoord[tex1 - 1].x, texturecoord[tex1 - 1].y); 
     vertex.normal(normal[norm1 - 1].x, normal[norm1 - 1].y, normal[norm1 - 1].z); 
     vertex.colorRGBA(1, 1, 1, 1); 

     data[pos1 - 1] = vertex; 

     vertex2.postion(position[pos2 - 1].x, position[pos2 - 1].y, position[pos2 - 1].z); 
     vertex2.textureUV(texturecoord[tex2 - 1].x, texturecoord[tex2 - 1].y); 
     vertex2.normal(normal[norm2 - 1].x, normal[norm2 - 1].y, normal[norm2 - 1].z); 
     vertex2.colorRGBA(1, 1, 1, 1); 

     data[pos2 - 1] = vertex2; 

     vertex3.postion(position[pos3 - 1].x, position[pos3 - 1].y, position[pos3 - 1].z); 
     vertex3.textureUV(texturecoord[tex3 - 1].x, texturecoord[tex3 - 1].y); 
     vertex3.normal(normal[norm3 - 1].x, normal[norm3 - 1].y, normal[norm3 - 1].z); 
     vertex3.colorRGBA(1, 1, 1, 1); 

     data[pos3 - 1] = vertex3; 
     indexdata.push_back(pos1-1); 
     indexdata.push_back(pos2-1); 
     indexdata.push_back(pos3-1); 
    } 
    } 
} 

,這是我的渲染功能。

void Loader::Model(char* model,int texture){ 
    if (!models[0]){ 
     openGL.Load(model); 
     data = openGL.data; 
     indexdata = openGL.indexdata; 
     models[0] = true; 
    } 
initeverything(); 
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); 

glActiveTexture(GL_TEXTURE0); 
glBindTexture(GL_TEXTURE_2D, texture); 

glBufferData(GL_ARRAY_BUFFER, data.size()*sizeof(Vertex), &data.front() , GL_STATIC_DRAW); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, data.size()*sizeof(int), &indexdata.front(), GL_STATIC_DRAW); 

cout << data[0].pos.x << endl; 

glEnableVertexArrayAttrib(vao, 0); 
glEnableVertexArrayAttrib(vao, 1); 
glEnableVertexArrayAttrib(vao, 2); 

glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, pos)); 
glVertexAttribPointer(1, 4, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, color)); 
glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, vertUV)); 

glDrawElementsBaseVertex(GL_TRIANGLES, openGL.indexdata.size()*sizeof(int), GL_UNSIGNED_INT, 0, 0); 

glBindBuffer(GL_ARRAY_BUFFER, 0); 
glBindVertexArray(0); 
} 

這是結果:enter image description here

而且它也發生在一個簡單的立方體。

enter image description here

是。那應該是一個立方體。

+0

一些更好的描述你的問題會有所幫助。 – Downvoter

回答

3

如果我不得不猜測,我會說「保持頂點順序」標誌可能是問題。 OpenGL通常剔除不是CounterClockWise的面,並基於被剔除的三角形(出現在表面上作爲受影響對象的其他面),可能這些三角形的頂點是以ClockWise順序繪製。

你可以,爲了簡單起見,乾脆告訴OpenGL不宰殺的面孔:

glDisable(GL_CULL_FACE); 

但是一個更好的解決辦法可能是,以確保纏繞順序是對你對象的所有面一致。

+0

禁止剔除臉部。結果可以在圖片上看到。 – Lacko97