PDA

View Full Version : Mesh loaded with Assimp not Rendering?



Arko4576
01-26-2014, 11:44 AM
I've been trying to load a 3ds model with Assimp, but once I get to the point of rendering nothing appears,
below is the rendering code I'm trying to use due to the original not rendering for me and below that is the mesh and texture loading code. So I my question in the end would be why is it not rendering?


void Core::RenderMesh(const ModelInstance& inst)
{
ModelAsset* asset = inst.asset;
Rendering::Program* shaders = asset->shaders;

shaders->use();

shaders->setUniform("camera", gCamera.matrix());
shaders->setUniform("model", inst.transform);
shaders->setUniform("tex", 0);

glEnable(GL_VERTEX_ARRAY);
glEnable(GL_NORMAL_ARRAY);
glEnable(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 0,asset->mesh.vertexArray);
glNormalPointer(GL_FLOAT, 0, asset->mesh.normalArray);

glActiveTexture(GL_TEXTURE0_ARB);
glTexCoordPointer(2, GL_FLOAT, 0, asset->mesh.uvArray);

glBindVertexArray(asset->vao);
glDrawArrays(GL_TRIANGLES,0,asset->mesh.numTriangles);

glDisable(GL_VERTEX_ARRAY);
glDisable(GL_NORMAL_ARRAY);
glDisable(GL_TEXTURE_COORD_ARRAY);

shaders->stopUsing();
}


void Mesh::LoadMesh(std::string filename)
{
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(filename, aiProcessPreset_TargetRealtime_Fast);

aiMesh *mesh = scene->mMeshes[0];

numTriangles = mesh->mNumFaces * 3;
int index = 0;
numUvCoords = mesh->GetNumUVChannels();

vertexArray = new float[mesh->mNumFaces * 3 * 3];
normalArray = new float[mesh->mNumFaces * 3 * 3];
uvArray = new float[mesh->mNumFaces * 3 * 2];

for (unsigned int i = 0; i < mesh->mNumFaces; i++)
{
const aiFace& face = mesh->mFaces[i];

//foreach index
for (int j = 0; j < 3; j++)//assume all faces are triangulated
{
aiVector3D uv = mesh->mTextureCoords[0][face.mIndices[j]];
memcpy(uvArray, &uv, sizeof(float)* 2);
uvArray += 2;

aiVector3D normal = mesh->mNormals[face.mIndices[j]];
memcpy(normalArray, &normal, sizeof(float)* 3);
normalArray += 3;

aiVector3D pos = mesh->mVertices[face.mIndices[j]];
memcpy(vertexArray, &pos, sizeof(float)* 3);
vertexArray += 3;
}
}

uvArray -= mesh->mNumFaces * 3 * 2;
normalArray -= mesh->mNumFaces * 3 * 3;
vertexArray -= mesh->mNumFaces * 3 * 3;
}

Dark Photon
01-27-2014, 06:49 AM
I think you may have a mistaken assumption about how VAOs work. The first time, you bind them, set up your vertex array and index bindings and enables, and draw. Subsequent times, you just bind the VAO and then draw (it caches all the vertex array and index bindings and enables).

In other words, its possible that your VAO binding right before you draw but "after" you set up your vtx/idx bindings/enables may be messing up your vertex attribute/index list bindings and enables.

Arko4576
01-31-2014, 03:18 PM
You were right! I had data getting added and messing up my texture coords and vertex coords.