I am creating a game using OpenGl in c++. Whenever I create a new model while the game is running, such as fire a bullet, there is a huge lag spike. The function that creates the model is below.
std::string jsonString;
jsonString = file->load(type);
json jf = json::parse(jsonString); //Might be causing the lag
indicesSizeTexture = jf["textureIndices"].size();
verticesSizeTexture = jf["textureVertices"].size();
indicesSizeCollision = jf["collisionIndices"].size();
verticesSizeCollision = jf["collisionVertices"].size();
verticesTexture = new float[verticesSizeTexture * 8];
verticesCollision = new float[verticesSizeCollision * 8];
verticesCollisionUpdated = new float[verticesSizeCollision * 8];
indicesTexture = new int[indicesSizeTexture];
indicesCollision = new int[indicesSizeCollision];
for (int i = 0; i < verticesSizeTexture; i++) { //responsible for just the texture vertices
verticesTexture[i] = jf["textureVertices"][i];
}
for (int i = 0; i < indicesSizeTexture; i++) { // responsible for just the texture indices
indicesTexture[i] = jf["textureIndices"][i];
}
for (int i = 0; i < verticesSizeCollision; i++) { //responsible for just the collision vertices
verticesCollision[i] = jf["collisionVertices"][i];
verticesCollisionUpdated[i] = verticesCollision[i];
}
for (int i = 0; i < indicesSizeCollision; i++) { // responsible for just the collision indices
indicesCollision[i] = jf["collisionIndices"][i];
}
//binds id
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &EBO);
glGenTextures(1, &texture);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verticesSizeTexture * 8 * sizeof(float), verticesTexture, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//texture
glBindTexture(GL_TEXTURE_2D, texture);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(texturePathString.c_str(), &width, &height, &nrChannels, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
I stripped out a lot of parts that I am almost certain aren't causing the lag. There is a lot of stuff going on, but it is mostly simple mathematical operations. The only parts that I think could be causing the lag is the json section used for loading the model data. The model data is stored in a variable from file
as a string. I need the json section for the data storage though. What could be causing the lag? should I find a different data storage type? What if I created a bullet offscreen on startup, then copied it whenever I needed it? The specific json library I am using is https://github.com/nlohmann/json
//Might be causing the lag
- what does your profiler say? Is this the cause of the lag or is it not? I presume you're not re-parsing your JSON, generating new vertex buffers, and loading new textures for every single bullet you spawn, only for the first unique instance of each kind of model? \$\endgroup\$