void OpenGLRenderInterface::drawMesh(const Eigen::Vector3d& _scale, const aiScene* _mesh) { if(_mesh) { glPushMatrix(); glScaled(_scale(0), _scale(1), _scale(2)); recursiveRender(_mesh, _mesh->mRootNode); glPopMatrix(); } }
void Model::drawModel(){ LOGGER_ENTER("Model", "drawModel"); glPushMatrix(); glScalef(modelScale, modelScale, modelScale); recursiveRender(model->mRootNode); glPopMatrix(); LOGGER_EXIT; }
// This function is taken from the examples coming with assimp void OpenGLRenderInterface::recursiveRender(const struct aiScene *sc, const struct aiNode* nd) { unsigned int i; unsigned int n = 0, t; aiMatrix4x4 m = nd->mTransformation; // update transform aiTransposeMatrix4(&m); glPushMatrix(); glMultMatrixf((float*)&m); // draw all meshes assigned to this node for (; n < nd->mNumMeshes; ++n) { const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]]; glPushAttrib(GL_POLYGON_BIT | GL_LIGHTING_BIT); // for applyMaterial() if(mesh->mMaterialIndex != (unsigned int)(-1)) // -1 is being used by us to indicate no material applyMaterial(sc->mMaterials[mesh->mMaterialIndex]); if(mesh->mNormals == nullptr) { glDisable(GL_LIGHTING); } else { glEnable(GL_LIGHTING); } for (t = 0; t < mesh->mNumFaces; ++t) { const struct aiFace* face = &mesh->mFaces[t]; GLenum face_mode; switch(face->mNumIndices) { case 1: face_mode = GL_POINTS; break; case 2: face_mode = GL_LINES; break; case 3: face_mode = GL_TRIANGLES; break; default: face_mode = GL_POLYGON; break; } glBegin(face_mode); for (i = 0; i < face->mNumIndices; i++) { int index = face->mIndices[i]; if(mesh->mColors[0] != nullptr) glColor4fv((GLfloat*)&mesh->mColors[0][index]); if(mesh->mNormals != nullptr) glNormal3fv(&mesh->mNormals[index].x); glVertex3fv(&mesh->mVertices[index].x); } glEnd(); } glPopAttrib(); // for applyMaterial() } // draw all children for (n = 0; n < nd->mNumChildren; ++n) { recursiveRender(sc, nd->mChildren[n]); } glPopMatrix(); }
void Model::recursiveRender(CommandBuffer* cmd, const ModelNode& node) { for (auto& node_data : node.mesh_data) { auto& mesh = _meshData[node_data.mesh_index]; cmd->bindDescriptorSet(node_data.model_descriptor_set.get()); cmd->drawIndexed(mesh.vertex_count, 1, mesh.vertex_offset, mesh.base_vertex, 0); cmd->unbindDescriptorSet(node_data.model_descriptor_set); } for (auto& child : node.child_nodes) { recursiveRender(cmd, *child); } }
//============================================================================== void OpenGLRenderInterface::drawMesh( const Eigen::Vector3d& scale, const aiScene* mesh) { if (!mesh) return; glPushMatrix(); glScaled(scale[0], scale[1], scale[2]); recursiveRender(mesh, mesh->mRootNode); glPopMatrix(); }
//---------------------------------------------------------------------------------------------- void DebugMultiPoly::recursiveRender(TriFragNode* currentNode) { glPushMatrix(); float maxTime = currentNode->keyframeTime.back(); float adjustedTime = fmodf(m_timeToLiveSeconds, maxTime); if (maxTime == 0.f) { adjustedTime = 0.f; //corrects for NaN } bool timeExceeded = false; int matrixFrame = -1; unsigned int keyframeIndex; for (keyframeIndex = 0; keyframeIndex < currentNode->transform.size() && !timeExceeded; keyframeIndex++) { if (adjustedTime < currentNode->keyframeTime[keyframeIndex]) { timeExceeded = true; } } glMultMatrixf(currentNode->transform[keyframeIndex-1]); //TODO reference the ttl seconds if (currentNode->sizeOfVBO > 0) { //TODO render thyself glBindBuffer(GL_ARRAY_BUFFER, currentNode->VBOID); //Note to self: this was moved from render() because it needs to be after bind buffer, but render is unsure if a VBO is being used currentNode->material->m_primaryLightSource = m_material.m_primaryLightSource; currentNode->material->m_lightBufferID = m_material.m_lightBufferID; currentNode->material->m_cameraPosition = m_material.m_cameraPosition; currentNode->material->m_shaderID = m_material.m_shaderID; currentNode->material->m_shaderProgram = m_material.m_shaderProgram; currentNode->material->prepareOpenGLForMaterial(); //m_material.prepareOpenGLForMaterial(); glDrawArrays(GL_TRIANGLES, 0, currentNode->sizeOfVBO); } glPopMatrix(); for (unsigned int triFragIndex = 0; triFragIndex < currentNode->children.size(); triFragIndex++) { recursiveRender(currentNode->children[triFragIndex]); } }
//---------------------------------------------------------------------------------------------- void DebugMultiPoly::render() { if(m_depthTest) { glEnable(GL_DEPTH_TEST); } else { glDisable(GL_DEPTH_TEST); } for(unsigned int TriFragIterator = 0; TriFragIterator < m_rootTriFragNodes.size(); TriFragIterator++) { recursiveRender(m_rootTriFragNodes[TriFragIterator]); } }
void PhotonMapper::render(Scene &scene) { int xRes = scene.camera.xRes(); int yRes = scene.camera.yRes(); setRes(xRes, yRes); //clear m_rgbaBuffer m_rgbaBuffer.reset(Math::Color4f(1.0,1,1,1.0)); //setup progress reporting using Platform::Progress //for each pixel generate a camera ray if (scene.photonMap == NULL && scene.specularPhotonMap == NULL) { scene.emit_scatterPhotons(); } Platform::Progress progress = Platform::Progress("Raytracing Image", xRes*yRes); for (int i=0; i < xRes; i++) { #pragma omp parallel for for (int j=0; j<yRes; j++) { Ray r = Ray(); scene.camera.generateRay(r, i, j); Math::Vec3f col = recursiveRender(r, *(scene.photonMap), *(scene.specularPhotonMap), scene, true); m_rgbaBuffer(i, j) = Math::Vec4f(col.x, col.y, col.z, 1.0); } progress.step(yRes); } //Copy the final rendering to the texture glBindTexture(GL_TEXTURE_2D, m_fbo.colorTextureID(0)); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_fbo.width(), m_fbo.height(), GL_RGBA, GL_FLOAT, &m_rgbaBuffer(0,0)); glBindTexture(GL_TEXTURE_2D, 0); //Render to Screen m_fbo.blitFramebuffer(FBO_COLOR0); // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // m_fbo.displayAlphaAsFullScreenTexture(FBO_COLOR0); }
void Model::render(CommandBuffer* cmd) { cmd->bindVertexArrayObject(_vertexArrayObject.get()); recursiveRender(cmd, _rootNode); }
void Model::recursiveRender(const aiNode* node){ LOGGER_ENTER("Model", "recursiveRender"); glPushMatrix(); int m = node->mNumMeshes; aiMatrix4x4 masterTransform = node->mTransformation; /* Iterate through the node's meshes */ for(int i = 0; i < m; i++){ aiMesh* mesh = model->mMeshes[node->mMeshes[i]]; Mesh m; if(gettingVertsAndNormals){ m.mNumVertices = mesh->mNumVertices; m.vertices = new glm::vec3[m.mNumVertices]; m.normals = new glm::vec3[m.mNumVertices]; m.numFaces = mesh->mNumFaces; m.faceVertexIndices = new int*[m.numFaces]; m.name = mesh->mName.C_Str(); } aiVector3D* vertices = mesh->mVertices; aiVector3D* normals = mesh->mNormals; /*** Calculate Skeleton Transformations and Resulting Vertex Trasformations ***/ if(mesh->HasBones()){ vertices = new aiVector3D[mesh->mNumVertices]; normals = new aiVector3D[mesh->mNumVertices]; for(unsigned int b = 0; b < mesh->mNumBones; b++){ aiMatrix4x4 boneMatrix; const aiBone* bone = mesh->mBones[b]; aiNode* boneNode = findNodeRecurse(model->mRootNode, bone->mName); boneMatrix = bone->mOffsetMatrix; const aiNode* tmpNode = boneNode; while(tmpNode){ boneMatrix = tmpNode->mTransformation * boneMatrix; tmpNode = tmpNode->mParent; } const aiMatrix4x4& posTransform = boneMatrix; const aiMatrix3x3 normalTransform = aiMatrix3x3(posTransform); for(unsigned int w = 0; w < bone->mNumWeights; w++){ const aiVertexWeight& weight = bone->mWeights[w]; unsigned int vertexID = weight.mVertexId; const aiVector3D& srcPos = mesh->mVertices[vertexID]; const aiVector3D& srcNormal = mesh->mNormals[vertexID]; vertices[vertexID] += weight.mWeight * (posTransform * srcPos); normals[vertexID] += weight.mWeight * (normalTransform * srcNormal); } } } /* Bind the texture if a diffuse texture exists */ aiMaterial* mat = model->mMaterials[mesh->mMaterialIndex]; int texIndex = 0; aiString texPath; if(AI_SUCCESS == mat->GetTexture(aiTextureType_DIFFUSE, texIndex, &texPath)) { unsigned int texId = textureIdMap[texPath.data]; glBindTexture(GL_TEXTURE_2D, texId); } aiString matName; mat->Get(AI_MATKEY_NAME,matName); aiColor3D matDiffuseColour(0,0,0); if(mat->Get(AI_MATKEY_COLOR_DIFFUSE,matDiffuseColour) == AI_SUCCESS){ float fMatDiffuseColour[] = {matDiffuseColour.r, matDiffuseColour.g, matDiffuseColour.b}; glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fMatDiffuseColour); } aiColor3D matSpecularColour(0,0,0); if(mat->Get(AI_MATKEY_COLOR_SPECULAR,matSpecularColour) == AI_SUCCESS){ float fMatSpecularColour[] = {matSpecularColour.r, matSpecularColour.g, matSpecularColour.b}; glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fMatSpecularColour); } aiColor3D matAmbientColour(0,0,0); if(mat->Get(AI_MATKEY_COLOR_AMBIENT,matAmbientColour) == AI_SUCCESS){ float fMatAmbientColour[] = {matAmbientColour.r, matAmbientColour.g, matAmbientColour.b}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, fMatAmbientColour); } /* Draw Mesh Faces (Textures) */ aiVector3D* texCoords = mesh->mTextureCoords[0]; for(unsigned int j = 0; j < mesh->mNumFaces; j++){ unsigned int* indices = mesh->mFaces[j].mIndices; aiVector3D n1, n2, n3, v1, v2, v3; if(mesh->HasBones()){ n1 = normals[indices[0]]; n2 = normals[indices[1]]; n3 = normals[indices[2]]; v1 = vertices[indices[0]]; v2 = vertices[indices[1]]; v3 = vertices[indices[2]]; }else{ n1 = MODEL_ROT_MAT * (masterTransform * normals[indices[0]]); n2 = MODEL_ROT_MAT * (masterTransform * normals[indices[1]]); n3 = MODEL_ROT_MAT * (masterTransform * normals[indices[2]]); v1 = MODEL_ROT_MAT * (masterTransform * vertices[indices[0]]); v2 = MODEL_ROT_MAT * (masterTransform * vertices[indices[1]]); v3 = MODEL_ROT_MAT * (masterTransform * vertices[indices[2]]); } glm::vec3 tmpV1(v1.x,v1.y,v1.z); glm::vec3 tmpV2(v2.x,v2.y,v2.z); glm::vec3 tmpV3(v3.x,v3.y,v3.z); glm::vec3 faceNormal = glm::normalize(glm::cross(tmpV3 - tmpV2, tmpV1 - tmpV2)); if(gettingVertsAndNormals){ glm::vec3 tmpN1(n1.x,n1.y,n1.z); glm::vec3 tmpN2(n2.x,n2.y,n2.z); glm::vec3 tmpN3(n3.x,n3.y,n3.z); m.vertices[indices[0]] = tmpV1; m.vertices[indices[1]] = tmpV2; m.vertices[indices[2]] = tmpV3; m.normals[indices[0]] = faceNormal; m.normals[indices[1]] = faceNormal; m.normals[indices[2]] = faceNormal; m.faceVertexIndices[j] = new int[3]; m.faceVertexIndices[j][0] = indices[0]; m.faceVertexIndices[j][1] = indices[1]; m.faceVertexIndices[j][2] = indices[2]; meshesMap[matName.C_Str()] = m; }else{ glBegin(GL_POLYGON); if(mesh->HasTextureCoords(0)) glTexCoord2f(texCoords[indices[0]].x, 1 - texCoords[indices[0]].y); glNormal3f(faceNormal.x, faceNormal.y, faceNormal.z); glVertex3f(v1.x, v1.y, v1.z); if(mesh->HasTextureCoords(0)) glTexCoord2f(texCoords[indices[1]].x, 1 - texCoords[indices[1]].y); glVertex3f(v2.x, v2.y, v2.z); if(mesh->HasTextureCoords(0)) glTexCoord2f(texCoords[indices[2]].x, 1 - texCoords[indices[2]].y); glVertex3f(v3.x, v3.y, v3.z); glEnd(); } } /* Free the memory that was used for the animated vertices and normals */ if(mesh->HasBones()){ free(normals); free(vertices); } /* Remove any texture binding */ glBindTexture(GL_TEXTURE_2D, NULL); } /* Recurse Through Child Nodes */ for(unsigned int i = 0; i < node->mNumChildren; i++){ recursiveRender(node->mChildren[i]); } glPopMatrix(); LOGGER_EXIT; }