Speeding up loading of uniforms and drawing ...

Hello,

So I have this strange issue with my code working very slowly at first and then “speeding up” after about 15-30 seconds of running depending on the size of the model.

The code in question is my drawing functionality for objects.

The first code that is initially dreadfully slow is loading of uniforms into OpenGL and the second set of code is the actual draws.

The thing that really bothers me is when things speed up after as mentioned 30 seconds. I ensured that it isn’t system load, etc.

Has anyone seen this before?

Thanks.


void OpenGLObject::DrawMe(void) {

	ViewModelMatrix = (*ViewMatrix) * ModelMatrix;
	MVPMatrix = (*ProjectionMatrix) * (*ViewMatrix) * ModelMatrix;
	NormalMatrix = glm::transpose(glm::inverse(glm::mat3(MVPMatrix)));

	glBindVertexArray(VertextArrayObjectID);
	
	glUniformMatrix4fv((*AssociatedOpenGLProgram->GetMVPMatrixID()), 1, GL_FALSE, glm::value_ptr(MVPMatrix));
	glUniformMatrix4fv((*AssociatedOpenGLProgram->GetViewMatrixID()), 1, GL_FALSE, glm::value_ptr((*ViewMatrix)));
	glUniformMatrix4fv((*AssociatedOpenGLProgram->GetViewModelMatrixID()), 1, GL_FALSE, glm::value_ptr(ViewModelMatrix));
	glUniformMatrix3fv((*AssociatedOpenGLProgram->GetNormalMatrixID()), 1, GL_FALSE, glm::value_ptr(NormalMatrix));

	/*
	
	Slow code here.
	
	*/
	
	AssociatedMaterial->LoadColorsIntoOpenGL(&AssociatedOpenGLProgram[0]);
	
	/*
	
	End slow code.
	
	*/

	if (AssociatedMaterial->IsWireframeEnabled() != false && AssociatedOpenGLProgram->IsWireframeEnabled() == false) {

		GLfloat EnableWireframe = 1.0f;
		glUniform1fv((*AssociatedOpenGLProgram->GetEnableWireframeID()), 1, &EnableWireframe);
		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	}
	else {

		if (AssociatedMaterial->HasAssociatedTexture()) {

			GLfloat ObjectHasMaterial = 1.0f;
			glUniform1fv((*AssociatedOpenGLProgram->GetObjectHasTextureID()), 1, &ObjectHasMaterial);
			glUniform1i((*AssociatedOpenGLProgram->GetTextureSamplerID()), 1);

			AssociatedMaterial->BindTexture(1);

		}
		else {

			glBindTexture(GL_TEXTURE_2D, NULL);

		}

	}

	/*
	
	Slow code here.
	
	*/
	
	glDrawElementsInstanced(GL_TRIANGLES, NumOfIndices, GL_UNSIGNED_INT, NULL, 1);
	
	/*
	
	End slow code.
	
	*/

	if (AssociatedMaterial->IsWireframeEnabled() != false && AssociatedOpenGLProgram->IsWireframeEnabled() == false) {

		GLfloat EnableWireframe = 0.0f;
		glUniform1fv((*AssociatedOpenGLProgram->GetEnableWireframeID()), 1, &EnableWireframe);
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	}

}

And the referenced Uniforms function “AssociatedMaterial->LoadColorsIntoOpenGL(&AssociatedOpenGLProgram[0]);” :


void OpenGLCompatibilityMaterial::LoadColorsIntoOpenGL(OpenGLProgram* CurrentProgram) {

	glUniform4fv((*CurrentProgram->GetAmbientColorID()), 1, GetAmbientColor());
	glUniform4fv((*CurrentProgram->GetDiffuseColorID()), 1, GetDiffuseColor());
	glUniform4fv((*CurrentProgram->GetEmissiveColorID()), 1, GetEmissiveColor());
	glUniform4fv((*CurrentProgram->GetSpecularColorID()), 1, GetSpecularColor());

	GLfloat MeshShininess = GetShininess();
	glUniform1fv((*CurrentProgram->GetMeshShininessID()), 1, &MeshShininess);

}