Hey everyone,
I have been building a component entity system for my game and so far it has gone pretty well but today I have hit a problem.
Having just but it a system for creating and firing bullets. when i click to fire bullets it seems that i get some weird lag (most likely performance) that makes it seem as my ship is jolting forward. After looking at the VS time while debugging, it seems that after firing the bullet it takes around 30 ms to get back to the bullet system and most of this time is being take up by the init system (taking around 25 ms setting up VAO, VBO, shaders and textures). So my question (as someone who is still learning about opengl) is how would i decrease this time for setting all of this up. I will post the relevent code for the init systems and all other classes i use below.
Thanks for all help provided in advance :)
On a node about function times, it seems as the function "com->myshade->loadshader(com->vertexshadername.c_str(), GL_VERTEX_SHADER);" with the line "GLuint Shader = glCreateShader(type);" Is taking around 10 ms to complete which im guessing is why its taking so long but i don't really know why (the fragment call finished in less then a ms).
Init system
void initsystem::update(float dt, entitymanager *mymanager, bool &go)
{
std::vector<entity*> setupentites = mymanager->returnsetupent();
for (int i = 0; i < setupentites.size(); i++)
{
BasicModelcomponent *com = static_cast<BasicModelcomponent*>(setupentites[i]->getcomponent(BasicModelcomponent::TypeID));
texturecomp *textcomp = static_cast<texturecomp*>(setupentites[i]->getcomponent(texturecomp::TypeID));
vectorimage *entityvec = static_cast<vectorimage*>(setupentites[i]->getcomponent(vectorimage::TypeID));
skyboxcomp *mycomp = static_cast<skyboxcomp*>(setupentites[i]->getcomponent(skyboxcomp::TypeID));
if (com != NULL && com->setupstate == false)
{
com->vetrices = com->getdata->retuvetices();
com->normals = com->getdata->retunormals();
com->uvs = com->getdata->retutexterco();
com->myVAO = new VAO();
com->modeldata = new meshd();
com->UVdata = new meshd();
com->myshade = new shader();
com->myVAO->genbuffer(1);
com->myVAO->bindVAO();
com->modeldata->genbuffer();
com->modeldata->addvec3buffer(com->vetrices);
com->modeldata->addvec3buffer(com->normals);
com->modeldata->bufferdatavec3(GL_STATIC_DRAW);
com->modeldata->linkdata(0, 3, 0, 0);
int test = sizeof(glm::vec3) * com->vetrices.size();
com->modeldata->linkdata(1, 3, 0, (void*)test);
test += sizeof(glm::vec3) * com->normals.size();
com->modeldata->linkdata(3, 3, 0, (void*)test);
com->UVdata->genbuffer();
com->UVdata->addvec2buffer(com->uvs);
com->UVdata->bufferdatavec2(GL_STATIC_DRAW);
com->UVdata->linkdata(2, 2, 0, 0);
if (textcomp != NULL)
{
textcomp->mytexture->loadtexture();
textcomp->mytexture->gentexture(1);
textcomp->mytexture->twodtexturesetup(true);
}
com->modeldata->unbindbuffer();
com->UVdata->unbindbuffer();
com->myVAO->unbindVAO();
com->myshade->loadshader(com->vertexshadername.c_str(), GL_VERTEX_SHADER);
com->myshade->loadshader(com->fragmentshadername.c_str(), GL_FRAGMENT_SHADER);
com->myshade->linkshader();
bool linkok = com->myshade->linkcheck();
shipshaderlinks *SSLcom = static_cast<shipshaderlinks*>(setupentites[i]->getcomponent(shipshaderlinks::TypeID));
lightcomp *lightlinks = static_cast<lightcomp*>(setupentites[i]->getcomponent(lightcomp::TypeID));
if (SSLcom != NULL)
{
SSLcom->shaderModelMatLocation = glGetUniformLocation(com->myshade->returnprogram(), "modelMat");
SSLcom->shaderViewMatLocation = glGetUniformLocation(com->myshade->returnprogram(), "viewMat");
SSLcom->shaderProjMatLocation = glGetUniformLocation(com->myshade->returnprogram(), "projMat");
if (textcomp != NULL)
{
textcomp->texture = glGetUniformLocation(com->myshade->returnprogram(), "myTextureSampler");
textcomp->Gamma = glGetUniformLocation(com->myshade->returnprogram(), "gamma");
}
if (lightlinks != NULL)
{
lightlinks->strenght = glGetUniformLocation(com->myshade->returnprogram(), "strength");
lightlinks->cameralocation = glGetUniformLocation(com->myshade->returnprogram(), "camerapos");
lightlinks->shine = glGetUniformLocation(com->myshade->returnprogram(), "shine");
}
com->setupstate = true;
}
}