void Editor::saveLevel() { if (gDirtyLightmaps) { destroyCommon(); mScreenWait = new ScreenWait( METHOD_CALLBACK(Editor, lightMapsReady) ); initCommon(); setUpdateFrequency(10); Main::setState(mScreenWait, false); updateLightMap(true); } else { if (saveLevelToFile()) { sgLevel.saved = true; mScreenInfo = new ScreenInfo("level saved successfully"); Main::setState(mScreenInfo, false); } else { mScreenInfo = new ScreenInfo("operation failed"); Main::setState(mScreenInfo, false); } } }
void ofxShadowedTerrain::setLightDirection(ofVec3f _dir){ // rounding because we dont have sooo many precalculated pictures available _dir*=100.0; _dir.x=round(_dir.x); _dir.y=round(_dir.y); _dir.z=round(_dir.z); _dir/=100.0; if(_dir==ofVec3f(0,1,0))return; if(_dir==lightdir)return; lightdir=_dir; int accuracy=1; string filename="media/shadowmaps/sm"+ofToString(_dir.x, accuracy)+"_"+ofToString(_dir.y, accuracy)+"_"+ofToString(_dir.z, accuracy)+".png"; // Check if File already exists, if so return this file ifstream ifile(ofToDataPath(filename, true).c_str()); if(ifile){ shadowimg.loadImage(filename); return; } float lightdirfloat[3]; lightdirfloat[0]=lightdir.x; lightdirfloat[1]=lightdir.y; lightdirfloat[2]=lightdir.z; for(int i=0;i<numvalues;i++){ lightmapforcalc[i]=255; int ind=i*4; // Lichtfarbe lightmap[ind]=lightcolor.r; lightmap[ind+1]=lightcolor.g; lightmap[ind+2]=lightcolor.b; lightmap[ind+3]=lightcolor.a; } updateLightMap(heightmap, lightmapforcalc, gridw, lightdirfloat); for(int i=0;i<numvalues;i++){ float bright=lightmapforcalc[i]; int ind=i*4; lightmap[ind]=shadowcolor.r; lightmap[ind+1]=shadowcolor.g; lightmap[ind+2]=shadowcolor.b; lightmap[ind+3]=bright*2; } blurimg.setFromPixels(lightmapforcalc, gridw, gridh); blurimg.blurGaussian(3); shadowimg.setFromPixels(blurimg.getPixels(), gridw, gridh, OF_IMAGE_GRAYSCALE); shadowimg.saveImage(filename); }
/** * Update the lightmap and draw the terrain and decals. * This function first draws the terrain in black, and then uses additive blending to put the terrain layers * on it one by one. Finally the decals are drawn. */ void drawTerrain(const glm::mat4 &mvp) { const glm::vec4 paramsXLight(1.0f / world_coord(mapWidth) *((float)mapWidth / lightmapWidth), 0, 0, 0); const glm::vec4 paramsYLight(0, 0, -1.0f / world_coord(mapHeight) *((float)mapHeight / lightmapHeight), 0); /////////////////////////////////// // set up the lightmap texture glActiveTexture(GL_TEXTURE1); // bind the texture lightmap_tex_num->bind(); // we limit the framerate of the lightmap, because updating a texture is an expensive operation if (realTime - lightmapLastUpdate >= LIGHTMAP_REFRESH) { lightmapLastUpdate = realTime; updateLightMap(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); lightmap_tex_num->upload(0, 0, 0, lightmapWidth, lightmapHeight, gfx_api::pixel_format::rgb, lightmapPixmap); } /////////////////////////////////// // terrain culling cullTerrain(); glActiveTexture(GL_TEXTURE0); // shift the lightmap half a tile as lights are supposed to be placed at the center of a tile const glm::mat4 lightMatrix = glm::translate(glm::vec3(1.f / lightmapWidth / 2, 1.f / lightmapHeight / 2, 0.f)); ////////////////////////////////////// // canvas to draw on drawDepthOnly(mvp, paramsXLight, paramsYLight); /////////////////////////////////// // terrain drawTerrainLayers(mvp, paramsXLight, paramsYLight, lightMatrix); ////////////////////////////////// // decals drawDecals(mvp, paramsXLight, paramsYLight, lightMatrix); //////////////////////////////// // disable the lightmap texture glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE0); // leave everything in a sane state so it won't mess up somewhere else //glBindBuffer(GL_ARRAY_BUFFER, 0); // HACK Must unbind GL_ARRAY_BUFFER (don't know if it has to be unbound everywhere), otherwise text rendering may mysteriously crash. }
static void newLevel() { fprintf(stderr, "creating new level: (%d, %d)\n", sgLevel.size.x, sgLevel.size.y); sgLevel.start.x = 0; sgLevel.start.y = 0; sgLevel.finish.x = sgLevel.size.x - 1; sgLevel.finish.y = sgLevel.size.y - 1; initLevel(); for (int x = 0; x < sgLevel.size.x; x++) { for (int y = 0; y < sgLevel.size.y; y++) { KdCell::Range& range = sgLevel.kdLevelTree->get(x, y); { Block b; b.x = x; b.y = y; b.z = 0; b.dzx = 0; b.dzy = 0; b.dirty = true; range.start = sgLevel.blocks.insert(b); } range.end = range.start + 1; } } initCommon(); updateLightMap(); }
void GameTime::runNewAtomicTurnEvents() { updateLightMap(); }
bool loadLevelFromFile(const char* filename) { FILE* file = fopen(filename, "rt"); unsigned int version; unsigned int crc32; sgLevel.filename = filename; if (!file) { if (between(sgLevel.size.x, 1, MAX_LEVEL_SIZE) && between(sgLevel.size.y, 1, MAX_LEVEL_SIZE)) { newLevel(); return true; } else { fprintf(stderr, "can not open file: %s\n", filename); return false; } } /* version number */ if (fscanf(file, "v%u", &version) != 1 || version > THIS_CGM_VERSION) { fprintf(stderr, "incompatible version number: %u\n", version); fclose(file); return false; } resetCRC32(); /* read attributes */ readFieldCoord(file, sgLevel.start); readFieldCoord(file, sgLevel.finish); readFieldCoord(file, sgLevel.size); if (version >= 4) { readString(file, sgLevel.author); } initLevel(); /* reading data */ if (version >= 4) { int countBlocks = readInt(file); for (int i = 0; i < countBlocks; ++i) { KdCell::Range range; int x; int y; { Block b; readFieldBlock(file, b); b.dirty = true; x = b.x; y = b.y; range.start = sgLevel.blocks.insert(b); } range.end = range.start + 1; sgLevel.kdLevelTree->get(x, y) = range; } } else { for (int x = 0; x < sgLevel.size.x; x++) { for (int y = 0; y < sgLevel.size.y; y++) { KdCell::Range& range = sgLevel.kdLevelTree->get(x, y); { Block b; b.x = x; b.y = y; readFieldBlockV3(file, b); b.dirty = true; range.start = sgLevel.blocks.insert(b); } range.end = range.start + 1; } } } sgLevel.crc32 = getCRC32(); initCommon(); if (version >= 2) { if (fscanf(file, "%x\n", &crc32) != 1 || crc32 != getCRC32()) { fprintf(stderr, "1st checksum mismatch: %s\n", filename); fclose(file); return false; } { int cntSubLightMaps = sgAtlas->getCntAllocatedSubLightMaps(); int index = 0; if (version >= 3) { while (index < cntSubLightMaps) { int dataInt[SIZEOF_LIGHT_MAP]; readRLE(file, dataInt); toLightMap(index, false, dataInt); index++; } } else { importLightmapV2(file); } if (fscanf(file, "%x\n", &crc32) != 1 || crc32 != getCRC32()) { fprintf(stderr, "2st checksum mismatch: %s\n", filename); fclose(file); return false; } } } else { updateLightMap(); } fclose(file); if (!loadHighscoreFromFile()) { sgLevel.cntScoreCols = 0; } sgLevel.saved = true; return true; }