Hi, i'm a beginner with opengl, now i'm trying to load in my program few 3d models and to use some shaders but the problem is that is all already too slow.
I'm loading 2 models, one of them has 39282 vertices and 69451 faces, i know it's much but i thought having only 2 models would not have been problem.
It takes a lot for loading the models and then I have an average of 13 fps (i know fps are not so accurate but for now I think for me are enough) in the scene.
This is my scene:
When I load the models, i use assimp for copying the vertices, normals, uvs and faces in some std::vectors (the ones of my model class), i create the vbos and vao for them and i store the model information in another vector inside my scene object, then when i draw the scene, i scan all the models i have and and use this code for drawing:
(note: this is the code in my renderer class so in the upper section i have the vbo and vao set up i talked about earlier and in the lower the drawing section.)
#include "renderer.h"
#include <math.h>
Renderer::Renderer(Mesh* mesh, Material* material) :
vao(0),
vertexBuffer(0),
uvBuffer(0),
_mesh(mesh),
_material(material)
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//indexBuffer
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->faces().size() * sizeof(unsigned int), &mesh->faces()[0], GL_STATIC_DRAW);
//vertexbuffer
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, mesh->numVertices() * sizeof(glm::vec3), &mesh->vertices()[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
/*
//uvbuffer
glGenBuffers(1, &uvBuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
glBufferData(GL_ARRAY_BUFFER, mesh->uvs().size() * sizeof(glm::vec2), &mesh->uvs()[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_TRUE, 0, 0);
*/
//normalbuffer
glGenBuffers(1, &normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glBufferData(GL_ARRAY_BUFFER, mesh->normals().size() * sizeof(glm::vec3), &mesh->normals()[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_TRUE, 0, 0);
glBindVertexArray(0);
}
void Renderer::draw(Camera* camera, glm::mat4 model, Light* light) {
_material->shader()->use();
_material->shader()->setUniform("camera", camera->matrix());
_material->shader()->setUniform("model", model);
//_material->shader()->setUniform("view", camera->view());
// _material->shader()->setUniform("tex", 0); //to use GL_TEXTURE0
_material->shader()->setUniform("roughness", _material->roughness());
float Ks = ((_material->roughness()+8)/(8*3.141592));
_material->shader()->setUniform("Kd", _material->color() / 3.141592f);
_material->shader()->setUniform("Ks", _material->specularColor() * Ks);
_material->shader()->setUniform("lightPos", light->_position);
_material->shader()->setUniform("intensity", light->_intensity * light->_color);
//_material->shader()->setUniform("light.attenuation", light->_attenuation);
//_material->shader()->setUniform("light.ambientCoefficient", light->_ambientCoefficient);
_material->shader()->setUniform("cameraPos", camera->position());
/*
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _material->texture()->getID());
*/
//Bind vao and draw
glBindVertexArray(vao);
//glDrawArrays(GL_TRIANGLES, 0, _mesh->numVertices());
glDrawElements(GL_TRIANGLES, _mesh->faces().size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
_material->shader()->stopUsing();
}
(some of the code is commented because i'm doing some tests)
It's my rendering code bad? Can you say me what's the problem, why is everything so slow?
Thanks in advance for the help