c++ - 未处理的异常错误 visual studio 2010 C++ (open gl)

标签 c++ opengl unhandled-exception

<分区>

我正在尝试编写一个 3D 模型,我正在从 .obj 中读取它的所有内容都使用标准茶壶模型,因此我决定为实际任务创建一个宇宙飞船。然后我用茶壶文件更改它,然后将数组更改为足够大,以便在尝试运行我遇到未处理错误访问冲突错误的程序之前适合顶点和顶点法线等。以下是该程序使用我必须使用的 glut 库的所有代码..

作为旁注,我正在尝试制作它,以便我的相机作为原点围绕宇宙飞船移动。

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include <istream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>


using namespace std;

struct point3D
{
    float x;
    float y;
    float z;
};

struct camera
{
    point3D pos;
    point3D lookAt;
    point3D up;

};

camera cam = {0, 0, 5, 0, 0, 0, 0, 1, 0};


point3D v[162] = {};
point3D vn[74] = {};
point3D vt[150] = {};
point3D f[110][3] = {};

void init()
{
    glClearColor(0.5, 0.7, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 500);

}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); // reset the matrix
    gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
              cam.lookAt.x, cam.lookAt.y, cam.lookAt.z,
              cam.up.x, cam.up.y, cam.up.z);
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(0.0f, 0.0f, -15.0f);


    for(int i = 0; i < 110; i++)
    {
        glBegin(GL_TRIANGLES);
        int one = f[i][0].x;
        int two = f[i][1].x;
        int three = f[i][2].x;

        glVertex3fv(&(v[one].x));
        glVertex3fv(&(v[two].x));
        glVertex3fv(&(v[three].x));
        glEnd();
    }


    glFlush();
}


void specialKeyboard(int key, int x, int y)
{
    switch (key)
    {
        case GLUT_KEY_RIGHT:
            cam.pos.x+=0.2;
            break;
        case GLUT_KEY_LEFT:
            cam.pos.x-=0.2;
            break;
        case GLUT_KEY_UP:
            cam.pos.y+=0.2;
            break;
        case GLUT_KEY_DOWN:
            cam.pos.y-=0.2;
            break;
        case GLUT_KEY_PAGE_UP:
            cam.pos.z+=0.2;
            break;
        case GLUT_KEY_PAGE_DOWN:
            cam.pos.z-=0.2;
            break;
    }

    glutPostRedisplay();

}

void normalKeyboard(unsigned char key, int x, int y) {
    switch (key)
    {

        case 'w' :
            cam.lookAt.y+=0.5;
            break;
        case 'a' :
            cam.lookAt.y-=0.5;
            break;
        case 's' :
            cam.lookAt.x-=0.5;
            break;
        case 'd' :
            cam.lookAt.x+=0.5;
            break;
    }

    glutPostRedisplay();

}

int main(int argc, char* argv[])
{
    int numVert = 0;
    int numNormals= 0;
    int numcoords = 0;
    int numFaces = 0;
    string test;
    ifstream inputFile;
    inputFile.open("spaceship.obj");

    if (!inputFile.good())
        cout << "Problem with Input File";
    else
    {
        while(inputFile >> test) // FIXED
        {
            inputFile >> test; // SHOULD NOT BE HERE

            if (test == "v")
            {
                inputFile >> v[numVert].x;
                inputFile >> v[numVert].y;
                inputFile >> v[numVert].z;
                numVert++;
            }
            else if(test == "vn")
            {
                inputFile >> vn[numNormals].x;
                inputFile >> vn[numNormals].y;
                inputFile >> vn[numNormals].z;
                numNormals++;
            }
            else if(test == "vt")
            {
                inputFile >> vt[numcoords].x;
                inputFile >> vt[numcoords].y;
                inputFile >> vt[numcoords].z;
                numcoords++;
            }
            else if(test == "f")
            {
                string temp;

                for(int count = 0; count < 3; count++)
                {
                    inputFile >> temp;
                    stringstream stream(temp);

                    getline(stream, temp, '/');
                    f[numFaces][count].x = atof(temp.c_str());
                    getline(stream, temp, '/');
                    f[numFaces][count].y = atof(temp.c_str());
                    getline(stream, temp, '/');
                    f[numFaces][count].z = atof(temp.c_str());
                }

                numFaces++;
            }
        }

        glutInit(&argc, argv);
        glutCreateWindow("rendering a spaceship");
        glutDisplayFunc(display);
        glutSpecialFunc(specialKeyboard);
        glutKeyboardFunc(normalKeyboard);

        init();

        glutMainLoop();
    }
}

确切的错误如下“3dsassignments.exe 中 0x779815de 处未处理的异常:0xC0000005:访问冲突读取位置 0x40000010。”

最佳答案

在 Visual Studio 中捕获访问冲突的最简单方法是让调试器的内置异常陷阱为您中断冲突。您可以通过 Debug/Exceptions/Win32 检查“访问冲突”,然后简单地运行您的程序。请注意,这不会分析您的代码。它会捕获异常如果它发生,而不是如果它可能发生。

关于实际上是什么导致你的异常,我会冒险猜测使用 !inputFile.eof() 的逻辑当你的 while-loop 控制不会终止时认为它应该(并且作为旁注,使用 .eof() 作为循环控制条件几乎是不正确的)。尝试 while(inputFile >> test) 并删除 inputFile >> test;在循环体内。当您到达 EOF 并尝试读取它时,这将在流上设置失败位,这似乎就是您要查找的内容。

EDIT 在 OP 更改源代码后,inputFile >> test; 被双重读取,因此跳过了初始值。需要删除第二次提取(循环主体中的提取,而不是循环条件表达式)。

关于c++ - 未处理的异常错误 visual studio 2010 C++ (open gl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16151396/

相关文章:

c++ - 如何从命令行或其他应用程序打开桌面应用程序并将其置于最前面?

c++ - 如何在 Mac OSX 上使用 distutils 编译带有 python C++ 扩展的 OpenGL?

c++ - OpenGL纹理倒置

c# - 在 C# 单元测试中实现未处理的异常处理程序

c# - CryptographicException:填充无效且无法删除并且 View 状态 MAC 验证失败

asp.net - 如何处理 ASP.NET 中未处理的线程异常?

c++ - 在托管 C++ GUI 中通过另一个事件结束/中断/切换一个事件

c++ - LNK2005(已定义)找不到错误

c++ - 这个程序中 "&"有什么区别

python - 有没有比OpenGL更高级的图形库