2017-06-01 116 views
2

我目前正在開發Oculus Rift PC SDK。試圖從像Tiny Room Demo(DX11)那樣簡單的事情開始。看到這個教程在線3D模型加載到從外部文件中的場景(Rastertek 教程7:3D模型渲染)Oculus Tiny Room,Directx,將3D模型加載到場景中

的小房間演示創建模型是硬編碼的座標,並使得它

TriangleSet walls; 
    walls.AddSolidColorBox(10.1f, 0.0f, 20.0f, 10.0f, 4.0f, -20.0f, 0xff808080); // Left Wall 
    walls.AddSolidColorBox(10.0f, -0.1f, 20.1f, -10.0f, 4.0f, 20.0f, 0xff808080); // Back Wall 
    walls.AddSolidColorBox(-10.0f, -0.1f, 20.0f, -10.1f, 4.0f, -20.0f, 0xff808080); // Right Wall 
    Add(
     new Model(&walls, XMFLOAT3(0, 0, 0), XMFLOAT4(0, 0, 0, 1), 
      new Material(
       new Texture(false, 256, 256, Texture::AUTO_WALL) 
      ) 
     ) 
    ); 

void AddSolidColorBox(float x1, float y1, float z1, float x2, float y2, float z2, uint32_t c) 
{ 
    AddQuad(Vertex(XMFLOAT3(x1, y2, z1), ModifyColor(c, XMFLOAT3(x1, y2, z1)), z1, x1), 
      Vertex(XMFLOAT3(x2, y2, z1), ModifyColor(c, XMFLOAT3(x2, y2, z1)), z1, x2), 
      Vertex(XMFLOAT3(x1, y2, z2), ModifyColor(c, XMFLOAT3(x1, y2, z2)), z2, x1), 
      Vertex(XMFLOAT3(x2, y2, z2), ModifyColor(c, XMFLOAT3(x2, y2, z2)), z2, x2)); 
...} 

AddQuad(Vertex v0, Vertex v1, Vertex v2, Vertex v3) { AddTriangle(v0, v1, v2); AddTriangle(v3, v2, v1); } 

void AddTriangle(Vertex v0, Vertex v1, Vertex v2) 
{ 
    VALIDATE(numVertices <= (maxBuffer - 3), "Insufficient triangle set"); 
    for (int i = 0; i < 3; i++) Indices[numIndices++] = short(numVertices + i); 
    Vertices[numVertices++] = v0; 
    Vertices[numVertices++] = v1; 
    Vertices[numVertices++] = v2; 
} 
方式

試圖從本教程

TriangleSet models; 
    models.LoadModel("F:\\cube.txt"); 
    Add(
     new OBJModel(&models, XMFLOAT3(0, 0, 0), XMFLOAT4(0, 0, 0, 1), 
      new OBJMaterial(
       new Texture(false, 256, 256, Texture::AUTO_WHITE) 
       //new Texture(DirectX, L"wallpaper.jpg") 
      ) 
     ) 
    ); //3D Model 

void LoadModel(char* filename) 
{ 
    ifstream fin; 
    char input; 


    // Open the model file. 
    fin.open(filename); 

    // Read up to the value of vertex count. 
    fin.get(input); 
    while (input != ':') 
    { 
     fin.get(input); 
    } 

    // Read in the vertex count. 
    m_vertexCount = 0; 
    fin >> m_vertexCount; 

    // Read up to the beginning of the data. 
    fin.get(input); 
    while (input != ':') 
    { 
     fin.get(input); 
    } 
    fin.get(input); 
    fin.get(input); 

    // Read in the vertex data. 
    for (int i = 0; i<m_vertexCount; i++) 
    { 
     Indices[numIndices++] = short(numVertices + i); 
     //numVertices++; deleted 
     fin >> Vertices[numVertices].Pos.x >> Vertices[numVertices].Pos.y >> Vertices[numVertices].Pos.z; 
     fin >> Vertices[numVertices].U >> Vertices[numVertices].V; 
     fin >> Normals[numVertices].Norm.x >> Normals[numVertices].Norm.y >> Normals[numVertices].Norm.z; 
     Vertices[numVertices].C = ModifyColor(0xffffffff, Vertices[numVertices].Pos); 
     numVertices+=1; //new statement 
    } 

    // Close the model file. 
    fin.close(); 
} 

我沒有使用正常的,因爲從它本來的對象的紋理教程加載使用函數模型到場景中。相反,我將顏色定義爲純黃色。試圖保持加載模型的結構儘可能類似於微型房間演示。

我使用了與Tiny Room Demo相同的模型,材質和紋理(頂點着色器和像素着色器)。然而,在場景中呈現的東西並不是它應該呈現的樣子。

做了一步一步的調試,看看座標是否正確加載到Vertices [numVertices]中。似乎沒有問題。我試圖加載該文件被cube.txt

頂點數量:36

數據:

-1.0 1.0 -1.0 0.0 0.0 0.0 0.0 -1.0

1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0

-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0

-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0

1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0

1.0 -1.0 -1.0 1.0 1.0 0.0 0.0 -1.0

1.0 1.0 -1.0 0.0 0.0 1.0 0.0 0.0

1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0

1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0

1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0

1.0 1.0 1。 0 1.0 0.0 1.0 0.0 0.0

1.0 -1.0 1.0 1.0 1.0 1.0 0.0 0.0

1.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0

-1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0

1.0 - 1.0 1.0 0.0 1.0 0.0 0.0 1.0

1.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.0

-1.0 1.0 1.0 1.0 0.0 0.0 0.0 1。0

-1.0 -1.0 1.0 1.0 1.0 0.0 0.0 1.0

...

什麼是假設出現(除了沒有紋理) 3D cube

究竟出現了只是三角形的片段 TinyRoomDemo + 3D cube

不確定發生了什麼問題。請指教!非常感謝你:)

頂點和索引緩衝區

struct OBJModel 
{ 
XMFLOAT3  Pos; 
XMFLOAT4  Rot; 
OBJMaterial * Fill; 
DataBuffer * VertexBuffer; 
DataBuffer * IndexBuffer; 
int   NumIndices; 

OBJModel() : Fill(nullptr), VertexBuffer(nullptr), IndexBuffer(nullptr) {}; 
void Init(TriangleSet * t) 
{ 
    NumIndices = t->numIndices; 
    VertexBuffer = new DataBuffer(DIRECTX.Device, D3D11_BIND_VERTEX_BUFFER, &t->Vertices[0], t->numVertices * sizeof(Vertex)); 
    IndexBuffer = new DataBuffer(DIRECTX.Device, D3D11_BIND_INDEX_BUFFER, &t->Indices[0], t->numIndices * sizeof(short)); 
} 

...

DIRECTX.Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 

---------------- -------------------------------------------------- -------------------------------------------------- -------------

06/06/2017編輯: 三維模型數據:

頂點數:798

數據:

28.3005 0.415886 -45.8282 0.7216 0.720211 0 0 -1

28.3005 -0.809079 -45.8282 0.732222 0.720211 0 0 -1

-27.7441 -0.809079 -45.8282 0.732222 0.847836 0 0 -1

28.3005 0.415886 68.1056 0.459891 0.720286 0 1 -0

28.3005 0.415886 0.719341 -45.8282 0 0.720286 1-0

-27.7441 0.415886 -45.8282 0.719341 0.847911 0 1 -0

28.3005 -0.809079 68.1056 0.721603 0.720211 0 0 1

28.3005 0.415886 68.1056 0.732225 0.720211 0 0 1

-27.7441 0.415886 68.1056 0.732225 0.847836 0 0 1

28.3005 -0.8 09079 -45.8282 0.459891 0.720298 0 -1 -0

28.3005 -0.809079 68.1056 0.719341 0.720298 0 -1 -0

-27.7441 -0.809079 68.1056 0.719341 0.847923 0 -1 -0

28.3005 0.415886 68.1056 0.719341 0.70683 1 0 -0

...

+0

在你LoadModel功能,看來你開始通過增加numVertices循環。因此,頂點[0]沒有任何值。 –

+0

你用過索引緩衝區嗎?或只有頂點緩衝區?你可能已經搞砸了索引,如果你使用索引緩衝區,還要注意如果你使用三角形繪製三角形的方法,基於頂點你提供你應該使用trianglelist –

+0

@Sly_TheKing我使用頂點和索引緩衝區。以及三角形列表。已將代碼添加到上面的帖子中。謝謝:) –

回答

0

從u規定的房子中的數據,好像1個三角正面臨一種方式和第二面向相反的方向。

使用光柵沒有回剔除繪製此對象

+0

[光柵器](http://imgur.com/a/IJd4d) 它似乎是在cull_none。嗯...... :( –

+0

我意識到了這個問題,似乎並非所有的obj文件都是三角化的,因此加載的頂點要小得多,這就解釋了三角形缺失的頂點 –

+0

,那麼它們可能存儲爲三角形? 4個頂點並做三角形條。 –