void CBitmap::Save(string const& filename) { ilOriginFunc(IL_ORIGIN_UPPER_LEFT); ilEnable(IL_ORIGIN_SET); unsigned char* buf=new unsigned char[xsize*ysize*4]; /* HACK Flip the image so it saves the right way up. (Fiddling with ilOriginFunc didn't do anything?) Duplicated with ReverseYAxis. */ for(int y=0;y<ysize;++y){ for(int x=0;x<xsize;++x){ buf[((ysize-1-y)*xsize+x)*4+0]=mem[((y)*xsize+x)*4+0]; buf[((ysize-1-y)*xsize+x)*4+1]=mem[((y)*xsize+x)*4+1]; buf[((ysize-1-y)*xsize+x)*4+2]=mem[((y)*xsize+x)*4+2]; buf[((ysize-1-y)*xsize+x)*4+3]=mem[((y)*xsize+x)*4+3]; } } ilHint(IL_COMPRESSION_HINT, IL_USE_COMPRESSION); ilSetInteger (IL_JPG_QUALITY, 80); ILuint ImageName = 0; ilGenImages(1, &ImageName); ilBindImage(ImageName); ilTexImage(xsize,ysize,1,4,IL_RGBA,IL_UNSIGNED_BYTE,NULL); ilSetData(buf); ilSaveImage((char*)filename.c_str()); ilDeleteImages(1,&ImageName); delete[] buf; }
void FractureBox::boldenLines() { const int width = ilGetInteger(IL_IMAGE_WIDTH); const int height = ilGetInteger(IL_IMAGE_HEIGHT); const int depth = 3; //ilGetInteger(IL_IMAGE_DEPTH); ILubyte *newData = new ILubyte[width * height * depth]; ILubyte *oldData = ilGetData(); //Now loop through the data and when a black pixel is detected, copy the pixel into //the buffer surrounded by pixels for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { const int i = (depth * x) + (y * depth * width); if(oldData[i] == 0 && oldData[i+1] == 0 && oldData[i+2] == 0) { const int top = pixelOffset(i, 0, -1, width, height, depth); const int topLeft = pixelOffset(i, -1, -1, width, height, depth); const int topRight = pixelOffset(i, 1, -1, width, height, depth); const int bottom = pixelOffset(i, 0, 1, width, height, depth); const int bottomLeft = pixelOffset(i, -1, 1, width, height, depth); const int bottomRight = pixelOffset(i, 1, 1, width, height, depth); const int left = pixelOffset(i, -1, 0, width, height, depth); const int right = pixelOffset(i, 1, 0, width, height, depth); newData[i] = 0; newData[i + 1] = 0; newData[i + 2] = 0; setPixelBlack(top, newData); setPixelBlack(topLeft, newData); setPixelBlack(topRight, newData); setPixelBlack(bottom, newData); setPixelBlack(bottomLeft, newData); setPixelBlack(bottomRight, newData); setPixelBlack(left, newData); setPixelBlack(right, newData); } else { newData[i] = oldData[i]; newData[i+1] = oldData[i+1]; newData[i+2] = oldData[i+2]; } } } ilSetData(newData); delete[] newData; }
//------------------------------------------------------------------------------------ // Sets the image data //------------------------------------------------------------------------------------ u32 Image::SetData( u8* pData ) { ilBindImage(m_iImgID); return ilSetData(pData); } // SetData
void GeometryTerrain::computeLightmap(Vector3 _vlightSource, bool update) { bool bIntegrateNormals = false; std::vector<GLubyte> shadowMapTexture(GetWidth()*GetWidth()*4); float maxHeight = -99999.0f; for(int z =0; z <= GetLength(); ++z) for(int x = 0; x <= GetWidth(); ++x) maxHeight = max(getHeight(x,z), maxHeight); for(int z =0; z <= GetLength(); ++z) { for(int x = 0; x <= GetWidth(); ++x) { float ambientLight = 255; Ray lightRay(Vector3(x, getHeight(x, z), z), _vlightSource ); Vector3 current_ray_pos(Vector3(x, getHeight(x, z), z)); Vector3 direction_to_sun = lightRay.m_v3Direction.normalize(); int numRayHits = 0; while(!(current_ray_pos.x <= 0 || current_ray_pos.x >= GetWidth() || current_ray_pos.z <= 0 || current_ray_pos.z >= GetWidth() )) { if(current_ray_pos.y > maxHeight) break; // Is the terrain blocking the ray at this point? if(getHeight((int)floor(current_ray_pos.x), (int)floor(current_ray_pos.z)) > current_ray_pos.y) { numRayHits++; break; } //light still traveling... current_ray_pos += direction_to_sun; } float ambientLightNormals = 0; if(bIntegrateNormals) { Vector3 n( m_pNormals[x+z * (GetWidth()+1)].x, m_pNormals[x+z * (GetWidth()+1)].y, m_pNormals[x+z * (GetWidth()+1)].z ); ambientLightNormals = 0.5*( 1.0f + dot(n.normalize(), direction_to_sun) ); if(ambientLightNormals > 1.0f) ambientLightNormals = 1.0f; if(ambientLightNormals < 0.0f) ambientLightNormals = 0.0f; } if(numRayHits > 0) { //ambientLight = (current_ray_pos - Vector3(x,getHeight(x,z),z)).magnitude() - ambientLightNormals * 255; ambientLight = 170; if(ambientLight > 255) ambientLight = 255; if(ambientLight < 170) ambientLight = 170; } int index = (x + z * GetWidth()) * 3; for (int i = 0; i < 3; ++i) { shadowMapTexture[index + i] = (GLubyte)ambientLight; } } } for(int z =0; z <= GetLength(); ++z) { for(int x = 0; x <= GetWidth(); ++x) { int factor = 2; ColorOGL colCurrent; colCurrent.m_fRed = shadowMapTexture[(x + z * GetWidth()) * 3 + 0]; colCurrent.m_fGreen = shadowMapTexture[(x + z * GetWidth()) * 3 + 1]; colCurrent.m_fBlue = shadowMapTexture[(x + z * GetWidth()) * 3 + 2]; ColorOGL colT; ColorOGL colD; ColorOGL colL; ColorOGL colR; if(shadowMapTexture.size() > ((x + (z+factor) * GetWidth()) * 3 + 0) && shadowMapTexture.size() > ((x + (z+factor) * GetWidth()) * 3 + 1) && shadowMapTexture.size() > ((x + (z+factor) * GetWidth()) * 3 + 2) ) { colT.m_fRed = shadowMapTexture[(x + (z+factor) * GetWidth()) * 3 + 0]; colT.m_fGreen = shadowMapTexture[(x + (z+factor) * GetWidth()) * 3 + 1]; colT.m_fBlue = shadowMapTexture[(x + (z+factor) * GetWidth()) * 3 + 2]; } if(shadowMapTexture.size() > ((x + (z-factor) * GetWidth()) * 3 + 0) && shadowMapTexture.size() > ((x + (z-factor) * GetWidth()) * 3 + 1) && shadowMapTexture.size() > ((x + (z-factor) * GetWidth()) * 3 + 2) ) { colD.m_fRed = shadowMapTexture[(x + (z-factor) * GetWidth()) * 3 + 0]; colD.m_fGreen = shadowMapTexture[(x + (z-factor) * GetWidth()) * 3 + 1]; colD.m_fBlue = shadowMapTexture[(x + (z-factor) * GetWidth()) * 3 + 2]; } if(shadowMapTexture.size() > ( (x+factor + z * GetWidth()) * 3 + 0) && shadowMapTexture.size() > ((x+factor + z * GetWidth()) * 3 + 1) && shadowMapTexture.size() > ((x+factor + z * GetWidth()) * 3 + 2) ) { colL.m_fRed = shadowMapTexture[(x+factor + z * GetWidth()) * 3 + 0]; colL.m_fGreen = shadowMapTexture[(x+factor + z * GetWidth()) * 3 + 1]; colL.m_fBlue = shadowMapTexture[(x+factor + z * GetWidth()) * 3 + 2]; } if(shadowMapTexture.size() > (( x-factor + z * GetWidth()) * 3 + 0) && shadowMapTexture.size() > ((x-factor + z * GetWidth()) * 3 + 1) && shadowMapTexture.size() > ((x-factor + z * GetWidth()) * 3 + 2) ) { colR.m_fRed = shadowMapTexture[(x-factor + z * GetWidth()) * 3 + 0]; colR.m_fGreen = shadowMapTexture[(x-factor + z * GetWidth()) * 3 + 1]; colR.m_fBlue = shadowMapTexture[(x-factor + z * GetWidth()) * 3 + 2]; } shadowMapTexture[(x + z * GetWidth()) * 3 + 0] = (colT.m_fRed+colD.m_fRed+colR.m_fRed+colL.m_fRed+colCurrent.m_fRed)/5; shadowMapTexture[(x + z * GetWidth()) * 3 + 1] = (colT.m_fGreen+colD.m_fGreen+colR.m_fGreen+colL.m_fGreen+colCurrent.m_fGreen)/5; shadowMapTexture[(x + z * GetWidth()) * 3 + 2] = (colT.m_fBlue+colD.m_fBlue+colR.m_fBlue+colL.m_fBlue+colCurrent.m_fBlue)/5; } } if(update) { ResourceManager::getInstance()->getTexture2D("resources/textures/lightmap.tga")->bind(2); glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, GetWidth(), GetWidth(), GL_RGBA, GL_UNSIGNED_BYTE, &shadowMapTexture[0]); } else { ilTexImage(GetWidth(),GetWidth(), 1, 3, IL_RGB, IL_UNSIGNED_BYTE, &shadowMapTexture[0]); ilSetData(&shadowMapTexture[0]); ilSetInteger(IL_IMAGE_BITS_PER_PIXEL,32); ilEnable(IL_FILE_OVERWRITE); ilSave(IL_TGA, "resources/textures/lightmap.tga"); } }