mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-05-12 14:29:42 -05:00
Update data files and generate habitable zone texture on demand
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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<float>(distanceconstants::AstronomicalUnit);
|
||||
const float SolarRadius = static_cast<float>(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 = { " +
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -22,11 +22,15 @@
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#include <openspace/documentation/documentation.h>
|
||||
#include <openspace/documentation/verifier.h>
|
||||
#include <openspace/engine/downloadmanager.h>
|
||||
#include <openspace/engine/globals.h>
|
||||
#include <openspace/properties/triggerproperty.h>
|
||||
#include <openspace/rendering/renderengine.h>
|
||||
#include <openspace/scene/scenegraphnode.h>
|
||||
#include <ghoul/filesystem/cachemanager.h>
|
||||
#include <ghoul/filesystem/filesystem.h>
|
||||
#include <filesystem>
|
||||
|
||||
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<std::string>(L, 1);
|
||||
const ghoul::Dictionary& d = ghoul::lua::value<ghoul::Dictionary>(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<glm::vec3>(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<unsigned char> img(size * 3);
|
||||
img[0] = static_cast<unsigned char>(255 * color.r);
|
||||
img[1] = static_cast<unsigned char>(255 * color.g);
|
||||
img[2] = static_cast<unsigned char>(255 * color.b);
|
||||
|
||||
if (ppmFile.is_open()) {
|
||||
ppmFile << "P6" << std::endl;
|
||||
ppmFile << width << " " << height << std::endl;
|
||||
ppmFile << 255 << std::endl;
|
||||
ppmFile.write(reinterpret_cast<char*>(&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());
|
||||
|
||||
Reference in New Issue
Block a user