1 /******************************************************************************
2  *
3  * Project:  OpenCPN
4  *
5  ***************************************************************************
6  *   Copyright (C) 2013 by David S. Register                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.         *
22  ***************************************************************************
23  */
24 
25 #ifndef __GLCHARTCANVAS_H__
26 #define __GLCHARTCANVAS_H__
27 
28 #include <wx/glcanvas.h>
29 
30 #include "dychart.h"
31 
32 #include "ocpn_types.h"
33 #include "OCPNRegion.h"
34 #include "LLRegion.h"
35 #include "viewport.h"
36 #include "TexFont.h"
37 #include "ocpndc.h"
38 
39  #define FORMAT_BITS           GL_RGB
40 
41 #ifdef __OCPN__ANDROID__
42 #include "wx/qt/private/wxQtGesture.h"
43 #endif
44 
45 // Correct some deficincies in MacOS OpenGL include files
46 #ifdef __WXOSX__
47 typedef void (*PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
48 typedef void (*PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
49 typedef void (*PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers);
50 typedef void (*PFNGLGETBUFFERPARAMETERIVPROC) (GLenum target, GLenum pname, GLint *params);
51 typedef void (*PFNGLDELETERENDERBUFFERSEXTPROC) (GLsizei n, const GLuint *renderbuffers);
52 typedef void (*PFNGLDELETEFRAMEBUFFERSEXTPROC) (GLsizei n, const GLuint *framebuffers);
53 typedef void (*PFNGLCOMPRESSEDTEXSUBIMAGE1DPROC) (GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLsizei imageSize, const GLvoid *data);
54 typedef void (*PFNGLGETCOMPRESSEDTEXIMAGEPROC) (GLenum target, GLint level, GLvoid *img);
55 typedef GLenum (*PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC) (GLenum target);
56 typedef void (*PFNGLBINDRENDERBUFFEREXTPROC) (GLenum target, GLuint renderbuffer);
57 typedef void (*PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
58 typedef void (*PFNGLGENFRAMEBUFFERSEXTPROC) (GLsizei n, GLuint *framebuffers);
59 typedef void (*PFNGLGENRENDERBUFFERSEXTPROC) (GLsizei n, GLuint *renderbuffers);
60 typedef void (*PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
61 typedef void (*PFNGLCOMPRESSEDTEXIMAGE2DPROC) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);
62 typedef void (*PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
63 typedef void (*PFNGLRENDERBUFFERSTORAGEEXTPROC) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
64 typedef void (*PFNGLBINDFRAMEBUFFEREXTPROC) (GLenum target, GLuint framebuffer);
65 #endif
66 
67 
68 class glTexFactory;
69 class ChartCanvas;
70 
71 #define GESTURE_EVENT_TIMER 78334
72 #define ZOOM_TIMER 78335
73 #define GESTURE_FINISH_TIMER 78336
74 #define TEX_FADE_TIMER 78337
75 
76 typedef class{
77   public:
78     wxString Renderer;
79     GLenum TextureRectangleFormat;
80 
81     bool bOldIntel;
82     bool bCanDoVBO;
83     bool bCanDoFBO;
84 
85     //      Vertex Buffer Object (VBO) support
86     PFNGLGENBUFFERSPROC                 m_glGenBuffers;
87     PFNGLBINDBUFFERPROC                 m_glBindBuffer;
88     PFNGLBUFFERDATAPROC                 m_glBufferData;
89     PFNGLDELETEBUFFERSPROC              m_glDeleteBuffers;
90 
91     //      Frame Buffer Object (FBO) support
92     PFNGLGENFRAMEBUFFERSEXTPROC         m_glGenFramebuffers;
93     PFNGLGENRENDERBUFFERSEXTPROC        m_glGenRenderbuffers;
94     PFNGLFRAMEBUFFERTEXTURE2DEXTPROC    m_glFramebufferTexture2D;
95     PFNGLBINDFRAMEBUFFEREXTPROC         m_glBindFramebuffer;
96     PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC m_glFramebufferRenderbuffer;
97     PFNGLRENDERBUFFERSTORAGEEXTPROC     m_glRenderbufferStorage;
98     PFNGLBINDRENDERBUFFEREXTPROC        m_glBindRenderbuffer;
99     PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC  m_glCheckFramebufferStatus;
100     PFNGLDELETEFRAMEBUFFERSEXTPROC      m_glDeleteFramebuffers;
101     PFNGLDELETERENDERBUFFERSEXTPROC     m_glDeleteRenderbuffers;
102 
103     PFNGLCOMPRESSEDTEXIMAGE2DPROC       m_glCompressedTexImage2D;
104     PFNGLGETCOMPRESSEDTEXIMAGEPROC      m_glGetCompressedTexImage;
105 
106 }OCPN_GLCaps;
107 
108 void GetglEntryPoints( OCPN_GLCaps *pcaps );
109 GLboolean QueryExtension( const char *extName );
110 
111 
112 class ocpnGLOptions
113 {
114 public:
115     bool m_bUseAcceleratedPanning;
116     bool m_bUseCanvasPanning;
117 
118     bool m_bTextureCompression;
119     bool m_bTextureCompressionCaching;
120 
121     int m_iTextureDimension;
122     int m_iTextureMemorySize;
123 
124     bool m_GLPolygonSmoothing;
125     bool m_GLLineSmoothing;
126 };
127 
128 
129 class glTestCanvas : public wxGLCanvas
130 {
131 public:
132     glTestCanvas(wxWindow *parent);
~glTestCanvas()133     ~glTestCanvas() {};
134 
135 };
136 
137 
138 class ocpnDC;
139 class emboss_data;
140 class Route;
141 class ChartBaseBSB;
142 
143 class glChartCanvas : public wxGLCanvas
144 {
145 public:
146     static bool CanClipViewport(const ViewPort &vp);
147     static ViewPort ClippedViewport(const ViewPort &vp, const LLRegion &region);
148 
149     static bool HasNormalizedViewPort(const ViewPort &vp);
150     static void MultMatrixViewPort(ViewPort &vp, float lat=0, float lon=0);
151     static ViewPort NormalizedViewPort(const ViewPort &vp, float lat=0, float lon=0);
152 
153     static void RotateToViewPort(const ViewPort &vp);
154     static void DrawRegion( ViewPort &vp, const LLRegion &region);
155     static void SetClipRegion( ViewPort &vp, const LLRegion &region);
156     static void SetClipRect(const ViewPort &vp, const wxRect &rect, bool g_clear=false);
157     static void DisableClipRegion();
158     void SetColorScheme(ColorScheme cs);
159     void RenderTextures(float *coords, float *uvCoords, int nVertex, ViewPort *vp);
160     static void RenderSingleTexture(float *coords, float *uvCoords,ViewPort *vp, float dx, float dy, float angle);
161     void RenderColorRect(wxRect r, wxColor &color);
162 
163     static bool         s_b_useScissorTest;
164     static bool         s_b_useStencil;
165     static bool         s_b_useStencilAP;
166     static bool         s_b_useFBO;
167 
168     void SendJSONConfigMessage();
169 
170     glChartCanvas(wxWindow *parent, wxGLCanvas *share = NULL);
171 
172     ~glChartCanvas();
173 
174     void Init();
SetContext(wxGLContext * pcontext)175     void SetContext(wxGLContext *pcontext) { m_pcontext = pcontext; }
176 
177     void OnPaint(wxPaintEvent& event);
178     void OnEraseBG(wxEraseEvent& evt);
179     void Render();
180     void OnActivate ( wxActivateEvent& event );
181     void OnSize ( wxSizeEvent& event );
182     void MouseEvent(wxMouseEvent& event);
183     void FastPan(int dx, int dy);
184     void FastZoom(float factor, float cp_x, float cp_y, float post_x, float post_y);
185 //    void RenderCanvasBackingChart( ocpnDC dc, OCPNRegion chart_get_region);
186 //    void FastZoom(float factor);
187     void RenderCanvasBackingChart( ocpnDC &dc, OCPNRegion chart_get_region);
188 
189 #ifdef __OCPN__ANDROID__
190     void OnEvtPanGesture( wxQT_PanGestureEvent &event);
191     void OnEvtPinchGesture( wxQT_PinchGestureEvent &event);
192     void onGestureTimerEvent(wxTimerEvent &event);
193     void onGestureFinishTimerEvent(wxTimerEvent &event);
194 #endif
195 
196     void onZoomTimerEvent(wxTimerEvent &event);
197 
GetRendererString()198     wxString GetRendererString(){ return m_renderer; }
GetVersionString()199     wxString GetVersionString(){ return m_version; }
EnablePaint(bool b_enable)200     void EnablePaint(bool b_enable){ m_b_paint_enable = b_enable; }
201 
202     void Invalidate();
203     void RenderRasterChartRegionGL(ChartBase *chart, ViewPort &vp, LLRegion &region);
204 
205     void DrawGLOverLayObjects(void);
206     void GridDraw( );
207     void FlushFBO( void );
208 
209     void DrawDynamicRoutesTracksAndWaypoints( ViewPort &vp );
210     void DrawStaticRoutesTracksAndWaypoints( ViewPort &vp );
211 
212     void RenderAllChartOutlines( ocpnDC &dc, ViewPort &VP );
213     void RenderChartOutline( ocpnDC &dc, int dbIndex, ViewPort &VP );
214 
215     void DrawEmboss( emboss_data *emboss );
216     void ShipDraw(ocpnDC& dc);
217 
218     void SetupCompression();
CanAcceleratePanning()219     bool CanAcceleratePanning() { return m_b_BuiltFBO; }
UsingFBO()220     bool UsingFBO() { return m_b_BuiltFBO; }
221 
isInGesture()222     bool isInGesture(){ return m_binGesture; }
223 
224     time_t m_last_render_time;
225 
226     int viewport[4];
227     double mvmatrix[16], projmatrix[16];
228 
229     void SetupOpenGL();
230     ChartCanvas *m_pParentCanvas;
231 
232 protected:
233     void RenderS57TextOverlay( ViewPort &VPoint);
234     void RenderMBTilesOverlay( ViewPort &VPoint);
235     void RenderScene( bool bRenderCharts = true, bool bRenderOverlays = true);
236 
237 
238     void RenderGLAlertMessage();
239 
240     void RenderQuiltViewGL( ViewPort &vp, const OCPNRegion &rect_region );
241     void RenderQuiltViewGLText( ViewPort &vp, const OCPNRegion &rect_region );
242 
243     void BuildFBO();
244     bool buildFBOSize(int fboSize);
245 
246     void configureShaders( ViewPort &vp);
247 
248 //    void ComputeRenderQuiltViewGLRegion( ViewPort &vp, OCPNRegion &Region );
249     void RenderCharts(ocpnDC &dc, const OCPNRegion &rect_region);
250     void RenderNoDTA(ViewPort &vp, const LLRegion &region, int transparency = 255);
251     void RenderNoDTA(ViewPort &vp, ChartBase *chart);
252     void RenderWorldChart(ocpnDC &dc, ViewPort &vp, wxRect &rect, bool &world_view);
253 
254     void DrawFloatingOverlayObjects( ocpnDC &dc );
255     void DrawGroundedOverlayObjects(ocpnDC &dc, ViewPort &vp);
256 
257     void DrawChartBar( ocpnDC &dc );
258     void DrawQuiting();
259     void DrawCloseMessage(wxString msg);
260 
261     void DrawGLTidesInBBox(ocpnDC& dc, LLBBox& BBox);
262     void DrawGLCurrentsInBBox(ocpnDC& dc, LLBBox& BBox);
263 
264     void ZoomProject(float offset_x, float offset_y, float swidth, float sheight);
265 
266     void RendertoTexture(GLint tex);
267 
268     void fboFade(GLint tex0, GLint tex1);
269     void onFadeTimerEvent(wxTimerEvent &event);
270     bool m_inFade;
271 
272     wxGLContext       *m_pcontext;
273 
274     ocpnDC            m_gldc;
275 
276     int max_texture_dimension;
277 
278     bool m_bsetup;
279 
280     wxString m_renderer;
281     wxString m_version;
282     wxString m_extensions;
283 
284     ViewPort    m_cache_vp;
285     ChartBase   *m_cache_current_ch;
286 
287     bool        m_b_paint_enable;
288     int         m_in_glpaint;
289 
290     //    For FBO(s)
291     bool         m_b_DisableFBO;
292     bool         m_b_BuiltFBO;
293     bool         m_b_useFBOStencil;
294     GLuint       m_fb0;
295     GLuint       m_renderbuffer;
296 
297     GLuint       m_cache_tex[2];
298     GLuint       m_cache_page;
299     int          m_cache_tex_x;
300     int          m_cache_tex_y;
301 
302     GLuint      ownship_tex;
303     int         ownship_color;
304     wxSize      ownship_size, ownship_tex_size;
305 
306     GLuint      m_piano_tex;
307 
308     float       m_fbo_offsetx;
309     float       m_fbo_offsety;
310     float       m_fbo_swidth;
311     float       m_fbo_sheight;
312 
313     float       m_lastfbo_offsetx;
314     float       m_lastfbo_offsety;
315     float       m_lastfbo_swidth;
316     float       m_lastfbo_sheight;
317 
318     float       m_offsetxStep, m_offsetyStep, m_swidthStep, m_sheightStep;
319     float       m_runoffsetx, m_runoffsety, m_runswidth, m_runsheight;
320     float       m_nStep, m_nTotal, m_nRun;
321     bool        m_zoomFinal;
322     double      m_zoomFinalZoom;
323     int         m_zoomFinaldx, m_zoomFinaldy;
324     bool        m_bforcefull;
325 
326 
327     wxTimer     zoomTimer;
328 
329     double      m_fbo_lat, m_fbo_lon;
330     int         m_cc_x,m_cc_y;
331     wxPoint     m_lpinchPoint;
332 
333     bool        m_binPinch;
334     bool        m_binPan;
335     bool        m_binGesture;
336     bool        m_bfogit;
337     bool        m_benableFog;
338     bool        m_benableVScale;
339 
340     wxTimer     m_gestureEeventTimer;
341     wxTimer     m_gestureFinishTimer;
342     bool        m_bgestureGuard;
343     bool        m_bpinchGuard;
344     wxPoint     m_pinchStart;
345     double      m_pinchlat, m_pinchlon;
346 
347     wxTimer     m_fadeTimer;
348 
349     OCPNRegion  m_canvasregion;
350     TexFont     m_gridfont;
351 
352     int		m_LRUtime;
353 
354     GLuint       m_tideTex;
355     GLuint       m_currentTex;
356     int          m_tideTexWidth;
357     int          m_tideTexHeight;
358     int          m_currentTexWidth;
359     int          m_currentTexHeight;
360 
361     DECLARE_EVENT_TABLE()
362 };
363 
364 extern void BuildCompressedCache();
365 
366 #include "glTextureManager.h"
367 extern glTextureManager   *g_glTextureManager;
368 
369 #endif
370