Skip to content

Commit

Permalink
First implementation of EGL/Angle support. Tested with GLES3 on Direc…
Browse files Browse the repository at this point in the history
…tX11 platform.

Pixelbuffer currently not supported.
  • Loading branch information
remoe committed Aug 30, 2019
1 parent bd50af1 commit fbc31d9
Show file tree
Hide file tree
Showing 13 changed files with 767 additions and 139 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ _CPack_Packages/
#packages
*.tar.gz
*.zip

#individual build files
config.yaml
_build
21 changes: 19 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,25 @@ IF(APPLE)
SET(OSG_COMPILE_FRAMEWORKS_INSTALL_NAME_DIR "@executable_path/../Frameworks" CACHE STRING "install name dir for compiled frameworks")
ELSE()
# Non-Apple: Find OpenGL
FIND_PACKAGE(OpenGL)
FIND_PACKAGE(EGL)

# Support EGL on Windows
IF(OSG_WINDOWING_SYSTEM STREQUAL "Win32" AND (OSG_GLES2_AVAILABLE OR OSG_GLES3_AVAILABLE ) )
FIND_PACKAGE(EGL)

set(OSG_USE_EGL ON)

IF(OSG_GLES3_AVAILABLE)
FIND_PATH(OPENGL_INCLUDE_DIR GLES3/gl3.h)
ENDIF()
IF(OSG_GLES2_AVAILABLE)
FIND_PATH(OPENGL_INCLUDE_DIR GLES2/gl2.h)
ENDIF()
FIND_LIBRARY(OPENGL_gl_LIBRARY "libGLESv2.dll")
ELSE()
# Non-Apple: Find OpenGL
FIND_PACKAGE(OpenGL)
ENDIF()

ENDIF()
ENDIF()

Expand Down
24 changes: 13 additions & 11 deletions CMakeModules/FindEGL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@


FIND_PATH( EGL_INCLUDE_DIR
NAMES EGL/egl.h
HINTS ENV EGL_DIR
NAMES egl.h
PATH_SUFFIXES EGL
PATHS ENV EGL_DIR
)

FIND_LIBRARY(EGL_LIBRARY
NAMES EGL
HINTS ENV EGL_DIR
PATH_SUFFIXES lib
NAMES EGL libEGL.dll.lib
PATHS ENV EGL_DIR
)

# handle the QUIETLY and REQUIRED arguments and set
# EGL_FOUND to TRUE as appropriate
INCLUDE( FindPackageHandleStandardArgs )

FIND_PACKAGE_HANDLE_STANDARD_ARGS(EGL
REQUIRED_VARS EGL_LIBRARY EGL_INCLUDE_DIR)

MARK_AS_ADVANCED(
EGL_INCLUDE_DIR
EGL_LIBRARY
find_package_handle_standard_args(EGL
FOUND_VAR
EGL_FOUND
REQUIRED_VARS
EGL_LIBRARY
EGL_INCLUDE_DIR
FAIL_MESSAGE
"Could NOT find EGL, try to set the path to EGL root folder in the system variable EGL_DIR"
)
7 changes: 6 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,17 @@ IF(DYNAMIC_OPENSCENEGRAPH)
ADD_SUBDIRECTORY(osgfpdepth)
ENDIF()

IF(NOT OSG_GLES1_AVAILABLE AND NOT OSG_GLES2_AVAILABLE AND NOT OSG_GL3_AVAILABLE)
IF(NOT OSG_GLES1_AVAILABLE AND NOT OSG_GLES2_AVAILABLE AND NOT OSG_GLES3_AVAILABLE AND NOT OSG_GL3_AVAILABLE)
ADD_SUBDIRECTORY(osgscreencapture)
ADD_SUBDIRECTORY(osgmotionblur)
ADD_SUBDIRECTORY(osgteapot)
ENDIF()

# GLES3 example
IF(EGL_FOUND AND OSG_GLES3_AVAILABLE)
ADD_SUBDIRECTORY(osgsimpleGLES3)
ENDIF()

ADD_SUBDIRECTORY(osgphotoalbum)
ADD_SUBDIRECTORY(osgtessellate)
ADD_SUBDIRECTORY(osgtessellationshaders)
Expand Down
4 changes: 4 additions & 0 deletions examples/osgsimpleGLES3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SET(TARGET_SRC osgsimplegles3.cpp )

#### end var setup ###
SETUP_EXAMPLE(osgsimpleGLES3)
103 changes: 103 additions & 0 deletions examples/osgsimpleGLES3/osgsimplegles3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// This is public domain software and comes with
// absolutely no warranty. Use of public domain software
// may vary between counties, but in general you are free
// to use and distribute this software for any purpose.


// Example: OSG using EGL.
// The comment block at the end of the source describes building OSG
// for use with OpenGL 3.x.

#include <osgViewer/Viewer>
#include <osg/GraphicsContext>
#include <osg/Camera>
#include <osg/Viewport>
#include <osg/StateSet>
#include <osg/Program>
#include <osg/Shader>

osg::Geode* makeGeometry(float v)
{
osg::Geode* geode = new osg::Geode();
osg::Geometry* geom = new osg::Geometry();
osg::Vec3Array* verts = new osg::Vec3Array();
verts->push_back(osg::Vec3(v - 1, 0, 0));
verts->push_back(osg::Vec3(v + 1, 0, 0));
verts->push_back(osg::Vec3(v, 0, 2));
geom->setVertexArray(verts);
geom->setUseVertexBufferObjects(true);
osg::Vec4Array* colors = new osg::Vec4Array(osg::Array::BIND_OVERALL);
colors->push_back(osg::Vec4(0, 0, 1, 1));
geom->setColorArray(colors);
geom->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3));
geode->addDrawable(geom);
return geode;
}

int main( int argc, char** argv )
{
osg::ArgumentParser arguments( &argc, argv );
osgViewer::Viewer viewer(arguments);
viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);

osg::Geode* geode = makeGeometry(1.0);

const std::string vertexSource =
"#version 300 es\n"
"void main() { \n"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
"} \n";

osg::Shader* vShader = new osg::Shader(osg::Shader::VERTEX, vertexSource);

const std::string fragmentSource =
"#version 300 es\n"
"precision mediump float;\n"
"out vec4 color;\n"
"void main() { \n"
" color = vec4(0.5, 0.3, 0.3, 1.0);\n"
"} \n";

osg::Shader* fShader = new osg::Shader(osg::Shader::FRAGMENT, fragmentSource);

osg::Program* program = new osg::Program;
program->addShader(vShader);
program->addShader(fShader);
geode->getOrCreateStateSet()->setAttributeAndModes(program, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED);

osg::Group* root = new osg::Group();
root->addChild(geode);

#if 0
// Create a Camera that uses the above OpenGL context.
osg::Camera* cam = viewer.getCamera();
// Must set perspective projection for fovy and aspect.
cam->setProjectionMatrix( osg::Matrix::perspective( 30., (double)width/(double)height, 1., 100. ) );
// Unlike OpenGL, OSG viewport does *not* default to window dimensions.
cam->setViewport( new osg::Viewport( 0, 0, width, height ) );
#endif
viewer.setSceneData( root );

return( viewer.run() );
}

/*
Building OSG for OpenGLES3
OSG currently support GLES3 on Windows. This comment block describes the
necessary configuration steps.
* Add Google Angle libraries and include folder to CMake
CMake use FindEGL.cmake to find this. The library name of Google-Angle for Windows is "libEGL.dll.lib"
* Enable the following CMake variable:
OSG_GLES3_AVAILABLE
* Disable the following CMake variables:
OSG_GL1_AVAILABLE
OSG_GL2_AVAILABLE
OSG_GLES1_AVAILABLE
OSG_GLES2_AVAILABLE
*/
123 changes: 85 additions & 38 deletions include/osgViewer/api/Win32/GraphicsHandleWin32
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,98 @@
#define _WIN32_WINNT 0x0500 // Windows NT
#endif
#include <windows.h>
#if defined(OSG_USE_EGL)
#include "EGL/egl.h"
#endif

namespace osgViewer
{

/** Class to encapsulate platform-specific OpenGL context handle variables.
* Derived osg::GraphicsContext classes can inherit from this class to
* share OpenGL resources.*/
/** Class to encapsulate platform-specific OpenGL context handle variables.
* Derived osg::GraphicsContext classes can inherit from this class to
* share OpenGL resources.*/

class OSGVIEWER_EXPORT GraphicsHandleWin32
{
public:

GraphicsHandleWin32():
_hwnd(0),
_hdc(0),
_hglrc(0) {}

/** Set native window.*/
inline void setHWND(HWND hwnd) { _hwnd = hwnd; }

/** Get native window.*/
inline HWND getHWND() const { return _hwnd; }

/** Set device context.*/
inline void setHDC(HDC hdc) { _hdc = hdc; }

/** Get device context.*/
inline HDC getHDC() const { return _hdc; }

/** Set native OpenGL graphics context.*/
inline void setWGLContext(HGLRC hglrc) { _hglrc = hglrc; }

/** Get native OpenGL graphics context.*/
inline HGLRC getWGLContext() const { return _hglrc; }

protected:

HWND _hwnd;
HDC _hdc;
HGLRC _hglrc;

};
#if defined(OSG_USE_EGL)
class OSGVIEWER_EXPORT GraphicsHandleWin32
{
public:
struct EGLContextInfo {
EGLContextInfo() { clear(); }
EGLContextInfo(EGLContext _eglContext, EGLDisplay _eglDisplay, EGLSurface _eglSurface) : eglContext(_eglContext), eglDisplay(_eglDisplay), eglSurface(_eglSurface) {}
EGLContextInfo(const EGLContextInfo& o) : eglContext(o.eglContext), eglDisplay(o.eglDisplay), eglSurface(o.eglSurface) {}
EGLContextInfo& operator=(const EGLContextInfo& o) { eglContext = o.eglContext; eglDisplay = o.eglDisplay; eglSurface = o.eglSurface; return *this; }
void clear() { eglContext = EGL_NO_CONTEXT; eglDisplay = EGL_NO_DISPLAY; eglSurface = EGL_NO_SURFACE; }
bool isEmpty() { return eglContext == 0 && eglDisplay == 0; }
EGLContext eglContext;
EGLDisplay eglDisplay;
EGLSurface eglSurface;
};

}
GraphicsHandleWin32()
: _hwnd(0), _hdc(0) {}

/** Set native window.*/
inline void setHWND(HWND hwnd) { _hwnd = hwnd; }

/** Get native window.*/
inline HWND getHWND() const { return _hwnd; }

/** Set device context.*/
inline void setHDC(HDC hdc) { _hdc = hdc; }

/** Get device context.*/
inline HDC getHDC() const { return _hdc; }

/** Set native EGL graphics context.*/
inline void setEGLContext(const EGLContextInfo& eglContextInfo) { _eglContextInfo = eglContextInfo; }

/** Get native EGL graphics context.*/
inline const EGLContextInfo& getEGLContext() const { return _eglContextInfo; }

protected:

HWND _hwnd;
HDC _hdc;
EGLContextInfo _eglContextInfo;
};

#else
class OSGVIEWER_EXPORT GraphicsHandleWin32
{
public:

GraphicsHandleWin32() :
_hwnd(0),
_hdc(0),
_hglrc(0) {}

/** Set native window.*/
inline void setHWND(HWND hwnd) { _hwnd = hwnd; }

/** Get native window.*/
inline HWND getHWND() const { return _hwnd; }

/** Set device context.*/
inline void setHDC(HDC hdc) { _hdc = hdc; }

/** Get device context.*/
inline HDC getHDC() const { return _hdc; }

/** Set native OpenGL graphics context.*/
inline void setWGLContext(HGLRC hglrc) { _hglrc = hglrc; }

/** Get native OpenGL graphics context.*/
inline HGLRC getWGLContext() const { return _hglrc; }

protected:

HWND _hwnd;
HDC _hdc;
HGLRC _hglrc;

};
#endif
}

// Definitions required to create an OpenGL pixel format, from the WGL_ARB_pixel_format specification document.
// See http://www.opengl.org/registry/specs/ARB/wgl_pixel_format.txt
Expand Down
4 changes: 4 additions & 0 deletions include/osgViewer/api/Win32/GraphicsWindowWin32
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ class OSGVIEWER_EXPORT GraphicsWindowWin32 : public osgViewer::GraphicsWindow, p
virtual bool registerWindowProcedure();
virtual bool unregisterWindowProcedure();

#ifdef OSG_USE_EGL
virtual const GraphicsHandleWin32::EGLContextInfo createContextImplementation();
#else
virtual HGLRC createContextImplementation();
#endif
virtual bool createWindow();
virtual bool setWindow( HWND handle );

Expand Down
5 changes: 3 additions & 2 deletions include/osgViewer/api/Win32/PixelBufferWin32
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

namespace osgViewer
{

#if defined(OSG_USE_EGL)
#else
class OSGVIEWER_EXPORT PixelBufferWin32 : public osg::GraphicsContext, public osgViewer::GraphicsHandleWin32
{
public:
Expand Down Expand Up @@ -69,7 +70,7 @@ class OSGVIEWER_EXPORT PixelBufferWin32 : public osg::GraphicsContext, public os
bool _realized;
int _boundBuffer;
};

#endif
}

#endif
2 changes: 1 addition & 1 deletion src/osg/GLExtensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ OSG_INIT_SINGLETON_PROXY(GLExtensionDisableStringInitializationProxy, osg::getGL

#elif defined(WIN32)

#if defined(OSG_GLES2_AVAILABLE)
#if defined(OSG_GLES2_AVAILABLE) || defined(OSG_GLES3_AVAILABLE)
static HMODULE hmodule = GetModuleHandle(TEXT("libGLESv2.dll"));
return convertPointerType<void*, PROC>(GetProcAddress(hmodule, funcName));
#elif defined(OSG_GLES1_AVAILABLE)
Expand Down
5 changes: 5 additions & 0 deletions src/osgViewer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ ELSE()
ADD_DEFINITIONS(-DOSG_LIBRARY_STATIC)
ENDIF()

IF(OSG_USE_EGL)
ADD_DEFINITIONS(-DOSG_USE_EGL)
ENDIF()


SET(LIB_NAME osgViewer)


Expand Down
Loading

0 comments on commit fbc31d9

Please sign in to comment.