2014-09-06 65 views
0

我打算加載模型並使用OpenGL進行顯示。代碼運行良好,沒有錯誤,但沒有顯示任何內容。有人可以幫我解決這個問題嗎?加載模型並使用OpenGL顯示它?

的源代碼:

所有我猜你是混合的東西了的
int cube; 
int number_faces; 
int number_vertices; 

struct coordinate { 
    float x, y, z; 
    coordinate(float a, float b, float c): x(a), y(b), z(c) {}; 
}; 

struct face { 
    int faces[3]; 
    face (int f1, int f2, int f3){ 
     faces[0] = f1; 
     faces[1] = f2; 
     faces[2] = f3; 
    }; 


}; 

// function prototype 
bool loadObject(const char * file_name); 

int main(int argc, char ** argv) 
{ 

    //initialize Glut 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowPosition(w_x_position, w_y_position); 
    glutInitWindowSize(w_width, w_height); 
    glutCreateWindow("Assignment 1"); 



    //function call 
    init(); 
    glutDisplayFunc(display); 


    //loop main 
    glutMainLoop(); 


    return 0; 
} 



bool loadObject(const char * file_name){ 

    //initialize stuff 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 


    //create the vectors that will hold the file coordinates 
    std::vector<std::string*> points; 
    std::vector<coordinate*> vertex; 
    std::vector<face*> faces; 

    //check if the file opens 

    if (!file_name) { 
     return false; 
    } 

    // open file 
    FILE *fp = fopen(file_name, "r"); 
    if (!fp) { 
     return false; 
    } 

    // num of vertices and indices 
    fscanf(fp, "data%d%d", &number_vertices, &number_faces); 



    //push the content into the vertexs 
    for(int i=0;i<number_vertices;i++) { 

     float x_temp, y_temp, z_temp; 
     fscanf(fp, "%f%f%f", &x_temp, &y_temp,&z_temp); 
     vertex.push_back(new coordinate(x_temp, y_temp, z_temp)); 
    } 

    for (int y=0; y<number_faces;y++){ 

     int a,b,c; 
     fscanf(fp, "%d%d%d", &a, &b, &c); 
     faces.push_back(new face(a, b, c)); 
    } 


    //draw the modle 

    int number; 
    number = glGenLists(1); 

    glNewList(number, GL_COMPILE); 

    for(int i =0; i <faces.size();i++){ 
     glBegin(GL_TRIANGLES); 

     glVertex3f(vertex[faces[i]->faces[0]-1]->x, vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z); 

     glVertex3f(vertex[faces[i]->faces[1]-1]->x, vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z); 

     glVertex3f(vertex[faces[i]->faces[2]-1]->x, vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z); 

     glEnd(); 


    } 
    glEndList(); 






    //delete the poiters for memory space 
    for(int i=0;i<points.size();i++){ 
     delete points[i]; 
    } 

    for(int i=0;i<faces.size();i++){ 
     delete faces[i]; 
    } 

    for(int i=0;i<vertex.size();i++){ 
     delete vertex[i]; 
    } 

    return number; 

} 
+0

請[提高您的問題](http://stackoverflow.com/posts/25699679/edit)。減少到給你一個_issue_問題的代碼部分! – 2014-09-06 11:20:14

+0

我的問題是爲什麼不顯示立方體。代碼中沒有錯誤? – toogz 2014-09-06 11:28:00

+0

調用'loadObject'在哪裏? 'glutDisplayFunc(display)'所需的函數'display'在哪裏?除此之外,顯示列表,即時模式等已經過時了,但我必須承認,現代OpenGL的入門級是很高的。 – Marius 2014-09-06 12:32:05

回答

0

第一。

的步驟應該是:

  1. 啓動PROGRAMM
  2. 創建所需要的OpenGL
  3. 一切導入模型。你只需要做一次。對於現代的OpenGL,您可以創建一個所謂的VertexBuffer來存儲頂點。
  4. 開始的GameLoop通常至少應該有三個部分
    • 從球員像鍵盤和鼠標
    • 更新對應你的遊戲邏輯的場景和用戶的輸入
    • 繪製一切解析輸入到屏幕
  5. 如果用戶決定如此,請關閉程序並清理所有內容。

你能提供的函數指針,以饜足這就要求你輸入,更新和REDER方法使用過剩,也有空閒方法和其他各種你可以提供,所以你並不需要使自己的循環。

第二個問題我可以在你的代碼旁看到不太好的結構,因爲你缺少設置Modelview Matrix。你只是設置了投影矩陣而沒有modelMatrix。但爲了更好的理解,讓我稍微解釋一下gameLoop的渲染部分。

渲染方法將繪製所有內容到屏幕/窗口。對於每個模型,你必須提供一個模型視圖矩陣,它將翻譯,旋轉和縮放網格/模型和一個投影矩陣,這將把3D世界帶到一個2D屏幕上(關於數學和更多細節還有更多的細節,但我們會在這裏保持簡短和簡單)。在較舊版本的OpenGL中,已經有一個堆棧可以在其上推送或彈出矩陣。堆棧頂部的矩陣將用於繪製網格。如果您希望另一個投影和/或modelmatrix用於第二個或第三個網格,則只需將堆棧頂部的矩陣更改即可。在加載網格時,您不需要提供任何其他信息。只需爲每個頂點定位,uv或顏色。 您繪製的圖形大致如此(僞代碼):

ClearEverythingFromLastCall(); glMatrixMode(GL_PROJECTION); SetProjectionMatrixOnStack(); (pushMatrix) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); ChangeMatrixForCurrentObject(); drawObject(); glutSwapBuffers(); //這個shpuld是你的最後一行方法

drawObject取決於你使用的是什麼技術(fixedFunctionPipeline從舊的OpenGL版本,或從OpenGL 3.0和更新的(我認爲是更好的)的可編程管道))。

所以在你的代碼中有很多錯誤,而不是破壞你代碼的「一個」小錯誤。我強烈建議先從一個好的教程開始。對於較老的OpenGL,我推薦使用http://nehe.gamedev.net/http://videotutorialsrock.com/(如果你能講德語,我也可以給你一些德語教程)。兩者都從最初的固定管道開始。

+0

非常感謝您的評論! – toogz 2014-09-06 15:25:33