mirror of https://github.com/mosra/magnum.git
Browse Source
New class EglInterface now handles only EGL, AbstractXContext is able to take any OpenGL interface sublassed from AbstractGlInterface.pull/279/head
7 changed files with 502 additions and 272 deletions
@ -0,0 +1,53 @@ |
|||||||
|
#ifndef Magnum_Contexts_AbstractGlInterface_h |
||||||
|
#define Magnum_Contexts_AbstractGlInterface_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class Magnum::Contexts::AbstractGlInterface |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace Magnum { namespace Contexts { |
||||||
|
|
||||||
|
/** @brief Base for OpenGL interfaces */ |
||||||
|
template<class Display, class VisualId, class Window> class AbstractGlInterface { |
||||||
|
public: |
||||||
|
/**
|
||||||
|
* @brief Get visual ID |
||||||
|
* |
||||||
|
* Initializes the interface on given display and returns visual ID. |
||||||
|
*/ |
||||||
|
virtual VisualId getVisualId(Display nativeDisplay) = 0; |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destructor |
||||||
|
* |
||||||
|
* Finalizes and closes the interface. |
||||||
|
*/ |
||||||
|
virtual ~AbstractGlInterface() {} |
||||||
|
|
||||||
|
/** @brief Create context */ |
||||||
|
virtual void createContext(Window nativeWindow) = 0; |
||||||
|
|
||||||
|
/** @brief Make the context current */ |
||||||
|
virtual void makeCurrent() = 0; |
||||||
|
|
||||||
|
/** @brief Swap buffers */ |
||||||
|
virtual void swapBuffers() = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,227 @@ |
|||||||
|
#ifndef Magnum_Contexts_AbstractXContext_h |
||||||
|
#define Magnum_Contexts_AbstractXContext_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class Magnum::Contexts::AbstractXContext |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "Magnum.h" |
||||||
|
|
||||||
|
#include <X11/Xlib.h> |
||||||
|
#include <X11/Xutil.h> |
||||||
|
/* undef Xlib nonsense to avoid conflicts */ |
||||||
|
#undef None |
||||||
|
#undef Always |
||||||
|
|
||||||
|
#include "AbstractContext.h" |
||||||
|
#include "AbstractGlInterface.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Contexts { |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Base for X11-based contexts |
||||||
|
|
||||||
|
Supports keyboard and mouse handling. |
||||||
|
|
||||||
|
@note Not meant to be used directly, see subclasses. |
||||||
|
*/ |
||||||
|
class AbstractXContext: public AbstractContext { |
||||||
|
public: |
||||||
|
/**
|
||||||
|
* @brief Constructor |
||||||
|
* @param glInterface Interface to OpenGL |
||||||
|
* @param argc Count of arguments of `main()` function |
||||||
|
* @param argv Arguments of `main()` function |
||||||
|
* @param title Window title |
||||||
|
* @param size Window size |
||||||
|
* |
||||||
|
* Creates window with double-buffered OpenGL ES 2 context. |
||||||
|
*/ |
||||||
|
AbstractXContext(AbstractGlInterface<Display*, VisualID, Window>* glInterface, int& argc, char** argv, const std::string& title = "Magnum X/EGL context", const Math::Vector2<GLsizei>& size = Math::Vector2<GLsizei>(800, 600)); |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destructor |
||||||
|
* |
||||||
|
* Deletes context and destroys the window. |
||||||
|
*/ |
||||||
|
virtual ~AbstractXContext() = 0; |
||||||
|
|
||||||
|
int exec(); |
||||||
|
|
||||||
|
/** @{ @name Drawing functions */ |
||||||
|
|
||||||
|
protected: |
||||||
|
/** @copydoc GlutContext::viewportEvent() */ |
||||||
|
virtual void viewportEvent(const Math::Vector2<GLsizei>& size) = 0; |
||||||
|
|
||||||
|
/** @copydoc GlutContext::drawEvent() */ |
||||||
|
virtual void drawEvent() = 0; |
||||||
|
|
||||||
|
/** @copydoc GlutContext::swapBuffers() */ |
||||||
|
inline void swapBuffers() { glInterface->swapBuffers(); } |
||||||
|
|
||||||
|
/** @todo implement */ |
||||||
|
inline void redraw() {} |
||||||
|
|
||||||
|
/*@}*/ |
||||||
|
|
||||||
|
/** @{ @name Keyboard handling */ |
||||||
|
|
||||||
|
public: |
||||||
|
/** @brief Key */ |
||||||
|
enum class Key: KeySym { |
||||||
|
Up = XK_Up, /**< Up arrow */ |
||||||
|
Down = XK_Down, /**< Down arrow */ |
||||||
|
Left = XK_Left, /**< Left arrow */ |
||||||
|
Right = XK_Right, /**< Right arrow */ |
||||||
|
F1 = XK_F1, /**< F1 */ |
||||||
|
F2 = XK_F2, /**< F2 */ |
||||||
|
F3 = XK_F3, /**< F3 */ |
||||||
|
F4 = XK_F4, /**< F4 */ |
||||||
|
F5 = XK_F5, /**< F5 */ |
||||||
|
F6 = XK_F6, /**< F6 */ |
||||||
|
F7 = XK_F7, /**< F7 */ |
||||||
|
F8 = XK_F8, /**< F8 */ |
||||||
|
F9 = XK_F9, /**< F9 */ |
||||||
|
F10 = XK_F10, /**< F10 */ |
||||||
|
F11 = XK_F11, /**< F11 */ |
||||||
|
F12 = XK_F12, /**< F12 */ |
||||||
|
Home = XK_Home, /**< Home */ |
||||||
|
End = XK_End, /**< End */ |
||||||
|
PageUp = XK_Page_Up, /**< Page up */ |
||||||
|
PageDown = XK_Page_Down, /**< Page down */ |
||||||
|
|
||||||
|
Space = XK_space, /**< Space */ |
||||||
|
Comma = XK_comma, /**< Comma */ |
||||||
|
Period = XK_period, /**< Period */ |
||||||
|
Minus = XK_minus, /**< Minus */ |
||||||
|
Plus = XK_plus, /**< Plus */ |
||||||
|
Slash = XK_slash, /**< Slash */ |
||||||
|
Percent = XK_percent, /**< Percent */ |
||||||
|
Equal = XK_equal, /**< Equal */ |
||||||
|
|
||||||
|
Zero = XK_0, /**< Zero */ |
||||||
|
One = XK_1, /**< One */ |
||||||
|
Two = XK_2, /**< Two */ |
||||||
|
Three = XK_3, /**< Three */ |
||||||
|
Four = XK_4, /**< Four */ |
||||||
|
Five = XK_5, /**< Five */ |
||||||
|
Six = XK_6, /**< Six */ |
||||||
|
Seven = XK_7, /**< Seven */ |
||||||
|
Eight = XK_8, /**< Eight */ |
||||||
|
Nine = XK_9, /**< Nine */ |
||||||
|
|
||||||
|
A = XK_a, /**< Small letter A */ |
||||||
|
B = XK_b, /**< Small letter B */ |
||||||
|
C = XK_c, /**< Small letter C */ |
||||||
|
D = XK_d, /**< Small letter D */ |
||||||
|
E = XK_e, /**< Small letter E */ |
||||||
|
F = XK_f, /**< Small letter F */ |
||||||
|
G = XK_g, /**< Small letter G */ |
||||||
|
H = XK_h, /**< Small letter H */ |
||||||
|
I = XK_i, /**< Small letter I */ |
||||||
|
J = XK_j, /**< Small letter J */ |
||||||
|
K = XK_k, /**< Small letter K */ |
||||||
|
L = XK_l, /**< Small letter L */ |
||||||
|
M = XK_m, /**< Small letter M */ |
||||||
|
N = XK_n, /**< Small letter N */ |
||||||
|
O = XK_o, /**< Small letter O */ |
||||||
|
P = XK_p, /**< Small letter P */ |
||||||
|
Q = XK_q, /**< Small letter Q */ |
||||||
|
R = XK_r, /**< Small letter R */ |
||||||
|
S = XK_s, /**< Small letter S */ |
||||||
|
T = XK_t, /**< Small letter T */ |
||||||
|
U = XK_u, /**< Small letter U */ |
||||||
|
V = XK_v, /**< Small letter V */ |
||||||
|
W = XK_w, /**< Small letter W */ |
||||||
|
X = XK_x, /**< Small letter X */ |
||||||
|
Y = XK_y, /**< Small letter Y */ |
||||||
|
Z = XK_z /**< Small letter Z */ |
||||||
|
}; |
||||||
|
|
||||||
|
protected: |
||||||
|
/**
|
||||||
|
* @brief Key press event |
||||||
|
* @param key Key pressed |
||||||
|
* @param position Cursor position |
||||||
|
* |
||||||
|
* Called when an key is pressed. Default implementation does nothing. |
||||||
|
*/ |
||||||
|
virtual void keyPressEvent(Key key, const Math::Vector2<int>& position); |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Key press event |
||||||
|
* @param key Key released |
||||||
|
* @param position Cursor position |
||||||
|
* |
||||||
|
* Called when an key is released. Default implementation does nothing. |
||||||
|
*/ |
||||||
|
virtual void keyReleaseEvent(Key key, const Math::Vector2<int>& position); |
||||||
|
|
||||||
|
/*@}*/ |
||||||
|
|
||||||
|
/** @{ @name Mouse handling */ |
||||||
|
|
||||||
|
public: |
||||||
|
/** @brief Mouse button */ |
||||||
|
enum class MouseButton: unsigned int { |
||||||
|
Left = Button1, /**< Left button */ |
||||||
|
Middle = Button2, /**< Middle button */ |
||||||
|
Right = Button3, /**< Right button */ |
||||||
|
WheelUp = Button4, /**< Wheel up */ |
||||||
|
WheelDown = Button5 /**< Wheel down */ |
||||||
|
}; |
||||||
|
|
||||||
|
protected: |
||||||
|
/**
|
||||||
|
* @brief Mouse press event |
||||||
|
* |
||||||
|
* Called when mouse button is pressed. Default implementation does |
||||||
|
* nothing. |
||||||
|
*/ |
||||||
|
virtual void mousePressEvent(MouseButton button, const Math::Vector2<int>& position); |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Mouse release event |
||||||
|
* |
||||||
|
* Called when mouse button is released. Default implementation does |
||||||
|
* nothing. |
||||||
|
*/ |
||||||
|
virtual void mouseReleaseEvent(MouseButton button, const Math::Vector2<int>& position); |
||||||
|
|
||||||
|
/*@}*/ |
||||||
|
|
||||||
|
private: |
||||||
|
Display* display; |
||||||
|
Window window; |
||||||
|
Atom deleteWindow; |
||||||
|
|
||||||
|
AbstractGlInterface<Display*, VisualID, Window>* glInterface; |
||||||
|
|
||||||
|
/** @todo Get this from the created window */ |
||||||
|
Math::Vector2<GLsizei> viewportSize; |
||||||
|
}; |
||||||
|
|
||||||
|
inline void AbstractXContext::keyPressEvent(AbstractXContext::Key, const Math::Vector2<int>&) {} |
||||||
|
inline void AbstractXContext::keyReleaseEvent(AbstractXContext::Key, const Math::Vector2<int>&) {} |
||||||
|
inline void AbstractXContext::mousePressEvent(AbstractXContext::MouseButton, const Math::Vector2<int>&) {} |
||||||
|
inline void AbstractXContext::mouseReleaseEvent(AbstractXContext::MouseButton, const Math::Vector2<int>&) {} |
||||||
|
|
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,86 @@ |
|||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "EglInterface.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Contexts { |
||||||
|
|
||||||
|
EglInterface::~EglInterface() { |
||||||
|
eglDestroyContext(display, context); |
||||||
|
eglDestroySurface(display, surface); |
||||||
|
eglTerminate(display); |
||||||
|
} |
||||||
|
|
||||||
|
VisualId EglInterface::getVisualId(EGLNativeDisplayType nativeDisplay) { |
||||||
|
/* Initialize */ |
||||||
|
display = eglGetDisplay(nativeDisplay); |
||||||
|
eglInitialize(display, 0, 0); |
||||||
|
#ifndef MAGNUM_TARGET_GLES |
||||||
|
eglBindAPI(EGL_OPENGL_API); |
||||||
|
#else |
||||||
|
eglBindAPI(EGL_OPENGL_ES_API); |
||||||
|
#endif |
||||||
|
|
||||||
|
/* Choose EGL config */ |
||||||
|
static const EGLint attribs[] = { |
||||||
|
EGL_RED_SIZE, 1, |
||||||
|
EGL_GREEN_SIZE, 1, |
||||||
|
EGL_BLUE_SIZE, 1, |
||||||
|
EGL_DEPTH_SIZE, 1, |
||||||
|
#ifndef MAGNUM_TARGET_GLES |
||||||
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, |
||||||
|
#else |
||||||
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
||||||
|
#endif |
||||||
|
EGL_NONE |
||||||
|
}; |
||||||
|
EGLint configCount; |
||||||
|
if(!eglChooseConfig(display, attribs, &config, 1, &configCount)) { |
||||||
|
Error() << "Cannot get EGL visual config"; |
||||||
|
exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
/* Get visual ID */ |
||||||
|
EGLint visualId; |
||||||
|
if(!eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &visualId)) { |
||||||
|
Error() << "Cannot get native visual ID"; |
||||||
|
exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
return visualId; |
||||||
|
} |
||||||
|
|
||||||
|
void EglInterface::createContext(EGLNativeWindowType window) { |
||||||
|
static const EGLint contextAttributes[] = { |
||||||
|
#ifdef MAGNUM_TARGET_GLES |
||||||
|
EGL_CONTEXT_CLIENT_VERSION, 2, |
||||||
|
#endif |
||||||
|
EGL_NONE |
||||||
|
}; |
||||||
|
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttributes); |
||||||
|
if(!context) { |
||||||
|
Error() << "Cannot create EGL context"; |
||||||
|
exit(1); |
||||||
|
} |
||||||
|
surface = eglCreateWindowSurface(display, config, window, NULL); |
||||||
|
if(!surface) { |
||||||
|
Error() << "Cannot create window surface"; |
||||||
|
exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
/** @bug Fixme: On desktop OpenGL and Mesa EGL implementation OpenGL version is 1.0, which is wrong */ |
||||||
|
} |
||||||
|
|
||||||
|
}} |
||||||
@ -0,0 +1,71 @@ |
|||||||
|
#ifndef Magnum_Contexts_EglInterface_h |
||||||
|
#define Magnum_Contexts_EglInterface_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class Magnum::Contexts::EglInterface |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "Magnum.h" |
||||||
|
|
||||||
|
#ifndef SUPPORT_X11 |
||||||
|
#define SUPPORT_X11 // OpenGL ES on BeagleBoard needs this (?)
|
||||||
|
#endif |
||||||
|
#include <EGL/egl.h> |
||||||
|
|
||||||
|
#include "AbstractGlInterface.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Contexts { |
||||||
|
|
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
/* EGL returns visual ID as int, but Xorg expects long unsigned int */ |
||||||
|
#ifdef __unix__ |
||||||
|
typedef VisualID VisualId; |
||||||
|
#else |
||||||
|
typedef EGLInt VisualId; |
||||||
|
#endif |
||||||
|
#endif |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief EGL interface |
||||||
|
|
||||||
|
Used in XEglContext. |
||||||
|
*/ |
||||||
|
class EglInterface: public AbstractGlInterface<EGLNativeDisplayType, VisualId, EGLNativeWindowType> { |
||||||
|
public: |
||||||
|
~EglInterface(); |
||||||
|
|
||||||
|
VisualId getVisualId(EGLNativeDisplayType nativeDisplay); |
||||||
|
void createContext(EGLNativeWindowType nativeWindow); |
||||||
|
|
||||||
|
inline void makeCurrent() { |
||||||
|
eglMakeCurrent(display, surface, surface, context); |
||||||
|
} |
||||||
|
|
||||||
|
inline void swapBuffers() { |
||||||
|
eglSwapBuffers(display, surface); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
EGLDisplay display; |
||||||
|
EGLConfig config; |
||||||
|
EGLSurface surface; |
||||||
|
EGLContext context; |
||||||
|
}; |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
Loading…
Reference in new issue