X-Git-Url: http://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=model.cpp;h=ee1159532728a0a834705fa9de8eb9dd91c35932;hp=8046e29434fcf467b29942dc56aa5b60b76b84cf;hb=511a2c92fcb9dda82dd5d38b91ea03790d0cb7b2;hpb=ba9c738e8660304aa0341eb44118e63502a4a009 diff --git a/model.cpp b/model.cpp index 8046e29..ee11595 100644 --- a/model.cpp +++ b/model.cpp @@ -17,7 +17,8 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { aiVector3D v = aiMesh->mNormals[i]; normals.push_back(glm::vec3(v.x, v.y, v.z)); } else { - normals.push_back(glm::vec3(0)); + std::cerr << "Missing normals" << std::endl; + exit(1); } if (aiMesh->HasTangentsAndBitangents()) { aiVector3D t = aiMesh->mTangents[i]; @@ -25,8 +26,8 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { aiVector3D b = aiMesh->mBitangents[i]; bitangents.push_back(glm::vec3(b.x, b.y, b.z)); } else { - tangents.push_back(glm::vec3(0)); - bitangents.push_back(glm::vec3(0)); + std::cerr << "Missing tangents: make sure blender has UV maps" << std::endl; + exit(1); } // check for texture coord set 0 if (aiMesh->HasTextureCoords(0)) { @@ -59,13 +60,13 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { GLuint vertexVbo = vbos[0], normalVbo = vbos[1], texCoordVbo = vbos[2], indicesVbo = vbos[3]; GLuint tangentVbo = vbos[4], bitangentVbo = vbos[5]; - GLuint posLoc = glGetAttribLocation(progId, "vPosition"); + GLuint posLoc = glGetAttribLocation(progId, "pos"); glBindBuffer(GL_ARRAY_BUFFER, vertexVbo); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(posLoc); glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); - GLuint normalLoc = glGetAttribLocation(progId, "vNormal"); + GLuint normalLoc = glGetAttribLocation(progId, "unscaledNormal"); glBindBuffer(GL_ARRAY_BUFFER, normalVbo); glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW); glEnableVertexAttribArray(normalLoc); @@ -77,21 +78,21 @@ Model::Mesh::Mesh(const aiMesh *aiMesh, GLuint progId) { glEnableVertexAttribArray(texCoordLoc); glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0); - GLuint tangentLoc = glGetAttribLocation(progId, "tangent"); - glBindBuffer(GL_ARRAY_BUFFER, tangentVbo); - glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW); - glEnableVertexAttribArray(tangentLoc); - glVertexAttribPointer(tangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); + /* GLuint tangentLoc = glGetAttribLocation(progId, "tangent"); */ + /* glBindBuffer(GL_ARRAY_BUFFER, tangentVbo); */ + /* glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW); */ + /* glEnableVertexAttribArray(tangentLoc); */ + /* glVertexAttribPointer(tangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); */ - GLuint bitangentLoc = glGetAttribLocation(progId, "bitangent"); - glBindBuffer(GL_ARRAY_BUFFER, bitangentVbo); - glBufferData(GL_ARRAY_BUFFER, bitangents.size() * sizeof(glm::vec3), &bitangents[0], GL_STATIC_DRAW); - glEnableVertexAttribArray(bitangentLoc); - glVertexAttribPointer(bitangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); + /* GLuint bitangentLoc = glGetAttribLocation(progId, "bitangent"); */ + /* glBindBuffer(GL_ARRAY_BUFFER, bitangentVbo); */ + /* glBufferData(GL_ARRAY_BUFFER, bitangents.size() * sizeof(glm::vec3), &bitangents[0], GL_STATIC_DRAW); */ + /* glEnableVertexAttribArray(bitangentLoc); */ + /* glVertexAttribPointer(bitangentLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); */ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVbo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW); -}; +} Model::Node::Node(const aiNode &node, GLuint progId): ai(node), progId(progId) { for (int i = 0; i < node.mNumMeshes; i++) { @@ -113,6 +114,7 @@ glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) { void Model::Node::draw( const std::vector &meshes, const std::vector &materials, + const Skybox skybox, glm::mat4 parentTrans = glm::mat4(1)) const { GLuint modelLoc = glGetUniformLocation(progId, "model"); @@ -125,14 +127,28 @@ void Model::Node::draw( const std::vector &meshes, Material material = materials[mesh.materialIndex]; material.bind(); + glUniform1i(glGetUniformLocation(progId, "irradianceMap"), 4); + glActiveTexture(GL_TEXTURE4); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getIrradianceMap()); + + glUniform1i(glGetUniformLocation(progId, "prefilterMap"), 5); + glActiveTexture(GL_TEXTURE5); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getPrefilterMap()); + + glUniform1i(glGetUniformLocation(progId, "brdfMap"), 6); + glActiveTexture(GL_TEXTURE6); + glBindTexture(GL_TEXTURE_2D, skybox.getBRDFMap()); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m)); glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0); } - for (Node *child: children) child->draw(meshes, materials, m); + for (Node *child: children) child->draw(meshes, materials, skybox, m); } -Model::Model(const std::string &path, GLuint progId): progId(progId) { +Model::Model(const std::string &path, Program p): program(p) { + glUseProgram(p.progId); + const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals); if (!scene) { @@ -142,19 +158,21 @@ Model::Model(const std::string &path, GLuint progId): progId(progId) { for (int i = 0; i < scene->mNumMeshes; i++) { const aiMesh *mesh = scene->mMeshes[i]; - meshes.push_back(Mesh(mesh, progId)); + meshes.push_back(Mesh(mesh, p.progId)); } for (unsigned int i = 0; i < scene->mNumMaterials; i++) { const aiMaterial &material = *scene->mMaterials[i]; - materials.push_back(Material(material, progId)); + materials.push_back(Material(material, p.progId)); } - root = new Node(*(scene->mRootNode), progId); + root = new Node(*(scene->mRootNode), p.progId); } -void Model::draw() const { - root->draw(meshes, materials); +void Model::draw(Skybox skybox) const { + glUseProgram(program.progId); + root->draw(meshes, materials, skybox); + program.validate(); } Model::Node* Model::find(const std::string &name) {