#include "model.hpp"
+#include "error.hpp"
#include <iostream>
#include <assimp/postprocess.h>
#include <glm/gtc/type_ptr.hpp>
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)) {
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);
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++) {
void Model::Node::draw( const std::vector<Mesh> &meshes,
const std::vector<Material> &materials,
+ const Skybox skybox,
glm::mat4 parentTrans = glm::mat4(1)) const {
GLuint modelLoc = glGetUniformLocation(progId, "model");
Material material = materials[mesh.materialIndex];
material.bind();
+ glUniform1i(glGetUniformLocation(progId, "skybox"), 5);
+ glActiveTexture(GL_TEXTURE5);
+ glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getTexture());
+
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, Program p): program(p) {
+Model::Model(const std::string &path, Program p, Skybox s): program(p), skybox(s) {
+ glUseProgram(p.progId);
+
const aiScene *scene = importer.ReadFile(path,
aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals);
if (!scene) {
void Model::draw() const {
glUseProgram(program.progId);
- root->draw(meshes, materials);
+ root->draw(meshes, materials, skybox);
+ program.validate();
}
Model::Node* Model::find(const std::string &name) {