Texture* Graphics::LoadTexture(const std::string& tex) { Texture* newTex = graphicsAPI->MakeTexture("Content/Textures/" + tex); textures_.insert(newTex->GetName(), newTex); newTex->Initialize(); return newTex; }
void TextureAtlasLoader::LoadTexture (const std::string& filename, TextureAtlas* texAtlas) { Texture* texture = Resources::LoadTexture (filename); texAtlas->SetName (texture->GetName ()); texAtlas->SetSize (texture->GetSize ()); texAtlas->SetPixels (texture->GetPixels (), 4u * texture->GetSize ().width * texture->GetSize ().height); }
bool Material::Save(XMLElement& dest) const { if (dest.IsNull()) { LOGERROR("Can not save material to null XML element"); return false; } // Write techniques for (unsigned i = 0; i < techniques_.Size(); ++i) { const TechniqueEntry& entry = techniques_[i]; if (!entry.technique_) continue; XMLElement techniqueElem = dest.CreateChild("technique"); techniqueElem.SetString("name", entry.technique_->GetName()); techniqueElem.SetInt("quality", entry.qualityLevel_); techniqueElem.SetFloat("loddistance", entry.lodDistance_); } // Write texture units for (unsigned j = 0; j < MAX_MATERIAL_TEXTURE_UNITS; ++j) { Texture* texture = GetTexture((TextureUnit)j); if (texture) { XMLElement textureElem = dest.CreateChild("texture"); textureElem.SetString("unit", textureUnitNames[j]); textureElem.SetString("name", texture->GetName()); } } // Write shader parameters for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator j = shaderParameters_.Begin(); j != shaderParameters_.End(); ++j) { XMLElement parameterElem = dest.CreateChild("parameter"); parameterElem.SetString("name", j->second_.name_); parameterElem.SetVectorVariant("value", j->second_.value_); } // Write culling modes XMLElement cullElem = dest.CreateChild("cull"); cullElem.SetString("value", cullModeNames[cullMode_]); XMLElement shadowCullElem = dest.CreateChild("shadowcull"); shadowCullElem.SetString("value", cullModeNames[shadowCullMode_]); // Write depth bias XMLElement depthBiasElem = dest.CreateChild("depthbias"); depthBiasElem.SetFloat("constant", depthBias_.constantBias_); depthBiasElem.SetFloat("slopescaled", depthBias_.slopeScaledBias_); return true; }
Texture * Graphics::LoadBumpMap(const std::string & map) { std::vector <unsigned char> png; std::vector <unsigned char> image; std::vector <Vector4> normals; //x - tan //y - bitan unsigned width, height; lodepng::load_file(png, "Content/Textures/" + map); auto error = lodepng::decode(image, width, height, png); if(error) { auto realerror = lodepng_error_text(error); __debugbreak(); } for(unsigned heightEntry = 0; heightEntry < height; ++heightEntry) { for(unsigned widthEntry = 0; widthEntry < width; ++widthEntry) { Vector3 uTan; Vector3 vTan; int leftSample; int rightSample; if((widthEntry + 1) < width) { rightSample = as_2d(image, width, heightEntry * 4, (widthEntry + 1) * 4); } else { rightSample = as_2d(image, width, heightEntry * 4, widthEntry * 4); } if(widthEntry > 0) { leftSample = as_2d(image, width, heightEntry * 4, (widthEntry - 1) * 4); } else { leftSample = as_2d(image, width, heightEntry * 4, widthEntry * 4); } uTan = Vector3(1.0f, 0.0f, (rightSample - leftSample)); int topSample; int bottomSample; if((heightEntry + 1) < height) { topSample = as_2d(image, width, (heightEntry + 1) * 4, widthEntry * 4); } else { topSample = as_2d(image, width, heightEntry * 4, widthEntry * 4); } if(heightEntry > 0) { bottomSample = as_2d(image, width, (heightEntry - 1) * 4, widthEntry * 4); } else { bottomSample = as_2d(image, width, heightEntry * 4, widthEntry * 4); } vTan = Vector3(0.0f, 1.0f, (topSample - bottomSample)); uTan.Normalize(); vTan.Normalize(); Vector3 normal = uTan.Cross(vTan); normals.push_back(Vector4(normal.GetNormalized(), 0.0f)); } } std::vector<unsigned char> normalColorVersion; for(auto& it : normals) { normalColorVersion.push_back((it.x + 1) * 0.5f * 255); normalColorVersion.push_back((it.y + 1) * 0.5f * 255); normalColorVersion.push_back((it.z + 1) * 0.5f * 255); normalColorVersion.push_back(255); } TextureDesc desc; desc.size.x = width; desc.size.y = height; desc.writable = false; Texture* newTex = graphicsAPI->MakeTexture(normalColorVersion, desc); textures_.insert(newTex->GetName(), newTex); newTex->Initialize(); return newTex; }
/** * @brief * Creates/recreates the texture surface */ void SCRenderToTexture::CreateSurfaceTexture() { // Get the renderer Renderer &cRenderer = GetSceneContext()->GetRendererContext().GetRenderer(); // Delete the old render to texture buffer surface if (m_pSurfaceTextureBuffer) { delete m_pSurfaceTextureBuffer; m_pSurfaceTextureBuffer = nullptr; } // Delete the old resulting texture Texture *pTexture = m_pTextureHandler->GetResource(); if (pTexture) { pTexture->Delete(); m_pTextureHandler->SetResource(nullptr); } // Get the desired texture buffer format TextureBuffer::EPixelFormat nFormat; switch (m_nFormat) { case 0: nFormat = TextureBuffer::R8G8B8; break; case 1: nFormat = TextureBuffer::R8G8B8A8; break; default: nFormat = TextureBuffer::R8G8B8A8; break; } // Create 'render to texture buffer' surface if (m_bCube) { m_pSurfaceTextureBuffer = cRenderer.CreateSurfaceTextureBufferCube(m_nWidth, nFormat, m_nSurfaceFlags); } else { // If possible, we use a standard 2D texture buffer if (cRenderer.GetCapabilities().bTextureBufferNonPowerOfTwo || (Math::IsPowerOfTwo(m_nWidth) && Math::IsPowerOfTwo(m_nHeight))) m_pSurfaceTextureBuffer = cRenderer.CreateSurfaceTextureBuffer2D(Vector2i(m_nWidth, m_nHeight), nFormat, m_nSurfaceFlags); else m_pSurfaceTextureBuffer = cRenderer.CreateSurfaceTextureBufferRectangle(Vector2i(m_nWidth, m_nHeight), nFormat, m_nSurfaceFlags); } if (m_pSurfaceTextureBuffer) { // Setup the surface painter SurfacePainter *pSurfacePainter = cRenderer.CreateSurfacePainter(m_sPainter); m_pSurfaceTextureBuffer->SetPainter(pSurfacePainter); if (pSurfacePainter && pSurfacePainter->IsInstanceOf("PLScene::SPScene")) { // Do NOT let the renderer update this surface by default, WE update it inside DrawPre() m_pSurfaceTextureBuffer->SetActive(false); // THIS is the scene root :) SPScene *pPainter = static_cast<SPScene*>(pSurfacePainter); pPainter->SetRootContainer(this); // Tell the surface scene painter about the 'conrete scene' SceneNode *pSceneNode = GetByName(m_sSceneName); pPainter->SetSceneContainer((pSceneNode && pSceneNode->IsContainer()) ? static_cast<SceneContainer*>(pSceneNode) : nullptr); // Set default scene renderer pPainter->SetDefaultSceneRenderer(m_sSceneRenderer); } } // Add the texture TextureManager &cTextureManager = cRenderer.GetRendererContext().GetTextureManager(); // If there's already a texture with this name we have to get another, still free resource name if (cTextureManager.GetByName(m_sTextureName)) { // Find an unused resource name String sName = m_sTextureName + "_0"; for (uint32 i=1; cTextureManager.GetByName(sName); i++) sName = m_sTextureName + '_' + static_cast<int>(i); // We have found an unused name m_sTextureName = sName; } if (m_pSurfaceTextureBuffer && m_pSurfaceTextureBuffer->GetTextureBuffer()) { pTexture = cTextureManager.CreateTexture(m_sTextureName, *m_pSurfaceTextureBuffer->GetTextureBuffer()); if (pTexture) m_sTextureName = pTexture->GetName(); m_pTextureHandler->SetResource(pTexture); } }
bool ManageTextureScreen::Update() { assert( m_ScreenMgr ); Vehicle* veh = m_ScreenMgr->GetVehiclePtr(); vector< Geom* > select_vec = veh->GetActiveGeomPtrVec(); if ( select_vec.size() != 1 ) { Hide(); return false; } // Redo list on each update. m_TextureMgrUI->compChoice->clear(); m_CompDropDownList.clear(); std::vector<Geom *> geomVec = veh->FindGeomVec( veh->GetGeomVec() ); for( int i = 0; i < ( int )geomVec.size(); i++ ) { CompDropDownItem item; item.GeomName = geomVec[i]->GetName(); item.GeomID = geomVec[i]->GetID(); // Hack to add duplicate names char str[256]; sprintf( str, "%d", i ); item.GUIIndex = m_TextureMgrUI->compChoice->add( str ); m_CompDropDownList.push_back( item ); } // Fill Hacked char array with correct names. for( int i = 0; i < ( int )m_CompDropDownList.size(); i++ ) { m_TextureMgrUI->compChoice->replace( m_CompDropDownList[i].GUIIndex, m_CompDropDownList[i].GeomName.c_str() ); } // Set compChoice to current selected and update texture dropdown list. for( int i = 0; i < ( int )m_CompDropDownList.size(); i++ ) { if( m_CompDropDownList[i].GeomID == select_vec[0]->GetID() ) { m_TextureMgrUI->compChoice->value( m_CompDropDownList[i].GUIIndex ); // Update Texture Dropdown List. // // Redo texture list on each update. m_TextureMgrUI->textureChoice->clear(); m_TexDropDownList.clear(); // Clear preview window. VSPGraphic::Viewport * viewport = m_GlWin->getGraphicEngine()->getDisplay()->getViewport(); assert( viewport ); viewport->getBackground()->removeImage(); // Load Textures... TextureMgr * texMgr = select_vec[0]->m_GuiDraw.getTextureMgr(); std::vector<Texture*> texInfos = texMgr->FindTextureVec( texMgr->GetTextureVec() ); for( int j = 0; j < ( int )texInfos.size(); j++ ) { TexDropDownItem item; item.TexInfo = texInfos[j]; // Hack to add duplicate names char str[256]; sprintf( str, "%d", j ); item.GUIIndex = m_TextureMgrUI->textureChoice->add( str ); m_TexDropDownList.push_back( item ); } // Fill Hacked char array with correct names. for( int j = 0; j < ( int )m_TexDropDownList.size(); j++ ) { m_TextureMgrUI->textureChoice->replace( m_TexDropDownList[j].GUIIndex, m_TexDropDownList[j].TexInfo->GetName().c_str() ); } if( !m_TexDropDownList.empty() ) { if( m_SelectedTexItem ) { m_TextureMgrUI->textureChoice->value( m_SelectedTexItem->GUIIndex ); } else { // On refresh list, if nothing is selected, pick last item on list. m_TextureMgrUI->textureChoice->value( m_TexDropDownList[m_TexDropDownList.size() - 1].GUIIndex ); } UpdateCurrentSelected(); assert( m_SelectedTexItem ); viewport->getBackground()->attachImage( VSPGraphic::GlobalTextureRepo()-> get2DTexture( m_SelectedTexItem->TexInfo->m_FileName.c_str() ) ); } else { ResetCurrentSelected(); // Force redraw empty list. m_TextureMgrUI->textureChoice->redraw(); } // Update Sliders and Buttons. if( m_SelectedTexItem ) { Texture * info = select_vec[0]->m_GuiDraw.getTextureMgr()->FindTexture( m_SelectedTexItem->TexInfo->GetID() ); m_TextureMgrUI->textureNameInput->value( info->GetName().c_str() ); m_UScaleSlider.Update( info->m_UScale.GetID() ); m_WScaleSlider.Update( info->m_WScale.GetID() ); m_UPosSlider.Update( info->m_U.GetID() ); m_WPosSlider.Update( info->m_W.GetID() ); m_TransparencySlider.Update( info->m_Transparency.GetID() ); m_FlipUButton.Update( info->m_FlipU.GetID() ); m_FlipWButton.Update( info->m_FlipW.GetID() ); } break; } } m_GlWin->redraw(); return true; }
void TextureManager::DebugInfoRender(const float &fRatio, const PosQuat &pq) { Texture *pCurrent = 0; //render currently selected texture for debug display if (m_dwSelectedIndex < m_vTextures.size()) { pCurrent = m_vTextures.at(m_dwSelectedIndex); } if (!pCurrent) { return; } glColor4f(1.0f, 1.0f, 1.0f, 1.0f); GLboolean bLightingWasOn = glIsEnabled(GL_LIGHTING); if (bLightingWasOn) { //turn off lighting glDisable(GL_LIGHTING); } GLboolean bTextureWasOn = glIsEnabled(GL_TEXTURE_2D); if (!bTextureWasOn) { //turn textures on if it wasn't already on glEnable(GL_TEXTURE_2D); } glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_2D, pCurrent->GetTexture()); glMultMatrixf(pq.quat.ToMatrix().m); float fScale = 0.33f; float fBorder = fScale * 1.05f; float fBorderSize = fBorder - fScale; glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-fScale, -fScale, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-fScale, fScale, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(fScale, fScale, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(fScale, -fScale, 0.0f); glEnd(); glDisable(GL_TEXTURE_2D); glBegin(GL_QUADS); glColor3f(1.0f, 0.0f, 1.0f); glNormal3f(0.0f, 0.0f, 1.0f); glVertex3f(-fBorder, -fBorder, 0.0f); glVertex3f(-fBorder, fBorder, 0.0f); glVertex3f(-fBorder + fBorderSize, fBorder, 0.0f); glVertex3f(-fBorder + fBorderSize, -fBorder, 0.0f); glVertex3f(fBorder, -fBorder, 0.0f); glVertex3f(fBorder, fBorder, 0.0f); glVertex3f(fBorder - fBorderSize, fBorder, 0.0f); glVertex3f(fBorder - fBorderSize, -fBorder, 0.0f); glVertex3f(-fBorder, -fBorder, 0.0f); glVertex3f(-fBorder, -fBorder + fBorderSize, 0.0f); glVertex3f(fBorder, -fBorder + fBorderSize, 0.0f); glVertex3f(fBorder, -fBorder, 0.0f); glVertex3f(-fBorder, fBorder, 0.0f); glVertex3f(-fBorder, fBorder - fBorderSize, 0.0f); glVertex3f(fBorder, fBorder - fBorderSize, 0.0f); glVertex3f(fBorder, fBorder, 0.0f); glEnd(); glColor3f(1.0f, 1.0f, 1.0f); //reset matrices so text is always in the correct spot glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, fRatio, 0.1, 50.0); //update this so we dont get crappy distortion glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 1.75, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); size_t size = 50 + strlen(pCurrent->GetName()); char *pDetails = (char *)malloc(sizeof(char) * size); memset(pDetails, 0, size); sprintf_s(pDetails, size, "%dx%d, %u bytes, %s", pCurrent->GetSizeX(), pCurrent->GetSizeY(), pCurrent->GetSizeInBytes(), pCurrent->GetName()); glRasterPos2f(-fRatio, 0.97f); for (const char *c = pDetails; *c != '\0'; c++) { glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *c); } free(pDetails); this->RenderHelpText(fRatio); glEnable(GL_TEXTURE_2D); if (!bTextureWasOn) { //disable textures if it wasn't on initially glDisable(GL_TEXTURE_2D); } if (bLightingWasOn) { //if it was on, turn it back on! glEnable(GL_LIGHTING); } }