Visual artifacts when loading OBJ model with Assimp - opengl

I am trying to integrate the Assimp loader to my framework. Everything is rendered fine, but in this spider model I'm rendering, its fangs are not being drawn as expected (see following picture).
Below is the relevant code snippet:
//Storing the Indices
for (unsigned int t = 0; t < mesh->mNumFaces; ++t) {
aiFace* face = &mesh->mFaces[t];
memcpy(&faceArray[index], face->mIndices, 3*sizeof(unsigned int));
index += 3;
}
//Storing the Vertices
for (unsigned int t = 0; t < mesh->mNumVertices; ++t) {
aiVector3D vertex ;
if (mesh->HasPositions()) {
vertex = mesh->mVertices[t];
memcpy(&vertexArray[index], &vertex,3*sizeof(float));
}
index += 3;
}
//Render module
void model::helperDraw(GLuint vertexBufferID, GLuint indexBufferID, GLuint textureID)
{
GLint indexSize;
glBindBuffer(GL_ARRAY_BUFFER,vertexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBufferID);
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &indexSize);
glBindTexture( GL_TEXTURE_2D, textureID);
glDrawElements(GL_TRIANGLES, indexSize/sizeof(GLuint), GL_UNSIGNED_INT, 0);
}
What could be wrong with my code?

There is nothing obviously wrong with your code. One possible cause for these rendering artefacts is that the OBJ model you load has some faces that are triangles an some faces that are not. You are rendering everything as GL_TRIANGLES, but the OBJ format can specify faces as quads, triangle-strips, triangles and even other more exotic things like patches.
Assimp has a mesh triangulation facility that can make your life a lot easier when dealing with these multi-format mesh files, such as the OBJ. Try passing the flag aiProcess_Triangulate to the load method of the importer or even to the post-processing method if you do post-processing as a separate step. This is likely to fix the issue.

Related

Rendering surfaces in OpenGL with depth test correctly

I am wondering how to render surfaces using depth test correctly. In my case it is not working although it has been enabled. I tried many combinations but can not figure out what is being done wrong, it might been some ordering of OpenGL commands, or it might be something I am missing completely.
I have this code that uses opengl to render a 2d game I am working on. I want to enable z buffering and depth test to simplify things in the code. I read a number of tutorials online and made changes as instructed but can not figure out why it is not working.
the code of the main function is shown below, I am changing the values of z for the two squares to be -10 and -25 and swapping them later on, but I always get the first square rendered over the second one no matter what values I use:
void MainGame::RenderTestUI()
{
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLSLProgram *ActiveShader = nullptr;
ActiveShader = &ColorShader;
ActiveShader->Use();
GLint Location1 = ActiveShader->GetUniformLocation("cam");
glm::mat4 tmp = Camera.GetCameraMatrix();
glUniformMatrix4fv(Location1, 1, GL_FALSE, &tmp[0][0]);
glActiveTexture(GL_TEXTURE0);
GLint Location2 = ActiveShader->GetUniformLocation("basic");
glUniform1f(Location2, 0);
glBindTexture(GL_TEXTURE_2D, GameTextures.ID);
CurrentBoundTexture = GameTextures.ID;
RenderingBatch.StartAddingVerticies();
this->GameMap.TileList[1].FillSixVerticies(RenderingBatch.VertexListPtr, 0, 0);
RenderingBatch.VertexCount += 6;
for (int i = 0; i < 6; i++)
RenderingBatch.VertexListPtr[i].z = -10; // first face
this->GameMap.TileList[2].FillSixVerticies(&RenderingBatch.VertexListPtr[RenderingBatch.VertexCount], 8, 8);
RenderingBatch.VertexCount += 6;
for (int i = 0; i < 6; i++)
RenderingBatch.VertexListPtr[i+6].z = -25; // second face
RenderingBatch.EndAddingVerticies();
RenderingBatch.CreateVBO();
RenderingBatch.Render();
ActiveShader->Unuse();
// swap buffers
SDL_GL_SwapWindow(GameWindow);
}
The end result is always the same regardless of the value of z i am assigning to the two faces, the result could be seen here:
any advice is highly appreciated.
When setting up the SDL surface to draw on, did you ask for a depth buffer prior to calling SDL_CreateWindow?
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

Multiple textures in Crisis Nanosuit - Model loading using Assimp

I want to load Crisis Nanosuit model using Assimp in OpenGL/GLSL. The model has several meshes as described by the node tree in assimp. Each mesh is associated with one or more textures (diffuse or specular etc). How do I render textures over the model and still do it with a single draw call?
I have been able to load models without textures so far. And here is how I did it: I used the node tree to find out how many meshes are present in the model and stacked them using an Array-of-structures. This contained float values of Positions, Normals, Texcoords, Color_Ambient, Color_diffuse, Color_specular and Shininess into a buffer, VBO. Since the meshes are stacked, the index array was offset for each mesh accordingly. Finally with a single draw call, the model was rendered successfully. Here is the entire code and the relavent parts are as follows
struct Vertex
{
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texcoord;
glm::vec3 colorambient;
glm::vec3 colordiffuse;
glm::vec3 colorspecular;
float shininess;
};
// Creating a nodestack of all the meshes
void modelloader::NodeTreeTraversal(aiNode *node)
{
if(node->mNumChildren==0)
nodestack.push_back(node);
else
for(unsigned int i=0; i<node->mNumChildren; i++)
this->NodeTreeTraversal(node->mChildren[i]);
}
// Look into assimp data structures for data and populate them into opengl's vbo's and ebo.
void modelloader::ProcessMeshes()
{
// currently this method loads vertex positions, normals, textures;
// also loads material info such as ambient, diffuse and specular colors with shininess as 16.0f
Vertex vertex;
unsigned int offset_faces=0;
for(unsigned int i=0; i<this->nodestack.size(); i++)
{
aiNode *node = nodestack[i];
for(unsigned int j=0; j<node->mNumMeshes; j++)
{
aiMesh *mesh = this->scene->mMeshes[node->mMeshes[j]];
aiColor4D ambient;
aiColor4D diffuse;
aiColor4D specular;
if(this->scene->HasMaterials()) {
aiMaterial *mtl = scene->mMaterials[mesh->mMaterialIndex];
aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient);
aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse);
aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular);
}
// load all mesh data
for(unsigned int k=0; k<mesh->mNumVertices; k++)
{
// positions and normals
vertex.position = glm::vec3(mesh->mVertices[k].x, mesh->mVertices[k].y, mesh->mVertices[k].z); // load positions
vertex.normal = glm::vec3(mesh->mNormals[k].x, mesh->mNormals[k].y, mesh->mNormals[k].z); // load normals
// load textures
if(this->scene->HasTextures())
vertex.texcoord = glm::vec2(mesh->mTextureCoords[0][k].x, mesh->mTextureCoords[0][k].y);
else vertex.texcoord = glm::vec2(0.0f, 0.0f);
// load materials
vertex.colorambient = glm::vec3(ambient.r, ambient.g, ambient.b);
vertex.colordiffuse = glm::vec3(diffuse.r, diffuse.g, diffuse.b);
vertex.colorspecular = glm::vec3(specular.r, specular.g, specular.b);
vertex.shininess = 16.0f;
// push back all the data for each vertex
meshdata.push_back(vertex);
}
// create index data
for(unsigned int l=0; l<mesh->mNumFaces; l++) {
this->indices.push_back(mesh->mFaces[l].mIndices[0]+offset_faces);
this->indices.push_back(mesh->mFaces[l].mIndices[1]+offset_faces);
this->indices.push_back(mesh->mFaces[l].mIndices[2]+offset_faces);
}
offset_faces = offset_faces+mesh->mNumVertices;
}
}
this->MeshData = &meshdata[0].position.x;
this->MeshDataSize = meshdata.size() * 18 * sizeof(float);
this->Indices = indices.data();
this->IndicesSize = indices.size()*sizeof(unsigned int);
}
// draw call
void modelloader::RenderModel()
{
glBindVertexArray(this->VAO);
glDrawElements(GL_TRIANGLES, this->IndicesSize/sizeof(unsigned int), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
Here is the output image.
Now, when loading textures (separate image files for each part of the body), when I activated all the textures atonce, it stretched each texture file over the entire body. How do I do it properly?
My preliminary thoughts are: Activate all the texture files. Add an attribute called "mesh_number" in the VBO and in the fragment shader, use the appropriate texture corresponding to the "mesh_number". I dont know if this will work. How is it usually done? Do you have any code samples?
This problem gets solved when the draw call is applied to each mesh in the model as done here.
1) But aren't draw calls expensive? Shouldn't I be drawing the entire mesh at one go? 2) Should I create a single image file of all the body parts collaged into one; much like a sprite sheet?
You need to activate each texture via:
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, <cour texture id>);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, <cour texture id>);
So all the textures can be rendered for your draw-call.

OpenGL ES glTexImage2D optimization

My task is to show several pictures. I implemented it as a class to make several instances. Each instance represents a picture. It compiles shaders, set two triangles and loads picture data in constructor. The main program creates instances and then goes to loop to switch prigramid and call render() method for each instance.
while(true)
for (uint g = 0; g < pictures.size(); g++){
glUseProgram(pictures[g]->ProgramId);
pictures[g]->render();
}
It works well and shows the pictures but I do not like it. It could be done much better.
Here is partial code of the class
Picture::Picture(picPosition* pPosition, const char * fileName)
:BasePicture(pPosition)
{
pos = glGetAttribLocation(ProgramId, "position");
uv = glGetAttribLocation(ProgramId, "texture_vert");
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
int n;
textureData = stbi_load(fileName, &picWidth, &picHeight, &n, STBI_rgb_alpha);
TextureID = glGetUniformLocation(ProgramId, "myTextureSampler");
glBindTexture(GL_TEXTURE_2D, TextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glActiveTexture(GL_TEXTURE0);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
//calculating the vertex matrix using MVP calculated in parent class
for (int i = 0; i < 6; i++)
ModeledVerts.push_back(MVP * verts[i]);
v = glm::value_ptr(ModeledVerts[0]);
}
Picture::~Picture()
{
stbi_image_free(textureData);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &uvbuffer);
}
void Picture::render()
{
glBufferData(GL_ARRAY_BUFFER, 96, v, GL_STATIC_DRAW);
glVertexAttribPointer(pos, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*) 0);
glEnableVertexAttribArray(pos);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesUV), verticesUV, GL_STATIC_DRAW);
glVertexAttribPointer(uv, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*) 0);
glEnableVertexAttribArray(uv);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, picWidth, picHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
I played a lot with the code to make render() function as light as possible but I cannot make it lighter then it is now.
The biggest problem is sending textureData every time (glTexImage2D). The data never gets changed. I tried to move it to constructor but in this case all picture objects show the same picture that was loaded latest. It looks like one instance overrides texture data uploaded before. I am looking for a way to load the picture data once in the constructor and not every time it renders. It looks like there is something in OpenGL API for that but I do not know it yet.
Another improvement could be for taking vertex data set up from render(). That data never changes. But it is not that significant as glTexImage2D call in render().
Could you point me to the OpenGL API to separate shader's data? Or show me what I am doing wrong...
You stated:
It works well and shows the pictures but I do not like it. It could be done much better.
From a design approach I think that this may help you.
Separate the functionality of opening, reading & parsing the image files for texture data from the actual texture struct or class. This would be a sample pseudo code:
struct Texture {
unsigned int width;
unsigned int height;
bool hasTransparency;
GLint id; // ID that is used by OpenGL to setActive, bind, and pass to shaders as either a uniform or sampler2D.
std::string filenameAndPath; // Keep this filename associated with texture so you can prevent trying to reload the same file over and over.
GLuchar* data; // the actual color - pixel texture data.
}
// This function will handle the opening and reading in of the texture data
// it would return back the ID value generated by OpenGL which will also be
// stored into the texture struct. The texture struct is returned by reference so that it can be populated with data.
GLuint loadTexture( const char* filenameAndPath, Texture& texture, /*loading parameters & flags*/ ) {
// Check to see if file exists or is already loaded
if ( fileanameAndPath already exists ) {
// get the existing ID from the already loaded texture
// and just return that.
} else {
// Try to open the new file for reading.
// parse the data for general purposes that will support
// your application. You can simply use `stbi_load` as it is a fairly
// decent third party library.
// Check the internal formatting of how the data is stored
// Compression, pixel orientation etc.
// configure the data to your needs (color format),
// (flipping the pixels either horizontally, vertically or both),
// now copy the actual pixel data into your buffer.
// close the file handle
// save all the information into your struct
// return the ID value that was generated by OpenGL
}
}
The within your main engine code before the render loop you will want to load your texture(s) from file and then you can use this Texture object where needed. Finally in your render loop is where you would want to set the texture(s) to active and bind them to the render target and pass them off to your shaders. In some cases you might want to set them active & bind them before your render loop depending on the type of shader-technique you are implementing.
Answering my own question.
The solution is to use atlas map. Software generates atlas that contains all pictures, upload it once (glTexImage2D) and use coordinates for each picture. That improves performance very significantly as glTexImage2D was called just once.

Multiple Vertex Array Objects: Proper way to display multiple primitives?

I'm having a hard time getting multiple vertex array objects to render multiple primitives. Nearly all the OpenGL tutorials I've found online show using only a single VAO, so I'm not sure what I might be doing wrong.
I'm using Qt-OpenGL and trying to render a square and a cube (on multiple VAOs).
Using the following code, I'm only getting one primitive displayed on the screen (whichever one is initialized second). I can see either primitive when I turn off initialization of the other one, but not at the same time.
Data Struct:
struct VBO : public QOpenGLBuffer {
};
struct VAO : public QOpenGLVertexArrayObject {
VBO vbo[1];
};
enum { CIRCLE, RECT, NUM_VAOs };
enum { POS, NUM_VBOs };
VAO vao[NUM_VAOs];
Initialization:
static void init_objects() {
for(int i = 0; i < NUM_VAOs; ++i) {
vao[i].create();
}
vao[CIRCLE].bind();
for(int i = 0; i < NUM_VBOs; ++i) {
vao[CIRCLE].vbo[i].create();
vao[CIRCLE].vbo[i].setUsagePattern( QOpenGLBuffer::StaticDraw );
}
vao[CIRCLE].vbo[POS].bind();
vao[CIRCLE].vbo[POS].allocate(circle.getVertexData(), circle.getNumVertices()*3*sizeof(float));
vao[CIRCLE].release();
// repeat for RECTANGLE instead of CIRCLE
vao[RECT].bind();
for(int i = 0; i < NUM_VBOs; ++i) {
vao[RECT].vbo[i].create();
vao[RECT].vbo[i].setUsagePattern( QOpenGLBuffer::StaticDraw );
}
vao[RECT].vbo[POS].bind();
vao[RECT].vbo[POS].allocate(circle.getVertexData(), circle.getNumVertices()*3*sizeof(float));
}
Rendering Code:
void game::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
vao[RECT].bind();
vao[RECT].vbo[POS].bind();
QMatrix4x4 id;
Game.setMVP( id );
Game.setColor( Colour(0.0, 1.0, 0.0) );
glDrawElements(GL_TRIANGLES, rect.getSolidIndices().size()*sizeof(unsigned int),
GL_UNSIGNED_INT, &(rect.getSolidIndices()[0]));
glFinish();
vao[RECT].release();
// Now for circle:
vao[CIRCLE].bind();
vao[CIRCLE].vbo[POS].bind();
Game.setMVP( id );
Game.setColor( Colour(1.0, 0.0, 0.0) );
glDrawElements(GL_TRIANGLES, circle.getSolidIndices().size()*sizeof(unsigned int),
GL_UNSIGNED_INT, &(circle.getSolidIndices()[0]));
glFlush();
}
I've tried reading the data from the buffers before rendering (they are distinct and what I would expect for each primitive), so I know the write occurred properly. I'm guessing now I might be binding the buffers wrong, or missing a step while rendering.

Rendering mesh polygons in OpenGL - very slow

I recently switched from intermediate mode and have a new rendering process. There must be something I am not understanding. I think it has something to do with the indices.
Here is my diagram: Region->Mesh->Polygon Array->3 vertex indices which references the master list of vertices.
Here my render code:
// Render the mesh
void WLD::render(GLuint* textures, long curRegion, CFrustum cfrustum)
{
int num = 0;
// Set up rendering states
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Set up my indices
GLuint indices[3];
// Cycle through the PVS
while(num < regions[curRegion].visibility.size())
{
int i = regions[curRegion].visibility[num];
// Make sure the region is not "dead"
if(!regions[i].dead && regions[i].meshptr != NULL)
{
// Check to see if the mesh is in the frustum
if(cfrustum.BoxInFrustum(regions[i].meshptr->min[0], regions[i].meshptr->min[2], regions[i].meshptr->min[1], regions[i].meshptr->max[0], regions[i].meshptr->max[2], regions[i].meshptr->max[1]))
{
// Cycle through every polygon in the mesh and render it
for(int j = 0; j < regions[i].meshptr->polygonCount; j++)
{
// Assign the index for the polygon to the index in the huge vertex array
// This I think, is redundant
indices[0] = regions[i].meshptr->poly[j].vertIndex[0];
indices[1] = regions[i].meshptr->poly[j].vertIndex[1];
indices[2] = regions[i].meshptr->poly[j].vertIndex[2];
// Enable texturing and bind the appropriate texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[regions[i].meshptr->poly[j].tex]);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].u);
// Draw
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
}
}
}
num++;
}
// End of rendering - disable states
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
Sorry if I left anything out. And I really appreciate feedback and help with this. I would even consider paying someone who is good with OpenGL and optimization to help me with this.
There is no point in using array rendering if you're only rendering 3 vertices at a time. The idea is to send thousands through with a single call. That is, you render a single "Polygon Array" or "Mesh" with one call.