i keep getting this error when trying to load an OBJ file into my project
atioglxx.pdb not loaded
with the following exception
Exception thrown at 0x53A083FF (atioglxx.dll) in Reality.exe: 0xC0000005: Access violation reading location 0x0894F000.
sometimes i get this error and sometimes i don't and have my model on the screen. So, i tried to debug the code and found that glBufferData function is what causes this error but couldn't figure out what is the problem with it.
Here the OBJ Loaded function
bool Mesh::LoadOBJ(std::string objFile)
{
std::vector<glm::vec3> position;
std::vector<glm::vec2> UVs;
std::vector<glm::vec3> normals;
std::vector< float > vertices;
std::vector<unsigned int> indices;
std::unordered_map< std::string, unsigned int> isProcessed;
std::ifstream myFile;
myFile.open(objFile);
if (!myFile.is_open())
{
std::cout << "Error Openening OBJ file : " << objFile;
return false;
}
unsigned int cnt = 1;
while (!myFile.eof())
{
std::string type;
myFile >> type;
float x, y, z;
if (type == "v") {
myFile >> x >> y >> z;
glm::vec3 v(x, y, z);
position.push_back(v);
}
else if (type == "vt") {
myFile >> x >> y;
glm::vec2 v(x, y);
UVs.push_back(v);
}
else if (type == "vn") {
myFile >> x >> y >> z;
glm::vec3 v(x, y, z);
normals.push_back(v);
}
else if (type == "f") {
std::string p1, p2, p3;
std::vector<std::string> vertex(3);
myFile >> p1;
if (!isProcessed[p1]) {
isProcessed[p1] = cnt;
indices.push_back(cnt - 1);
vertex[0] = "";
vertex[1] = "";
vertex[2] = "";
int c = 0;
for (int i = 0; i < p1.size(); ++i) {
if (p1[i] == '/') {
++c;
continue;
}
vertex[c] += p1[i];
}
if (vertex[0].size() > 0) {
int vertexIndex = std::stoi(vertex[0]);
--vertexIndex;
vertices.push_back(position[vertexIndex].x);
vertices.push_back(position[vertexIndex].y);
vertices.push_back(position[vertexIndex].z);
}
if (vertex[1].size() > 0) {
int UVsIndex = std::stoi(vertex[1]);
--UVsIndex;
vertices.push_back(UVs[UVsIndex].x);
vertices.push_back(UVs[UVsIndex].y);
}
if (vertex[2].size() > 0) {
int normalIndex = std::stoi(vertex[2]);
--normalIndex;
vertices.push_back(normals[normalIndex].x);
vertices.push_back(normals[normalIndex].y);
vertices.push_back(normals[normalIndex].z);
}
++cnt;
}
else {
indices.push_back(isProcessed[p1] - 1);
}
myFile >> p2;
if (!isProcessed[p2]) {
isProcessed[p2] = cnt;
indices.push_back(cnt - 1);
vertex[0] = "";
vertex[1] = "";
vertex[2] = "";
int c = 0;
for (int i = 0; i < p2.size(); ++i) {
if (p2[i] == '/') {
++c;
continue;
}
vertex[c] += p2[i];
}
if (vertex[0].size() > 0) {
int vertexIndex = std::stoi(vertex[0]);
--vertexIndex;
vertices.push_back(position[vertexIndex].x);
vertices.push_back(position[vertexIndex].y);
vertices.push_back(position[vertexIndex].z);
}
if (vertex[1].size() > 0) {
int UVsIndex = std::stoi(vertex[1]);
--UVsIndex;
vertices.push_back(UVs[UVsIndex].x);
vertices.push_back(UVs[UVsIndex].y);
}
if (vertex[2].size() > 0) {
int normalIndex = std::stoi(vertex[2]);
--normalIndex;
vertices.push_back(normals[normalIndex].x);
vertices.push_back(normals[normalIndex].y);
vertices.push_back(normals[normalIndex].z);
}
++cnt;
}
else {
indices.push_back(isProcessed[p2] - 1);
}
myFile >> p3;
if (!isProcessed[p3]) {
isProcessed[p3] = cnt;
indices.push_back(cnt - 1);
vertex[0] = "";
vertex[1] = "";
vertex[2] = "";
int c = 0;
for (int i = 0; i < p3.size(); ++i) {
if (p3[i] == '/') {
++c;
continue;
}
vertex[c] += p3[i];
}
if (vertex[0].size() > 0) {
int vertexIndex = std::stoi(vertex[0]);
--vertexIndex;
vertices.push_back(position[vertexIndex].x);
vertices.push_back(position[vertexIndex].y);
vertices.push_back(position[vertexIndex].z);
}
if (vertex[1].size() > 0) {
int UVsIndex = std::stoi(vertex[1]);
--UVsIndex;
vertices.push_back(UVs[UVsIndex].x);
vertices.push_back(UVs[UVsIndex].y);
}
if (vertex[2].size() > 0) {
int normalIndex = std::stoi(vertex[2]);
--normalIndex;
vertices.push_back(normals[normalIndex].x);
vertices.push_back(normals[normalIndex].y);
vertices.push_back(normals[normalIndex].z);
}
++cnt;
}
else {
indices.push_back(isProcessed[p3] - 1);
}
}
mVAO = new VertexArrayObject(vertices , vertices.size() , indices , static_cast<unsigned int>(indices.size()));
myFile.close();
return true ;
and here is the constructor of my VertexArray class
VertexArrayObject::VertexArrayObject(std::vector<float>& vertices, int VBOsize, std::vector<unsigned int>& indecies, unsigned int EBOsize):
EBOsize(EBOsize)
{
glGenVertexArrays(1, &mVAOiD);
glBindVertexArray(mVAOiD);
glGenBuffers(1, &mVBOiD);
glBindBuffer(GL_ARRAY_BUFFER, mVBOiD);
glBufferData(GL_ARRAY_BUFFER, 8 * VBOsize * sizeof(float) , &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &mEBOiD);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBOiD);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, EBOsize * sizeof(unsigned int), &indecies[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), reinterpret_cast<void*>(sizeof(float) * 3));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), reinterpret_cast<void*>(sizeof(float) * 5));
}
and this is the OBJ file for the model i'm trying to render Rock.obj
Note this is my first question on stackoverflow so please take it easy on me.
The computation of the buffer size in bytes is wrong. verizes.size()
is not the number of vertex attribute, it is the number of float
elements in the std::vector
.
You pass vertices.size()
to the argument VBOsize
of VertexArrayObject
s constructor:
mVAO = new VertexArrayObject(vertices , vertices.size(), indices ,static_cast<unsigned int>(indices.size()));
In the constructor VBOsize
is multiplied by 8:
VertexArrayObject::VertexArrayObject(std::vector<float>& vertices, int VBOsize, std::vector<unsigned int>& indecies, unsigned int EBOsize) :EBOsize(EBOsize) { // [...] glBufferData(GL_ARRAY_BUFFER, 8 * VBOsize * sizeof(float) , &vertices[0], GL_STATIC_DRAW); // [...]
If VBOsize
is the number of vertices, then you have to divide vertices.size()
by 8:
mVAO = new VertexArrayObject(vertices, vertices.size() , indices , static_cast<unsigned int>(indices.size()));
mVAO = new VertexArrayObject(vertices, vertices.size() / 8, indices, static_cast<unsigned int>(indices.size()));
Anyway, I recommend to change the computation of the buffer size:
glBufferData(GL_ARRAY_BUFFER, 8 * VBOsize * sizeof(float) , &vertices[0], GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
User contributions licensed under CC BY-SA 3.0