void Lux::Core::Transform::ApplyTransform() { if (m_TransformDirty) { m_TransformMatrix = mat4x4(1.0f); m_TransformMatrix *= toMat4(m_LocalRotation); m_TransformMatrix = translate(m_TransformMatrix, m_Position); m_TransformMatrix *= toMat4(m_Rotation); m_TransformMatrix = scale(m_TransformMatrix, m_Scale); // Calculate forward vector m_Forward = vec3(0, 0, -1) * m_LocalRotation; // Calculate Right vector m_Right = vec3(1, 0, 0) * m_LocalRotation; // Calculate Up vector m_Up = vec3(0, 1, 0) * m_LocalRotation; m_InverseTranslationMatrix = m_TransformMatrix; m_InverseTranslationMatrix[3][0] *= -1; m_InverseTranslationMatrix[3][1] *= -1; m_InverseTranslationMatrix[3][2] *= -1; m_TransformDirty = false; } }
void Entity :: updateTransform(){ btTransform trans; btVector3 tmpVec3Min; btVector3 tmpVec3Max; //properties->motionState->setWorldTransform properties->motionState->getWorldTransform(trans); /* properties->rigidBody->getAabb(tmpVec3Min, tmpVec3Max); bounds[0] = vec3((float)tmpVec3Min.getX(), (float)tmpVec3Min.getY(), (float)tmpVec3Min.getZ()); bounds[1] = vec3((float)tmpVec3Max.getX(), (float)tmpVec3Min.getY(), (float)tmpVec3Min.getZ()); bounds[2] = vec3((float)tmpVec3Min.getX(), (float)tmpVec3Min.getY(), (float)tmpVec3Max.getZ()); bounds[3] = vec3((float)tmpVec3Max.getX(), (float)tmpVec3Min.getY(), (float)tmpVec3Max.getZ()); bounds[4] = vec3((float)tmpVec3Min.getX(), (float)tmpVec3Max.getY(), (float)tmpVec3Min.getZ()); bounds[5] = vec3((float)tmpVec3Max.getX(), (float)tmpVec3Max.getY(), (float)tmpVec3Min.getZ()); bounds[6] = vec3((float)tmpVec3Min.getX(), (float)tmpVec3Max.getY(), (float)tmpVec3Max.getZ()); bounds[7] = vec3((float)tmpVec3Max.getX(), (float)tmpVec3Max.getY(), (float)tmpVec3Max.getZ()); */ vec3 _position = vec3( (float)trans.getOrigin().getX(), (float)trans.getOrigin().getY() , (float)trans.getOrigin().getZ() ); setPosition(_position); btQuaternion tmpQuat = trans.getRotation(); vec4 s = vec4(tmpQuat.getX(), tmpQuat.getY(), tmpQuat.getZ(), tmpQuat.getW() ); rotation = toMat4( quat(s.x, s.y, s.z, s.w) ); transform = translate * rotation * scale; }
void Actor::tick(float deltaTime) { GameObject::tick(deltaTime); // Generate the object matrix objectMatrix = mat4(1.0f); objectMatrix = glm::scale(objectMatrix, scale); objectMatrix = toMat4(rotation) * objectMatrix; objectMatrix = glm::translate(mat4(1.0f), location) * objectMatrix; }
void core::Model::Animation::buildBoneTree(const aiScene *scene, aiNode *node, BoneNode *bNode, Model *m){ if(scene->HasAnimations()){ if(m->boneID.find(node->mName.data) != m->boneID.end()){ BoneNode tempNode; tempNode.name = node->mName.data; tempNode.parent = bNode; tempNode.nodeTransform = toMat4(&node->mTransformation); tempNode.boneTransform = boneOffset[tempNode.name]; bNode->children.push_back(tempNode); } if(node->mNumChildren > 0){ for(unsigned int x = 0; x < node->mNumChildren; x++){ if(m->boneID.find(node->mName.data) != m->boneID.end()) buildBoneTree(scene, node->mChildren[x], &bNode->children[bNode->children.size() - 1], m); else buildBoneTree(scene, node->mChildren[x], bNode, m); } } } };
void Scene::Command(const std::vector<std::string>& strings, const std::vector<float>& f) { if (strings.size() == 0) return; std::string c = strings[0]; if (c == "screen") { // syntax: screen width height // realtime->setScreen(int(f[1]),int(f[2])); width = int(f[1]); height = int(f[2]); } else if (c == "camera") { // syntax: camera x y z ry <orientation spec> // Eye position (x,y,z), view orientation (qw qx qy qz), frustum height ratio ry // realtime->setCamera(Vector3f(f[1],f[2],f[3]), Orientation(5,strings,f), f[4]); camera->eye = Vector3f(f[1], f[2], f[3]); camera->orient = Orientation(5, strings, f); camera->ry = f[4]; } else if (c == "ambient") { // syntax: ambient r g b // Sets the ambient color. Note: This parameter is temporary. // It will be ignored once your raytracer becomes capable of // accurately *calculating* the true ambient light. // realtime->setAmbient(Vector3f(f[1], f[2], f[3])); ambient = Vector3f(f[1], f[2], f[3]); } else if (c == "brdf") { // syntax: brdf r g b r g b alpha // later: brdf r g b r g b alpha r g b ior // First rgb is Diffuse reflection, second is specular reflection. // third is beer's law transmission followed by index of refraction. // Creates a Material instance to be picked up by successive shapes currentMat = new BRDF(Vector3f(f[1], f[2], f[3]), Vector3f(f[4], f[5], f[6]), f[7], Vector3f(f[8], f[9], f[10]), f[11]); } else if (c == "light") { // syntax: light r g b // The rgb is the emission of the light // Creates a Material instance to be picked up by successive shapes currentMat = new Light(Vector3f(f[1], f[2], f[3])); } else if (c == "sphere") { // syntax: sphere x y z r // Creates a Shape instance for a sphere defined by a center and radius // realtime->sphere(Vector3f(f[1], f[2], f[3]), f[4], currentMat); shapes.push_back(new Sphere(Vector3f(f[1], f[2], f[3]), f[4], currentMat)); if (currentMat->isLight()) emitters.push_back(shapes.back()); } else if (c == "box") { // syntax: box bx by bz dx dy dz // Creates a Shape instance for a box defined by a corner point and diagonal vector // realtime->box(Vector3f(f[1], f[2], f[3]), Vector3f(f[4], f[5], f[6]), currentMat); shapes.push_back(new Box(Vector3f(f[1], f[2], f[3]), Vector3f(f[4], f[5], f[6]), currentMat)); if (currentMat->isLight()) emitters.push_back(shapes.back()); } else if (c == "cylinder") { // syntax: cylinder bx by bz ax ay az r // Creates a Shape instance for a cylinder defined by a base point, axis vector, and radius // realtime->cylinder(Vector3f(f[1], f[2], f[3]), Vector3f(f[4], f[5], f[6]), f[7], currentMat); shapes.push_back(new Cylinder(Vector3f(f[1], f[2], f[3]), Vector3f(f[4], f[5], f[6]), f[7], currentMat)); if (currentMat->isLight()) emitters.push_back(shapes.back()); } else if (c == "mesh") { // syntax: mesh filename tx ty tz s <orientation> // Creates many Shape instances (one per triangle) by reading // model(s) from filename. All triangles are rotated by a // quaternion (qw qx qy qz), uniformly scaled by s, and // translated by (tx ty tz) . Matrix4f modelTr = translate(Vector3f(f[2],f[3],f[4])) *scale(Vector3f(f[5],f[5],f[5])) *toMat4(Orientation(6,strings,f)); ReadAssimpFile(strings[1], modelTr); } else { fprintf(stderr, "\n*********************************************\n"); fprintf(stderr, "* Unknown command: %s\n", c.c_str()); fprintf(stderr, "*********************************************\n\n"); } }
void core::ModelLoader::processMesh(const aiScene *scene, aiNode *node, aiMesh *mesh, Model *m){ Model::Mesh tempMesh; tempMesh.weights.resize(mesh->mNumVertices); std::fill(tempMesh.weights.begin(), tempMesh.weights.end(), glm::vec4(1.f)); tempMesh.boneID.resize(mesh->mNumVertices); std::fill(tempMesh.boneID.begin(), tempMesh.boneID.end(), glm::vec4(-333.f)); tempMesh.baseModelMatrix = toMat4(&node->mTransformation); if(node->mParent != NULL) tempMesh.baseModelMatrix *= toMat4(&node->mParent->mTransformation); for(unsigned x = 0; x < mesh->mNumVertices; x++){ glm::vec3 tempV; tempV.x = mesh->mVertices[x].x; tempV.y = mesh->mVertices[x].y; tempV.z = mesh->mVertices[x].z; tempMesh.vertices.push_back(tempV); glm::vec2 tempUV; tempUV.x = mesh->mTextureCoords[0][x].x; tempUV.y = mesh->mTextureCoords[0][x].y; tempMesh.uvs.push_back(tempUV); if(mesh->HasNormals()){ glm::vec3 tempN; tempN.x = mesh->mNormals[x].x; tempN.y = mesh->mNormals[x].y; tempN.z = mesh->mNormals[x].z; } } for(unsigned int x = 0; x < mesh->mNumFaces; x++){ tempMesh.indices.push_back(mesh->mFaces[x].mIndices[0]); tempMesh.indices.push_back(mesh->mFaces[x].mIndices[1]); tempMesh.indices.push_back(mesh->mFaces[x].mIndices[2]); } if(scene->HasMaterials()){ aiMaterial *mat = scene->mMaterials[mesh->mMaterialIndex]; if(mat->GetTextureCount(aiTextureType_DIFFUSE) > 0){ aiString path; mat->GetTexture(aiTextureType_DIFFUSE, 0, &path); std::string finalFP = m->rootPath + path.C_Str(); tempMesh.image.loadFromFile(finalFP); } } if(mesh->HasBones()){ for(std::size_t x = 0; x < mesh->mNumBones; x++){ unsigned int index = 0; if(m->boneID.find(mesh->mBones[x]->mName.data) == m->boneID.end()) index = m->boneID.size(); else index = m->boneID[mesh->mBones[x]->mName.data]; m->boneID[mesh->mBones[x]->mName.data] = index; for(std::size_t y = 0; y < m->animations[m->currentAnim].channels.size(); y++) if(m->animations[m->currentAnim].channels[y].name == mesh->mBones[x]->mName.data) m->animations[m->currentAnim].boneOffset[mesh->mBones[x]->mName.data] = toMat4(&mesh->mBones[x]->mOffsetMatrix); for(std::size_t y = 0; y < mesh->mBones[x]->mNumWeights; y++){ unsigned int vertexID = mesh->mBones[x]->mWeights[y].mVertexId; if(tempMesh.boneID[vertexID].x == -333){ tempMesh.boneID[vertexID].x = (float)index; tempMesh.weights[vertexID].x = mesh->mBones[x]->mWeights[y].mWeight; }else if(tempMesh.boneID[vertexID].y == -333){ tempMesh.boneID[vertexID].y = (float)index; tempMesh.weights[vertexID].y = mesh->mBones[x]->mWeights[y].mWeight; }else if(tempMesh.boneID[vertexID].z == -333){ tempMesh.boneID[vertexID].z = (float)index; tempMesh.weights[vertexID].z = mesh->mBones[x]->mWeights[y].mWeight; }else if(tempMesh.boneID[vertexID].w == -333){ tempMesh.boneID[vertexID].w = (float)index; tempMesh.weights[vertexID].w = mesh->mBones[x]->mWeights[y].mWeight; } } } } m->meshes.push_back(tempMesh); };
Mat4 Quat::getMat4() { Mat4 result; toMat4(result); return result; }