Skip to content

Commit

Permalink
Fix custom_scene_viewer for macOS (#256)
Browse files Browse the repository at this point in the history
* Examples: fix custom_scene_viewer for macOS

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.

Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com>

* Examples: custom_scene_viewer ensure compliance with code standards

1. Use camel case for variables.

Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com>
  • Loading branch information
srmainwaring authored Mar 1, 2021
1 parent 278f274 commit 6d93957
Show file tree
Hide file tree
Showing 2 changed files with 53 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
25 changes: 23 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 ogreEngine("ogre");
if (_argc > 1)
{
ogreEngine = _argv[1];
}

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

0 comments on commit 6d93957

Please sign in to comment.