mardi 21 août 2018

C++/OpenGL Random Segmentation Fault

So I'm writing a chunk based procedurally generated terrain game and am running into two errors:

Basically, the way it's working is it generates chunks around the player position, once per game loop.

for (int i = RENDER_RADIUS; i >= 0; i--)
    {
        for (int j = RENDER_RADIUS; j >= 0; j--)
        {
            terr.renderChunk(glm::ivec2(c->getXOff() + i, c->getZOff() + j), cubeShader);
            terr.renderChunk(glm::ivec2(c->getXOff() - i, c->getZOff() - j), cubeShader);
            terr.renderChunk(glm::ivec2(c->getXOff() - i, c->getZOff() + j), cubeShader);
            terr.renderChunk(glm::ivec2(c->getXOff() + i, c->getZOff() - j), cubeShader);
        }
    }

In terr.renderChunk I am using a unordered_map that uses the chunks position as the key and the chunk is the value. If the unordered_map doesn't find the chunk then the position gets added to terr.updateList.

Then, back in the game loop:

if (!terr.updateList.empty())
    {
        terr.updateChunk(terr.updateList[terr.updateList.size()-1]);
        terr.world[terr.updateList[terr.updateList.size()-1]]->render(cubeShader);
        terr.updateList.pop_back();
    }

In a separate line, I'm ensuring that the players current chunk is loaded as well.

To generate a chunks VBO I added the indices to the chunks vector points and then build it as so:

glGenVertexArrays(1, &this->VAO);
glBindVertexArray(this->VAO);

// vertice VBO
glGenBuffers(1, &this->VBO_VERT);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO_VERT);
glBufferData(GL_ARRAY_BUFFER, this->points.size() * sizeof(glm::vec3), &this->points[0][0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);

// texture coords
glGenBuffers(1, &this->VBO_UV);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO_UV);
glBufferData(GL_ARRAY_BUFFER, this->uvs.size() * sizeof(glm::vec2), &this->uvs[0][0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(0));
glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

Now, it all generally works, but I randomly get a segmentation fault and have debugged it down to my render function:

void Chunk::render(Shader shader)
{
    shader.setMat4("transform", offsetMatrix);
    shader.setFloat("transparency", 1.0f);
    glBindVertexArray(VAO);
    cout << "size " << points.size() << endl;
    glDrawArrays(GL_TRIANGLES, 0, points.size()); //RIGHT HERE CAUSES THE SEGFAULTS
    cout << "TEST2" << endl;
}

The segmentation fault seems to happen randomly, however, I do believe it doesn't happen on new chunks but rather going back over old ones.

My question was, is there anything specific with OpenGL/C++ that I'm unaware of that could be causing it?

The other error I'm getting that may be related but I've debugged less is I'm getting chunk rendering errors as so where it renders random terrain but when I go into it, collision still works as if terrain was where it should be.

I realize this is a long question, but any support is really appreciated!

Aucun commentaire:

Enregistrer un commentaire