문제

Im trying to code a 3D model that I am reading in verts from a .obj It was all working with a standard teapot model, so I decided to create a spaceship for the actual assignment. I then changed it with the teapot file and then changed the arrays to be big enough for the vert and vertex normal's etc to fit before before trying to run the program to which i got a unhandled error access violation error. Below is all of the code that the program uses glut library which I have to use..

As a side note I am trying to make it so my camera moves around the spaceship as its origin.

#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();
    }
}

The exact error is as follow "Unhandled exception at 0x779815de in 3dsassignments.exe: 0xC0000005: Access violation reading location 0x40000010."

도움이 되었습니까?

해결책

The easiest way to catch access violations in Visual Studio is to let their debugger's built-in exception-trap break on violations for you. You can that by Debug/Exceptions/Win32 has "Access Violations" checked, then simply run your program. Note, this will NOT analyze your code. It will catch an exception if it happens, not if it may happen.

Regarding what is actually causing your exception, I would hazard to guess logic for using !inputFile.eof() for your while-loop control is not terminating when you think it should (and as a side note, it is almost never correct to use .eof() as a loop control condition). Try while(inputFile >> test) and delete the inputFile >> test; in the loop body. This will set the fail-bit on the stream when you have reached EOF and attempted to read past it, which it appears is what you are looking for.

EDIT After the OP changed the source code, the inputFile >> test; is being double-read and consequently skipping the initial value. The second extraction (the one in the loop body, not the loop condition expression) needs to be deleted.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top