Unhandled exception at 0x1000bbae in OCTREE.exe: 0xC0000005: Access violation writing location 0x000000a8
I'm getting the above runtime error on the following code on line:
glutMainLoop();
Why?
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cassert>
#include <math.h>
#include <gl/glut.h>
#include <stdlib.h>
class Vertex {
public: float X;
float Y;
float Z;
public: Vertex (float first, float second, float third){
X=first;
Y=second;
Z=third;
}
};
using namespace std;
vector <Vertex> vertexCoordinates;
vector <vector<int>> faces;
vector <vector<float>> faceNormals;
vector <vector<float>> faceCenters;
void loadOff(string inputFileName) {
int vertexCount; int faceCount; int edgeCount;
ifstream inputFileStream;
inputFileStream.open(inputFileName.data());
assert(inputFileStream.is_open());
string actualLine;
inputFileStream>>vertexCount;
inputFileStream>>faceCount;
inputFileStream>>edgeCount;
for (int loadVertexIndex=0; loadVertexIndex<vertexCount; loadVertexIndex++){
float X;
float Y;
float Z;
inputFileStream>>(float)X;
inputFileStream>>(float)Y;
inputFileStream>>(float)Z;
vertexCoordinates.push_back(Vertex(X,Y,Z));
if(inputFileStream.eof())break;
}
for (int faceIndex=0; faceIndex<=faceCount; faceIndex++){ //<= faceCount?
string line;
getline(inputFileStream, line);
istringstream actualLineStream(line);
std::vector<int> face((std::istream_iterator<int>(actualLineStream)),
std::istream_iterator<int>());
faces.push_back(face);
}//end for faceCount
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glutSwapBuffers();
glutPostRedisplay();
}
void init(){
//loadOff("spider.off");
//BLACK BACKGROUND
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_BACK, GL_LINE);
glPolygonMode(GL_FRONT, GL_LINE);
}
void reshape(int w, int h) {
glViewport (0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
}
void keyboard (unsigned char key, int x, int y){
}
void specialKeys(int key, int x, int y) {
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
开发者_如何学Python glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize(800,600);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
You are getting an exception trying to write at 0x000000a8. What is happening is that somewhere your code or the openGL is receiving a null pointer rather than a valid pointer. The code in question is trying to update something at an offset of 168 (0xa8) from that pointer.
I have not used GLUT for 7 years, so take my answer with a grain of salt. This said, your init()
function makes me a bit concerned. I believe that when you write GLUT programs, you should only issue OpenGL commands in the GLUT callback functions. I'm not sure there is an active OpenGL context when your init()
function is executed. Does the program crash if you remove the call to init()
?
精彩评论