Assimp opengl loading problems - c++

I'm trying to load and render a 3D model i exported from blender to 3ds format.
I'm using Assimp to load the model and OpenGL (GLEW) to render it. for some reason some. in some of the models only parts of the model gets rendered.
for some this can be fixed by selecting all objects in blender and clicking join. But for others this does not solve the problem.
Here is the code Im using to load the models in:
(all of the "Array"s here are std::vector)
void Mesh::recursiveProcess(aiNode* node,const aiScene* scene) {
for(int i=0;i<node->mNumMeshes;i++) {
aiMesh* mesh=scene->mMeshes[node->mMeshes[i]];
processMesh(mesh, scene);
}
for(int i=0;i<node->mNumChildren;i++) {
recursiveProcess(node->mChildren[i], scene);
}
}
void Mesh::processMesh(aiMesh* m,const aiScene* scene) {
for(int i=0;i<m->mNumVertices;i++) {
vertexArray.push_back(m->mVertices[i].x);
vertexArray.push_back(m->mVertices[i].y);
vertexArray.push_back(m->mVertices[i].z);
if(m->mNormals!=NULL) {
normalArray.push_back(m->mNormals[i].x);
normalArray.push_back(m->mNormals[i].y);
normalArray.push_back(m->mNormals[i].z);
}
if(m->mTextureCoords[0]!=NULL) {
uvArray.push_back(m->mTextureCoords[0][i].x);
uvArray.push_back(m->mTextureCoords[0][i].y);
}
if(m->mTangents!=NULL) {
tangentArray.push_back(m->mTangents[i].x);
tangentArray.push_back(m->mTangents[i].y);
tangentArray.push_back(m->mTangents[i].z);
}
}
for(int i=0;i<m->mNumFaces;i++) {
aiFace face=m->mFaces[i];
for(int j=0;j<face.mNumIndices;j++) {
indexArray.push_back(face.mIndices[j]);
}
}
}
void Mesh::Load(string path) {
vertexArray.clear();
indexArray.clear();
normalArray.clear();
uvArray.clear();
tangentArray.clear();
const aiScene* scene=aiImportFile(path.c_str(),
aiProcess_GenSmoothNormals |
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_FindInvalidData);
const char* err = aiGetErrorString();
if(err!=NULL&&sizeof(err) > 4 && scene==NULL) {
cout << "The file wasn't successfuly opened " << path << " Because " << err;
return;
}
recursiveProcess(scene->mRootNode,scene);
if(uvArray.size()==0) uvArray.push_back(0);
if(normalArray.size()==0) normalArray.push_back(0);
if(tangentArray.size()==0) tangentArray.push_back(0);
}
and here is how I'm rendering them;
first i create the buffers. i do this once
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertexArray.size() * sizeof(float), vertexArray.data(), GL_STATIC_DRAW);
glGenBuffers(1, &indexbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexArray.size() * sizeof(unsigned int), indexArray.data(), GL_STATIC_DRAW);
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uvArray.size() * sizeof(float), uvArray.data(), GL_STATIC_DRAW);
then I execute this in the main render loop
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0);
camera.Update();
mvp = camera.getProjection() * camera.getView() * Model;
shader.SetUniformLocationMatrix4fv("MVP", &mvp[0][0]);
glBindTexture(GL_TEXTURE_2D, akm_tex);
glUseProgram(shader);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexbuffer);
glDrawElements(
GL_TRIANGLES, // mode
indexArray.size(), // count
GL_UNSIGNED_INT, // type
(void*)0 // element array buffer offset
);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
here is how a model looks like in my program:
and here is how it look in blender

You missed to apply the transformations of the nodes to the vertex buffers you are rendering. Each assimp-node stores a local transformation which need to get applied to all its assigned meshes.
You can introduce in your shader a uniform-variable to represent the global transformation for the vertices. During rendering you need to multiply the local transformation with the global transformation and apply it via this uniform matrix.
Or you can just use glPushMAtrix and glPopMatrix and apply the local transformation of your current node as you can see here (recursive_render)

Related

glDrawElements throws error code GL_INVALID_ENUM (0x500)

I tried to draw a textured square using OpenGL and indexes. At first, I draw a simple white square using VAOs and VBOs. After that, I tried to create an index buffer object to draw the same simple white square but it doesn't draw anything and it throws error core GL_INVALID_ENUM (0x500). This error code is thrown after calling glDrawElements.
Here there are some parts of my code:
Function that creates Index Buffer Object, VAO, and VBO:
void Object::loadObject(const float *lpfVertices, size_t uVerticesSize, const char *lpbElementsList, size_t uNumElements) {
this->uNumElements = uNumElements;
glGenVertexArrays(1, &uVertexArrayID);
glBindVertexArray(uVertexArrayID);
glGenBuffers(1, &uVertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, uVertexBufferID);
glBufferData(GL_ARRAY_BUFFER, uVerticesSize, (void *)lpfVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, NULL);
glEnableVertexAttribArray(0);
glGenBuffers(1, &uElemetsListID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uElemetsListID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, uNumElements, lpbElementsList, GL_STATIC_DRAW);
}
Function that render my object:
void Object::renderObject() {
glBindVertexArray(uVertexArrayID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uElemetsListID);
glDrawElements(GL_TRIANGLES, uNumElements, GL_BYTE, NULL);
}
Part of the main code:
object.loadObject(lpfTriangleVertices, sizeof(lpfTriangleVertices), lpbElementsList, sizeof(lpbElementsList));
uProgID = loadShader("default.vs", "default.fs");
while(!glfwWindowShouldClose(lpstWndID)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(uProgID);
object.renderObject();
glfwSwapBuffers(lpstWndID);
}
glDrawElements(GL_TRIANGLES, uNumElements, GL_BYTE, NULL);
^^^^^^^
GL_BYTE is not a valid argument for type in glDrawElements():
type
Specifies the type of the values in indices. Must be one of GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT.

Exception at glDrawElements

I'm trying to load a model into my project and I get an exception at glDrawElements.
I read the model file (.nfg), and retain the vertices and indices into vectors, and I use Vertex Buffer Object to bind my model.
I tried this:
I modified the fourth parameter from (GLvoid*)(sizeof(Vector3) * x)
to (GLvoid*)(offset(Vertex, attribute)), but didn't do anything (in the link, the problem was that he was sending memory address in the 4th parameter, and I thought maybe I was sending the wrong parameter to the wrong attribute, which still, would be a problem when actually showing the model).
I'm using OpenGL ES 2.0 and I'm not doing this project for neither Android or iOS; currently working in Visual Studio 2013 on Windows 8.1
The model loader:
void loadModelNfg(const std::string &filename,
GLuint &vbo, GLuint &ibo, GLuint &num, Shaders shaders){
// put here the verteces and indices from the file
std::vector<Vertex> vertices;
std::vector<GLushort> indices;
_loadModelNfg(filename, vertices, indices);
std::cout << "Mesh Loader: loaded file: " << filename << "\n";
// creates OpenGL objects necessary for drawing
GLuint gl_vertex_buffer_object, gl_index_buffer_object;
// vertex buffer object -> object in which to keep the vertices
glGenBuffers(1, &gl_vertex_buffer_object);
glBindBuffer(GL_ARRAY_BUFFER, gl_vertex_buffer_object);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex),
&vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// index buffer object -> object in which to keep the indices
glGenBuffers(1, &gl_index_buffer_object);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_index_buffer_object);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort),
&indices[0], GL_STATIC_DRAW);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
vbo = gl_vertex_buffer_object;
ibo = gl_index_buffer_object;
num = indices.size();
}
Calling the previous function:
// for now, global variables:
GLuint vbo, ibo, num;
Shader myShaders;
int Init ( ESContext* esContext ) {
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
// this one works: tried with a triangle
int ret = myShaders.Init("../Resources/Shaders/TriangleShaderVS.vs",
"../Resources/Shaders/TriangleShaderFS.fs");
if (ret == 0)
loadModelNfg("../../ResourcesPacket/Models/Bila.nfg", vbo, ibo, num, myShaders);
return ret;
}
Drawing the model:
void Draw(ESContext* esContext) {
Matrix world;
world.SetIdentity();
Matrix view = c.getView();
Matrix persp = c.getPerspective();
Matrix trans = world * view *persp;
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(myShaders.program);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
if (myShaders.positionAttribute != -1) {
glEnableVertexAttribArray(myShaders.positionAttribute);
glVertexAttribPointer(myShaders.positionAttribute, 3, GL_FLOAT,
GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, pos)));
}
if (myShaders.normalAttribute != -1) {
glEnableVertexAttribArray(myShaders.normalAttribute);
glVertexAttribPointer(myShaders.normalAttribute, 3, GL_FLOAT,
GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, norm)));
}
if (myShaders.binormalAttribute != -1) {
glEnableVertexAttribArray(myShaders.binormalAttribute);
glVertexAttribPointer(myShaders.binormalAttribute, 3, GL_FLOAT,
GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, binorm)));
}
if (myShaders.tangentAttribute != -1) {
glEnableVertexAttribArray(myShaders.tangentAttribute);
glVertexAttribPointer(myShaders.tangentAttribute, 3, GL_FLOAT,
GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, tgt)));
}
if (myShaders.texcoordAttribute != -1) {
glEnableVertexAttribArray(myShaders.texcoordAttribute);
glVertexAttribPointer(myShaders.texcoordAttribute, 2, GL_FLOAT,
GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, uv)));
}
if (myShaders.colorAttribute != -1) {
glEnableVertexAttribArray(myShaders.colorAttribute);
glVertexAttribPointer(myShaders.colorAttribute, 3, GL_FLOAT,
GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, col)));
}
if (myShaders.MVPuniform != -1) {
glUniformMatrix4fv(myShaders.MVPuniform, 1, GL_FALSE, (GLfloat*) trans.m);
}
// HERE GETS EXCEPTION
glDrawElements(GL_TRIANGLES, num, GL_UNSIGNED_SHORT, (GLvoid*) 0);
eglSwapBuffers (esContext->eglDisplay, esContext->eglSurface);
}
I am not sure that I am correctly binding the buffers in the loadModelNfg() function.
From what can this problem come and how can it be resolved?
EDIT:
GL_VENDOR: Imagination Technologies (Host GL: 'Intel');
GL_RENDERER: PowerVR PVRVFrame 4.2SGX 530 (Host 'Intel(R) HD Graphics 400');
GL_VERSION: OpenGL ES 2.0 (SDK build: 2.04.24.0809)
EDIT:
I surrounded the function with try-catch statement, but it still breaks when calling it:
try {
glDrawElements(GL_TRIANGLES, num, GL_UNSIGNED_SHORT, (GLvoid*)0);
}
catch (const std::exception& e) {
std::cout << e.what() << "\n";
}
I forgot to mention that the project/solution builds successful (after cleaning, or by rebuild).
After learning that OpenGL doesn't throw exceptions, I started looking how it handles errors. I found out that it "returns" error codes (or 0 if success), which can be found with glGetError().
Going withglGetError() through the code, I found out that the error was caused by glUseProgram(myShaders.program);.
Knowing that, I went through the functions which used myShaders variable, and I found that, after calling loadModelNfg("../../ResourcesPacket/Models/Bila.nfg", vbo, ibo, num, myShaders);, the variable got change.
Remembering that I don't use it anymore, I just removed it, and everything was fine.
What is strange is that I didn't modified the myShaders variable anywhere in that function (the code in the question is the final one). The problem, I think, is that I didn't declared the parameter const Shaders shaders.
So, the conclusion:
use glGetError() and breakpoints in code to find the real problem. It may not be the where it breaks!
PS: I hope it's ok that I put this as an answer. If it's not, I'll update the question.

show two objects at once

Well the origins of this questions lie there
https://stackoverflow.com/questions/20820456/strange-behavior-in-application-using-glfw
But I decided to simplify question with less text data and pictures(Now I use triangles)
I've got two objects - both objects are using vbo. I initalize every object using constructor and method init
Character::Character() {
glm::vec3 vert[] = {
glm::vec3(-.5f, -.5f, 0.0f) ,
glm::vec3(.5f, -.5f, 0.0f) ,
glm::vec3(0.0f, .5f, 0.0f)
};
vertices.insert(vertices.begin(), vert, vert + 3);
}
void Character::init() {
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
}
void Character::draw() {
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glDisableVertexAttribArray(0);
}
In another class glfwinitializer I keep both objects in std::vector<Character>. So in function main I create two objects and then push_back them into vector.
Draw loop is simple - I iterate through vector and call draw method
while(!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int i = 0; i < scene_items.size(); i++) {
scene_items[i]->draw();
}
this->navigator();
glfwSwapBuffers(window);
glfwPollEvents();
}
Method navigator calculates new position of objects when objects are selected. In every method, where I update data of vector vertices I call method
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
For example(method move_offset is called in navigator)
void Character::move_offset(double x_offset, double y_offset) {
for(int i = 0; i < vertices.size(); i++) {
vertices[i].x += x_offset;
vertices[i].y += y_offset;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
};
But two triangles are not shown on the screen at the same time - when I select one object - another object disappears. When I click on disappeared triangle - it appears and another disappears. (There is also one triangle in the initial location but it cannot be moved)
Why? Is there problem with buffers?
EDIT: PROJECT REPRODUCING PROBLEM WITH ADDITIONAL LIBS
visual studio 2010 project (9 mb)
project with libs
I haven't looked at the full source code in the linked project, but from what you have pasted here, you seem not to call glBindBuffer() in your move_offset() method - thus overwriting the buffer of whatever object was last bound (probably the last one drawn in your loop).

VertexBufferObject with interleaved normals

I have a working Vertex-Buffer-Object but I need to add the normals.
The normales are stored in the same array as the vertex positons. They are interleaved
Vx Vy Vz Nx Ny Nz
This is my code so far:
GLfloat values[NUM_POINTS*3 + NUM_POINTS*3];
void initScene() {
for(int i = 0; i < (NUM_POINTS) ; i = i+6){
values[i+0] = bunny[i];
values[i+1] = bunny[i+1];
values[i+2] = bunny[i+2];
values[i+3] = normals[i];
values[i+4] = normals[i+1];
values[i+5] = normals[i+2];
}
glGenVertexArrays(1,&bunnyVAO);
glBindVertexArray(bunnyVAO);
glGenBuffers(1, &bunnyVBO);
glBindBuffer(GL_ARRAY_BUFFER, bunnyVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(bunny), bunny, GL_STATIC_DRAW);
glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0,0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &bunnyIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bunnyIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangles), triangles, GL_STATIC_DRAW);
// unbind active buffers //
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void renderScene() {
if (bunnyVBO != 0) {
// x: bind VAO //
glEnableClientState(GL_VERTEX_ARRAY);
glBindVertexArray(bunnyVAO);
glDrawElements(GL_TRIANGLES, NUM_TRIANGLES, GL_UNSIGNED_INT, NULL);
glDisableClientState(GL_VERTEX_ARRAY);
// unbind active buffers //
glBindVertexArray(0);
}
}
I can see something on the screen but it is not right as the normals are not used correctly...
How can I use the values array correctly connected with my code so far.
You need to call glVertexAttribPointer two times, once for the vertices and once for the normals. This is how you tell OpenGL how your data is layed out inside your vertex buffer.
// Vertices consist of 3 floats, occurring every 24 bytes (6 floats),
// starting at byte 0.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, 0);
// Normals consist of 3 floats, occurring every 24 bytes starting at byte 12.
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, 12);
This is assuming that your normal attribute in your shader has an index of 1.

GLSL passing indiced normals to shader

I generated model (Suzie) in blender and exported it to .obj file with normals. During loading mode to my app i noticed that numbers of vertices and normals are diffrent (2012 and 1967).
I try to implement simple cell shading. The problem is in passing normals to shader. For storing vertex data i use vectors from glm.
std::vector<unsigned int> face_indices;
std::vector<unsigned int> normal_indices;
std::vector<glm::vec3> geometry;
std::vector<glm::vec3> normals;
Result i've got so far
Buffers Layout
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glBufferData(GL_ARRAY_BUFFER, geometry.size() * sizeof(glm::vec3), &geometry[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, NormalVBOID);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_DYNAMIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VIndexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, face_indices.size() * sizeof(unsigned int), &face_indices[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
Rendering fragment
glBindVertexArray(VAO);
glPolygonMode(GL_FRONT_AND_BACK, GL_QUADS);
glDrawElements(GL_QUADS, face_indices.size(), GL_UNSIGNED_INT, (void*)0);
glBindVertexArray(0);
The reason that had such wierd problem was that some normals were used more than once to preserve disk space so i had to rearrange them in a proper order. So the solution is pretty trival.
geometry.clear();
normals.clear();
geometry.resize(vv.size());
normals.resize(vv.size());
for (unsigned int i = 0; i < face_indices.size(); i++)
{
int vi = face_indices[i];
int ni = normal_indices[i];
glm::vec3 v = vv [vi];
glm::vec3 n = vn [ni];
geometry[vi] = v ;
normals[vi] = n ;
indices.push_back(vi);
}
You should also keep in mind that using the smooth modifier in Blender before export will in some cases help ensure that you have 1 normal per vertex (you may or may not need to also set per-vert normal view instead of face-normal view...can't rem so you'll have to test). This is because by default, blender uses per-face normals. The smooth modifier ("w" hotkey menu)
will switch it to per-vertex norms. Then when you export, you export verts and norms as usual, and the number should match. It doesn't always, but this has worked for me in the past.
This could possibly mean less unnecessary juggling of your data during import.