diff --git a/data/assets/scene/milkyway/exoplanets/exoplanets_data.asset b/data/assets/scene/milkyway/exoplanets/exoplanets_data.asset index b49f091844..b75b5aa1a5 100644 --- a/data/assets/scene/milkyway/exoplanets/exoplanets_data.asset +++ b/data/assets/scene/milkyway/exoplanets/exoplanets_data.asset @@ -2,6 +2,6 @@ local DataPath = asset.syncedResource({ Name = "Exoplanet Data Files", Type = "HttpSynchronization", Identifier = "exoplanets_data", - Version = 1 + Version = 2 }) asset.export("DataPath", DataPath) diff --git a/modules/exoplanets/exoplanetsmodule_lua.inl b/modules/exoplanets/exoplanetsmodule_lua.inl index 9fdd5eb45b..af0f07e67a 100644 --- a/modules/exoplanets/exoplanetsmodule_lua.inl +++ b/modules/exoplanets/exoplanetsmodule_lua.inl @@ -46,17 +46,15 @@ namespace openspace::exoplanets::luascriptfunctions { constexpr const char* ExoplanetsGuiPath = "/Milky Way/Exoplanets/Exoplanet Systems/"; -constexpr const char* LookUpTablePath = "${SYNC}/http/exoplanets_data/1/lookup.txt"; +constexpr const char* LookUpTablePath = "${SYNC}/http/exoplanets_data/2/lookup.txt"; constexpr const char* ExoplanetsDataPath = - "${SYNC}/http/exoplanets_data/1/exoplanets_data.bin"; + "${SYNC}/http/exoplanets_data/2/exoplanets_data.bin"; constexpr const char* StarTextureFile = "${SYNC}/http/exoplanets_textures/1/sun.jpg"; constexpr const char* NoDataTextureFile = "${SYNC}/http/exoplanets_textures/1/grid-32.png"; constexpr const char* DiscTextureFile = "${SYNC}/http/exoplanets_textures/1/disc_texture.png"; -constexpr const char* HabitableZoneTextureFile = - "${SYNC}/http/exoplanets_textures/1/hz_disc_texture.png"; const float AU = static_cast(distanceconstants::AstronomicalUnit); const float SolarRadius = static_cast(distanceconstants::SolarRadius); @@ -443,7 +441,9 @@ void createExoplanetSystem(const std::string& starName) { "Type = 'StaticRotation'," "Rotation = " + ghoul::to_string(rotationMat3) + "" "}," - "Texture = openspace.absPath('" + HabitableZoneTextureFile + "')," + "Texture = openspace.absPath(" + "openspace.createPixelImage('exo_habitable_zone', {0, 0.92, 0.81})" + ")," "Size = " + std::to_string(center) + "," "Eccentricity = 0," "Offset = { " + diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index d51f322e91..da16976af6 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -1543,6 +1543,17 @@ scripting::LuaLibrary OpenSpaceEngine::luaLibrary() { "string, string", "Removes a tag (second argument) from a scene graph node (first argument)" }, + { + "createPixelImage", + &luascriptfunctions::createPixelImage, + {}, + "string, vec3", + "Creates a 1 pixel image with a certain color in the cache folder and " + "returns the path to the file. If a cached file with the given name " + "already exists, the path to that file is returned. The first argument " + "is the name of the file, without extension. The second is the RGB " + "color, given as {r, g, b} with values between 0 and 1." + }, { "isMaster", &luascriptfunctions::isMaster, diff --git a/src/engine/openspaceengine_lua.inl b/src/engine/openspaceengine_lua.inl index e9e647296c..19cf96a6f7 100644 --- a/src/engine/openspaceengine_lua.inl +++ b/src/engine/openspaceengine_lua.inl @@ -22,11 +22,15 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ +#include +#include #include #include #include #include #include +#include +#include #include namespace openspace::luascriptfunctions { @@ -288,6 +292,71 @@ int downloadFile(lua_State* L) { return 0; } +/** +* \ingroup LuaScripts +* createPixelImage(): +* Creates a one pixel image with a given color and returns the p +*/ +int createPixelImage(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 2, "lua::createPixelImage"); + + const std::string& name = ghoul::lua::value(L, 1); + const ghoul::Dictionary& d = ghoul::lua::value(L, 2); + + using namespace openspace::documentation; + const std::string& key = "color"; + ghoul::Dictionary colorDict = {{ key, d }}; + TestResult res = DoubleVector3Verifier()(colorDict, key); + + if (!res.success) { + return ghoul::lua::luaError(L, + "Invalid color. Expected three double values {r, g, b} in range 0 to 1" + ); + } + + const glm::vec3 color = colorDict.value(key); + + const std::string& fileName = FileSys.cacheManager()->cachedFilename( + fmt::format("{}.ppm", name), + "", + ghoul::filesystem::CacheManager::Persistent::Yes + ); + + const bool hasCachedFile = FileSys.fileExists(fileName); + if (hasCachedFile) { + LDEBUGC("OpenSpaceEngine", fmt::format("Cached file '{}' used", fileName)); + ghoul::lua::push(L, fileName); + return 1; + } + else { + // Write the color to a ppm file + static std::mutex fileMutex; + std::lock_guard guard(fileMutex); + std::ofstream ppmFile(fileName, std::ofstream::binary | std::ofstream::trunc); + + unsigned int width = 1; + unsigned int height = 1; + unsigned int size = width * height; + std::vector img(size * 3); + img[0] = static_cast(255 * color.r); + img[1] = static_cast(255 * color.g); + img[2] = static_cast(255 * color.b); + + if (ppmFile.is_open()) { + ppmFile << "P6" << std::endl; + ppmFile << width << " " << height << std::endl; + ppmFile << 255 << std::endl; + ppmFile.write(reinterpret_cast(&img[0]), size * 3); + ppmFile.close(); + ghoul::lua::push(L, fileName); + return 1; + } + else { + return ghoul::lua::luaError(L, "Could not open ppm file for writing."); + } + } +} + int isMaster(lua_State* L) { ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::isMaster"); ghoul::lua::push(L, global::windowDelegate->isMaster());