void AssimpImporter::GetMaterialTexture( IZ_UINT nMtrlIdx, IZ_UINT nTexIdx, izanagi::S_MTRL_TEXTURE& sTex) { IZ_ASSERT(nMtrlIdx < m_scene->mNumMaterials); const auto mtrl = m_scene->mMaterials[nMtrlIdx]; sTex.type.flags = 0; IZ_UINT texPos = 0; for (IZ_UINT i = 0; i < AI_TEXTURE_TYPE_MAX; i++) { aiTextureType type = (aiTextureType)i; auto cnt = mtrl->GetTextureCount(type); if (texPos <= nTexIdx && nTexIdx < texPos + cnt) { aiString path; auto idx = nTexIdx - texPos; mtrl->GetTexture( type, idx, &path); sTex.name.SetString(path.C_Str()); sTex.key = sTex.name.GetKeyValue(); switch (type) { case aiTextureType::aiTextureType_DIFFUSE: // Nothing to do. break; case aiTextureType::aiTextureType_NORMALS: sTex.type.isNormal = IZ_TRUE; break; case aiTextureType::aiTextureType_SPECULAR: sTex.type.isSpecular = IZ_TRUE; break; case aiTextureType::aiTextureType_OPACITY: sTex.type.isTranslucent = IZ_TRUE; break; case aiTextureType::aiTextureType_REFLECTION: sTex.type.isEnvironment = IZ_TRUE; break; default: IZ_ASSERT(IZ_FALSE); break; } break; } texPos += cnt; } }
Mesh::Mesh(const std::string & modelName, aiMesh * mesh, const aiScene * scene) { static gl::TextureManager manager; std::vector<Vertex> vertices; std::vector<GLuint> indices; if (!mesh->HasTextureCoords(0)) { throw std::runtime_error("Mesh does not have texture coords"); } auto material = scene->mMaterials[mesh->mMaterialIndex]; bool useTexture = false; if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0) { useTexture = true; aiString path; material->GetTexture(aiTextureType_DIFFUSE, 0, &path); // TODO: Passing a const char * instead of a std::string texture_ = &manager.loadTexture(locationAppend(modelName, path.C_Str())); } for (unsigned i = 0; i < mesh->mNumVertices; ++i) { glm::vec2 uv; if (useTexture) { uv = {mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y}; } vertices.emplace_back(Vertex{ {mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z}, {mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z}, uv }); } for (unsigned i = 0; i < mesh->mNumFaces; ++i) { auto face = mesh->mFaces[i]; for (unsigned j = 0; j < face.mNumIndices; ++j) { indices.emplace_back(face.mIndices[j]); } } numTriangles_ = indices.size(); vao_.gen(); glBindVertexArray(vao_); vbo_.gen(); glBindBuffer(GL_ARRAY_BUFFER, vbo_); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW); ebo_.gen(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, normal)); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, uv)); glEnableVertexAttribArray(2); glBindVertexArray(0); }
void CheckStar(_lp_vertex star, int idx) { if (idx < _num_stars) { star->z++; if (star->z > nearest) { star->x = (rand() % Width) - (Width / 2); star->y = (rand() % Height) - (Height / 2); star->z = (rand() % nearest); star->tex_type = -1; //set to not have texture by default star->id = -1; int sidx = (rand() % GetTextureCount()); if (GetTextureCount() == 1) sidx = 0; star->tex_type = GetTextureType(sidx); star->id = GetTextureID(sidx); } } }
IZ_BOOL AssimpImporter::GetMaterial( IZ_UINT nMtrlIdx, izanagi::S_MTRL_MATERIAL& sMtrl) { IZ_ASSERT(nMtrlIdx < m_scene->mNumMaterials); const auto mtrl = m_scene->mMaterials[nMtrlIdx]; // name { aiString str; auto result = mtrl->Get("?mat.name", 0, 0, str); IZ_ASSERT(result == aiReturn::aiReturn_SUCCESS); sMtrl.name.SetString(str.C_Str()); sMtrl.keyMaterial = sMtrl.name.GetKeyValue(); } sMtrl.numTex = 0; for (IZ_UINT i = 0; i < AI_TEXTURE_TYPE_MAX; i++) { sMtrl.numTex += mtrl->GetTextureCount((aiTextureType)i); } sMtrl.numParam = 0; sMtrl.paramBytes = 0; IZ_CHAR buf[32] = { 0 }; std::vector<aiMaterialProperty*> props; for (IZ_UINT i = 0; i < mtrl->mNumProperties; i++) { auto prop = mtrl->mProperties[i]; if (prop->mType != aiPropertyTypeInfo::aiPTI_String) { ::sprintf_s(buf, "%s\0", prop->mKey.C_Str()); const char* name = nullptr; for (IZ_UINT n = 0; n < strlen(buf); n++) { if (buf[n] == '.') { name = &buf[n + 1]; break; } } IZ_ASSERT(name); // TODO if (strcmp(name, "diffuse") == 0) { sMtrl.paramBytes += prop->mDataLength; props.push_back(prop); } else if (strcmp(name, "specular") == 0) { sMtrl.paramBytes += prop->mDataLength; props.push_back(prop); } else if (strcmp(name, "ambient") == 0) { sMtrl.paramBytes += prop->mDataLength; props.push_back(prop); } } } if (props.size() > 0) { sMtrl.numParam = props.size(); m_props.insert(std::make_pair(nMtrlIdx, props)); } return IZ_TRUE; }
std::vector<Vertex> ModelLoader::load(int& numTriangles, int& numTextures) { std::ifstream fileCheck(filename); Vertex tempVert; std::vector<Vertex> geometry; std::deque<aiVector3D> texCoord; numTriangles = 0; numTextures = 0; for(int i = 0; i < 3; i++) tempVert.color[i] = 1.0; if(!fileCheck) { std::cerr << "Error: Unable to open object file" << std::endl; exit(-1); } fileCheck.close(); Assimp::Importer importer; auto scene = importer.ReadFile(filename, aiProcessPreset_TargetRealtime_Fast); if(!scene) { std::cerr << "Error: " << importer.GetErrorString() << std::endl; exit(-1); } std::cout << "Material Count: " << scene->mNumMaterials << std::endl; if(scene->HasMaterials()) { const auto& material = scene->mMaterials[0]; aiColor3D color(0.0f,0.0f,0.0); material->Get(AI_MATKEY_COLOR_DIFFUSE, color); tempVert.color[0] = color.r; tempVert.color[1] = color.g; tempVert.color[2] = color.b; } for(unsigned int i = 0; i < scene->mNumMeshes; i++) { auto mesh = scene->mMeshes[i]; if(scene->mNumMaterials > i+1) { auto material = scene->mMaterials[i+1]; aiColor3D color(1.0f,1.0f,1.0f); material->Get(AI_MATKEY_COLOR_DIFFUSE, color); tempVert.color[0] = color.r; tempVert.color[1] = color.g; tempVert.color[2] = color.b; if(material->GetTextureCount(aiTextureType_DIFFUSE) > 0) { aiString path; if(material->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) { std::cout << "Texture Path: " << path.C_Str() << std::endl; numTextures++; loadTexture(path.C_Str()); /* for(unsigned int j = 0; j < mesh->mNumVertices; j++) { const auto textureCoords = mesh->HasTextureCoords(0) ? mesh->mTextureCoords[0][j] : aiVector3D(0.0f,0.0f,0.0f); //texCoord.emplace_back(textureCoords); } */ } } } numTriangles += mesh->mNumFaces; for(unsigned int j = 0; j < mesh->mNumFaces; j++) { const auto& face = mesh->mFaces[j]; for(unsigned int k = 0; k < face.mNumIndices; k++) { const auto& vertex = mesh->mVertices[face.mIndices[k]]; tempVert.position[0] = vertex.x; tempVert.position[1] = vertex.y; tempVert.position[2] = vertex.z; const auto& textureVertex = mesh->HasTextureCoords(0) ? mesh->mTextureCoords[0][face.mIndices[k]] : aiVector3D(0,0,0); tempVert.textCoord[0] = textureVertex[0]; tempVert.textCoord[1] = textureVertex[1]; geometry.push_back(tempVert); } } } //loadTexture("checkerboard.jpg"); std::cout << "size: " << geometry.size() << std::endl << "bytes: " << sizeof(tempVert) * geometry.size() << std::endl; return geometry; }
NS_IMETHODIMP WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* handleReport, nsISupports* data, bool) { #define REPORT(_path, _kind, _units, _amount, _desc) \ do { \ nsresult rv; \ rv = handleReport->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \ _kind, _units, _amount, \ NS_LITERAL_CSTRING(_desc), data); \ NS_ENSURE_SUCCESS(rv, rv); \ } while (0) REPORT("webgl-texture-memory", KIND_OTHER, UNITS_BYTES, GetTextureMemoryUsed(), "Memory used by WebGL textures.The OpenGL" " implementation is free to store these textures in either video" " memory or main memory. This measurement is only a lower bound," " actual memory usage may be higher for example if the storage" " is strided."); REPORT("webgl-texture-count", KIND_OTHER, UNITS_COUNT, GetTextureCount(), "Number of WebGL textures."); REPORT("webgl-buffer-memory", KIND_OTHER, UNITS_BYTES, GetBufferMemoryUsed(), "Memory used by WebGL buffers. The OpenGL" " implementation is free to store these buffers in either video" " memory or main memory. This measurement is only a lower bound," " actual memory usage may be higher for example if the storage" " is strided."); REPORT("explicit/webgl/buffer-cache-memory", KIND_HEAP, UNITS_BYTES, GetBufferCacheMemoryUsed(), "Memory used by WebGL buffer caches. The WebGL" " implementation caches the contents of element array buffers" " only.This adds up with the webgl-buffer-memory value, but" " contrary to it, this one represents bytes on the heap," " not managed by OpenGL."); REPORT("webgl-buffer-count", KIND_OTHER, UNITS_COUNT, GetBufferCount(), "Number of WebGL buffers."); REPORT("webgl-renderbuffer-memory", KIND_OTHER, UNITS_BYTES, GetRenderbufferMemoryUsed(), "Memory used by WebGL renderbuffers. The OpenGL" " implementation is free to store these renderbuffers in either" " video memory or main memory. This measurement is only a lower" " bound, actual memory usage may be higher for example if the" " storage is strided."); REPORT("webgl-renderbuffer-count", KIND_OTHER, UNITS_COUNT, GetRenderbufferCount(), "Number of WebGL renderbuffers."); REPORT("explicit/webgl/shader", KIND_HEAP, UNITS_BYTES, GetShaderSize(), "Combined size of WebGL shader ASCII sources and translation" " logs cached on the heap."); REPORT("webgl-shader-count", KIND_OTHER, UNITS_COUNT, GetShaderCount(), "Number of WebGL shaders."); REPORT("webgl-context-count", KIND_OTHER, UNITS_COUNT, GetContextCount(), "Number of WebGL contexts."); #undef REPORT return NS_OK; }
FbxModelData* FBXLoader::loadModel( const char* aFile ) { FBX_LOG("FBXLoader Creating ModelData..."); myLoadingModel = new FbxModelData; FBX_LOG("Success!"); FBX_LOG("FBXLoader Creating TextureData..."); myLoadingModel->myTextureData = new TextureData(); FBX_LOG("Success!"); FBX_LOG("FBXLoader Loading Scene..."); auto scene = LoadScene(aFile); FBX_LOG("Successfully loaded scene!"); FBX_LOG("FBXLoader Loading Textures..."); //TextureData const int lTextureCount = scene->GetTextureCount(); for (int lTextureIndex = 0; lTextureIndex < lTextureCount; ++lTextureIndex) { FbxTexture * lTexture = scene->GetTexture(lTextureIndex); FbxFileTexture * lFileTexture = reinterpret_cast<FbxFileTexture*>(lTexture); if (lFileTexture && !lFileTexture->GetUserDataPtr()) { const FbxString lFileName = lFileTexture->GetFileName(); unsigned int lTextureObject = 0; lTextureObject; bool lStatus = false; lStatus; const FbxString lAbsFbxFileName = FbxPathUtils::Resolve(aFile); const FbxString lAbsFolderName = FbxPathUtils::GetFolderName(lAbsFbxFileName); const FbxString lTextureFileName = lAbsFolderName + "\\" + lFileTexture->GetRelativeFileName();// FbxPathUtils::GetFileName(lFileName); const FbxString lResolvedFileName = lAbsFolderName + "\\" + FbxPathUtils::GetFileName(lFileName);// lFileTexture->GetRelativeFileName();;// FbxPathUtils::Bind(lAbsFolderName, lTextureFileName); TextureInfo info; info.myFileName = lResolvedFileName; //info.myFileName += "\\"; info.myFileName = lFileTexture->GetRelativeFileName(); myLoadingModel->myTextureData->myTextures.push_back(info); lFileTexture->SetFileName(lResolvedFileName); } } FBX_LOG("Success!"); FBX_LOG("FBXLoader Loading Animations..."); FbxArray<FbxString*> animationNames; FbxArray<FbxPose*> poses; scene->FillAnimStackNameArray(animationNames); scene->FillPoseArray(poses); FbxAnimStack * lCurrentAnimationStack = nullptr; FbxAnimLayer* lCurrentAnimLayer = nullptr; if(animationNames.GetCount() > 0) { lCurrentAnimationStack = scene->FindMember<FbxAnimStack>(animationNames[0]->Buffer()); if (lCurrentAnimationStack != NULL) { lCurrentAnimLayer = lCurrentAnimationStack->GetMember<FbxAnimLayer>(); } } //lCurrentAnimLayer->IsR myLoadingModel->myAnimation = new AnimationData(); FbxPose* pose = nullptr; if(poses.GetCount() > 0) { pose = poses[0]; } LoadAnimation(*myLoadingModel->myAnimation,scene->GetRootNode(),FbxAMatrix(),pose, lCurrentAnimLayer,-1); LoadNodeRecursive(myLoadingModel, *myLoadingModel->myAnimation, scene->GetRootNode(), FbxAMatrix(), pose, lCurrentAnimLayer, -1); FBX_LOG("Success!"); return myLoadingModel; }