首页 > 代码库 > 三维引擎设计-图形窗口封装
三维引擎设计-图形窗口封装
一:概述:
每个操作系统都有自己的图形系统,三维引擎会抽象出一个窗口,然后通过继承的方式,子类分别封装不同平台下面的窗口,另外,三维图形API也支持将内容渲染到其他表面上,比如纹理中,所以三维引擎也会抽象出一个纹理,再通过继承的方式,由子类分别封装不同图形API的纹理。
窗口和纹理,都可以看成一个抽象的画布,接收三维引擎的结果渲染到这个画布上,总体来说,一个抽象的画布,代表抽象的窗口或纹理,不同平台和图形API下的窗口和纹理又子类实现。这是三维引擎封装窗口系统的一种方法。
二:OSG的设计:
1:继承关系:
osg::GraphicsContext(图形系统抽象)
|-osg::GraphicsWindow(窗口及渲染环境抽象)
|-osg::GraphicsWindowFOX()
|-osg::GraphicsWindowWX(WX窗口)
|-osg::GraphicsWindowQt(QT窗口)
|-osg::GraphicsWindowCarbon(MacOS窗口)
|-osg::GraphicsWindowCocoa(Mac OS X窗口)
|-osg::GraphicsWindowIOS(IOS窗口)
|-osg::GraphicsWindowWin32(Win32窗口)
|-osg::GraphicsWindowX11(X11窗口)
|-osg::PixelBufferCarbon(MacOS像素缓存)
|-osg::PixelBufferCocoa(Mac OS X像素缓存)
|-osg::PixelBufferWin32(W32像素缓存)
|-osg::PixelBufferX11(X11像素缓存)
三:OGRE的设计:
1:继承关系:
OgreRenderTarget(抽象画布,接收引擎绘制结果)
|-OgreRenderWindow(抽象窗口)
|-OgreWin32Window
|-OgreGLXWindow
|-OgreAndroidWindow
|-OgreD3D9RenderWindow
|-OgreD3D11RenderWindow
|-OgreEAGL2Window
|-OgreOSXCocoaWindow
|-OgreOSXWindow
|-OgreSDLWindow
|-OgreNaClWindow
|-OgreRenderTexture
|-D3D11RenderTexture
|-D3D9RenderTexture
|-GLESRenderTexture
|-GLES2RenderTexture
四: 窗口封装例子:
#ifndef CANVAS_H #define CANVAS_H #include <iostream> #include <string> #define CALLBACK #define ES_WINDOW_RGB 0 #define ES_WINDOW_ALPHA 1 #define ES_WINDOW_DEPTH 2 #define ES_WINDOW_STENCIL 4 #define ES_WINDOW_MULTISAMPLE 8 namespace TDEngine { typedef void (CALLBACK *TDEngineKeyFunc)(unsigned char, int, int); typedef void (CALLBACK *TDEngineMouseFunc)(int, int, int, int); typedef void (CALLBACK *TDEngineDrawFunc)(); class Canvas { private: virtual bool initDisplay(void) { return false; } virtual bool initWindow(void) { return false; } virtual bool initContext(void) { return false; } virtual bool userInterrupt(void) { return false; } public: Canvas() { _width = 0; _height = 0; _flags = 0; _keyFunc = 0; _mouseFunc = 0; _drawFunc = 0; _currFps = 0; _frames = 0; _totalTime = 0; _wndType = 0; _eglDisplay = 0; _eglContext = 0; _eglSurface = 0; _eglConfig = 0; _clientWidth = 0; _clientHeight = 0; _handler = NULL; disableRootEvents = 0; } virtual ~Canvas() { } virtual bool createWindow(int winWidth, int winHeight, int winOptions,std::string winTitle = "") =0; virtual void mainLoop(void) {} virtual void registerKeyboardFunc(TDEngineKeyFunc keyFunc) {} virtual void registerMouseFunc(TDEngineMouseFunc mouseFunc) {} virtual void registerDrawFunc(TDEngineDrawFunc drawFunc) {} virtual void addEventListener(std::string name, ScreenActionHandler action,bool bubble) = 0; virtual void removeEventListener(std::string name,ScreenActionHandler action, bool bubble) = 0; virtual void removeEventALLListener() = 0; virtual void fireEvents(const std::string& name,EventOption& eventOption) = 0; public: int _width; int _height; int _flags; std::string _title; TDEngineKeyFunc _keyFunc; TDEngineMouseFunc _mouseFunc; TDEngineDrawFunc _drawFunc; float _currFps; int _frames; float _totalTime; unsigned long _wndType; void* _eglDisplay; void* _eglContext; void* _eglSurface; void* _eglConfig; int _clientWidth; int _clientHeight; ScreenSpaceEventHandler* _handler; std::map<std::string, ScreenActionHandler> _callbackActionList; bool disableRootEvents; }; } #endif // CANVAS_H
#ifndef CANVASX11_H #define CANVASX11_H #include <X11/Xlib.h> #include <X11/Xatom.h> #include <X11/Xutil.h> #include "Canvas.h" #include "ScreenSpaceEventHandler.h" namespace Cesium { class CanvasX11: public Canvas { public: Window _win; private: virtual bool initDisplay(void); virtual bool initWindow(void); virtual bool initContext(void); virtual bool userInterrupt(void); public: CanvasX11(); virtual ~CanvasX11(); virtual bool createWindow(int winWidth, int winHeight, int winOptions, std::string winTitle = ""); virtual void mainLoop(void); virtual void registerKeyboardFunc(CesiumKeyFunc keyFun); virtual void registerMouseFunc(CesiumMouseFunc mouseFunc); virtual void registerDrawFunc(CesiumDrawFunc drawFunc); virtual void fireEvents(const std::string& name, EventOption& eventOption); virtual void addEventListener(std::string name, ScreenActionHandler action, bool bubble); virtual void removeEventALLListener(){ } void removeEventListener(std::string name, ScreenActionHandler callback, bool bubble); static void moveMouse(Canvas& canvas, MouseButtons button, const Cartesian2& startPosition, const Cartesian2& endPosition, bool shiftKey = false); Display* get_xDisplay(); private: Display* _xDisplay; Window _rootWin; private: typedef ScreenActionHandler CallFunc; typedef std::vector<CallFunc> CallFuncList; typedef std::map<std::string, CallFuncList> CallBacks; CallBacks _callbacks; std::vector<CallFunc> _keydown; std::vector<CallFunc> _mousemove; std::vector<CallFunc> _mouseup; std::vector<CallFunc> _mousedown; std::vector<CallFunc> _dblclick; std::vector<CallFunc> _mousewheel; std::vector<CallFunc> _touchstart; std::vector<CallFunc> _touchmove; std::vector<CallFunc> _touchend; S32 _numRegistered; bool _disableRootEvents; }; } #endif // CANVASX11_H
三维引擎设计-图形窗口封装