/// update terrain generator preview texture //-------------------------------------------------------------------------------------------------------------------------- void App::updateTerPrv(bool first) { if (!first && !ovTerPrv) return; if (terPrvTex.isNull()) return; HardwarePixelBufferSharedPtr pbuf = terPrvTex->getBuffer(); pbuf->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pb = pbuf->getCurrentLock(); using Ogre::uint8; uint8* p = static_cast<uint8*>(pb.data); const static float fB[2] = { 90.f, 90.f}, fG[2] = {255.f,160.f}, fR[2] = { 90.f,255.f}; const float s = TerPrvSize * 0.5f, s1 = 1.f/s; const float ox = pSet->gen_ofsx, oy = pSet->gen_ofsy; for (int y = 0; y < TerPrvSize; ++y) for (int x = 0; x < TerPrvSize; ++x) { float fx = ((float)x - s)*s1, fy = ((float)y - s)*s1; // -1..1 float c = Noise(x*s1-oy, y*s1+ox, pSet->gen_freq, pSet->gen_oct, pSet->gen_persist) * 0.8f; // par fit bool b = c >= 0.f; c = b ? powf(c, pSet->gen_pow) : -powf(-c, pSet->gen_pow); int i = b ? 0 : 1; c = b ? c : -c; //c *= pSet->gen_scale; //no uint8 bR = c * fR[i], bG = c * fG[i], bB = c * fB[i]; *p++ = bR; *p++ = bG; *p++ = bB; *p++ = 255;//bG > 32 ? 255 : 0; } pbuf->unlock(); }
TexturePtr RandomTools::generateRandomVelocityTexture() { // PPP: Temp workaround for DX 11 which does not seem to like usage dynamic // TextureUsage usage = (Root::getSingletonPtr()->getRenderSystem()->getName()=="Direct3D11 Rendering Subsystem") ? // TU_DEFAULT : TU_DYNAMIC; TexturePtr texPtr = TextureManager::getSingleton().createManual( "RandomVelocityTexture", // ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "General", TEX_TYPE_1D, 1024, 1, 1, 0, PF_FLOAT32_RGBA);//, //usage); HardwarePixelBufferSharedPtr pixelBuf = texPtr->getBuffer(); // Lock the buffer so we can write to it. pixelBuf->lock(HardwareBuffer::HBL_DISCARD); const PixelBox &pb = pixelBuf->getCurrentLock(); float *randomData = static_cast<float*>(pb.data); // float randomData[NUM_RAND_VALUES * 4]; for(int i = 0; i < NUM_RAND_VALUES * 4; i++) { randomData[i] = float( (rand() % 10000) - 5000 ); } // PixelBox pixelBox(1024, 1, 1, PF_FLOAT32_RGBA, &randomData[0]); // pixelBuf->blitFromMemory(pixelBox); pixelBuf->unlock(); return texPtr; }
//----------------------------------------------------------------------------- void HardwarePixelBuffer::blit(const HardwarePixelBufferSharedPtr &src) { blit(src, Box(0,0,0,src->getWidth(),src->getHeight(),src->getDepth()), Box(0,0,0,mWidth,mHeight,mDepth) ); }
void HardwarePixelBuffer::blit(const HardwarePixelBufferSharedPtr &src, const Image::Box &srcBox, const Image::Box &dstBox) { if (isLocked() || src->isLocked()) { WIND_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Source and destination buffer may not be locked!", "HardwarePixelBuffer::blit"); } if (src.get() == this) { WIND_EXCEPT(Exception::ERR_INVALIDPARAMS, "Source must not be the same object", "HardwarePixelBuffer::blit"); } const PixelBox &srclock = src->lock(srcBox, HBL_READ_ONLY); LockOptions method = HBL_NORMAL; if (dstBox.left == 0 && dstBox.top == 0 && dstBox.front == 0 && dstBox.right == mWidth && dstBox.bottom == mHeight && dstBox.back == mDepth) { method = HBL_DISCARD; } const PixelBox &dstlock = lock(dstBox, method); if (dstlock.getWidth() != srclock.getWidth() || dstlock.getHeight() != srclock.getHeight() || dstlock.getDepth() != srclock.getDepth()) { Image::scale(srclock, dstlock); } else { PixelUtil::bulkPixelConversion(srclock, dstlock); } unlock(); src->unlock(); }
void CoverageMap::updateTexture() { // write the edit buffer into the texture's pixel buffer HardwarePixelBufferSharedPtr buffer = mTexture->getBuffer(); PixelBox pixelBox (mWidth, mHeight, 1, getFormat(mChannels), mData); Image::Box imageBox (0, 0, mWidth, mHeight); buffer->blitFromMemory(pixelBox, imageBox); }
void WebView::createMaterial() { if(opacity > 1) opacity = 1; else if(opacity < 0) opacity = 0; if(!Bitwise::isPO2(viewWidth) || !Bitwise::isPO2(viewHeight)) { if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_NON_POWER_OF_2_TEXTURES)) { if(Root::getSingleton().getRenderSystem()->getCapabilities()->getNonPOW2TexturesLimited()) compensateNPOT = true; } else compensateNPOT = true; #ifdef __APPLE__ //cus those fools always report #t when I ask if they support this or that //and then fall back to their buggy and terrible software driver which has never once in my life rendered a single correct frame. compensateNPOT=true; #endif if(compensateNPOT) { texWidth = Bitwise::firstPO2From(viewWidth); texHeight = Bitwise::firstPO2From(viewHeight); } } // Create the texture #if defined(HAVE_AWESOMIUM) || !defined(__APPLE__) TexturePtr texture = TextureManager::getSingleton().createManual( viewName + "Texture", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, texWidth, texHeight, 0, PF_BYTE_BGRA, TU_DYNAMIC_WRITE_ONLY_DISCARDABLE, this); HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); texDepth = Ogre::PixelUtil::getNumElemBytes(pixelBox.format); texPitch = (pixelBox.rowPitch*texDepth); uint8* pDest = static_cast<uint8*>(pixelBox.data); memset(pDest, 128, texHeight*texPitch); pixelBuffer->unlock(); #endif MaterialPtr material = MaterialManager::getSingleton().create(viewName + "Material", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); matPass = material->getTechnique(0)->getPass(0); matPass->setSceneBlending(SBT_TRANSPARENT_ALPHA); matPass->setDepthWriteEnabled(false); baseTexUnit = matPass->createTextureUnitState(viewName + "Texture"); baseTexUnit->setTextureFiltering(texFiltering, texFiltering, FO_NONE); if(texFiltering == FO_ANISOTROPIC) baseTexUnit->setTextureAnisotropy(4); }
bool gkOgreCompositorHelper::createHalftoneTexture() { using namespace Ogre; try { if (TextureManager::getSingleton().resourceExists(COMP_HALFTONE_TEX_NAME)) return true; //already created TexturePtr tex = TextureManager::getSingleton().createManual( COMP_HALFTONE_TEX_NAME, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_3D, 64,64,64, 0, PF_A8 ); HardwarePixelBufferSharedPtr ptr = tex->getBuffer(0,0); ptr->lock(HardwareBuffer::HBL_DISCARD); const PixelBox &pb = ptr->getCurrentLock(); uint8 *data = static_cast<uint8*>(pb.data); size_t height = pb.getHeight(); size_t width = pb.getWidth(); size_t depth = pb.getDepth(); size_t rowPitch = pb.rowPitch; size_t slicePitch = pb.slicePitch; for (size_t z = 0; z < depth; ++z) { for (size_t y = 0; y < height; ++y) { for(size_t x = 0; x < width; ++x) { float fx = 32-(float)x+0.5f; float fy = 32-(float)y+0.5f; float fz = 32-((float)z)/3+0.5f; float distanceSquare = fx*fx+fy*fy+fz*fz; data[slicePitch*z + rowPitch*y + x] = 0x00; if (distanceSquare < 1024.0f) data[slicePitch*z + rowPitch*y + x] += 0xFF; } } } ptr->unlock(); } catch (Exception &e) { gkPrintf("[CMP] FAILED - Halftone Texture Creation. %s", e.getFullDescription().c_str()); return false; } return true; }
void FlashControl::createMaterial() { texture.setNull(); MaterialManager::getSingletonPtr()->remove(name + "Material"); TextureManager::getSingletonPtr()->remove(name + "Texture"); texWidth = width; texHeight = height; if(!Bitwise::isPO2(width) || !Bitwise::isPO2(height)) { if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_NON_POWER_OF_2_TEXTURES)) { if(Root::getSingleton().getRenderSystem()->getCapabilities()->getNonPOW2TexturesLimited()) compensateNPOT = true; } else compensateNPOT = true; if(compensateNPOT) { texWidth = Bitwise::firstPO2From(width); texHeight = Bitwise::firstPO2From(height); } } // Create the texture texture = TextureManager::getSingleton().createManual( name + "Texture", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, (uint)texWidth, (uint)texHeight, 0, isTransparent? PF_BYTE_BGRA : PF_BYTE_BGR, TU_DYNAMIC_WRITE_ONLY_DISCARDABLE, this); HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); texDepth = Ogre::PixelUtil::getNumElemBytes(pixelBox.format); texPitch = (pixelBox.rowPitch*texDepth); uint8* pDest = static_cast<uint8*>(pixelBox.data); memset(pDest, 128, texHeight*texPitch); pixelBuffer->unlock(); materialName = name + "Material"; MaterialPtr material = MaterialManager::getSingleton().create(materialName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Pass* matPass = material->getTechnique(0)->getPass(0); matPass->setSceneBlending(SBT_TRANSPARENT_ALPHA); matPass->setDepthWriteEnabled(false); texUnit = matPass->createTextureUnitState(name + "Texture"); texUnit->setTextureFiltering(FO_NONE, FO_NONE, FO_NONE); invalidateTotally(); }
//----------------------------------------------------------------------- void PagingLandScapeHorizon::update() { if (material_enabled) { const PixelBox srcBox = mVisImage.getPixelBox(); HardwarePixelBufferSharedPtr Texbuffer = mVisTex->getBuffer (0, 0); const PixelBox lock = Texbuffer->lock (srcBox, HardwareBuffer::HBL_DISCARD); // lock.data can now be freely accessed PixelUtil::bulkPixelConversion(srcBox, lock); Texbuffer->unlock(); } }
void SaveImage(TexturePtr TextureToSave, String filename) { HardwarePixelBufferSharedPtr readbuffer; readbuffer = TextureToSave->getBuffer(0, 0); readbuffer->lock(HardwareBuffer::HBL_NORMAL ); const PixelBox &readrefpb = readbuffer->getCurrentLock(); uchar *readrefdata = static_cast<uchar*>(readrefpb.data); Image img; img = img.loadDynamicImage (readrefdata, TextureToSave->getWidth(), TextureToSave->getHeight(), TextureToSave->getFormat()); img.save(filename); readbuffer->unlock(); }
//----------------------------------------------------------------------------- void GLESTextureBuffer::blit(const HardwarePixelBufferSharedPtr &src, const Image::Box &srcBox, const Image::Box &dstBox) { GLESTextureBuffer *srct = static_cast<GLESTextureBuffer *>(src.getPointer()); // TODO: Check for FBO support first // Destination texture must be 2D // Source texture must be 2D if((src->getUsage() & TU_RENDERTARGET) == 0 && (srct->mTarget == GL_TEXTURE_2D)) { blitFromTexture(srct, srcBox, dstBox); } else { GLESHardwarePixelBuffer::blit(src, srcBox, dstBox); } }
MaterialPtr Visuals::getMaterial(std::string name, int red, int green, int blue, int alpha) { // Create the texture TexturePtr texture = TextureManager::getSingleton().createManual( name, // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, // type 256, 256, // width & height 0, // number of mipmaps PF_BYTE_BGRA, // pixel format TU_DEFAULT); // usage; should be TU_DYNAMIC_WRITE_ONLY_DISCARDABLE for // textures updated very often (e.g. each frame) // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* pDest = static_cast<uint8*>(pixelBox.data); // Fill in some pixel data. This will give a semi-transparent blue, // but this is of course dependent on the chosen pixel format. for (size_t j = 0; j < 256; j++) { for(size_t i = 0; i < 256; i++) { *pDest++ = blue; // B *pDest++ = green; // G *pDest++ = red; // R *pDest++ = alpha; // A } } // Unlock the pixel buffer pixelBuffer->unlock(); MaterialPtr material = MaterialManager::getSingleton().create(name, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); material->getTechnique(0)->getPass(0)->createTextureUnitState(name); material->getTechnique(0)->getPass(0)->setSceneBlending(SBT_TRANSPARENT_ALPHA); return material; }
/* static MaterialPtr MakeDefaultMaterial() { const int def_width=256; const int def_height=256; const char*defTexName="DefaultTexture"; const char*defMatName="DefaultMaterial"; if( MaterialManager::getSingleton().resourceExists(defMatName)) return (MaterialPtr)MaterialManager::getSingleton().getByName(defMatName); TexturePtr texture = TextureManager::getSingleton().createManual( defTexName, // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, // type def_width, def_height, // width & height 0, // number of mipmaps PF_BYTE_RGBA, // pixel format TU_DEFAULT); // usage; should be TU_DYNAMIC_WRITE_ONLY_DISCARDABLE for // textures updated very often (e.g. each frame) // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* pDest = static_cast<uint8*>(pixelBox.data); // Fill in some pixel data. This will give a semi-transparent blue, // but this is of course dependent on the chosen pixel format. for (size_t j = 0; j < def_height; j++) for(size_t i = 0; i < def_width; i++) { *pDest++ = 255; // R *pDest++ = 0; // G *pDest++ = 255; // B *pDest++ = 255; // A } // Unlock the pixel buffer pixelBuffer->unlock(); // Create a material using the texture MaterialPtr material = MaterialManager::getSingleton().create( defMatName, // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); material->getTechnique(0)->getPass(0)->createTextureUnitState(defTexName); //material->getTechnique(0)->getPass(0)->setSceneBlending(SBT_TRANSPARENT_ALPHA); return material; } MaterialPtr GetLodMaterial(const std::string& name) { std::string matname(name + ".Material"); std::string texname(name + ".Texture"); if( MaterialManager::getSingleton().resourceExists(matname)) return (MaterialPtr)MaterialManager::getSingleton().getByName(matname); TexturePtr texture; int alpha = 0; if(TextureManager::getSingleton().resourceExists(texname)) { texture=TextureManager::getSingleton().getByName(texname); }else { angel::pLodData ldata=angel::LodManager.LoadFile( name ); BYTE*data= &((*ldata)[0]); if(!data) return MakeDefaultMaterial(); int size = (int)ldata->size(); int psize = *(int*)(data+0x14); unsigned int unpsize1 = *(int*)(data+0x10); unsigned long unpsize2 = *(int*)(data+0x28); if( psize+0x30+0x300 != size ) return MakeDefaultMaterial(); if( unpsize2 && unpsize2 < unpsize1) return MakeDefaultMaterial(); BYTE* pal = data + 0x30 + psize; BYTE*unpdata = new BYTE[unpsize2 ]; boost::scoped_array<BYTE> sunpdata(unpdata); if ( uncompress( unpdata, &unpsize2 , data + 0x30, psize ) != Z_OK ) return MakeDefaultMaterial(); int width = *(WORD*)(data+0x18); int height = *(WORD*)(data+0x1a); int imgsize = width*height; BYTE *pSrc=unpdata; // Create the texture texture = TextureManager::getSingleton().createManual( name + ".Texture", // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, // type width, height, // width & height 0, // number of mipmaps PF_BYTE_BGRA, // pixel format TU_DEFAULT); // usage; should be TU_DYNAMIC_WRITE_ONLY_DISCARDABLE for // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* pDest = static_cast<uint8*>(pixelBox.data); // Fill in some pixel data. This will give a semi-transparent blue, // but this is of course dependent on the chosen pixel format. for (int j = 0; j < width; j++) for(int i = 0; i < height; i++) { int index=*pSrc++; int r = pal[index*3+0]; int g = pal[index*3+1]; int b = pal[index*3+2]; int a = 0xff; if( index == 0 && ((r == 0 && g >250 && b > 250) || (r > 250 && g ==0 && b > 250))) { alpha=1; a= 0; r=g=b=0; } *pDest++ = r; // G *pDest++ = g; // R *pDest++ = b; *pDest++ = a; // A } // Unlock the pixel buffer pixelBuffer->unlock(); } // Create a material using the texture MaterialPtr material = MaterialManager::getSingleton().create( matname, // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); material->getTechnique(0)->getPass(0)->createTextureUnitState(texname); if(alpha) material->getTechnique(0)->getPass(0)->setSceneBlending(SBT_TRANSPARENT_ALPHA); }*/ static TexturePtr GetDefaultTexture() { const int def_width=256; const int def_height=256; const char*defTexName="DefaultTexture"; if( TextureManager::getSingleton().resourceExists(defTexName)) return (TexturePtr )TextureManager::getSingleton().getByName(defTexName); angel::Log << "loading default texture" << angel::aeLog::endl; TexturePtr texture = TextureManager::getSingleton().createManual( defTexName, // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, // type def_width, def_height, // width & height 0, // number of mipmaps PF_BYTE_RGBA, // pixel format TU_DEFAULT);//|TU_AUTOMIPMAP); // usage; should be TU_DYNAMIC_WRITE_ONLY_DISCARDABLE for // textures updated very often (e.g. each frame) // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* pDest = static_cast<uint8*>(pixelBox.data); // Fill in some pixel data. This will give a semi-transparent blue, // but this is of course dependent on the chosen pixel format. for (size_t j = 0; j < def_height; j++) for(size_t i = 0; i < def_width; i++) { *pDest++ = 255; // R *pDest++ = 0; // G *pDest++ = 255; // B *pDest++ = 255; // A } // Unlock the pixel buffer pixelBuffer->unlock(); return texture; }
bool gkOgreCompositorHelper::createDitherTexture(int width, int height) { using namespace Ogre; try { if (TextureManager::getSingleton().resourceExists(COMP_DITHER_TEX_NAME)) return true; //already created TexturePtr tex = TextureManager::getSingleton().createManual( COMP_DITHER_TEX_NAME, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, width, height, 1, 0, PF_A8 ); HardwarePixelBufferSharedPtr ptr = tex->getBuffer(0,0); ptr->lock(HardwareBuffer::HBL_DISCARD); const PixelBox &pb = ptr->getCurrentLock(); uint8 *data = static_cast<uint8*>(pb.data); size_t height = pb.getHeight(); size_t width = pb.getWidth(); size_t rowPitch = pb.rowPitch; for (size_t y = 0; y < height; ++y) for(size_t x = 0; x < width; ++x) data[rowPitch*y + x] = (uint8)Ogre::Math::RangeRandom(64.0,192); ptr->unlock(); } catch (Exception &e) { gkPrintf("[CMP] FAILED - Dither Texture Creation. %s", e.getFullDescription().c_str()); return false; } return true; }
void OgreText::setPanelColor(int R, int G, int B, int I) { // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = texture_->getBuffer(); //directly modify pixel buffer in texture to change color // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* pDest = static_cast<uint8*>(pixelBox.data); *pDest++ = R; *pDest++ = G; *pDest++ = B; *pDest++ = I; // Unlock the pixel buffer pixelBuffer->unlock(); }
void WebView::update() { #ifdef HAVE_AWESOMIUM if(maxUpdatePS) if(timer.getMilliseconds() - lastUpdateTime < 1000 / maxUpdatePS) return; updateFade(); if(usingMask) baseTexUnit->setAlphaOperation(LBX_SOURCE1, LBS_MANUAL, LBS_CURRENT, fadeValue * opacity); else if(isWebViewTransparent) baseTexUnit->setAlphaOperation(LBX_BLEND_TEXTURE_ALPHA, LBS_MANUAL, LBS_TEXTURE, fadeValue * opacity); else baseTexUnit->setAlphaOperation(LBX_SOURCE1, LBS_MANUAL, LBS_CURRENT, fadeValue * opacity); if(!webView->isDirty()) return; TexturePtr texture = TextureManager::getSingleton().getByName(viewName + "Texture"); HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* destBuffer = static_cast<uint8*>(pixelBox.data); webView->render(destBuffer, (int)texPitch, (int)texDepth); if(isWebViewTransparent && !usingMask && ignoringTrans) { for(int row = 0; row < texHeight; row++) for(int col = 0; col < texWidth; col++) alphaCache[row * alphaCachePitch + col] = destBuffer[row * texPitch + col * 4 + 3]; } pixelBuffer->unlock(); lastUpdateTime = timer.getMilliseconds(); #endif }
void StandByState::convertIplToTexture(IplImage* img,TexturePtr texture) { HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer();//Get the Pixel Buffer for Texture pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); //Lock the buffer const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); //Get the pixel box for data pointer unsigned char* pDest = static_cast<unsigned char*>(pixelBox.data); unsigned char* videoPtr=(unsigned char*)(img->imageData); //Get the pointer to the video frame for (int r=0; r<videoHeight; r++) { for(int c=0; c<videoWidth; c++) { for (int p=0; p<pix_size; p++) *(pDest++)=*(videoPtr++);//Copy the data if(pix_size==3) //Ogre uses 4 bytes per pixel, so add an additional pass if video is RGB pDest++; } pDest+=empty_byte; //If there are empty bytes at the end of the rows, add them to go to the correct location videoPtr+=empty_byte; } pixelBuffer->unlock();//Unlock the pixel buffer }
void Sample_VolumeTex::generate() { /* Evaluate julia fractal for each point */ Julia julia(global_real, global_imag, global_theta); const float scale = 2.5; const float vcut = 29.0f; const float vscale = 1.0f/vcut; HardwarePixelBufferSharedPtr buffer = ptex->getBuffer(0, 0); Ogre::StringStream d; d << "HardwarePixelBuffer " << buffer->getWidth() << " " << buffer->getHeight() << " " << buffer->getDepth(); LogManager::getSingleton().logMessage(d.str()); buffer->lock(HardwareBuffer::HBL_NORMAL); const PixelBox &pb = buffer->getCurrentLock(); d.str(""); d << "PixelBox " << pb.getWidth() << " " << pb.getHeight() << " " << pb.getDepth() << " " << pb.rowPitch << " " << pb.slicePitch << " " << pb.data << " " << PixelUtil::getFormatName(pb.format); LogManager::getSingleton().logMessage(d.str()); Ogre::uint32 *pbptr = static_cast<Ogre::uint32*>(pb.data); for(size_t z=pb.front; z<pb.back; z++) { for(size_t y=pb.top; y<pb.bottom; y++) { for(size_t x=pb.left; x<pb.right; x++) { if(z==pb.front || z==(pb.back-1) || y==pb.top|| y==(pb.bottom-1) || x==pb.left || x==(pb.right-1)) { // On border, must be zero pbptr[x] = 0; } else { float val = julia.eval(((float)x/pb.getWidth()-0.5f) * scale, ((float)y/pb.getHeight()-0.5f) * scale, ((float)z/pb.getDepth()-0.5f) * scale); if(val > vcut) val = vcut; PixelUtil::packColour((float)x/pb.getWidth(), (float)y/pb.getHeight(), (float)z/pb.getDepth(), (1.0f-(val*vscale))*0.7f, PF_A8R8G8B8, &pbptr[x]); } } pbptr += pb.rowPitch; } pbptr += pb.getSliceSkip(); } buffer->unlock(); }
Terrain* SnowTerrain::getTerrain() { if(!mTerrainGroup) return NULL; Terrain *t = mTerrainGroup->getTerrain(0,0); return t; TerrainGroup::TerrainIterator ti = mTerrainGroup->getTerrainIterator(); while (ti.hasMoreElements()) { Ogre::uint32 tkey = ti.peekNextKey(); TerrainGroup::TerrainSlot* ts = ti.getNext(); if (ts->instance && ts->instance->isLoaded()) { float* heights = ts->instance->getHeightData(); //PixelBox* pBox = ts->instance->calculateNormals()); TexturePtr texturePtr = ts->instance->getTerrainNormalMap(); HardwarePixelBufferSharedPtr buf = texturePtr->getBuffer(); size_t bytes = buf->getSizeInBytes(); size_t h = buf->getHeight(); size_t w = buf->getWidth(); size_t d = buf->getDepth(); PixelFormat f = PF_BYTE_RGB;//buf->getFormat(); uint8* tmpData = (uint8*)OGRE_MALLOC(w * h * 3, MEMCATEGORY_GENERAL); memset(tmpData,0,w*h*3); PixelBox pBox(w, h, d, f, tmpData); buf->blitToMemory(pBox); OGRE_FREE(tmpData, MEMCATEGORY_GENERAL); } } return NULL; }
TexturePtr TerrainManager::getVertexColours(ESM::Land* land, int cellX, int cellY, int fromX, int fromY, int size) { TextureManager* const texMgr = TextureManager::getSingletonPtr(); const std::string colourTextureName = "VtexColours_" + boost::lexical_cast<std::string>(cellX) + "_" + boost::lexical_cast<std::string>(cellY) + "_" + boost::lexical_cast<std::string>(fromX) + "_" + boost::lexical_cast<std::string>(fromY); TexturePtr tex = texMgr->getByName(colourTextureName); if ( !tex.isNull() ) { return tex; } tex = texMgr->createManual(colourTextureName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, size, size, 0, PF_BYTE_BGR); HardwarePixelBufferSharedPtr pixelBuffer = tex->getBuffer(); pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* pDest = static_cast<uint8*>(pixelBox.data); if ( land != NULL ) { const char* const colours = land->mLandData->mColours; for ( int y = 0; y < size; y++ ) { for ( int x = 0; x < size; x++ ) { const size_t colourOffset = (y+fromY)*3*65 + (x+fromX)*3; assert( colourOffset < 65*65*3 && "Colour offset is out of the expected bounds of record" ); const unsigned char r = colours[colourOffset + 0]; const unsigned char g = colours[colourOffset + 1]; const unsigned char b = colours[colourOffset + 2]; //as is the case elsewhere we need to flip the y const size_t imageOffset = (size - 1 - y)*size*4 + x*4; pDest[imageOffset + 0] = b; pDest[imageOffset + 1] = g; pDest[imageOffset + 2] = r; } } } else { for ( int y = 0; y < size; y++ ) { for ( int x = 0; x < size; x++ ) { for ( int k = 0; k < 3; k++ ) { *pDest++ = 0; } } } } pixelBuffer->unlock(); return tex; }
//-------------------------------------------------------------------------- void Texture::_loadImages( const ConstImagePtrList& images ) { if(images.size() < 1) OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot load empty vector of images", "Texture::loadImages"); // Set desired texture size and properties from images[0] mSrcWidth = mWidth = images[0]->getWidth(); mSrcHeight = mHeight = images[0]->getHeight(); mSrcDepth = mDepth = images[0]->getDepth(); // Get source image format and adjust if required mSrcFormat = images[0]->getFormat(); if (mTreatLuminanceAsAlpha && mSrcFormat == PF_L8) { mSrcFormat = PF_A8; } if (mDesiredFormat != PF_UNKNOWN) { // If have desired format, use it mFormat = mDesiredFormat; } else { // Get the format according with desired bit depth mFormat = PixelUtil::getFormatForBitDepths(mSrcFormat, mDesiredIntegerBitDepth, mDesiredFloatBitDepth); } // The custom mipmaps in the image have priority over everything size_t imageMips = images[0]->getNumMipmaps(); if(imageMips > 0) { mNumMipmaps = mNumRequestedMipmaps = images[0]->getNumMipmaps(); // Disable flag for auto mip generation mUsage &= ~TU_AUTOMIPMAP; } // Create the texture createInternalResources(); // Check if we're loading one image with multiple faces // or a vector of images representing the faces size_t faces; bool multiImage; // Load from multiple images? if(images.size() > 1) { faces = images.size(); multiImage = true; } else { faces = images[0]->getNumFaces(); multiImage = false; } // Check wether number of faces in images exceeds number of faces // in this texture. If so, clamp it. if(faces > getNumFaces()) faces = getNumFaces(); if (TextureManager::getSingleton().getVerbose()) { // Say what we're doing StringUtil::StrStreamType str; str << "Texture: " << mName << ": Loading " << faces << " faces" << "(" << PixelUtil::getFormatName(images[0]->getFormat()) << "," << images[0]->getWidth() << "x" << images[0]->getHeight() << "x" << images[0]->getDepth() << ") with "; if (!(mMipmapsHardwareGenerated && mNumMipmaps == 0)) str << mNumMipmaps; if(mUsage & TU_AUTOMIPMAP) { if (mMipmapsHardwareGenerated) str << " hardware"; str << " generated mipmaps"; } else { str << " custom mipmaps"; } if(multiImage) str << " from multiple Images."; else str << " from Image."; // Scoped { // Print data about first destination surface HardwarePixelBufferSharedPtr buf = getBuffer(0, 0); str << " Internal format is " << PixelUtil::getFormatName(buf->getFormat()) << "," << buf->getWidth() << "x" << buf->getHeight() << "x" << buf->getDepth() << "."; } LogManager::getSingleton().logMessage( LML_NORMAL, str.str()); } // Main loading loop // imageMips == 0 if the image has no custom mipmaps, otherwise contains the number of custom mips for(size_t mip = 0; mip<=imageMips; ++mip) { for(size_t i = 0; i < faces; ++i) { PixelBox src; if(multiImage) { // Load from multiple images src = images[i]->getPixelBox(0, mip); } else { // Load from faces of images[0] src = images[0]->getPixelBox(i, mip); } // Sets to treated format in case is difference src.format = mSrcFormat; if(mGamma != 1.0f) { // Apply gamma correction // Do not overwrite original image but do gamma correction in temporary buffer MemoryDataStreamPtr buf; // for scoped deletion of conversion buffer buf.bind(OGRE_NEW MemoryDataStream( PixelUtil::getMemorySize( src.getWidth(), src.getHeight(), src.getDepth(), src.format))); PixelBox corrected = PixelBox(src.getWidth(), src.getHeight(), src.getDepth(), src.format, buf->getPtr()); PixelUtil::bulkPixelConversion(src, corrected); Image::applyGamma(static_cast<uint8*>(corrected.data), mGamma, corrected.getConsecutiveSize(), static_cast<uchar>(PixelUtil::getNumElemBits(src.format))); // Destination: entire texture. blitFromMemory does the scaling to // a power of two for us when needed getBuffer(i, mip)->blitFromMemory(corrected); } else { // Destination: entire texture. blitFromMemory does the scaling to // a power of two for us when needed getBuffer(i, mip)->blitFromMemory(src); } } } // Update size (the final size, not including temp space) mSize = getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat); }
void WriteToTexture(const String &str, TexturePtr destTexture, Image::Box destRectangle, Font* font, const ColourValue &color, char justify, bool wordwrap) { using namespace Ogre; if (destTexture->getHeight() < destRectangle.bottom) destRectangle.bottom = destTexture->getHeight(); if (destTexture->getWidth() < destRectangle.right) destRectangle.right = destTexture->getWidth(); if (!font->isLoaded()) font->load(); TexturePtr fontTexture = (TexturePtr) TextureManager::getSingleton().getByName(font->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName()); HardwarePixelBufferSharedPtr fontBuffer = fontTexture->getBuffer(); HardwarePixelBufferSharedPtr destBuffer = destTexture->getBuffer(); PixelBox destPb = destBuffer->lock(destRectangle,HardwareBuffer::HBL_NORMAL); // The font texture buffer was created write only...so we cannot read it back :o). One solution is to copy the buffer instead of locking it. (Maybe there is a way to create a font texture which is not write_only ?) // create a buffer size_t nBuffSize = fontBuffer->getSizeInBytes(); unsigned char* buffer = (unsigned char*)calloc(nBuffSize, sizeof(unsigned char)); // create pixel box using the copy of the buffer PixelBox fontPb(fontBuffer->getWidth(), fontBuffer->getHeight(),fontBuffer->getDepth(), fontBuffer->getFormat(), buffer); fontBuffer->blitToMemory(fontPb); unsigned char* fontData = static_cast<unsigned char*>( fontPb.data ); unsigned char* destData = static_cast<unsigned char*>( destPb.data ); const size_t fontPixelSize = PixelUtil::getNumElemBytes(fontPb.format); const size_t destPixelSize = PixelUtil::getNumElemBytes(destPb.format); const size_t fontRowPitchBytes = fontPb.rowPitch * fontPixelSize; const size_t destRowPitchBytes = destPb.rowPitch * destPixelSize; Box *GlyphTexCoords; GlyphTexCoords = new Box[str.size()]; Font::UVRect glypheTexRect; size_t charheight = 0; size_t charwidth = 0; for (unsigned int i = 0; i < str.size(); i++) { if ((str[i] != '\t') && (str[i] != '\n') && (str[i] != ' ')) { glypheTexRect = font->getGlyphTexCoords(str[i]); GlyphTexCoords[i].left = glypheTexRect.left * fontTexture->getSrcWidth(); GlyphTexCoords[i].top = glypheTexRect.top * fontTexture->getSrcHeight(); GlyphTexCoords[i].right = glypheTexRect.right * fontTexture->getSrcWidth(); GlyphTexCoords[i].bottom = glypheTexRect.bottom * fontTexture->getSrcHeight(); if (GlyphTexCoords[i].getHeight() > charheight) charheight = GlyphTexCoords[i].getHeight(); if (GlyphTexCoords[i].getWidth() > charwidth) charwidth = GlyphTexCoords[i].getWidth(); } } size_t cursorX = 0; size_t cursorY = 0; size_t lineend = destRectangle.getWidth(); bool carriagreturn = true; for (unsigned int strindex = 0; strindex < str.size(); strindex++) { switch(str[strindex]) { case ' ': cursorX += charwidth; break; case '\t': cursorX += charwidth * 3; break; case '\n': cursorY += charheight; carriagreturn = true; break; default: { //wrapping if ((cursorX + GlyphTexCoords[strindex].getWidth()> lineend) && !carriagreturn ) { cursorY += charheight; carriagreturn = true; } //justify if (carriagreturn) { size_t l = strindex; size_t textwidth = 0; size_t wordwidth = 0; while( (l < str.size() ) && (str[l] != '\n')) { wordwidth = 0; switch (str[l]) { case ' ': wordwidth = charwidth; ++l; break; case '\t': wordwidth = charwidth *3; ++l; break; case '\n': l = str.size(); } if (wordwrap) while((l < str.size()) && (str[l] != ' ') && (str[l] != '\t') && (str[l] != '\n')) { wordwidth += GlyphTexCoords[l].getWidth(); ++l; } else { wordwidth += GlyphTexCoords[l].getWidth(); l++; } if ((textwidth + wordwidth) <= destRectangle.getWidth()) textwidth += (wordwidth); else break; } if ((textwidth == 0) && (wordwidth > destRectangle.getWidth())) textwidth = destRectangle.getWidth(); switch (justify) { case 'c': cursorX = (destRectangle.getWidth() - textwidth)/2; lineend = destRectangle.getWidth() - cursorX; break; case 'r': cursorX = (destRectangle.getWidth() - textwidth); lineend = destRectangle.getWidth(); break; default: cursorX = 0; lineend = textwidth; break; } carriagreturn = false; } //abort - net enough space to draw if ((cursorY + charheight) > destRectangle.getHeight()) goto stop; //draw pixel by pixel for (size_t i = 0; i < GlyphTexCoords[strindex].getHeight(); i++ ) for (size_t j = 0; j < GlyphTexCoords[strindex].getWidth(); j++) { float alpha = color.a * (fontData[(i + GlyphTexCoords[strindex].top) * fontRowPitchBytes + (j + GlyphTexCoords[strindex].left) * fontPixelSize +1 ] / 255.0); float invalpha = 1.0 - alpha; size_t offset = (i + cursorY) * destRowPitchBytes + (j + cursorX) * destPixelSize; ColourValue pix; PixelUtil::unpackColour(&pix,destPb.format,&destData[offset]); pix = (pix * invalpha) + (color * alpha); PixelUtil::packColour(pix,destPb.format,&destData[offset]); } cursorX += GlyphTexCoords[strindex].getWidth(); }//default }//switch }//for stop: delete[] GlyphTexCoords; destBuffer->unlock(); // Free the memory allocated for the buffer free(buffer); buffer = 0; }
void WebView::resize(int width, int height) { if(width == viewWidth && height == viewHeight) return; viewWidth = width; viewHeight = height; int newTexWidth = viewWidth; int newTexHeight = viewHeight; if(!Bitwise::isPO2(viewWidth) || !Bitwise::isPO2(viewHeight)) { if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_NON_POWER_OF_2_TEXTURES)) { if(Root::getSingleton().getRenderSystem()->getCapabilities()->getNonPOW2TexturesLimited()) compensateNPOT = true; } else compensateNPOT = true; compensateNPOT=true; if(compensateNPOT) { newTexWidth = Bitwise::firstPO2From(viewWidth); newTexHeight = Bitwise::firstPO2From(viewHeight); } } overlay->resize(viewWidth, viewHeight); #ifdef HAVE_AWESOMIUM webView->resize(viewWidth, viewHeight); #endif uint16 oldTexWidth = texWidth; uint16 oldTexHeight = texHeight; texWidth = newTexWidth; texHeight = newTexHeight; if (compensateNPOT) { Ogre::Real u1,v1,u2,v2; getDerivedUV(u1, v1, u2,v2); overlay->panel->setUV(u1, v1, u2, v2); } if (texWidth == oldTexWidth && texHeight == oldTexHeight) return; matPass->removeAllTextureUnitStates(); maskTexUnit = 0; #if defined(HAVE_AWESOMIUM)|| !defined (__APPLE__) Ogre::TextureManager::getSingleton().remove(viewName + "Texture"); TexturePtr texture = TextureManager::getSingleton().createManual( viewName + "Texture", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, texWidth, texHeight, 0, PF_BYTE_BGRA, TU_DYNAMIC_WRITE_ONLY_DISCARDABLE, this); HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); texDepth = Ogre::PixelUtil::getNumElemBytes(pixelBox.format); texPitch = (pixelBox.rowPitch*texDepth); uint8* pDest = static_cast<uint8*>(pixelBox.data); memset(pDest, 128, texHeight*texPitch); pixelBuffer->unlock(); #endif baseTexUnit = matPass->createTextureUnitState(viewName + "Texture"); baseTexUnit->setTextureFiltering(texFiltering, texFiltering, FO_NONE); if(texFiltering == FO_ANISOTROPIC) baseTexUnit->setTextureAnisotropy(4); if(usingMask) { setMask(maskImageParameters.first, maskImageParameters.second); } else if(alphaCache) { delete[] alphaCache; alphaCache = new unsigned char[texWidth * texHeight]; alphaCachePitch = texWidth; } }
void WebView::setMask(std::string maskFileName, std::string groupName) { if(usingMask) { if(maskTexUnit) { matPass->removeTextureUnitState(1); maskTexUnit = 0; } if(!TextureManager::getSingleton().getByName(viewName + "MaskTexture").isNull()) TextureManager::getSingleton().remove(viewName + "MaskTexture"); } if(alphaCache) { delete[] alphaCache; alphaCache = 0; } if(maskFileName == "") { usingMask = false; maskImageParameters.first = ""; maskImageParameters.second = ""; if(isWebViewTransparent) { setTransparent(true); update(); } return; } maskImageParameters.first = maskFileName; maskImageParameters.second = groupName; if(!maskTexUnit) { maskTexUnit = matPass->createTextureUnitState(); maskTexUnit->setIsAlpha(true); maskTexUnit->setTextureFiltering(FO_NONE, FO_NONE, FO_NONE); maskTexUnit->setColourOperationEx(LBX_SOURCE1, LBS_CURRENT, LBS_CURRENT); maskTexUnit->setAlphaOperation(LBX_MODULATE); } Image srcImage; srcImage.load(maskFileName, groupName); Ogre::PixelBox srcPixels = srcImage.getPixelBox(); unsigned char* conversionBuf = 0; if(srcImage.getFormat() != Ogre::PF_BYTE_A) { size_t dstBpp = Ogre::PixelUtil::getNumElemBytes(Ogre::PF_BYTE_A); conversionBuf = new unsigned char[srcImage.getWidth() * srcImage.getHeight() * dstBpp]; Ogre::PixelBox convPixels(Ogre::Box(0, 0, srcImage.getWidth(), srcImage.getHeight()), Ogre::PF_BYTE_A, conversionBuf); Ogre::PixelUtil::bulkPixelConversion(srcImage.getPixelBox(), convPixels); srcPixels = convPixels; } TexturePtr maskTexture = TextureManager::getSingleton().createManual( viewName + "MaskTexture", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, texWidth, texHeight, 0, PF_BYTE_A, TU_STATIC_WRITE_ONLY); HardwarePixelBufferSharedPtr pixelBuffer = maskTexture->getBuffer(); pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); size_t maskTexDepth = Ogre::PixelUtil::getNumElemBytes(pixelBox.format); alphaCachePitch = pixelBox.rowPitch; alphaCache = new unsigned char[alphaCachePitch*texHeight]; uint8* buffer = static_cast<uint8*>(pixelBox.data); memset(buffer, 0, alphaCachePitch * texHeight); size_t minRowSpan = std::min(alphaCachePitch, srcPixels.rowPitch); size_t minHeight = std::min(texHeight, (unsigned short)srcPixels.getHeight()); if(maskTexDepth == 1) { for(unsigned int row = 0; row < minHeight; row++) memcpy(buffer + row * alphaCachePitch, (unsigned char*)srcPixels.data + row * srcPixels.rowPitch, minRowSpan); memcpy(alphaCache, buffer, alphaCachePitch*texHeight); } else if(maskTexDepth == 4) { size_t destRowOffset, srcRowOffset, cacheRowOffset; for(unsigned int row = 0; row < minHeight; row++) { destRowOffset = row * alphaCachePitch * maskTexDepth; srcRowOffset = row * srcPixels.rowPitch; cacheRowOffset = row * alphaCachePitch; for(unsigned int col = 0; col < minRowSpan; col++) alphaCache[cacheRowOffset + col] = buffer[destRowOffset + col * maskTexDepth + 3] = ((unsigned char*)srcPixels.data)[srcRowOffset + col]; } } else { OGRE_EXCEPT(Ogre::Exception::ERR_RT_ASSERTION_FAILED, "Unexpected depth and format were encountered while creating a PF_BYTE_A HardwarePixelBuffer. Pixel format: " + StringConverter::toString((uint32)pixelBox.format) + ", Depth:" + StringConverter::toString(maskTexDepth), "WebView::setMask"); } pixelBuffer->unlock(); if(conversionBuf) delete[] conversionBuf; maskTexUnit->setTextureName(viewName + "MaskTexture"); usingMask = true; }
void D3D10HardwarePixelBuffer::blit(const HardwarePixelBufferSharedPtr &rsrc, const Image::Box &srcBox, const Image::Box &dstBox) { if ( (srcBox.getWidth() != dstBox.getWidth()) || (srcBox.getHeight() != dstBox.getHeight()) || (srcBox.getDepth() != dstBox.getDepth()) ) { OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3D10 device cannot copy a subresource - source and dest size are not the same and they have to be the same in DX10.", "D3D10HardwarePixelBuffer::blit"); } D3D10_BOX srcBoxDx10 = OgreImageBoxToDx10Box(srcBox); D3D10HardwarePixelBuffer * rsrcDx10 = static_cast<D3D10HardwarePixelBuffer *>(rsrc.get()); switch(mParentTexture->getTextureType()) { case TEX_TYPE_1D: { mDevice->CopySubresourceRegion( mParentTexture->GetTex1D(), static_cast<UINT>(mSubresourceIndex), static_cast<UINT>(dstBox.left), 0, 0, rsrcDx10->mParentTexture->GetTex1D(), static_cast<UINT>(rsrcDx10->mSubresourceIndex), &srcBoxDx10); if (mDevice.isError()) { String errorDescription = mDevice.getErrorDescription(); OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3D10 device cannot copy 1d subresource Region\nError Description:" + errorDescription, "D3D10HardwarePixelBuffer::blit"); } } break; case TEX_TYPE_CUBE_MAP: case TEX_TYPE_2D: { mDevice->CopySubresourceRegion( mParentTexture->GetTex2D(), static_cast<UINT>(mSubresourceIndex), static_cast<UINT>(dstBox.left), static_cast<UINT>(dstBox.top), mFace, rsrcDx10->mParentTexture->GetTex2D(), static_cast<UINT>(rsrcDx10->mSubresourceIndex), &srcBoxDx10); if (mDevice.isError()) { String errorDescription = mDevice.getErrorDescription(); OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3D10 device cannot copy 2d subresource Region\nError Description:" + errorDescription, "D3D10HardwarePixelBuffer::blit"); } } break; case TEX_TYPE_3D: { mDevice->CopySubresourceRegion( mParentTexture->GetTex2D(), static_cast<UINT>(mSubresourceIndex), static_cast<UINT>(dstBox.left), static_cast<UINT>(dstBox.top), static_cast<UINT>(dstBox.front), rsrcDx10->mParentTexture->GetTex2D(), static_cast<UINT>(rsrcDx10->mSubresourceIndex), &srcBoxDx10); if (mDevice.isError()) { String errorDescription = mDevice.getErrorDescription(); OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3D10 device cannot copy 3d subresource Region\nError Description:" + errorDescription, "D3D10HardwarePixelBuffer::blit"); } } break; } _genMipmaps(); }
//------------------------------------------------------------------------------ void SegmentedDynamicLightManager::updateTextureFromSegmentedLists(const Camera* i_pCamera) { float spotIntensity = 1; HardwarePixelBufferSharedPtr pBuf = mLightTexture->getBuffer(); void* pStartPos = pBuf->lock(HardwareBuffer::HBL_DISCARD); uint16* pData = (uint16*)pStartPos; size_t remainBufWidth = mTextureWidth; for(size_t j = 0; j < SDL_SEGMENT_GRID_SIZE; ++j) { //assign first row with number of indexes in the block float maxRow = (float)(mSegmentedLightGrid[j].size() - 1 + SDL_TEXTURE_DATA_ROWS); PixelUtil::packColour(maxRow,0.0f,0.0f,0.0f,PF_FLOAT16_RGBA, pData); pData += 4 * SDL_LIGHT_DATA_SIZE; remainBufWidth -= SDL_LIGHT_DATA_SIZE; } //advance the remaining space of the row pData += 4 * remainBufWidth; for(size_t i = 0 ; i < SDL_LIGHT_PER_BLOCK ; ++i) { remainBufWidth = mTextureWidth; for(size_t j = 0; j < SDL_SEGMENT_GRID_SIZE; ++j) { if (i < mSegmentedLightGrid[j].size()) { const Light* pLight = mSegmentedLightGrid[j][i]; const Vector3& position = pLight->getDerivedPosition(true); Vector3 direction = -pLight->getDerivedDirection(); direction.normalise(); // Update spotlight parameters. Vector3 spotParam; float inverseRange = 1.0f / (float)pLight->getAttenuationRange(); float spotAngle = -1; float spotInvAngleRange = std::numeric_limits<float>::max(); if (pLight->getType() == Light::LT_SPOTLIGHT) { Real phi = Math::Cos(pLight->getSpotlightOuterAngle().valueRadians() * 0.5f); Real theta = Math::Cos(pLight->getSpotlightInnerAngle().valueRadians() * 0.5f); spotAngle = (float)phi; spotInvAngleRange = 1.0f / (float)(theta - phi); } PixelUtil::packColour( (float)position.x, (float)position.y, (float)position.z, inverseRange, PF_FLOAT16_RGBA, pData); pData += 4; PixelUtil::packColour( (float)direction.x, (float)direction.y, (float)direction.z, spotAngle, PF_FLOAT16_RGBA, pData); pData += 4; PixelUtil::packColour( pLight->getDiffuseColour().r * spotIntensity, pLight->getDiffuseColour().g * spotIntensity, pLight->getDiffuseColour().b * spotIntensity, spotInvAngleRange, PF_FLOAT16_RGBA, pData); pData += 4; } else { //assign position zero with zero width PixelUtil::packColour(0.0f,0.0f,0.0f,std::numeric_limits<float>::max(), PF_FLOAT16_RGBA, pData); pData += 4; for(int d = 0 ; d < (SDL_LIGHT_DATA_SIZE - 1) ; ++d) { PixelUtil::packColour(0.0f,0.0f,0.0f,0.0f,PF_FLOAT16_RGBA, pData); pData += 4; } } remainBufWidth -= 3; } //advance the remaining space of the row pData += 4 * remainBufWidth; } //Check for memory overrun if (pBuf->getSizeInBytes() != (const char*)(void*)pData - (const char*)pStartPos) { throw "memory overrun"; } pBuf->unlock(); }
void LodTextureManager::loadResource(Ogre::Resource* res) { int alpha = 0; angel::Log << angel::aeLog::debug <<"loading texture " << res->getName() << angel::aeLog::endl; angel::pLodData ldata = angel::LodManager.LoadFileData( res->getName() ); // angel::pLodData ldata=angel::LodManager.LoadFile( res->getName() ); if(!ldata) return GetDefaultTexture(res); angel::pLodData hdr = angel::LodManager.LoadFileHdr( res->getName() ); BYTE*data= &((*ldata)[0]); BYTE*hdrdata= &((*hdr)[0]); int size = (int)ldata->size(); int psize = *(int*)(hdrdata+0x4); unsigned int unpsize1 = *(int*)(hdrdata+0x0); unsigned long unpsize2 = *(int*)(hdrdata+0x18); if( unpsize2+0x300 != size ) { //angel::Log <<"texture " << res->getName() << " error datasize " << unpsize2 << "/" << size<< angel::aeLog::endl; //return GetDefaultTexture(res); return loadSprite(res,hdr,ldata); } // if( unpsize2 && unpsize2 < unpsize1) // return GetDefaultTexture(res); BYTE* pal = data + unpsize2; int width = *(WORD*)(hdrdata+0x8); int height = *(WORD*)(hdrdata+0xa); int imgsize = width*height; BYTE *pSrc=data; /* BYTE*data= &((*ldata)[0]); int size = (int)ldata->size(); int psize = *(int*)(data+0x14); unsigned int unpsize1 = *(int*)(data+0x10); unsigned long unpsize2 = *(int*)(data+0x28); if( psize+0x30+0x300 != size ) return GetDefaultTexture(res); if( unpsize2 && unpsize2 < unpsize1) return GetDefaultTexture(res); BYTE* pal = data + 0x30 + psize; BYTE*unpdata = new BYTE[unpsize2 ]; boost::scoped_array<BYTE> sunpdata(unpdata); if ( uncompress( unpdata, &unpsize2 , data + 0x30, psize ) != Z_OK ) return; int width = *(WORD*)(data+0x18); int height = *(WORD*)(data+0x1a); int imgsize = width*height; BYTE *pSrc=unpdata;*/ int nummipmaps = 3; // Create the texture Texture* texture = static_cast<Texture*>(res); texture->setTextureType(TEX_TYPE_2D); texture->setWidth(width); texture->setHeight(height); texture->setNumMipmaps(nummipmaps); texture->setFormat(PF_BYTE_BGRA); texture->setUsage(TU_DEFAULT); texture->setDepth(1); texture->setHardwareGammaEnabled(false); texture->setFSAA(0); texture->createInternalResources(); /*TextureManager::getSingleton().createManual( name + ".Texture", // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, // type width, height, // width & height nummipmaps, // number of mipmaps PF_BYTE_BGRA, // pixel format TU_DEFAULT); // usage; should be TU_DYNAMIC_WRITE_ONLY_DISCARDABLE for */ // Fill in some pixel data. This will give a semi-transparent blue, // but this is of course dependent on the chosen pixel format. int w=width; int h=height; int n=0,off=0; nummipmaps = (int)texture->getNumMipmaps(); for ( n = 0,off= 0; off < (int)unpsize2 && n <nummipmaps + 1 ; n++) { if( w < 1 || h <1 ) break; // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(0,n); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* pDest = static_cast<uint8*>(pixelBox.data); for (int j = 0; j < w; j++) for(int i = 0; i < h; i++) { int index=*pSrc++; int r = pal[index*3+0]; int g = pal[index*3+1]; int b = pal[index*3+2]; int a = 0xff; if( index == 0 && ((r == 0 && g >250 && b > 250) || (r > 250 && g ==0 && b > 250))) { alpha=1; a= 0; r=g=b=0; } *pDest++ = b; // G *pDest++ = g; // R *pDest++ = r; *pDest++ = a; // A } pixelBuffer->unlock(); //off += w*h; w/=2; h/=2; } // Unlock the pixel buffer }
TexturePtr GetLodTexture(const std::string& name) { std::string texname(name + ".Texture"); if(TextureManager::getSingleton().resourceExists(texname)) return TextureManager::getSingleton().getByName(texname); angel::Log << "loading texture " << name << angel::aeLog::endl; int alpha = 0; angel::pLodData ldata=angel::LodManager.LoadFile( name ); if(!ldata) return GetDefaultTexture(); BYTE*data= &((*ldata)[0]); int size = (int)ldata->size(); int psize = *(int*)(data+0x14); unsigned int unpsize1 = *(int*)(data+0x10); unsigned long unpsize2 = *(int*)(data+0x28); if( psize+0x30+0x300 != size ) return GetDefaultTexture(); if( unpsize2 && unpsize2 < unpsize1) return GetDefaultTexture(); BYTE* pal = data + 0x30 + psize; BYTE*unpdata = new BYTE[unpsize2 ]; boost::scoped_array<BYTE> sunpdata(unpdata); if ( uncompress( unpdata, &unpsize2 , data + 0x30, psize ) != Z_OK ) return GetDefaultTexture(); int width = *(WORD*)(data+0x18); int height = *(WORD*)(data+0x1a); int imgsize = width*height; BYTE *pSrc=unpdata; int nummipmaps = 3; // Create the texture TexturePtr texture = TextureManager::getSingleton().createManual( name + ".Texture", // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, // type width, height, // width & height nummipmaps, // number of mipmaps PF_BYTE_BGRA, // pixel format TU_DEFAULT); // usage; should be TU_DYNAMIC_WRITE_ONLY_DISCARDABLE for // Fill in some pixel data. This will give a semi-transparent blue, // but this is of course dependent on the chosen pixel format. int w=width; int h=height; int n=0,off=0; nummipmaps = (int)texture->getNumMipmaps(); for ( n = 0,off= 0; off < (int)unpsize2 && n <nummipmaps + 1 ; n++) { if( w < 1 || h <1 ) break; // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(0,n); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); uint8* pDest = static_cast<uint8*>(pixelBox.data); for (int j = 0; j < w; j++) for(int i = 0; i < h; i++) { int index=*pSrc++; int r = pal[index*3+0]; int g = pal[index*3+1]; int b = pal[index*3+2]; int a = 0xff; if( index == 0 && ((r == 0 && g >250 && b > 250) || (r > 250 && g ==0 && b > 250))) { alpha=1; a= 0; r=g=b=0; } *pDest++ = b; // G *pDest++ = g; // R *pDest++ = r; *pDest++ = a; // A } pixelBuffer->unlock(); //off += w*h; w/=2; h/=2; } // Unlock the pixel buffer return texture; }
//-------------------------------------------------------------------------- void CompositorDemo::createTextures(void) { using namespace Ogre; TexturePtr tex = TextureManager::getSingleton().createManual( "HalftoneVolume", "General", TEX_TYPE_3D, 64,64,64, 0, PF_A8 ); HardwarePixelBufferSharedPtr ptr = tex->getBuffer(0,0); ptr->lock(HardwareBuffer::HBL_DISCARD); const PixelBox &pb = ptr->getCurrentLock(); uint8 *data = static_cast<uint8*>(pb.data); size_t height = pb.getHeight(); size_t width = pb.getWidth(); size_t depth = pb.getDepth(); size_t rowPitch = pb.rowPitch; size_t slicePitch = pb.slicePitch; for (size_t z = 0; z < depth; ++z) { for (size_t y = 0; y < height; ++y) { for(size_t x = 0; x < width; ++x) { float fx = 32-(float)x+0.5f; float fy = 32-(float)y+0.5f; float fz = 32-((float)z)/3+0.5f; float distanceSquare = fx*fx+fy*fy+fz*fz; data[slicePitch*z + rowPitch*y + x] = 0x00; if (distanceSquare < 1024.0f) data[slicePitch*z + rowPitch*y + x] += 0xFF; } } } ptr->unlock(); Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->getViewport(0); TexturePtr tex2 = TextureManager::getSingleton().createManual( "DitherTex", "General", TEX_TYPE_2D, vp->getActualWidth(),vp->getActualHeight(),1, 0, PF_A8 ); HardwarePixelBufferSharedPtr ptr2 = tex2->getBuffer(0,0); ptr2->lock(HardwareBuffer::HBL_DISCARD); const PixelBox &pb2 = ptr2->getCurrentLock(); uint8 *data2 = static_cast<uint8*>(pb2.data); size_t height2 = pb2.getHeight(); size_t width2 = pb2.getWidth(); size_t rowPitch2 = pb2.rowPitch; for (size_t y = 0; y < height2; ++y) { for(size_t x = 0; x < width2; ++x) { data2[rowPitch2*y + x] = Ogre::Math::RangeRandom(64.0,192); } } ptr2->unlock(); }
//Parse sceneMetaData into UserTexture void KinectDevice::ParseUserTexture(xn::SceneMetaData *sceneMetaData, bool m_front) { #if SHOW_DEPTH || SHOW_BAR //TexturePtr texture = TextureManager::getSingleton().getByName("MyDepthTexture"); //TexturePtr texture = TextureManager::getSingleton().getByName("MyDepthTexture2"); if(mUserTexture.isNull()) return; // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = mUserTexture->getBuffer(); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); unsigned char* pDest = static_cast<unsigned char*>(pixelBox.data); // Get label map const XnLabel* pUsersLBLs = sceneMetaData->Data(); for (size_t j = 0; j < KINECT_DEPTH_HEIGHT; j++) { pDest = static_cast<unsigned char*>(pixelBox.data) + j*pixelBox.rowPitch*4; #if SHOW_DEPTH for(size_t i = 0; i < KINECT_DEPTH_WIDTH; i++) #elif SHOW_BAR for(size_t i = 0; i < 50; i++) #endif { // fix i if we are mirrored uint fixed_i = i; if(!m_front) { fixed_i = KINECT_DEPTH_WIDTH - i; } // determine color #if SHOW_DEPTH unsigned int color = GetColorForUser(pUsersLBLs[j*KINECT_DEPTH_WIDTH + fixed_i]); // if we have a candidate, filter out the rest if (m_candidateID != 0) { if (m_candidateID == pUsersLBLs[j*KINECT_DEPTH_WIDTH + fixed_i]) { color = GetColorForUser(1); if( j > KINECT_DEPTH_HEIGHT*(1 - m_pStartPoseDetector->GetDetectionPercent())) { //highlight user color |= 0xFF070707; } if( j < KINECT_DEPTH_HEIGHT*(m_pEndPoseDetector->GetDetectionPercent())) { //hide user color &= 0x20F0F0F0; } } else { color = 0; } } #elif SHOW_BAR // RED. kinda. unsigned int color = 0x80FF0000; if( j > KINECT_DEPTH_HEIGHT*(1 - m_pStartPoseDetector->GetDetectionPercent())) { //highlight user color |= 0xFF070707; } if( j < KINECT_DEPTH_HEIGHT*(m_pEndPoseDetector->GetDetectionPercent())) { //hide user color &= 0x20F0F0F0; } if ((m_pStartPoseDetector->GetDetectionPercent() == 1) || (m_pEndPoseDetector->GetDetectionPercent() == 1)) { color = 0; } #endif // write to output buffer *((unsigned int*)pDest) = color; pDest+=4; } } // Unlock the pixel buffer pixelBuffer->unlock(); #endif // SHOW_DEPTH }