gpt4 book ai didi

c++ - Oculus Tiny Room,Directx,将 3D 模型加载到场景中

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:29 25 4
gpt4 key购买 nike

我目前正在开发 Oculus Rift PC SDK。试图从更简单的东西开始,比如 Tiny Room Demo(DX11)。在网上看到这个教程,将 3D 模型从外部文件加载到场景中(Rastertek教程 7:3D 模型渲染)

Tiny Room Demo 创建模型的方式是硬编码坐标并渲染它

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。

我使用了与 Tiny Room Demo 相同的模型、 Material 和纹理(顶点着色器和像素着色器)。然而,渲染到场景中的东西并没有像它应该的那样出现。

逐步调试以查看坐标是否正确加载到 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);

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

2017 年 6 月 6 日编辑:3D模型数据:

顶点数: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 -45.8282 0.719341 0.720286 0 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.809079 -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

...

最佳答案

从你为房子提供的数据来看,似乎有 1 个三角形面向一个方向,而第二个三角形面向相反的方向。

使用光栅化器没有反向剔除来绘制这个对象

关于c++ - Oculus Tiny Room,Directx,将 3D 模型加载到场景中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44301773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com