GLuint FileAssociatedTexture::getOrCreate2D( const QString & fileName , OpenGLFunctions & gl , const GLenum wrap_s , const GLenum wrap_t , const GLenum mag_filter , const GLenum min_filter) { QFileInfo fi(fileName); if (!fi.exists()) { qWarning() << fileName << " does not exist: texture has no associated file."; return -1; } QString filePath(fi.absoluteFilePath()); if (s_texturesByFilePath.contains(filePath)) return s_texturesByFilePath[filePath]; QImage image = getOrCreateImage(filePath); if (image.isNull()) return -1; instance()->m_fileSystemWatcher->addPath(filePath); GLuint texture = loadTexture2D(-1, wrap_s, wrap_t, mag_filter, min_filter, image, gl); s_texturesByFilePath[filePath] = texture; return texture; }
void GL2TextureManager::update() { bool loadedSomething = false; GL2Texture* loadTexture; while (rawData.tryPop(loadTexture)) { loadTexture2D(loadTexture->imgData, *loadTexture); releaseStbiRawData(loadTexture->imgData); const u32 ram = loadTexture->getUsingRamMemory(); const u32 gpuMem = loadTexture->getUsingGPUMemory(); ramInUse += ram; gpuMemInUse += gpuMem; LOG(BitEngine::EngineLog, BE_LOG_VERBOSE) << loadTexture->getResourceId() << " RAM: " << BitEngine::BytesToMB(ram) << " MB - GPU Memory: " << BitEngine::BytesToMB(gpuMem) << " MB"; loadedSomething = true; finishedLoading(loadTexture->getMeta()); } if (loadedSomething) { LOG(BitEngine::EngineLog, BE_LOG_VERBOSE) << "TextureManager MEMORY: RAM: " << BitEngine::BytesToMB(getCurrentRamUsage()) << " MB - GPU Memory: " << BitEngine::BytesToMB(getCurrentGPUMemoryUsage()) << " MB"; } }
void Texture::loadCubemap(int flag) { size_t p = name.rfind("."); std::string ext = name.substr(p); std::string base = name.substr(0,p); for(unsigned int i = 0; i < 6; i++) loadTexture2D(base+CUBEMAPSUFFIXE[i]+ext, GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT+i, flag); }
void SkyBoxSurface::onStart(){ FileLog::getInstance()->init(); // 加载纹理 lpSkyBox = new SkyBox(SkyBox::TYPE_BOX); lpSkyBox->loadBmp(0, "/Users/lichsword/Documents/workspace_apple/others/nehe-tuts/Data/redsky/redsky_down.bmp"); lpSkyBox->loadBmp(1, "/Users/lichsword/Documents/workspace_apple/others/nehe-tuts/Data/redsky/redsky_up.bmp"); lpSkyBox->loadBmp(2, "/Users/lichsword/Documents/workspace_apple/others/nehe-tuts/Data/redsky/redsky_east.bmp"); lpSkyBox->loadBmp(3, "/Users/lichsword/Documents/workspace_apple/others/nehe-tuts/Data/redsky/redsky_west.bmp"); lpSkyBox->loadBmp(4, "/Users/lichsword/Documents/workspace_apple/others/nehe-tuts/Data/redsky/redsky_north.bmp"); lpSkyBox->loadBmp(5, "/Users/lichsword/Documents/workspace_apple/others/nehe-tuts/Data/redsky/redsky_south.bmp"); // 绑定纹理 int size = SkyBox::SIZE_BOX; // lpTextures = new GLuint[size]; glGenTextures(size, lpTextures); ErrorReport::getInstance()->print(""); Image * lpImages = lpSkyBox->getImageRef(); for(int i=0; i<size; i++){ loadTexture2D(&lpTextures[i], lpImages[i]); } // 使用2D纹理 glEnable(GL_TEXTURE_2D); // 使用平滑着色 glShadeModel(GL_SMOOTH); // 设置黑色背景 glClearColor(0.1f, 0.1f, 0.1f, 0.1f); // 设置深度缓存 glClearDepth(1.0f); // 使用深度缓存 glEnable(GL_DEPTH_TEST); // 设置深度方程 glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); /** * func: 设置纹理的环境参数 * use GL_MODULATE instead of GL_REPLACE if lighting is being used GL_REPLACE */ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); setProjection(); // 设置环境光 glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); // 设置光源位置 glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); // 启用0号光源 glEnable(GL_LIGHT0); glBindTexture(GL_TEXTURE_2D, lpTextures[0]); }
Texture::Texture(const char *filename, GLuint target, int flag) : name(filename), target(target) { flag |= UNSIGNED_BYTE|RGB; //RGB avoid an exception in extractParams extractParams(flag); genTexture(); if(target == TEXTURE_CUBE_MAP) loadCubemap(flag); else if(target == TEXTURE_2D) loadTexture2D(name, target, flag); texParameter(GL_TEXTURE_MAG_FILTER, magFilter); texParameter(GL_TEXTURE_MIN_FILTER, minFilter); }
void App::openImageDialog(TextureSlot *texture_slot, bool load_cube_cross) { char *out_filepath = nullptr; nfdresult_t result = NFD_OpenDialog("tga,png,bmp,jpg,hdr", nullptr, &out_filepath); SDL_RaiseWindow(sdl_window); // workaround: focus window again after dialog closes if (result == NFD_OKAY) { if (load_cube_cross) { int out_size; GLuint loaded_texture_cube = loadTextureCubeCross(out_filepath, /*build_mipmaps*/true, &out_size); if (loaded_texture_cube) { texture_slot->clear(); texture_slot->target = GL_TEXTURE_CUBE_MAP; texture_slot->texture = loaded_texture_cube; texture_slot->image_width = out_size; texture_slot->image_height = out_size; texture_slot->image_filepath = out_filepath; } } else { int out_width, out_height; GLuint loaded_texture = loadTexture2D(out_filepath, /*build_mipmaps*/true, &out_width, &out_height); if (loaded_texture) { // loading successful texture_slot->clear(); texture_slot->target = GL_TEXTURE_2D; setWrapTexture2D(GL_REPEAT, GL_REPEAT); texture_slot->texture = loaded_texture; texture_slot->image_width = out_width; texture_slot->image_height = out_height; texture_slot->image_filepath = out_filepath; } } } }
void TextureMapSurface::onStart(){ glEnable(GL_TEXTURE_2D);// 开启2D纹理 lpImages = new Image[6]; lpImages[0].loadImage(bmp_file_0);// 加载位图 /* 第2个纹理不能加载,一加载第1个纹理就失效,奇怪! */ lpImages[1].loadImage(bmp_file_1);// 加载位图 lpImages[2].loadImage(bmp_file_2);// 加载位图 lpImages[3].loadImage(bmp_file_3);// 加载位图 lpImages[4].loadImage(bmp_file_4);// 加载位图 lpImages[5].loadImage(bmp_file_5);// 加载位图 // lpTextureID = new GLuint[6]; // glGenTextures(6, lpTextureID);// 生成纹理对象索引,赋值给变量 glGenTextures(1, &texture0); glGenTextures(1, &texture1); glGenTextures(1, &texture2); glGenTextures(1, &texture3); glGenTextures(1, &texture4); glGenTextures(1, &texture5); // ErrorReport::getInstance()->print(""); //--------------------------- loadTexture2D(&texture0, lpImages[0]); // ErrorReport::getInstance()->print(""); loadTexture2D(&texture1, lpImages[1]); loadTexture2D(&texture2, lpImages[2]); loadTexture2D(&texture3, lpImages[3]); loadTexture2D(&texture4, lpImages[4]); loadTexture2D(&texture5, lpImages[5]); //--------------------------- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // 线形滤波 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 线形滤波 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);// mipmap // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);// mipmap glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //消除 texture 边缘的接缝 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /** * func: 设置纹理的环境参数 * use GL_MODULATE instead of GL_REPLACE if lighting is being used */ // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); setProjection(); // do clear temp image data buffer. // TODO }