Last active
April 6, 2022 12:59
-
-
Save remoe/2afaabbd2c47c62c19762f2a14d99d2a to your computer and use it in GitHub Desktop.
osgearth simple SDL sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <osgDB/Registry> | |
#ifdef __EMSCRIPTEN__ | |
#include <emscripten.h> | |
#endif | |
#include <SDL2/SDL.h> | |
#include <osgDB/ReadFile> | |
#include <osgViewer/Viewer> | |
#include <osgEarthUtil/EarthManipulator> | |
#include <osgEarthUtil/ExampleResources> | |
#include <osgEarth/GLUtils> | |
#include <osgEarth/Layer> | |
#include <osgGA/StateSetManipulator> | |
#include <osgViewer/ViewerEventHandlers> | |
#include <osgDB/WriteFile> | |
#include <osgViewer/Viewer> | |
#include <osgEarth/Registry> | |
#include <osg/Notify> | |
#include <osgEarth/Notify> | |
// Initialize OSG plugins statically. | |
USE_OSGPLUGIN(osg2) | |
USE_OSGPLUGIN(osg) | |
USE_OSGPLUGIN(zip) | |
USE_OSGPLUGIN(tiff) | |
USE_OSGPLUGIN(earth) | |
USE_OSGPLUGIN(osgearth_osg) | |
USE_OSGPLUGIN(osgearth_gdal) | |
USE_OSGPLUGIN(osgearth_engine_rex) | |
USE_OSGEARTH_LAYER(image) | |
USE_DOTOSGWRAPPER_LIBRARY(osg) | |
USE_DOTOSGWRAPPER_LIBRARY(osgShadow) | |
USE_DOTOSGWRAPPER_LIBRARY(osgSim) | |
USE_DOTOSGWRAPPER_LIBRARY(osgTerrain) | |
USE_DOTOSGWRAPPER_LIBRARY(osgViewer) | |
USE_SERIALIZER_WRAPPER_LIBRARY(osg) | |
USE_SERIALIZER_WRAPPER_LIBRARY(osgShadow) | |
USE_SERIALIZER_WRAPPER_LIBRARY(osgSim) | |
USE_SERIALIZER_WRAPPER_LIBRARY(osgTerrain) | |
using namespace osgEarth; | |
using namespace osgEarth::Util; | |
osg::ref_ptr<osgViewer::Viewer> mViewer; | |
// Stand alone function that is called by Emscripten to run the app. | |
void loop() | |
{ | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) | |
{ | |
osgViewer::GraphicsWindow *gw = | |
dynamic_cast<osgViewer::GraphicsWindow *>( | |
mViewer->getCamera()->getGraphicsContext()); | |
if (!gw) | |
{ | |
return; | |
} | |
osgGA::EventQueue &eventQueue = *(gw->getEventQueue()); | |
switch (event.type) | |
{ | |
case SDL_MOUSEMOTION: | |
eventQueue.mouseMotion(event.motion.x, event.motion.y); | |
return; | |
case SDL_MOUSEBUTTONDOWN: | |
if(event.button.clicks > 1) { | |
eventQueue.mouseDoubleButtonPress(event.button.x, event.button.y, event.button.button); | |
} else { | |
eventQueue.mouseButtonPress(event.button.x, event.button.y, event.button.button); | |
} | |
printf("osgEarth Test. Application. Mouse button down\n"); | |
return; | |
case SDL_MOUSEBUTTONUP: | |
eventQueue.mouseButtonRelease(event.button.x, event.button.y, event.button.button); | |
printf("osgEarth Test. Application. Mouse button up\n"); | |
return; | |
case SDL_MOUSEWHEEL: | |
eventQueue.mouseScroll2D(event.wheel.x, event.wheel.y, eventQueue.getTime()); | |
return; | |
case SDL_KEYUP: | |
eventQueue.keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.scancode); | |
printf("osgEarth Test. Application. key up\n"); | |
return; | |
case SDL_KEYDOWN: | |
eventQueue.keyPress( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.scancode); | |
printf("osgEarth Test. Application. key down\n"); | |
return; | |
case SDL_WINDOWEVENT_RESIZED: | |
eventQueue.windowResize(0, 0, event.window.data1, event.window.data2 ); | |
return; | |
default: | |
break; | |
} | |
return; | |
} | |
mViewer->frame(); | |
} | |
void | |
configureView( osgViewer::View* view ) | |
{ | |
// default uniform values: | |
GLUtils::setGlobalDefaults(view->getCamera()->getOrCreateStateSet()); | |
// add some stock OSG handlers: | |
view->addEventHandler(new osgViewer::StatsHandler()); | |
view->addEventHandler(new osgViewer::WindowSizeHandler()); | |
view->addEventHandler(new osgViewer::ThreadingHandler()); | |
view->addEventHandler(new osgViewer::LODScaleHandler()); | |
view->addEventHandler(new osgGA::StateSetManipulator(view->getCamera()->getOrCreateStateSet())); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
osgEarth::setNotifyLevel(osg::INFO); | |
// Make sure SDL is working. | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) | |
{ | |
printf("osgEarth Test. Could not init SDL: '%s'\n", SDL_GetError()); | |
return 1; | |
} | |
// Clean SDL up at exit. | |
atexit(SDL_Quit); | |
// Configure rendering context. | |
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); | |
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); | |
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); | |
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
// GLES3 | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); | |
// Create rendering window. | |
int width = 800; | |
int height = 600; | |
SDL_Window* window = | |
SDL_CreateWindow( | |
"osgEarth Test", | |
SDL_WINDOWPOS_CENTERED, | |
SDL_WINDOWPOS_CENTERED, | |
width, | |
height, | |
SDL_WINDOW_OPENGL); | |
if (!window) | |
{ | |
printf("osgEarth Test. Could not create window: '%s'\n", SDL_GetError()); | |
return 1; | |
} | |
SDL_GL_CreateContext(window); | |
// Create OpenSceneGraph viewer. | |
mViewer = new osgViewer::Viewer(); | |
// Use single thread: CRITICAL for Emscripten. | |
mViewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded); | |
// Tell the database pager to not modify the unref settings | |
mViewer->getDatabasePager()->setUnrefImageDataAfterApplyPolicy( true, false ); | |
// thread-safe initialization of the OSG wrapper manager. Calling this here | |
// prevents the "unsupported wrapper" messages from OSG | |
osgDB::Registry::instance()->getObjectWrapperManager()->findWrapper("osg::Image"); | |
// install our default manipulator (do this before calling load) | |
mViewer->setCameraManipulator( new EarthManipulator() ); | |
// disable the small-feature culling | |
mViewer->getCamera()->setSmallFeatureCullingPixelSize(-1.0f); | |
// set a near/far ratio that is smaller than the default. This allows us to get | |
// closer to the ground without near clipping. If you need more, use --logdepth | |
mViewer->getCamera()->setNearFarRatio(0.0001); | |
mViewer->setUpViewerAsEmbeddedInWindow(0, 0, width, height); | |
osgEarth::Registry::instance()->getCapabilities(); | |
// load an earth file, and support all or our example command-line options | |
// and earth file <external> tags | |
std::string fname("simple.earth"); | |
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(fname); | |
osg::ref_ptr<MapNode> mapNode = MapNode::get(node.get()); | |
if ( !mapNode.valid() ) | |
{ | |
OE_WARN << "Loaded scene graph does not contain a MapNode - aborting" << std::endl; | |
return 0L; | |
} | |
// collect the views | |
osgViewer::Viewer::Views views; | |
if (mViewer) | |
{ | |
mViewer->getViews(views); | |
} | |
// warn about not having an earth manip | |
for (osgViewer::Viewer::Views::iterator view = views.begin(); view != views.end(); ++view) | |
{ | |
EarthManipulator* manip = dynamic_cast<EarthManipulator*>((*view)->getCameraManipulator()); | |
if ( manip == 0L ) | |
{ | |
OE_WARN << "Helper used before installing an EarthManipulator" << std::endl; | |
} | |
} | |
osg::Group* root = new osg::Group(); | |
root->addChild( node ); | |
for (osgViewer::Viewer::Views::iterator view = views.begin(); view != views.end(); ++view) | |
{ | |
configureView( *view ); | |
} | |
if ( root ) | |
{ | |
mViewer->setSceneData( root ); | |
//Metrics::run(viewer); | |
} | |
// Render asynchronously. | |
emscripten_set_main_loop(loop, -1, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you create cmake too for this file?!!