void MainScreen::onInit(ScreenContext& sc) { // Store engine ref mEngine = sc.GetEngine(); // Store file data cache ref mFileDataCache = sc.GetFileDataCache(); // Cube rotation state mRotationData.degreesInc = 0.05f; mRotationData.rotating = false; // Camera initial position mCamera.SetPos(glm::vec3(0, 0, 8)); // Add sample UV Sphere ModelData sphereModel = GenUVSphere(1, 32, 32); mEngine->GetModelStore().Load("4", std::move(sphereModel)); // Create world objects SetupWorld(); // Create world lights SetupLights(); // Cam shouldn't follow character initially mFollowingCharacter = false; // Initial choosen moving light mMovingLightIndex = 0; // Init character mCharacter.Init(&mEngine->GetWindow(), mScene.get()); // Load the skybox ImageLoader imLoader; auto& cubemapStore = mEngine->GetCubemapStore(); cubemapStore.Load(skybox, imLoader.Load(*(*mFileDataCache)["ext/Assets/Textures/Skybox/Bluesky/bluesky.tga"], "tga")); mEngine->GetSkyboxRenderer().SetCubemapId(cubemapStore[skybox]->id); // Load the irr map mEngine->GetCubemapStore().Load(irrmap, imLoader.Load(*(*mFileDataCache)["ext/Assets/Textures/Skybox/Bluesky/bluesky_irr.tga"], "tga")); // Load the rad map for (unsigned int i = 0; i < 9; ++i) { mEngine->GetCubemapStore().Load( radmap, imLoader.Load(*(*mFileDataCache)[ "ext/Assets/Textures/Skybox/Bluesky/bluesky_rad_" + std::to_string(i) + ".tga"], "tga"), i); } // Do not show AABBs by default mShowAABBs = false; // Do not show Debug info by default mShowDbgInfo = false; // Init renderform creator mRenderformCreator = std::make_unique<RenderformCreator>(&(mEngine->GetModelStore()), &(mEngine->GetMaterialStore())); }
void CDefFile::loadFrame(size_t frame, size_t group, ImageLoader &loader) const { std::map<size_t, std::vector <size_t> >::const_iterator it; it = offset.find(group); assert (it != offset.end()); const ui8 * FDef = data+it->second[frame]; const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef); SSpriteDef sprite; sprite.format = read_le_u32(&sd.format); sprite.fullWidth = read_le_u32(&sd.fullWidth); sprite.fullHeight = read_le_u32(&sd.fullHeight); sprite.width = read_le_u32(&sd.width); sprite.height = read_le_u32(&sd.height); sprite.leftMargin = read_le_u32(&sd.leftMargin); sprite.topMargin = read_le_u32(&sd.topMargin); ui32 currentOffset = sizeof(SSpriteDef); ui32 BaseOffset = sizeof(SSpriteDef); loader.init(Point(sprite.width, sprite.height), Point(sprite.leftMargin, sprite.topMargin), Point(sprite.fullWidth, sprite.fullHeight), palette); switch (sprite.format) { case 0: { //pixel data is not compressed, copy data to surface for (ui32 i=0; i<sprite.height; i++) { loader.Load(sprite.width, FDef[currentOffset]); currentOffset += sprite.width; loader.EndLine(); } break; } case 1: { //for each line we have offset of pixel data const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+currentOffset); currentOffset += sizeof(ui32) * sprite.height; for (ui32 i=0; i<sprite.height; i++) { //get position of the line currentOffset=BaseOffset + read_le_u32(RWEntriesLoc + i); ui32 TotalRowLength = 0; while (TotalRowLength<sprite.width) { ui8 type=FDef[currentOffset++]; ui32 length=FDef[currentOffset++] + 1; if (type==0xFF)//Raw data { loader.Load(length, FDef + currentOffset); currentOffset+=length; } else// RLE { loader.Load(length, type); } TotalRowLength += length; } loader.EndLine(); } break; } case 2: { currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset); for (ui32 i=0; i<sprite.height; i++) { ui32 TotalRowLength=0; while (TotalRowLength<sprite.width) { ui8 SegmentType=FDef[currentOffset++]; ui8 code = SegmentType / 32; ui8 length = (SegmentType & 31) + 1; if (code==7)//Raw data { loader.Load(length, FDef[currentOffset]); currentOffset += length; } else//RLE { loader.Load(length, code); } TotalRowLength+=length; } loader.EndLine(); } break; } case 3: { for (ui32 i=0; i<sprite.height; i++) { currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset+i*2*(sprite.width/32)); ui32 TotalRowLength=0; while (TotalRowLength<sprite.width) { ui8 segment = FDef[currentOffset++]; ui8 code = segment / 32; ui8 length = (segment & 31) + 1; if (code==7)//Raw data { loader.Load(length, FDef + currentOffset); currentOffset += length; } else//RLE { loader.Load(length, code); } TotalRowLength += length; } loader.EndLine(); } break; } default: logGlobal->errorStream()<<"Error: unsupported format of def file: "<<sprite.format; break; } };
void* TextureProcessorMain(std::vector<std::string>* args) { auto start = std::chrono::high_resolution_clock::now(); std::string filename; int numArgs = args->size(); if (numArgs > 0) { filename = (*args)[0]; } else { cout << "ERROR : You must specify a filename."; cin.get(); } if (numArgs > 1) { for (int i = 1; i < numArgs; ++i) { std::string argument = (*args)[i]; if (argument.find("texformat") != string::npos) { if (numArgs >= i + 1) texFormat = (TEXTURE_FORMAT)atoi((*args)[i + 1].c_str()); else cout << "Please provide a valid texture format."; } else if (argument.find("mipmaps") != string::npos) { generateMipmaps = true; } else if (argument.find("memory") != string::npos) { useMemory = true; } else if (argument.find("reverse") != string::npos) { reverseChannels = true; } else if (argument.find("flipvertical") != string::npos) { flipVertically = true; } } } string extension = filename.substr(filename.find(".") + 1); ImageLoader* loader = NULL; if (extension == "jpg" || extension == "jpeg") { loader = new JpegLoader(); } else if (extension == "png") { loader = new PngLoader(); } else if (extension == "bmp") { loader = new BmpLoader(); } /*ilInit(); ilEnable(IL_FILE_OVERWRITE); ilEnable(IL_ORIGIN_SET); ilSetInteger(IL_ORIGIN_MODE, IL_ORIGIN_UPPER_LEFT); */ auto startLoad = std::chrono::high_resolution_clock::now(); loader->Load(filename, reverseChannels == true ? ImageLoader::PixelChannelOrder::ORDER_BGR : ImageLoader::PixelChannelOrder::ORDER_RGB); if (flipVertically) loader->FlipVertically(); auto endLoad = std::chrono::high_resolution_clock::now(); long long timeLoad = std::chrono::duration_cast<std::chrono::milliseconds>(endLoad - startLoad).count(); //TextureLoader loader = TextureLoader(filename.c_str()); //std::cout << "timeLoad : " << timeLoad << "\n"; //loader.SetTextureFormat(texFormat); auto startMip = std::chrono::high_resolution_clock::now(); if(generateMipmaps) loader->GenerateMipmaps(); auto endMip = std::chrono::high_resolution_clock::now(); long long timeMip = std::chrono::duration_cast<std::chrono::milliseconds>(endMip - startMip).count(); // std::cout << "timeMip : " << timeMip << "\n"; if (!useMemory) { // Texture loaded, now write a binary file. filename = strtok((char *)filename.c_str(), "."); // Remove the extension. char* extension = strtok(NULL, "."); // Get the new filename with extension .bbt filename = strtok((char*)filename.c_str(), "."); // Remove the extension. filename += ".bbt"; loader->WriteBinary(filename.c_str(), generateMipmaps); delete loader; } else // Memory. { /*auto startMem = std::chrono::high_resolution_clock::now(); int totalSize = jpegLoader->GetTotalSizeToWrite(); char* memory = AllocateMem(totalSize); jpegLoader->WriteMemory(memory, generateMipmaps); auto endMem = std::chrono::high_resolution_clock::now(); auto timeMem = std::chrono::duration_cast<std::chrono::milliseconds>(endMem - startMem).count(); auto end = std::chrono::high_resolution_clock::now(); auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); SetBinary(memory); return memory; */ return loader; } return 0; }
void MaterialScreen::onInit(ScreenContext& sc) { // Store engine ref mEngine = sc.GetEngine(); // Store file data cache ref mFileDataCache = sc.GetFileDataCache(); // Add sample UV Sphere ModelData sphereModel = GenUVSphere(1, 32, 32); mEngine->GetModelStore().Load("sphere", std::move(sphereModel)); // Create scene mScene = std::make_unique<Scene>(); // Create materials int id = 0; for (int metallic = 0; metallic <= 1; ++metallic) { for (float roughness = 0.0f; roughness < 1.0f; roughness += 0.1f) { for (float reflectivity = 0.0f; reflectivity < 1.0; reflectivity += 0.1f) { // Create and store the material std::string materialName; materialName += metallic == 1 ? "m" : "d"; materialName += "r" + std::to_string(roughness); materialName += "f" + std::to_string(reflectivity); Material m; m.SetMetallic(static_cast<float>(metallic)); m.SetRoughness(roughness); m.SetFresnel(reflectivity); m.SetDiffuseColor(glm::vec3(255.0f, 0.0f, 0.0f)); mEngine->GetMaterialStore().Load(materialName, m); // Create and store the object that will be made of the material previously defined SceneNode* node = mScene->CreateNode( "sphere", {materialName}, std::to_string(id), Category::Normal, sphereModel.boundingBox ); node->Move(glm::vec3(roughness * 20.0f -10.0f + 22.0f * metallic, reflectivity * 20.0f - 10.0f, 0.0f)); ++id; } } } // Setup scene lights Lights& lights = mEngine->GetRenderer().GetLights(); // Add directional light DirLight dirLight; dirLight.direction = glm::vec3(-0.3f, -0.5f, -0.5f); dirLight.color = glm::vec3(0.9f); lights.dirLights.push_back(dirLight); // Camera initial position mCamera.SetPos(glm::vec3(-6, 8, 12)); mCamera.Look(std::make_tuple(450.0f, 450.0f)); // Load the skybox ImageLoader imLoader; auto& cubemapStore = mEngine->GetCubemapStore(); cubemapStore.Load(skybox, imLoader.Load(*(*mFileDataCache)["ext/Assets/Textures/Skybox/Indoors/indoors.tga"], "tga")); mEngine->GetSkyboxRenderer().SetCubemapId(cubemapStore[skybox]->id); // Load the irr map mEngine->GetCubemapStore().Load(irrmap, imLoader.Load(*(*mFileDataCache)["ext/Assets/Textures/Skybox/Indoors/indoors_irr.tga"], "tga")); // Load the rad map for (unsigned int i = 0; i < 9; ++i) { mEngine->GetCubemapStore().Load( radmap, imLoader.Load(*(*mFileDataCache)[ "ext/Assets/Textures/Skybox/Indoors/indoors_rad_" + std::to_string(i) + ".tga"], "tga"), i); } // Init renderform creator mRenderformCreator = std::make_unique<RenderformCreator>(&(mEngine->GetModelStore()), &(mEngine->GetMaterialStore())); }