OpenGL:3D模型没有正确渲染,只渲染为2D?

时间:2014-10-15 01:22:20

标签: opengl 3d d assimp

我正在尝试使用Assimp加载一个简单的八面体。这就是模型的样子:

enter image description here

然而,这是我的代码呈现的内容:

enter image description here

显然,这是不正确的。我不确定我的代码在哪里出错,不过我怀疑它是使用OpenGL,而不是Assimp代码。

这是我的代码:

    auto scene = scoped!AssimpScene(assimp,"octahedron.obj",aiProcessPreset_TargetRealtime_Fast);
auto mesh = scene.scene().mMeshes[0]; 

auto vertex = new float[mesh.mNumFaces*3*3];
auto normal = new float[mesh.mNumFaces*3*3];
int b = 0;
for(int i = 0; i < mesh.mNumFaces;i++)
{
    auto face = mesh.mFaces[i];
    for(int j = 0; j < 3; j++)
    {

         auto va  = cast(float[3])mesh.mVertices[face.mIndices[j]];
         vertex[b..b+3] = va;


         auto n  = cast(float[3])mesh.mNormals[face.mIndices[j]];
         normal[b..b+3] = n;
         b+=3;
    }
}
while(!sdl2.keyboard().isPressed(SDLK_ESCAPE))
{
    sdl2.processEvents(); 
    //clear screen
    glViewport(0, 0, width,height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glVertexPointer(3,GL_FLOAT,0,cast(const(void*))vertex);
    glNormalPointer(GL_FLOAT,0,cast(const(void*))normal);


    glDrawArrays(GL_TRIANGLES,0,mesh.mNumFaces*3);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    window.swapBuffers();

}
scene.close();
assimp.close();
}

代码是不稳定的,可以大大改进,虽然我对OpenGL很新,所以我不确定究竟是怎么回事。任何有关这方面的建议都将不胜感激!

以下是在运行时生成的日志以供参考:

2014-10-14T17:19:26.0681112:assimp.d:loggingCallbackAssimp:149 
info: assimp: Info,  T0: Load octahedron.obj

2014-10-14T17:19:26.0681631:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: Assimp 3.0.1262 amd64 gcc debug shared singlethreaded

2014-10-14T17:19:26.0681916:assimp.d:loggingCallbackAssimp:149 
info: assimp: Info,  T0: Found a matching importer for this file format

2014-10-14T17:19:26.0682121:assimp.d:loggingCallbackAssimp:149 
info: assimp: Info,  T0: Import root directory is './'

2014-10-14T17:19:26.0682681:assimp.d:loggingCallbackAssimp:149 
info: assimp: Info,  T0: Entering post processing pipeline

2014-10-14T17:19:26.0682903:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: GenUVCoordsProcess begin

2014-10-14T17:19:26.0683055:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: GenUVCoordsProcess finished

2014-10-14T17:19:26.0683206:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: TriangulateProcess begin

2014-10-14T17:19:26.0683351:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: TriangulateProcess finished. There was nothing to be done.

2014-10-14T17:19:26.0683503:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: SortByPTypeProcess begin

2014-10-14T17:19:26.0683655:assimp.d:loggingCallbackAssimp:149 
info: assimp: Info,  T0: Points: 0, Lines: 0, Triangles: 1, Polygons: 0 (Meshes, X = removed)

2014-10-14T17:19:26.0683812:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: SortByPTypeProcess finished

2014-10-14T17:19:26.0683975:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: GenFaceNormalsProcess begin

2014-10-14T17:19:26.0684129:assimp.d:loggingCallbackAssimp:149 
info: assimp: Info,  T0: GenFaceNormalsProcess finished. Face normals have been calculated

2014-10-14T17:19:26.0684297:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: Generate spatially-sorted vertex cache

2014-10-14T17:19:26.0684487:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: CalcTangentsProcess begin

2014-10-14T17:19:26.068466:assimp.d:loggingCallbackAssimp:149 
info: assimp: Error, T0: Failed to compute tangents; need UV data in channel0

2014-10-14T17:19:26.0684825:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: CalcTangentsProcess finished

2014-10-14T17:19:26.0684983:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: JoinVerticesProcess begin

2014-10-14T17:19:26.0685253:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: Mesh 0 (unnamed) | Verts in: 24 out: 24 | ~0%

2014-10-14T17:19:26.0685428:assimp.d:loggingCallbackAssimp:149 
info: assimp: Debug, T0: JoinVerticesProcess finished 

2014-10-14T17:19:26.0685593:assimp.d:loggingCallbackAssimp:149 
info: assimp: Info,  T0: Leaving post processing pipeline

2 个答案:

答案 0 :(得分:1)

你做的很多事情都没有,openGL比你想象的要低得多,你必须指定基质和照明等,即使是传统的GL。

http://www.arcsynthesis.org/gltut/index.html

推荐教程

答案 1 :(得分:0)

您不进行任何相机转换。如果你想快速修复,你可以使用gluLookAt,但我建议不要使用弃用的OpenGL。这是一个很好的教程:https://open.gl/