Merge pull request #949 from YunfeiBai/master

EGL OpenGL window.
This commit is contained in:
erwincoumans
2017-02-07 08:39:12 -08:00
committed by GitHub
8 changed files with 223 additions and 132 deletions

View File

@@ -13,7 +13,11 @@
#include "../OpenGLWindow/Win32OpenGLWindow.h" #include "../OpenGLWindow/Win32OpenGLWindow.h"
#else #else
//let's cross the fingers it is Linux/X11 //let's cross the fingers it is Linux/X11
#ifdef BT_USE_EGL
#include "../OpenGLWindow/EGLOpenGLWindow.h"
#else
#include "../OpenGLWindow/X11OpenGLWindow.h" #include "../OpenGLWindow/X11OpenGLWindow.h"
#endif //BT_USE_EGL
#endif //_WIN32 #endif //_WIN32
#endif//__APPLE__ #endif//__APPLE__
#include "../ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.h" #include "../ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.h"

View File

@@ -1,5 +1,5 @@
//portions of this file are copied from GLFW egl_context.c/egl_context.h // portions of this file are copied from GLFW egl_context.c/egl_context.h
//======================================================================== //========================================================================
// GLFW 3.3 EGL - www.glfw.org // GLFW 3.3 EGL - www.glfw.org
@@ -30,178 +30,231 @@
#ifdef BT_USE_EGL #ifdef BT_USE_EGL
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "OpenGLInclude.h"
#include "third_party/GL/EGL/egl.h"
#include "third_party/GL/gl/include/EGL/eglext.h"
#include "third_party/GL/gl/include/GL/gl.h"
#include "EGLOpenGLWindow.h" #include "EGLOpenGLWindow.h"
struct EGLInternalData2 {
struct EGLInternalData2
{
bool m_isInitialized; bool m_isInitialized;
int m_windowWidth; int m_windowWidth;
int m_windowHeight; int m_windowHeight;
b3KeyboardCallback m_keyboardCallback; b3KeyboardCallback m_keyboardCallback;
b3WheelCallback m_wheelCallback; b3WheelCallback m_wheelCallback;
b3ResizeCallback m_resizeCallback; b3ResizeCallback m_resizeCallback;
b3MouseButtonCallback m_mouseButtonCallback; b3MouseButtonCallback m_mouseButtonCallback;
b3MouseMoveCallback m_mouseMoveCallback; b3MouseMoveCallback m_mouseMoveCallback;
EGLBoolean success;
EGLint num_configs;
EGLConfig egl_config;
EGLSurface egl_surface;
EGLContext egl_context;
EGLDisplay egl_display;
EGLInternalData2() EGLInternalData2()
:m_isInitialized(false), : m_isInitialized(false),
m_windowWidth(0), m_windowWidth(0),
m_windowHeight(0), m_windowHeight(0),
m_keyboardCallback(0), m_keyboardCallback(0),
m_wheelCallback(0), m_wheelCallback(0),
m_resizeCallback(0), m_resizeCallback(0),
m_mouseButtonCallback(0), m_mouseButtonCallback(0),
m_mouseMoveCallback(0) m_mouseMoveCallback(0) {}
{
}
}; };
EGLOpenGLWindow::EGLOpenGLWindow() EGLOpenGLWindow::EGLOpenGLWindow() { m_data = new EGLInternalData2(); }
{
m_data = new EGLInternalData2(); EGLOpenGLWindow::~EGLOpenGLWindow() { delete m_data; }
void EGLOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci) {
m_data->m_windowWidth = ci.m_width;
m_data->m_windowHeight = ci.m_height;
} EGLint egl_config_attribs[] = {EGL_RED_SIZE,
8,
EGLOpenGLWindow::~EGLOpenGLWindow() EGL_GREEN_SIZE,
{ 8,
delete m_data; EGL_BLUE_SIZE,
} 8,
EGL_DEPTH_SIZE,
8,
void EGLOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci) EGL_SURFACE_TYPE,
{ EGL_PBUFFER_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_BIT,
EGL_NONE};
} EGLint egl_pbuffer_attribs[] = {
EGL_WIDTH, m_data->m_windowWidth, EGL_HEIGHT, m_data->m_windowHeight,
void EGLOpenGLWindow::closeWindow() EGL_NONE,
{ };
} // Initialize EGL display
PFNEGLQUERYDEVICESEXTPROC eglQueryDevicesEXT =
void EGLOpenGLWindow::runMainLoop() (PFNEGLQUERYDEVICESEXTPROC)eglGetProcAddress("eglQueryDevicesEXT");
{ if (eglQueryDevicesEXT == nullptr) m_data->egl_display = EGL_NO_DISPLAY;
} PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT =
(PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress(
float EGLOpenGLWindow::getTimeInSeconds() "eglGetPlatformDisplayEXT");
{ if (eglGetPlatformDisplayEXT == nullptr) m_data->egl_display = EGL_NO_DISPLAY;
return 0.;
}
bool EGLOpenGLWindow::requestedExit() const
{
return false;
}
void EGLOpenGLWindow::setRequestExit()
{
} const int max_devices = 32;
EGLDeviceEXT egl_devices[max_devices];
void EGLOpenGLWindow::startRendering() EGLint num_devices = 0;
{ EGLint egl_error = eglGetError();
if (!eglQueryDevicesEXT(max_devices, egl_devices, &num_devices) ||
egl_error != EGL_SUCCESS) {
printf("eglQueryDevicesEXT Failed.\n");
m_data->egl_display = EGL_NO_DISPLAY;
}
} for (EGLint i = 0; i < num_devices; ++i) {
EGLDisplay display = eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT,
void EGLOpenGLWindow::endRendering() egl_devices[i], nullptr);
{ if (eglGetError() == EGL_SUCCESS && display != EGL_NO_DISPLAY) {
int major, minor;
EGLBoolean initialized = eglInitialize(display, &major, &minor);
if (eglGetError() == EGL_SUCCESS && initialized == EGL_TRUE) {
m_data->egl_display = display;
}
}
}
m_data->success = eglBindAPI(EGL_OPENGL_API);
if (!m_data->success) {
printf("Failed to bind OpenGL API.\n");
exit(0);
}
m_data->success =
eglChooseConfig(m_data->egl_display, egl_config_attribs,
&m_data->egl_config, 1, &m_data->num_configs);
if (!m_data->success) {
printf("Failed to choose a valid an EGLConfig.\n");
exit(0);
}
m_data->egl_surface = eglCreatePbufferSurface(
m_data->egl_display, m_data->egl_config, egl_pbuffer_attribs);
m_data->egl_context = eglCreateContext(
m_data->egl_display, m_data->egl_config, EGL_NO_CONTEXT, nullptr);
eglMakeCurrent(m_data->egl_display, m_data->egl_surface, m_data->egl_surface,
m_data->egl_context);
printf("Finish creating EGL OpenGL window.\n");
const GLubyte* ven = glGetString(GL_VENDOR);
printf("GL_VENDOR=%s\n", ven);
const GLubyte* ren = glGetString(GL_RENDERER);
printf("GL_RENDERER=%s\n", ren);
const GLubyte* ver = glGetString(GL_VERSION);
printf("GL_VERSION=%s\n", ver);
const GLubyte* sl = glGetString(GL_SHADING_LANGUAGE_VERSION);
printf("GL_SHADING_LANGUAGE_VERSION=%s\n", sl);
int i = pthread_getconcurrency();
printf("pthread_getconcurrency()=%d\n", i);
} }
bool EGLOpenGLWindow::isModifierKeyPressed(int key) void EGLOpenGLWindow::closeWindow() {
{ eglMakeCurrent(m_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
return false; EGL_NO_CONTEXT);
eglDestroySurface(m_data->egl_display, m_data->egl_surface);
eglDestroyContext(m_data->egl_display, m_data->egl_context);
printf("Destroy EGL OpenGL window.\n");
} }
void EGLOpenGLWindow::setMouseMoveCallback(b3MouseMoveCallback mouseCallback) void EGLOpenGLWindow::runMainLoop() {}
{
float EGLOpenGLWindow::getTimeInSeconds() { return 0.; }
bool EGLOpenGLWindow::requestedExit() const { return false; }
void EGLOpenGLWindow::setRequestExit() {}
void EGLOpenGLWindow::startRendering() {
// printf("EGL window start rendering.\n");
glViewport(0, 0, m_data->m_windowWidth, m_data->m_windowHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
}
void EGLOpenGLWindow::endRendering() {
// printf("EGL window end rendering.\n");
eglSwapBuffers(m_data->egl_display, m_data->egl_surface);
}
bool EGLOpenGLWindow::isModifierKeyPressed(int key) { return false; }
void EGLOpenGLWindow::setMouseMoveCallback(b3MouseMoveCallback mouseCallback) {
m_data->m_mouseMoveCallback = mouseCallback; m_data->m_mouseMoveCallback = mouseCallback;
} }
b3MouseMoveCallback EGLOpenGLWindow::getMouseMoveCallback() b3MouseMoveCallback EGLOpenGLWindow::getMouseMoveCallback() {
{
return m_data->m_mouseMoveCallback; return m_data->m_mouseMoveCallback;
} }
void EGLOpenGLWindow::setMouseButtonCallback(
void EGLOpenGLWindow::setMouseButtonCallback(b3MouseButtonCallback mouseCallback) b3MouseButtonCallback mouseCallback) {
{
m_data->m_mouseButtonCallback = mouseCallback; m_data->m_mouseButtonCallback = mouseCallback;
} }
b3MouseButtonCallback EGLOpenGLWindow::getMouseButtonCallback() b3MouseButtonCallback EGLOpenGLWindow::getMouseButtonCallback() {
{
return m_data->m_mouseButtonCallback; return m_data->m_mouseButtonCallback;
} }
void EGLOpenGLWindow::setResizeCallback(b3ResizeCallback resizeCallback) void EGLOpenGLWindow::setResizeCallback(b3ResizeCallback resizeCallback) {
{
m_data->m_resizeCallback = resizeCallback; m_data->m_resizeCallback = resizeCallback;
} }
b3ResizeCallback EGLOpenGLWindow::getResizeCallback() b3ResizeCallback EGLOpenGLWindow::getResizeCallback() {
{
return m_data->m_resizeCallback; return m_data->m_resizeCallback;
} }
void EGLOpenGLWindow::setWheelCallback(b3WheelCallback wheelCallback) void EGLOpenGLWindow::setWheelCallback(b3WheelCallback wheelCallback) {
{
m_data->m_wheelCallback = wheelCallback; m_data->m_wheelCallback = wheelCallback;
} }
b3WheelCallback EGLOpenGLWindow::getWheelCallback() b3WheelCallback EGLOpenGLWindow::getWheelCallback() {
{
return m_data->m_wheelCallback; return m_data->m_wheelCallback;
} }
void EGLOpenGLWindow::setKeyboardCallback( b3KeyboardCallback keyboardCallback) void EGLOpenGLWindow::setKeyboardCallback(b3KeyboardCallback keyboardCallback) {
{
m_data->m_keyboardCallback = keyboardCallback; m_data->m_keyboardCallback = keyboardCallback;
} }
b3KeyboardCallback EGLOpenGLWindow::getKeyboardCallback() b3KeyboardCallback EGLOpenGLWindow::getKeyboardCallback() {
{
return m_data->m_keyboardCallback; return m_data->m_keyboardCallback;
} }
void EGLOpenGLWindow::setRenderCallback( b3RenderCallback renderCallback) void EGLOpenGLWindow::setRenderCallback(b3RenderCallback renderCallback) {}
{ void EGLOpenGLWindow::setWindowTitle(const char* title) {}
}
void EGLOpenGLWindow::setWindowTitle(const char* title)
{
}
float EGLOpenGLWindow::getRetinaScale() const float EGLOpenGLWindow::getRetinaScale() const { return 1.f; }
{
return 1.f;
}
void EGLOpenGLWindow::setAllowRetina(bool allow) void EGLOpenGLWindow::setAllowRetina(bool allow) {}
{
}
int EGLOpenGLWindow::getWidth() const { return m_data->m_windowWidth; }
int EGLOpenGLWindow::getWidth() const int EGLOpenGLWindow::getHeight() const { return m_data->m_windowHeight; }
{
return m_data->m_windowWidth;
}
int EGLOpenGLWindow::getHeight() const int EGLOpenGLWindow::fileOpenDialog(char* fileName, int maxFileNameLength) {
{
return m_data->m_windowHeight;
}
int EGLOpenGLWindow::fileOpenDialog(char* fileName, int maxFileNameLength)
{
return 0; return 0;
} }
#endif //BT_USE_EGL #endif // BT_USE_EGL

View File

@@ -31,13 +31,17 @@ float shadowMapWorldSize=10;
#ifndef __APPLE__ #ifndef __APPLE__
#ifndef glVertexAttribDivisor #ifndef glVertexAttribDivisor
#ifndef NO_GLEW
#define glVertexAttribDivisor glVertexAttribDivisorARB #define glVertexAttribDivisor glVertexAttribDivisorARB
#endif //NO_GLEW
#endif //glVertexAttribDivisor #endif //glVertexAttribDivisor
#ifndef GL_COMPARE_REF_TO_TEXTURE #ifndef GL_COMPARE_REF_TO_TEXTURE
#define GL_COMPARE_REF_TO_TEXTURE GL_COMPARE_R_TO_TEXTURE #define GL_COMPARE_REF_TO_TEXTURE GL_COMPARE_R_TO_TEXTURE
#endif //GL_COMPARE_REF_TO_TEXTURE #endif //GL_COMPARE_REF_TO_TEXTURE
#ifndef glDrawElementsInstanced #ifndef glDrawElementsInstanced
#ifndef NO_GLEW
#define glDrawElementsInstanced glDrawElementsInstancedARB #define glDrawElementsInstanced glDrawElementsInstancedARB
#endif //NO_GLEW
#endif #endif
#endif //__APPLE__ #endif //__APPLE__
#include "GLInstancingRenderer.h" #include "GLInstancingRenderer.h"

View File

@@ -27,8 +27,14 @@ subject to the following restrictions:
#ifdef GLEW_STATIC #ifdef GLEW_STATIC
#include "CustomGL/glew.h" #include "CustomGL/glew.h"
#else #else
#ifdef NO_GLEW
#define GL_GLEXT_LEGACY
#include "third_party/GL/gl/include/GL/gl.h"
#include "third_party/GL/gl/include/GL/glext.h"
#else
#include <GL/glew.h> #include <GL/glew.h>
#endif//GLEW_STATIC #endif //NO_GLEW
#endif //GLEW_STATIC
#ifdef _WINDOWS #ifdef _WINDOWS
#include <windows.h> #include <windows.h>

View File

@@ -34,7 +34,13 @@ subject to the following restrictions:
#ifdef GLEW_STATIC #ifdef GLEW_STATIC
#include "CustomGL/glew.h" #include "CustomGL/glew.h"
#else #else
#ifdef NO_GLEW
#define GL_GLEXT_LEGACY
#include "third_party/GL/gl/include/GL/gl.h"
#include "third_party/GL/gl/include/GL/glext.h"
#else
#include <GL/glew.h> #include <GL/glew.h>
#endif //NO_GLEW
#endif //GLEW_STATIC #endif //GLEW_STATIC
#ifdef _WINDOWS #ifdef _WINDOWS

View File

@@ -20,7 +20,11 @@
#include "Win32OpenGLWindow.h" #include "Win32OpenGLWindow.h"
#else #else
//let's cross the fingers it is Linux/X11 //let's cross the fingers it is Linux/X11
#ifdef BT_USE_EGL
#include "EGLOpenGLWindow.h"
#else
#include "X11OpenGLWindow.h" #include "X11OpenGLWindow.h"
#endif //BT_USE_EGL
#endif //_WIN32 #endif //_WIN32
#endif//__APPLE__ #endif//__APPLE__
#include <stdio.h> #include <stdio.h>
@@ -115,25 +119,27 @@ SimpleOpenGL2App::SimpleOpenGL2App(const char* title, int width, int height)
m_window->setWindowTitle(title); m_window->setWindowTitle(title);
#ifndef NO_GLEW
#ifndef __APPLE__ #ifndef __APPLE__
#ifndef _WIN32 #ifndef _WIN32
//some Linux implementations need the 'glewExperimental' to be true //some Linux implementations need the 'glewExperimental' to be true
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
#endif #endif //_WIN32
if (glewInit() != GLEW_OK) if (glewInit() != GLEW_OK)
{ {
b3Error("glewInit failed"); b3Error("glewInit failed");
exit(1); exit(1);
} }
if (!GLEW_VERSION_2_1) // check that the machine supports the 2.1 API. if (!GLEW_VERSION_2_1) // check that the machine supports the 2.1 API.
{ {
b3Error("GLEW_VERSION_2_1 needs to support 2_1"); b3Error("GLEW_VERSION_2_1 needs to support 2_1");
exit(1); // or handle the error in a nicer way exit(1); // or handle the error in a nicer way
} }
#endif #endif //__APPLE__
#endif //NO_GLEW
TwGenerateDefaultFonts(); TwGenerateDefaultFonts();

View File

@@ -12,7 +12,11 @@
#include "Win32OpenGLWindow.h" #include "Win32OpenGLWindow.h"
#else #else
//let's cross the fingers it is Linux/X11 //let's cross the fingers it is Linux/X11
#ifdef BT_USE_EGL
#include "EGLOpenGLWindow.h"
#else
#include "X11OpenGLWindow.h" #include "X11OpenGLWindow.h"
#endif //BT_USE_EGL
#endif //_WIN32 #endif //_WIN32
#endif//__APPLE__ #endif//__APPLE__
#include <stdio.h> #include <stdio.h>
@@ -151,19 +155,21 @@ SimpleOpenGL3App::SimpleOpenGL3App( const char* title, int width,int height, boo
b3Assert(glGetError() ==GL_NO_ERROR); b3Assert(glGetError() ==GL_NO_ERROR);
#ifndef NO_GLEW
#ifndef __APPLE__ #ifndef __APPLE__
#ifndef _WIN32 #ifndef _WIN32
//some Linux implementations need the 'glewExperimental' to be true //some Linux implementations need the 'glewExperimental' to be true
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
#endif #endif //_WIN32
if (glewInit() != GLEW_OK) if (glewInit() != GLEW_OK)
exit(1); // or handle the error in a nicer way exit(1); // or handle the error in a nicer way
if (!GLEW_VERSION_2_1) // check that the machine supports the 2.1 API. if (!GLEW_VERSION_2_1) // check that the machine supports the 2.1 API.
exit(1); // or handle the error in a nicer way exit(1); // or handle the error in a nicer way
#endif #endif //__APPLE__
#endif //NO_GLEW
glGetError();//don't remove this call, it is needed for Ubuntu glGetError();//don't remove this call, it is needed for Ubuntu
b3Assert(glGetError() ==GL_NO_ERROR); b3Assert(glGetError() ==GL_NO_ERROR);

View File

@@ -10,12 +10,18 @@
#include <OpenGL/OpenGL.h> #include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h> #include <OpenGL/gl.h>
#else #else
#ifdef GLEW_STATIC #ifdef GLEW_STATIC
#include "CustomGL/glew.h" #include "CustomGL/glew.h"
#else #else
#include <GL/glew.h> #ifdef NO_GLEW
#endif #define GL_GLEXT_LEGACY
#endif #include "third_party/GL/gl/include/GL/gl.h"
#include "third_party/GL/gl/include/GL/glext.h"
#else
#include <GL/glew.h>
#endif //NO_GLEW
#endif //GLEW_STATIC
#endif//(__APPLE__)
#include "FontData.h" #include "FontData.h"