//-------------------------------------------
void ofxAssimpModelLoader::getBoundingBoxForNode(const struct aiNode* nd,  struct aiVector3D* min, struct aiVector3D* max, struct aiMatrix4x4* trafo)
{
	struct aiMatrix4x4 prev;
	unsigned int n = 0, t;

	prev = *trafo;
	aiMultiplyMatrix4(trafo,&nd->mTransformation);

	for (; n < nd->mNumMeshes; ++n){
		const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
		for (t = 0; t < mesh->mNumVertices; ++t){
        	struct aiVector3D tmp = mesh->mVertices[t];
			aiTransformVecByMatrix4(&tmp,trafo);


			min->x = MIN(min->x,tmp.x);
			min->y = MIN(min->y,tmp.y);
			min->z = MIN(min->z,tmp.z);

			max->x = MAX(max->x,tmp.x);
			max->y = MAX(max->y,tmp.y);
			max->z = MAX(max->z,tmp.z);
		}
	}

	for (n = 0; n < nd->mNumChildren; ++n){
		this->getBoundingBoxForNode(nd->mChildren[n], min, max, trafo);
	}

	*trafo = prev;
}
Ejemplo n.º 2
0
void get_bounding_box_for_node (const struct aiScene *sc,
                                const struct aiNode* nd,
                                struct aiVector3D* min,
                                struct aiVector3D* max,
                                struct aiMatrix4x4* trafo)
{
	struct aiMatrix4x4 prev;
	unsigned int n = 0, t;
    
	prev = *trafo;
	aiMultiplyMatrix4(trafo,&nd->mTransformation);
    
	for (; n < nd->mNumMeshes; ++n) {
		const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]];
		for (t = 0; t < mesh->mNumVertices; ++t) {
            
			struct aiVector3D tmp = mesh->mVertices[t];
			aiTransformVecByMatrix4(&tmp,trafo);
            
			min->x = aisgl_min(min->x,tmp.x);
			min->y = aisgl_min(min->y,tmp.y);
			min->z = aisgl_min(min->z,tmp.z);
            
			max->x = aisgl_max(max->x,tmp.x);
			max->y = aisgl_max(max->y,tmp.y);
			max->z = aisgl_max(max->z,tmp.z);
		}
	}
    
	for (n = 0; n < nd->mNumChildren; ++n) {
		get_bounding_box_for_node(sc, nd->mChildren[n],min,max,trafo);
	}
	*trafo = prev;
}
/*!
*  \brief      Go through the scene nodes and load and transform all meshes them.
*  \author     Sascha Kaden
*  \param[in]  scene
*  \param[in]  node
*  \param[in]  transformation
*  \param[out] vector of meshes
*  \date       2017-05-09
*/
void getMeshes(const aiScene *scene, const aiNode *node, aiMatrix4x4 *trafo, std::vector<Mesh> &meshes, const bool useTrafo) {
    aiMatrix4x4 prevTrafo;

    prevTrafo = *trafo;
    aiMultiplyMatrix4(trafo, &node->mTransformation);

    for (size_t i = 0; i < node->mNumMeshes; ++i) {
        Mesh mesh;
        const aiMesh *aimesh = scene->mMeshes[node->mMeshes[i]];
        for (size_t j = 0; j < aimesh->mNumVertices; ++j) {
            aiVector3D vertex = aimesh->mVertices[j];
            if (useTrafo)
                aiTransformVecByMatrix4(&vertex, trafo);
            mesh.vertices.emplace_back(vertex.x, vertex.y, vertex.z);
        }
        for (size_t j = 0; j < aimesh->mNumFaces; ++j) {
            if (aimesh->mFaces[j].mNumIndices > 2) {
                mesh.faces.emplace_back(aimesh->mFaces[j].mIndices[0], aimesh->mFaces[j].mIndices[1], aimesh->mFaces[j].mIndices[2]);
            } else {
                Logging::warning("Face array is to short", "CadProcessing");
            }
        }
        meshes.push_back(mesh);
    }

    for (size_t i = 0; i < node->mNumChildren; ++i) {
        getMeshes(scene, node->mChildren[i], trafo, meshes);
    }
    *trafo = prevTrafo;
}
Ejemplo n.º 4
0
void AssimpScene::recursive_getBoundingBox(
	const aiScene *scene, const aiNode* nd,
	aiVector3D & sceneMin, aiVector3D & sceneMax, aiMatrix4x4* sceneMatrix)
{
	aiMatrix4x4 sceneMatrixOriginal = *sceneMatrix;
	aiMultiplyMatrix4(sceneMatrix, & nd->mTransformation);
	for (int n=0; n < nd->mNumMeshes; n++) {
		aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
		for (int t = 0; t < mesh->mNumVertices; t++) {
			aiVector3D vert = mesh->mVertices[t];
			aiTransformVecByMatrix4(&vert, sceneMatrix);
			sceneMin.x = std::min(sceneMin.x, vert.x);
			sceneMin.y = std::min(sceneMin.y, vert.y);
			sceneMin.z = std::min(sceneMin.z, vert.z);

			sceneMax.x = std::max(sceneMax.x, vert.x);
			sceneMax.y = std::max(sceneMax.y, vert.y);
			sceneMax.z = std::max(sceneMax.z, vert.z);

		}
	}
	//std::cout << "AssimpScene SceneMin " << sceneMin.x << " , " << sceneMin.y << " , " << sceneMin.z << std::endl;
	//std::cout << "AssimpScene SceneMax " << sceneMax.x << " , " << sceneMax.y << " , " << sceneMax.z << std::endl;
	for (int n = 0; n < nd->mNumChildren; n++) {
		recursive_getBoundingBox(scene,nd->mChildren[n], sceneMin, sceneMax,sceneMatrix);
	}
	*sceneMatrix = sceneMatrixOriginal;
}
Ejemplo n.º 5
0
void AssimpSimpleModelLoader::get_bounding_box_for_node (const aiNode* nd, 
	aiVector3D* min, 
	aiVector3D* max, 
	aiMatrix4x4* trafo
){
	aiMatrix4x4 prev;
	unsigned int n = 0, t;

	prev = *trafo;
	aiMultiplyMatrix4(trafo,&nd->mTransformation);

	for (; n < nd->mNumMeshes; ++n) {
		const aiMesh* mesh = this->scene->mMeshes[nd->mMeshes[n]];
		for (t = 0; t < mesh->mNumVertices; ++t) {

			aiVector3D tmp = mesh->mVertices[t];
			aiTransformVecByMatrix4(&tmp,trafo);

			min->x = aisgl_min(min->x,tmp.x);
			min->y = aisgl_min(min->y,tmp.y);
			min->z = aisgl_min(min->z,tmp.z);

			max->x = aisgl_max(max->x,tmp.x);
			max->y = aisgl_max(max->y,tmp.y);
			max->z = aisgl_max(max->z,tmp.z);
		}
	}

	for (n = 0; n < nd->mNumChildren; ++n) {
		get_bounding_box_for_node(nd->mChildren[n],min,max,trafo);
	}
	*trafo = prev;
}
Ejemplo n.º 6
0
void Model3D::get_bounding_box_for_node(const struct aiNode* nd, struct aiVector3D* min, struct aiVector3D* max, struct aiMatrix4x4* trafo) {
    struct aiMatrix4x4 prev; // Use struct keyword to show you want struct version of this, not normal typedef?
    unsigned int n = 0, t;

    prev = *trafo;
    aiMultiplyMatrix4(trafo, &nd->mTransformation);

    for (; n < nd->mNumMeshes; ++n) {
        const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
        for (t = 0; t < mesh->mNumVertices; ++t) {
            struct aiVector3D tmp = mesh->mVertices[t];
            aiTransformVecByMatrix4(&tmp, trafo);

            min->x = aisgl_min(min->x,tmp.x);
            min->y = aisgl_min(min->y,tmp.y);
            min->z = aisgl_min(min->z,tmp.z);

            max->x = aisgl_max(max->x,tmp.x);
            max->y = aisgl_max(max->y,tmp.y);
            max->z = aisgl_max(max->z,tmp.z);
        }
    }

    for (n = 0; n < nd->mNumChildren; ++n)
        get_bounding_box_for_node(nd->mChildren[n], min, max, trafo);

    *trafo = prev;
}
Ejemplo n.º 7
0
Archivo: assiqe.c Proyecto: r-lyeh/eve
void bake_mesh_skin(const struct aiMesh *mesh)
{
	int i, k, b;
	struct aiMatrix3x3 mat3;
	struct aiMatrix4x4 bonemat[1000], mat;
	struct aiVector3D *outpos, *outnorm;

	if (mesh->mNumBones == 0)
		return;

	outpos = malloc(mesh->mNumVertices * sizeof *outpos);
	outnorm = malloc(mesh->mNumVertices * sizeof *outnorm);
	memset(outpos, 0, mesh->mNumVertices * sizeof *outpos);
	memset(outnorm, 0, mesh->mNumVertices * sizeof *outpos);

	calc_abs_pose();

	for (i = 0; i < mesh->mNumBones; i++) {
		b = find_bone(mesh->mBones[i]->mName.data);
		bonemat[i] = bonelist[b].abspose;
		aiMultiplyMatrix4(&bonemat[i], &mesh->mBones[i]->mOffsetMatrix);
	}

	for (k = 0; k < mesh->mNumBones; k++) {
		struct aiBone *bone = mesh->mBones[k];
		b = find_bone(mesh->mBones[k]->mName.data);
		mat = bonemat[k];
		mat3.a1 = mat.a1; mat3.a2 = mat.a2; mat3.a3 = mat.a3;
		mat3.b1 = mat.b1; mat3.b2 = mat.b2; mat3.b3 = mat.b3;
		mat3.c1 = mat.c1; mat3.c2 = mat.c2; mat3.c3 = mat.c3;
		for (i = 0; i < bone->mNumWeights; i++) {
			struct aiVertexWeight vw = bone->mWeights[i];
			int v = vw.mVertexId;
			float w = vw.mWeight;
			struct aiVector3D srcpos = mesh->mVertices[v];
			struct aiVector3D srcnorm = mesh->mNormals[v];
			aiTransformVecByMatrix4(&srcpos, &mat);
			aiTransformVecByMatrix3(&srcnorm, &mat3);
			outpos[v].x += srcpos.x * w;
			outpos[v].y += srcpos.y * w;
			outpos[v].z += srcpos.z * w;
			outnorm[v].x += srcnorm.x * w;
			outnorm[v].y += srcnorm.y * w;
			outnorm[v].z += srcnorm.z * w;
		}
	}

	memcpy(mesh->mVertices, outpos, mesh->mNumVertices * sizeof *outpos);
	memcpy(mesh->mNormals, outnorm, mesh->mNumVertices * sizeof *outnorm);

	free(outpos);
	free(outnorm);
}
Ejemplo n.º 8
0
void InstanceStaticModelData::CreateSubmesh(unsigned int Index, const aiMesh* paiMesh, const aiNode* nd, const aiScene* pScene)
{
	InstancedMeshSubset* subset = new InstancedMeshSubset();

	Vertex* meshVertices = new Vertex[paiMesh->mNumVertices];
	unsigned int* meshIndices = new unsigned int[paiMesh->mNumFaces * 3];

	const aiMaterial* pMaterial = pScene->mMaterials[paiMesh->mMaterialIndex];

	if (pMaterial != nullptr)
	{
		// diffuse texture

		if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0)
		{
			aiString Path;

			if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, nullptr, nullptr, nullptr, nullptr, nullptr) == AI_SUCCESS)
			{
				std::string FullPath = Path.data;
				FullPath = "../Data/Textures/Diffuse/" + FullPath;
				Texture2D* tex2d = TextureManager::LoadTexture2D(FullPath.c_str());

				if (tex2d != nullptr)
				{
					Texture tex;
					tex.textureID = tex2d->GetID();
					tex.textureType = TEXTURE_DIFFUSE;
					subset->AddTexture(tex);
				}
				
			}
		}
		else
		{

			std::string FullPath = "../Data/Textures/Diffuse/noTexture.jpg";
			Texture2D* tex2d = TextureManager::LoadTexture2D(FullPath.c_str());
			Texture tex;
			tex.textureID = tex2d->GetID();
			tex.textureType = TEXTURE_DIFFUSE;
			subset->AddTexture(tex);
		}

		// normal texture
		if (pMaterial->GetTextureCount(aiTextureType_NORMALS) > 0)
		{
			aiString Path;

			if (pMaterial->GetTexture(aiTextureType_NORMALS, 0, &Path, nullptr, nullptr, nullptr, nullptr, nullptr) == AI_SUCCESS)
			{

				/*std::string FullPath = Path.data;

				mesh->m_Entries[Index].normalTexture = new D3D9Texture();
				if (!mesh->m_Entries[Index].normalTexture->Load("texture/" + FullPath, m_D3DDevice))
				{
				delete mesh->m_Entries[Index].normalTexture;
				mesh->m_Entries[Index].normalTexture = normalTexture;
				}*/
			}
		}

	}


	std::vector<Vertex> vertices;
	std::vector<GLuint> indices;

	const aiVector3D Zero3D(0.0f, 0.0f, 0.0f);
	aiMatrix4x4 trans = nd->mTransformation;
	for (unsigned int i = 0; i < paiMesh->mNumVertices; i++)
	{
		aiVector3D* pPos = &(paiMesh->mVertices[i]);
		aiTransformVecByMatrix4(pPos, &trans);
		const aiVector3D* pNormal = &(paiMesh->mNormals[i]);
		const aiVector3D* pTangent = &(paiMesh->mTangents[i]);
		const aiVector3D* pBinormal = &(paiMesh->mBitangents[i]);
		const aiVector3D* pTexCoord = &paiMesh->mTextureCoords[0][i];

		Vertex v(
			glm::vec3(pPos->x, pPos->y, pPos->z),
			glm::vec2(pTexCoord->x, pTexCoord->y),
			glm::vec3(pNormal->x, pNormal->y, pNormal->z),
			glm::vec3(pTangent->x, pTangent->y, pTangent->z),
			glm::vec3(pBinormal->x, pBinormal->y, pBinormal->z));

		vertices.push_back(v);

	}

	unsigned int index = 0;
	for (unsigned int i = 0; i < paiMesh->mNumFaces; i++)
	{
		const aiFace& Face = paiMesh->mFaces[i];
		assert(Face.mNumIndices == 3);
		indices.push_back(Face.mIndices[0]);
		indices.push_back(Face.mIndices[1]);
		indices.push_back(Face.mIndices[2]);

		index += 3;
	}


	subset->Setup(vertices, indices);

	boundingbox.CreateFromPoints((glm::vec3*)&vertices[0], vertices.size(), sizeof(Vertex));
	meshSubsets.push_back(subset);

}
Ejemplo n.º 9
0
Archivo: assiqe.c Proyecto: r-lyeh/eve
void export_node(FILE *out, const struct aiScene *scene, const struct aiNode *node,
	struct aiMatrix4x4 mat, char *clean_name)
{
	struct aiMatrix3x3 mat3;
	int i, a, k, t;

	aiMultiplyMatrix4(&mat, &node->mTransformation);
	mat3.a1 = mat.a1; mat3.a2 = mat.a2; mat3.a3 = mat.a3;
	mat3.b1 = mat.b1; mat3.b2 = mat.b2; mat3.b3 = mat.b3;
	mat3.c1 = mat.c1; mat3.c2 = mat.c2; mat3.c3 = mat.c3;

	if (!strstr(node->mName.data, "$ColladaAutoName$"))
		clean_name = clean_node_name((char*)node->mName.data);

	if (only_one_node && strcmp(clean_name, only_one_node))
		goto skip_mesh;

	for (i = 0; i < node->mNumMeshes; i++) {
		struct aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
		struct aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];

		if (mesh->mNumBones == 0 && dobone && !dorigid) {
			if (verbose)
				fprintf(stderr, "skipping rigid mesh %d in node %s (no bones)\n", i, clean_name);
			continue;
		}

		fprintf(stderr, "exporting mesh %s[%d]: %d vertices, %d faces\n",
				clean_name, i, mesh->mNumVertices, mesh->mNumFaces);

		fprintf(out, "\n");
		fprintf(out, "mesh \"%s\"\n", clean_name);
		fprintf(out, "material \"%s\"\n", find_material(material));

		struct vb *vb = (struct vb*) malloc(mesh->mNumVertices * sizeof(*vb));
		memset(vb, 0, mesh->mNumVertices * sizeof(*vb));

		// A rigidly animated node -- insert fake blend index/weights
		if (mesh->mNumBones == 0 && dobone) {
			a = find_bone((char*)node->mName.data);
			if (verbose)
				fprintf(stderr, "\trigid bone %d for mesh in node %s (no bones)\n", bonelist[a].number, node->mName.data);
			for (k = 0; k < mesh->mNumVertices; k++) {
				vb[k].b[0] = bonelist[a].number;
				vb[k].w[0] = 1;
				vb[k].n = 1;
			}
		}

		// Assemble blend index/weight array
		for (k = 0; k < mesh->mNumBones; k++) {
			struct aiBone *bone = mesh->mBones[k];
			a = find_bone(bone->mName.data);
			for (t = 0; t < bone->mNumWeights; t++) {
				struct aiVertexWeight *w = mesh->mBones[k]->mWeights + t;
				int idx = w->mVertexId;
				if (vb[idx].n < MAXBLEND) {
					vb[idx].b[vb[idx].n] = bonelist[a].number;
					vb[idx].w[vb[idx].n] = w->mWeight;
					vb[idx].n++;
				}
			}
		}

		for (k = 0; k < mesh->mNumVertices; k++) {
			struct aiVector3D vp = mesh->mVertices[k];
			if (!dobone)
				aiTransformVecByMatrix4(&vp, &mat);
			fprintf(out, "vp %.9g %.9g %.9g\n", vp.x, vp.y, vp.z);
			if (mesh->mNormals) {
				struct aiVector3D vn = mesh->mNormals[k];
				if (!dobone)
					aiTransformVecByMatrix3(&vn, &mat3);
				fprintf(out, "vn %.9g %.9g %.9g\n", vn.x, vn.y, vn.z);
			}

			if (mesh->mTextureCoords[0]) {
				float u = mesh->mTextureCoords[0][k].x;
				float v = 1 - mesh->mTextureCoords[0][k].y;
				fprintf(out, "vt %.9g %.9g\n", u, v);
			}
			for (t = 1; t <= MAX_UVMAP; t++) {
				if (mesh->mTextureCoords[t]) {
					float u = mesh->mTextureCoords[t][k].x;
					float v = 1 - mesh->mTextureCoords[t][k].y;
					fprintf(out, "v%d %.9g %.9g\n", FIRST_UVMAP+t-1, u, v);
				}
			}

			if (mesh->mColors[0]) {
				float r = mesh->mColors[0][k].r; r = floorf(r * 255) / 255;
				float g = mesh->mColors[0][k].g; g = floorf(g * 255) / 255;
				float b = mesh->mColors[0][k].b; b = floorf(b * 255) / 255;
				float a = mesh->mColors[0][k].a; a = floorf(a * 255) / 255;
				fprintf(out, "vc %.9g %.9g %.9g %.9g\n", r, g, b, a);
			}
			for (t = 1; t <= MAX_COL; t++) {
				if (mesh->mColors[t]) {
					float r = mesh->mColors[t][k].r; r = floorf(r * 255) / 255;
					float g = mesh->mColors[t][k].g; g = floorf(g * 255) / 255;
					float b = mesh->mColors[t][k].b; b = floorf(b * 255) / 255;
					float a = mesh->mColors[t][k].a; a = floorf(a * 255) / 255;
					fprintf(out, "v%d %.9g %.9g %.9g %.9g\n", FIRST_COL+t-1, r, g, b, a);
				}
			}

			if (dobone) {
				fprintf(out, "vb");
				for (t = 0; t < vb[k].n; t++) {
					fprintf(out, " %d %.9g", vb[k].b[t], vb[k].w[t]);
				}
				fprintf(out, "\n");
			}
		}

		for (k = 0; k < mesh->mNumFaces; k++) {
			struct aiFace *face = mesh->mFaces + k;
			if (face->mNumIndices == 3) {
				if (doflip)
					fprintf(out, "fm %d %d %d\n", face->mIndices[2], face->mIndices[1], face->mIndices[0]);
				else
					fprintf(out, "fm %d %d %d\n", face->mIndices[0], face->mIndices[1], face->mIndices[2]);
			} else if (face->mNumIndices == 4) {
				if (doflip)
					fprintf(out, "fm %d %d %d %d\n", face->mIndices[3], face->mIndices[2], face->mIndices[1], face->mIndices[0]);
				else
					fprintf(out, "fm %d %d %d %d\n", face->mIndices[0], face->mIndices[1], face->mIndices[2], face->mIndices[3]);
			} else if (face->mNumIndices > 4) {
				fprintf(stderr, "n-gon (%d) in mesh!\n", face->mNumIndices);
				int i1 = face->mIndices[0];
				int i2 = face->mIndices[1];
				for (a = 2; a < face->mNumIndices; a++) {
					int i3 = face->mIndices[a];
					if (doflip)
						fprintf(out, "fm %d %d %d\n", i3, i2, i1);
					else
						fprintf(out, "fm %d %d %d\n", i1, i2, i3);
					i2 = i3;
				}
			} else {
				fprintf(stderr, "skipping point/line primitive\n");
			}
		}

		free(vb);
	}

skip_mesh:

	for (i = 0; i < node->mNumChildren; i++)
		export_node(out, scene, node->mChildren[i], mat, clean_name);
}