首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未处理的异常错误visual studio 2010 C++ (open gl)

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

Stack Overflow用户
提问于 2013-04-22 23:56:17
回答 1查看 1.1K关注 0票数 1

我正在尝试编码一个3D模型,我正在从.obj中读取垂直于它的所有工作与标准茶壶模型,所以我决定创建一个实际任务的宇宙飞船。然后我用茶壶文件更改了它,然后更改了数组,使其足以容纳顶点和顶点法线等,然后尝试运行程序之前,我得到了一个未处理的错误访问冲突错误。下面是程序使用glut库的所有代码,我必须使用它。

顺便说一句,我正在试着让我的相机在宇宙飞船周围移动作为它的原点。

代码语言:javascript
复制
#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();
    }
}

确切的错误如下:"Unhandled at 0x779815de in 3dsassignments.exe: 0xC0000005: Access violation reading 0x40000010“。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-23 01:04:50

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

至于到底是什么导致了您的异常,我敢说您的while循环控制使用!inputFile.eof()的逻辑并没有在您认为它应该终止的时候终止(顺便说一句,使用.eof()作为循环控制条件几乎从来都不正确)。尝试测试并删除循环体中的inputFile >> % while(inputFile >> test);。当您到达EOF并试图读过它时,这将在流上设置失败位,这似乎就是您正在寻找的。

编辑在OP更改源代码之后,inputFile >> test;将被双重读取,从而跳过初始值。需要删除第二个提取(循环体中的提取,而不是循环条件表达式)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16151396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档