将简单网格加载到 OpenGL 中时遇到问题

Trouble loading simple mesh into OpenGL

本文关键字:遇到 问题 OpenGL 简单 简单网 网格 加载      更新时间:2023-10-16

我需要将3D对象网格导入到Visual Studio 2010上的OpenGL代码中。我是OpenGL的新手,所以我一直在从以下教程(第7号)中学习:

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading/

现在,本教程使用简单的代码而不是库来导入".obj"文件那些使用过本教程的人会知道教程创建者提供的"cube.obj"工作得很好。但是,当我尝试加载自己的简单立方体网格时,程序通知我"我们的简单解析器无法处理 .obj 文件。请尝试使用其他选项导出"。

我认为这是代码的问题,并转到他的 Assimp 教程:http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-9-vbo-indexing/

这一次,"Suzanne.obj"作为样本提供。再一次,每当我尝试将自己的对象导出到代码中时,程序现在都会崩溃,说"Visual Studio 已停止工作"。这只发生在我导出的对象上。为了缩小问题的原因,我使用了教程 7 和教程 9 中提供的"cube.obj",它工作得很好。因此,目前看来问题似乎在于从Blender导出对象。

我已经按照教程 7 中关于导出对象时需要检查哪些选项的所有说明进行操作。但是值得注意的是,我导出的对象的".obj"代码出现在一行中,没有任何换行符。这是我导出的简单立方体网格的 obj 代码:

# Blender v2.73 (sub 0) OBJ File: 'ROOM.blend'
# www.blender.org
mtllib room.mtl
o Cube_Cube.004
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 1.000000 1.000000 1.000000
vn -1.000000 0.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn 1.000000 0.000000 0.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
usemtl None
s off
f 6//1 2//1 1//1
f 7//2 3//2 2//2
f 8//3 4//3 3//3
f 5//4 1//4 4//4
f 2//5 3//5 4//5
f 7//6 6//6 5//6
f 5//1 6//1 1//1
f 6//2 7//2 2//2
f 7//3 8//3 3//3
f 8//4 5//4 4//4
f 1//5 2//5 4//5
f 8//6 7//6 5//6

相比之下,教程中提供的多维数据集的 .obj 代码如下:

# Blender3D v249 OBJ File: untitled.blend
# www.blender3d.org
mtllib cube.mtl
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.748573 0.750412
vt 0.749279 0.501284
vt 0.999110 0.501077
vt 0.999455 0.750380
vt 0.250471 0.500702
vt 0.249682 0.749677
vt 0.001085 0.750380
vt 0.001517 0.499994
vt 0.499422 0.500239
vt 0.500149 0.750166
vt 0.748355 0.998230
vt 0.500193 0.998728
vt 0.498993 0.250415
vt 0.748953 0.250920
vn 0.000000 0.000000 -1.000000
vn -1.000000 -0.000000 -0.000000
vn -0.000000 -0.000000 1.000000
vn -0.000001 0.000000 1.000000
vn 1.000000 -0.000000 0.000000
vn 1.000000 0.000000 0.000001
vn 0.000000 1.000000 -0.000000
vn -0.000000 -1.000000 0.000000
usemtl Material_ray.png
s off
f 5/1/1 1/2/1 4/3/1
f 5/1/1 4/3/1 8/4/1
f 3/5/2 7/6/2 8/7/2
f 3/5/2 8/7/2 4/8/2
f 2/9/3 6/10/3 3/5/3
f 6/10/4 7/6/4 3/5/4
f 1/2/5 5/1/5 2/9/5
f 5/1/6 6/10/6 2/9/6
f 5/1/7 8/11/7 6/10/7
f 8/11/7 7/12/7 6/10/7
f 1/2/8 2/9/8 3/13/8
f 1/2/8 3/13/8 4/14/8

调试崩溃的程序时,会发生以下异常:

tutorial09_AssImp.exe 中0x00007FF76255E2D5时未处理的异常: 0xC0000005:访问违规读取位置0x0000000000000000。

调用堆栈如下所示:

tutorial09_AssImp.exe!aiVector3t::aiVector3t

(const aiVector3t & o) Line 67 C++ tutorial09_AssImp.exe!loadAssImp(const char * path, std::vector> & index, std::vector,std::分配器

& Vertices, std::

vector,std::allocator & uvs, std::vector,std::allocator 和法线) 第 149 行 C++ tutorial09_AssImp.exe!main() 第 92 行 C++

此外,异常发生在 vector3.h 中,如下所示:

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2012, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms, 
with or without modification, are permitted provided that the following 
conditions are met:
* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.
* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file aiVector3D.h
 *  @brief 3D vector structure, including operators when compiling in C++
 */
#ifndef AI_VECTOR3D_H_INC
#define AI_VECTOR3D_H_INC
#include <math.h>

#include "./Compiler/pushpack1.h"
#ifdef __cplusplus
template<typename TReal> class aiMatrix3x3t;
template<typename TReal> class aiMatrix4x4t;
// ---------------------------------------------------------------------------
/** Represents a three-dimensional vector. */
template <typename TReal>
class aiVector3t 
{
public:
    aiVector3t () : x(), y(), z() {}
    aiVector3t (TReal _x, TReal _y, TReal _z) : x(_x), y(_y), z(_z) {}
    explicit aiVector3t (TReal _xyz) : x(_xyz), y(_xyz), z(_xyz) {}
    aiVector3t (const aiVector3t& o) : x(o.x), y(o.y), z(o.z) {} //exception generated here
public:
    // combined operators
    const aiVector3t& operator += (const aiVector3t& o);
    const aiVector3t& operator -= (const aiVector3t& o);
    const aiVector3t& operator *= (TReal f);
    const aiVector3t& operator /= (TReal f);
    // transform vector by matrix
    aiVector3t& operator *= (const aiMatrix3x3t<TReal>& mat);
    aiVector3t& operator *= (const aiMatrix4x4t<TReal>& mat);
    // access a single element
    TReal operator[](unsigned int i) const;
    TReal& operator[](unsigned int i);
    // comparison
    bool operator== (const aiVector3t& other) const;
    bool operator!= (const aiVector3t& other) const;
    template <typename TOther>
    operator aiVector3t<TOther> () const;
public:
    /** @brief Set the components of a vector
     *  @param pX X component
     *  @param pY Y component
     *  @param pZ Z component  */
    void Set( TReal pX, TReal pY, TReal pZ);
    /** @brief Get the squared length of the vector
     *  @return Square length */
    TReal SquareLength() const;

    /** @brief Get the length of the vector
     *  @return length */
    TReal Length() const;

    /** @brief Normalize the vector */
    aiVector3t& Normalize();

    /** @brief Componentwise multiplication of two vectors
     *  
     *  Note that vec*vec yields the dot product.
     *  @param o Second factor */
    const aiVector3t SymMul(const aiVector3t& o);
    TReal x, y, z;  
} PACK_STRUCT;

typedef aiVector3t<float> aiVector3D;
#else
struct aiVector3D {
    float x,y,z;
} PACK_STRUCT;
#endif // __cplusplus
#include "./Compiler/poppack1.h"
#ifdef __cplusplus

#endif // __cplusplus
#endif // AI_VECTOR3D_H_INC

显然,我在导出模型时没有做正确的事情。我可能做错了什么?我缺少一个步骤吗?

程序在 objloader 的第 149 行崩溃.cpp(调用堆栈说)这条线是关于紫外线坐标的。你的模型没有,应该敲响铃铛=)

顺便说一句,您将遇到与法线相同的问题。

因此,您有2个选择:

  • 使对象具有 UV 和法线
  • 使加载器支撑网格没有UV或法线。

由于您无论如何都需要它们,因此我推荐第一个。

在搅拌机中:

  • 对于UV,转到编辑模式,选择所有顶点("A"键),网格>UVs->自动展开;如果您遇到困难,教程 15 包含一个视频,其中显示了您需要的一切。
  • 对于法线,只需在导出到 OBJ 时勾选"导出法线"选项。

在任何情况下,输出 OBJ 必须具有 v(位置)、vn(法线)、vt (UV) 和 f('v 之间的连接)

编辑:解释为什么调用堆栈的顶部谈论aiVector3Dt:第 148 行是

aiVector3D UVW = mesh->mTextureCoords[0][i];

UVW 是 mesh->mTextureCoords[0][i] 的副本,它不指向内存中的有效位置,因为缓冲区未分配,因为您的 OBJ 没有任何缓冲区。因此,构造函数(在调用堆栈中:aiVector3Dt::aiVector3Dt() )在尝试将此内存块复制到 UVW 时崩溃。这里有一个小区别,因为崩溃发生在一行之后,但它只是一个编译器优化。所以,aiVector3D的代码是完全正确的,但你给了他一个糟糕的地址。您可以通过将当前帧设置为 loadAssImp() 并在调试器中看到这一点,并监视 mesh->mTextureCoords[0]。