Skip to content

Commit

Permalink
Examples: fix custom_scene_viewer for macOS
Browse files Browse the repository at this point in the history
1. Add OpenGL context settings for macOS to the DemoWindow.
2. Move glutInit to main
3. Add command line option to set the ogre engine.
  • Loading branch information
srmainwaring committed Feb 24, 2021
1 parent 8252ce4 commit cb40031
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
40 changes: 30 additions & 10 deletions examples/custom_scene_viewer/DemoWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*
*/

#if defined(__APPLE__)
#if __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#elif !defined(_WIN32)
#else
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
Expand Down Expand Up @@ -52,7 +53,11 @@ unsigned int imgh = 0;

bool g_initContext = false;

#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLContextObj g_context;
CGLContextObj g_glutContext;
#elif _WIN32
#else
GLXContext g_context;
Display *g_display;
GLXDrawable g_drawable;
Expand Down Expand Up @@ -159,7 +164,10 @@ void printFPS()
//////////////////////////////////////////////////
void displayCB()
{
#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLSetCurrentContext(g_context);
#elif _WIN32
#else
if (g_display)
{
glXMakeCurrent(g_display, g_drawable, g_context);
Expand All @@ -171,7 +179,10 @@ void displayCB()

camera->Capture(*g_image);

#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLSetCurrentContext(g_glutContext);
#elif _WIN32
#else
glXMakeCurrent(g_glutDisplay, g_glutDrawable, g_glutContext);
#endif

Expand All @@ -198,7 +209,10 @@ void idleCB()
//////////////////////////////////////////////////
void keyboardCB(unsigned char _key, int, int)
{
#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLSetCurrentContext(g_context);
#elif _WIN32
#else
if (g_display)
{
glXMakeCurrent(g_display, g_drawable, g_context);
Expand Down Expand Up @@ -241,7 +255,10 @@ void keyboardCB(unsigned char _key, int, int)
g_demo->SelectScene(index);
}

#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLSetCurrentContext(g_glutContext);
#elif _WIN32
#else
glXMakeCurrent(g_glutDisplay, g_glutDrawable, g_glutContext);
#endif
}
Expand All @@ -259,9 +276,6 @@ void initCamera(CameraPtr _camera)
//////////////////////////////////////////////////
void initContext()
{
int argc = 0;
char **argv = 0;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(imgw, imgh);
Expand All @@ -278,6 +292,9 @@ void initContext()
//////////////////////////////////////////////////
void run(ManualSceneDemoPtr _demo)
{
#if __APPLE__
g_context = CGLGetCurrentContext();
#endif
#if not (__APPLE__ || _WIN32)
g_context = glXGetCurrentContext();
g_display = glXGetCurrentDisplay();
Expand All @@ -290,6 +307,9 @@ void run(ManualSceneDemoPtr _demo)
initContext();
printUsage();

#if __APPLE__
g_glutContext = CGLGetCurrentContext();
#endif
#if not (__APPLE__ || _WIN32)
g_glutDisplay = glXGetCurrentDisplay();
g_glutDrawable = glXGetCurrentDrawable();
Expand Down
24 changes: 22 additions & 2 deletions examples/custom_scene_viewer/ManualSceneDemo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
* limitations under the License.
*
*/

#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#elif not defined(_WIN32)
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#endif

#include <iostream>
#include <ignition/common/Console.hh>
#include "ManualSceneDemo.hh"
Expand Down Expand Up @@ -169,8 +179,18 @@ void ManualSceneDemo::ChangeScene()

//////////////////////////////////////////////////
//////////////////////////////////////////////////
int main(int, char**)
int main(int _argc, char** _argv)
{
glutInit(&_argc, _argv);

// Expose engine name to command line because we can't instantiate both
// ogre and ogre2 at the same time
std::string ogre_engine("ogre");
if (_argc > 1)
{
ogre_engine = _argv[1];
}

common::Console::SetVerbosity(4);
//! [add scenes]
ManualSceneDemoPtr sceneDemo(new ManualSceneDemo);
Expand All @@ -188,7 +208,7 @@ int main(int, char**)
sceneDemo->AddScene(SceneBuilderPtr(new ShadowSceneBuilder(4)));
sceneDemo->AddScene(SceneBuilderPtr(new ShadowSceneBuilder(5)));
//! [add scenes]
sceneDemo->AddCamera("ogre");
sceneDemo->AddCamera(ogre_engine);
sceneDemo->AddCamera("optix");
sceneDemo->Run();
return 0;
Expand Down

0 comments on commit cb40031

Please sign in to comment.