OpenGL OBJLoader 异常未处理的错误

发布于 2025-01-10 09:20:38 字数 3723 浏览 0 评论 0原文

好吧,基本上我现在正在处理一个 OpenGL 项目,并且我目前正在处理最后一部分,即 OBJLoader(加载 .obj 文件),但是我收到此 Exception Unhandled 错误,并且我似乎找不到问题在标题中。我对编码有点陌生,所以我很确定我错过了一些简单的事情,但我需要你的帮助。为什么我会收到此错误?

Resubmission.exe 中 0x7508EC52 处出现未处理的异常:Microsoft C++ 异常:内存位置 0x012FEAE4 处的字符。

根据 Visual Studio 的这一行,我收到上述错误: while (std::getline(in_file, 行))

#pragma once

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<sstream>
#include<algorithm>
#include<glew.h>
#include<glfw3.h>
#include<glm.hpp>
#include<vec3.hpp>
#include<vec4.hpp>
#include<mat4x4.hpp>
#include<gtc/matrix_transform.hpp>
#include<gtc/type_ptr.hpp>
#include"Vertex.h"

static std::vector<Vertex> loadOBJ(const char* file_name)
{
    std::vector<glm::fvec3> vertex_positions;
    std::vector<glm::fvec2> vertex_texcoords;
    std::vector<glm::fvec3> vertex_normals;
    std::vector<GLint> vertex_position_indicies;
    std::vector<GLint> vertex_texcoord_indicies;
    std::vector<GLint> vertex_normal_indicies;
    std::vector<Vertex> vertices;
    std::stringstream ss;
    std::ifstream in_file(file_name);
    std::string line = "";
    std::string prefix = "";
    glm::vec3 temp_vec3;
    glm::vec2 temp_vec2;
    GLint temp_glint = 0;

    if (!in_file.is_open())
    {
        throw "ERROR::OBJLOADER::Could not open file.";
    }

    while (std::getline(in_file, line))
    {
        ss.clear();
        ss.str(line);
        ss >> prefix;

        if (prefix == "#")
        {}
        else if (prefix == "o")
        {}
        else if (prefix == "s")
        {}
        else if (prefix == "use_mtl")
        {}
        else if (prefix == "v") 
        {
            ss >> temp_vec3.x >> temp_vec3.y >> temp_vec3.z;
            vertex_positions.push_back(temp_vec3);
        }
        else if (prefix == "vt")
        {
            ss >> temp_vec2.x >> temp_vec2.y;
            vertex_texcoords.push_back(temp_vec2);
        }
        else if (prefix == "vn")
        {
            ss >> temp_vec3.x >> temp_vec3.y >> temp_vec3.z;
            vertex_normals.push_back(temp_vec3);
        }
        else if (prefix == "f")
        {
            int counter = 0;
            while (ss >> temp_glint)
            {
                if (counter == 0)
                    vertex_position_indicies.push_back(temp_glint);
                else if (counter == 1)
                    vertex_texcoord_indicies.push_back(temp_glint);
                else if (counter == 2)
                    vertex_normal_indicies.push_back(temp_glint);
                if (ss.peek() == '/')
                {
                    ++counter;
                    ss.ignore(1, '/');
                }
                else if (ss.peek() == ' ')
                {
                    ++counter;
                    ss.ignore(1, ' ');
                }
                if (counter > 2)
                    counter = 0;
            }
        }
        else
        {}
    }
    vertices.resize(vertex_position_indicies.size(), Vertex());
    for (size_t i = 0; i < vertices.size(); ++i)
    {
        vertices[i].position = vertex_positions[vertex_position_indicies[i] - 1];
        vertices[i].texcoord = vertex_texcoords[vertex_texcoord_indicies[i] - 1];
        vertices[i].normal = vertex_normals[vertex_normal_indicies[i] - 1];
        vertices[i].color = glm::vec3(1.f, 1.f, 1.f);
    }
    std::cout << "Nr of vertices: " << vertices.size() << "\n";
    std::cout << "OBJ file loaded!" << "\n";
    return vertices;
}

okay so basically I am working on an OpenGL project at the moment and I am currently working on the last part which is the OBJLoader(Loading .obj files), but I get this Exception Unhandled error and I can't seem to find the problem in the header. I am a little new to coding so I am pretty sure it is something simple I missed, but I need your help. Why am I getting this error?

Unhandled exception at 0x7508EC52 in Resubmission.exe: Microsoft C++ exception: char at memory location 0x012FEAE4.

I am getting this above mentioned error according to Visual Studio at this line:
while (std::getline(in_file, line))

#pragma once

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<sstream>
#include<algorithm>
#include<glew.h>
#include<glfw3.h>
#include<glm.hpp>
#include<vec3.hpp>
#include<vec4.hpp>
#include<mat4x4.hpp>
#include<gtc/matrix_transform.hpp>
#include<gtc/type_ptr.hpp>
#include"Vertex.h"

static std::vector<Vertex> loadOBJ(const char* file_name)
{
    std::vector<glm::fvec3> vertex_positions;
    std::vector<glm::fvec2> vertex_texcoords;
    std::vector<glm::fvec3> vertex_normals;
    std::vector<GLint> vertex_position_indicies;
    std::vector<GLint> vertex_texcoord_indicies;
    std::vector<GLint> vertex_normal_indicies;
    std::vector<Vertex> vertices;
    std::stringstream ss;
    std::ifstream in_file(file_name);
    std::string line = "";
    std::string prefix = "";
    glm::vec3 temp_vec3;
    glm::vec2 temp_vec2;
    GLint temp_glint = 0;

    if (!in_file.is_open())
    {
        throw "ERROR::OBJLOADER::Could not open file.";
    }

    while (std::getline(in_file, line))
    {
        ss.clear();
        ss.str(line);
        ss >> prefix;

        if (prefix == "#")
        {}
        else if (prefix == "o")
        {}
        else if (prefix == "s")
        {}
        else if (prefix == "use_mtl")
        {}
        else if (prefix == "v") 
        {
            ss >> temp_vec3.x >> temp_vec3.y >> temp_vec3.z;
            vertex_positions.push_back(temp_vec3);
        }
        else if (prefix == "vt")
        {
            ss >> temp_vec2.x >> temp_vec2.y;
            vertex_texcoords.push_back(temp_vec2);
        }
        else if (prefix == "vn")
        {
            ss >> temp_vec3.x >> temp_vec3.y >> temp_vec3.z;
            vertex_normals.push_back(temp_vec3);
        }
        else if (prefix == "f")
        {
            int counter = 0;
            while (ss >> temp_glint)
            {
                if (counter == 0)
                    vertex_position_indicies.push_back(temp_glint);
                else if (counter == 1)
                    vertex_texcoord_indicies.push_back(temp_glint);
                else if (counter == 2)
                    vertex_normal_indicies.push_back(temp_glint);
                if (ss.peek() == '/')
                {
                    ++counter;
                    ss.ignore(1, '/');
                }
                else if (ss.peek() == ' ')
                {
                    ++counter;
                    ss.ignore(1, ' ');
                }
                if (counter > 2)
                    counter = 0;
            }
        }
        else
        {}
    }
    vertices.resize(vertex_position_indicies.size(), Vertex());
    for (size_t i = 0; i < vertices.size(); ++i)
    {
        vertices[i].position = vertex_positions[vertex_position_indicies[i] - 1];
        vertices[i].texcoord = vertex_texcoords[vertex_texcoord_indicies[i] - 1];
        vertices[i].normal = vertex_normals[vertex_normal_indicies[i] - 1];
        vertices[i].color = glm::vec3(1.f, 1.f, 1.f);
    }
    std::cout << "Nr of vertices: " << vertices.size() << "\n";
    std::cout << "OBJ file loaded!" << "\n";
    return vertices;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文