ILboolean ilImage::Save(char *FileName, ILenum Type) { this->iGenBind(); return ilSave(Type, FileName); }
ILboolean ilImage::Save(ILconst_string FileName, ILenum Type) { this->iGenBind(); return ilSave(Type, FileName); }
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"); } }
bool CCLUDrawBase::SaveScreen2BMP(const char *pcFilename) { ILuint uImgID; int iWidth, iHeight; unsigned char *pImage; cStr csFilename, csBaseName, csExt; int iExtPos; bool bEPSFile = false; bool bJPS = false; csBaseName = csFilename = pcFilename; iExtPos = ('.' < csBaseName); csExt = csBaseName.Last(csFilename.Len() - iExtPos, 1); csBaseName |= -1; if (csExt == "eps") { csFilename = csBaseName + ".tmp"; bEPSFile = true; } else if (csExt == "jps") { csFilename = csBaseName + ".jpg"; bEPSFile = true; bJPS = true; } iWidth = m_iSizeX; iHeight = m_iSizeY; ilGenImages(1, &uImgID); ilBindImage(uImgID); if (iluEnlargeCanvas(iWidth, iHeight, 1) == IL_FALSE) { ilDeleteImages(1, &uImgID); return false; } ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); pImage = (unsigned char *) ilGetData(); // Get Pixels glPixelStorei(GL_PACK_ALIGNMENT,1); glReadBuffer(GL_FRONT_LEFT); glReadPixels( 0, 0, iWidth, iHeight, GL_RGB, GL_UNSIGNED_BYTE, pImage ); remove(csFilename.Str()); if (bEPSFile) { if (ilSave(IL_JPG, (ILstring) csFilename.Str()) == IL_FALSE) { ilDeleteImages(1, &uImgID); return false; } } else { if (ilSaveImage((ILstring) csFilename.Str()) == IL_FALSE) { ilDeleteImages(1, &uImgID); return false; } } ilDeleteImages(1, &uImgID); if (bEPSFile) { csBaseName += ".eps"; if (!ConvertJPEGtoPS(csFilename.Str(), csBaseName.Str())) return false; if (!bJPS) remove(csFilename.Str()); } return true; }
int main(int argc, char* argv[]) { ilInit(); unsigned int *pData = (unsigned int*)malloc(zlutSize*zlutSize*4); memset(pData, 0, zlutSize*zlutSize*4); // zone layout // *-----------* // | 0 / | \ 3 | // | / 4 | 7 \ | // |-----|-----| // | \ 5 | 6 / | // | 1 \ | / 2 | // *-----------* // for(int i=0; i<zlutSize; ++i) { for(int j=0; j<zlutSize; ++j) { // get uv. float u = (float(j)+0.5f)/zlutSize; float v = (float(i)+0.5f)/zlutSize; // compute the zone index. int index; float uu, vv; if(u < 0.5f) { if(v < 0.5f) { index = 0; uu = -u + 0.5f; vv = v; if(uu < vv) index += 4; } else { index = 1; uu = -u; vv = -v + 0.5f; if(uu < vv) index += 4; } } else { if(v < 0.5f) { index = 3; uu = u - 0.5f; vv = v; if(uu < vv) index += 4; } else { index = 2; uu = u - 1.0f; vv = -v + 0.5f; if(uu < vv) index += 4; } } // write the zone index. unsigned int value = 256 * index / 8; pData[i*zlutSize+j] = RGBA(value); } } // output as a targa image by using DevIL. ILuint iid; ilGenImages(1, &iid); ilBindImage(iid); ilTexImage(zlutSize, zlutSize, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, pData); ilSave(IL_TGA, "zlut.tga"); ilDeleteImages(1, &iid); free(pData); return 0; }
void main(int argc, char **argv) { ILubyte *Lump; ILubyte *final_tga; ILubyte *Data; ILuint Size; ILuint Image; FILE *File; ILenum Error; ILuint ilFormat; int x0,y0,x1,y1; //char filename[]="103,692-205,768.tga"; char filename[64]; if(argc!=0 && !strcmp(argv[2],"-laststep")) { File=fopen(argv[1], "r+"); fseek(File, 17, SEEK_SET); fprintf(File, "%c", 0x20); fclose(File); ilInit(); ilGenImages(1, &Image); ilBindImage(Image); if(!ilLoadImage(argv[1])) { printf("Error loading image final.tga"); return ; } ilEnable(IL_FILE_OVERWRITE); strcpy(filename, argv[1]); *strchr(filename, '\\')='\0'; strcat(filename, "\\final.png"); if(!ilSave(IL_PNG, filename)) printf("Error occurs when saving image."); else printf("Image is saved successfuly."); return ; } strcpy(filename, strchr(argv[2], '\\')+1); sscanf(filename, "%d, %d - %d, %d", &x0,&y0,&x1,&y1); //printf("%d %d %d %d\n", x0,y0,x1,y1); x0--; //start from (0,0), not (1,1) y0--; ilInit(); File = fopen(argv[2], "rb"); fseek(File, 0, SEEK_END); Size = ftell(File); fclose(File); Data = (ILubyte*)malloc(Size); ilGenImages(1, &Image); ilBindImage(Image); if(!ilLoadImage(argv[2])) { printf("Error loading image %s", filename); return ; } ilFormat=ilGetInteger(IL_IMAGE_FORMAT); ilCopyPixels(x0, y0, 0, x1-x0, y1-y0, 1, ilFormat, IL_UNSIGNED_BYTE, Data); if(!ilLoadImage(argv[1])) { printf("Error loading image final.tga"); return ; } ilSetPixels(x0, y0, 0, x1-x0, y1-y0, 1, ilFormat, IL_UNSIGNED_BYTE, Data); ilEnable(IL_FILE_OVERWRITE); if(!ilSaveImage(argv[1])) printf("Error occurs when saving image."); else printf("Image is saved successfuly."); ilDeleteImages(1, &Image); ilShutDown(); while ((Error = ilGetError()) != IL_NO_ERROR) { printf("%d: %s/n", Error, iluErrorString(Error)); } }
void testSavers2(ILenum type, const TCHAR* targetName, const TCHAR* targetExt) { TCHAR targetFN[MAX_PATH]; // Test ilSave _tcscpy(targetFN, targetName); _tcscat(targetFN, L".ilSave."); _tcscat(targetFN, targetExt); DWORD t1 = GetTickCount(); if (!ilSave(type, targetFN)) { printf("testSavers2: Failed to save " PathCharMod " using ilSave\n", targetFN); ++errors; } DWORD t2 = GetTickCount(); printf(PathCharMod " using ilSave: %i ms\n", targetFN, t2-t1); // Test ilSaveF _tcscpy(targetFN, targetName); _tcscat(targetFN, L".ilSaveF."); _tcscat(targetFN, targetExt); FILE* file = _wfopen(targetFN, L"wb"); if (!ilSaveF(type, file)) { printf("testSavers2: Failed to save " PathCharMod " using ilSaveF\n", targetFN); ++errors; } fclose(file); // Test ilSaveL _tcscpy(targetFN, targetName); _tcscat(targetFN, L"ilSaveL."); _tcscat(targetFN, targetExt); size_t lumpSize = ilDetermineSize(type); BYTE* lump = new BYTE[lumpSize]; ILuint writtenToLump = ilSaveL(type, lump, lumpSize); if (writtenToLump > 0) { FILE* file = _wfopen(targetFN, L"wb"); size_t writtenToFile = fwrite(lump, 1, lumpSize, file); if (writtenToLump != writtenToFile) { printf("testSavers2: Failed to write " PathCharMod " after ilSaveL\n", targetFN); ++errors; } fclose(file); } else { printf("testSavers2: Failed to save " PathCharMod " using ilSaveL\n", targetFN); ++errors; } delete lump; // Test ilSaveFuncs wcscpy(targetFN, targetName); wcscat(targetFN, L".ilSaveFuncs."); wcscat(targetFN, targetExt); writeFile = _wfopen(targetFN, L"wb"); if (writeFile != NULL) { ilSetWrite(NULL, NULL, myPutc, mySeek, myTell, myWrite); if (!ilSaveFuncs(type)) printf("testSavers2: Failed to save " PathCharMod " using ilSave\n", targetFN); fclose(writeFile); } else printf("testSavers2: Failed to open " PathCharMod " for writing\n", targetFN); }