Stellarium 0.12.4
StelProjector.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2003 Fabien Chereau
4  * Copyright (C) 2012 Matthew Gates
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19  */
20 
21 #ifndef _STELPROJECTOR_HPP_
22 #define _STELPROJECTOR_HPP_
23 
24 #include "StelProjectorType.hpp"
25 #include "VecMath.hpp"
26 #include "StelSphereGeometry.hpp"
27 #include "renderer/StelGLSLShader.hpp"
28 
29 
39 {
40 public:
41  friend class StelCore;
42 
48  {
49  public:
60  virtual bool init(class StelGLSLShader* shader) = 0;
61 
68  virtual void preDraw(class StelGLSLShader* shader) = 0;
69 
73  virtual void postDraw(class StelGLSLShader* shader) = 0;
74  };
75 
76  class ModelViewTranform;
79  typedef QSharedPointer<ModelViewTranform> ModelViewTranformP;
80 
84  {
85  public:
86  ModelViewTranform() {;}
87  virtual ~ModelViewTranform() {;}
88  virtual void forward(Vec3d&) const =0;
89  virtual void backward(Vec3d&) const =0;
90  virtual void forward(Vec3f&) const =0;
91  virtual void backward(Vec3f&) const =0;
92 
93  virtual void combine(const Mat4d&)=0;
94  virtual ModelViewTranformP clone() const=0;
95 
108  virtual bool setupGLSLTransform(class StelGLSLShader* shader) = 0;
109 
114  virtual void setGLSLUniforms(class StelGLSLShader* shader) = 0;
115 
117  virtual void disableGLSLTransform(class StelGLSLShader* shader) = 0;
118 
119  virtual Mat4d getApproximateLinearTransfo() const=0;
120  };
121 
123  {
124  public:
125  Mat4dTransform(const Mat4d& m);
126  void forward(Vec3d& v) const;
127  void backward(Vec3d& v) const;
128  void forward(Vec3f& v) const;
129  void backward(Vec3f& v) const;
130  void combine(const Mat4d& m);
131  Mat4d getApproximateLinearTransfo() const;
132  ModelViewTranformP clone() const;
133 
134  virtual bool setupGLSLTransform(StelGLSLShader* shader)
135  {
136  if(!shader->hasVertexShader("Mat4dTransform"))
137  {
138  static const QString source(
139  "uniform mat4 modelViewMatrix;\n"
140  "\n"
141  "vec4 modelViewForward(in vec4 v)\n"
142  "{\n"
143  " return modelViewMatrix * v;\n"
144  "}\n");
145 
146  if(!shader->addVertexShader("Mat4dTransform", source))
147  {
148  return false;
149  }
150  qDebug() << "Build log after adding a Mat4d transform shader: "
151  << shader->log();
152  }
153  shader->enableVertexShader("Mat4dTransform");
154  return true;
155  }
156 
157  virtual void setGLSLUniforms(StelGLSLShader* shader)
158  {
159  shader->setUniformValue("modelViewMatrix", transfoMatf);
160  }
161 
162  virtual void disableGLSLTransform(StelGLSLShader* shader)
163  {
164  shader->disableVertexShader("Mat4dTransform");
165  }
166 
167  private:
169  Mat4d transfoMat;
170  Mat4f transfoMatf;
171  };
172 
176  {
179  };
180 
184  {
185  StelProjectorParams() : viewportXywh(0, 0, 256, 256), fov(60.f), gravityLabels(false), defautAngleForGravityText(0.f), maskType(MaskNone), viewportCenter(128.f, 128.f), flipHorz(false), flipVert(false) {;}
186  Vector4<int> viewportXywh;
187  float fov;
191  float zNear, zFar;
194  bool flipHorz, flipVert;
195  };
196 
198  virtual ~StelProjector() {}
199 
201  // Methods which must be reimplemented by all instance of StelProjector
203  virtual QString getNameI18() const = 0;
205  virtual QString getDescriptionI18() const {return "No description";}
207  QString getHtmlSummary() const;
209  virtual float getMaxFov() const = 0;
216  virtual bool forward(Vec3f& v) const = 0;
218  virtual bool backward(Vec3d& v) const = 0;
220  virtual float deltaZoom(float fov) const = 0;
221 
225  bool intersectViewportDiscontinuity(const Vec3d& p1, const Vec3d& p2) const;
226  bool intersectViewportDiscontinuity(const SphericalCap& cap) const;
227 
229  virtual float fovToViewScalingFactor(float fov) const = 0;
231  virtual float viewScalingFactorToFov(float vsf) const = 0;
232 
236  bool getFlagGravityLabels() const;
237 
239  const Vec4i& getViewport() const;
240 
242  Vec2f getViewportCenter() const;
243 
245  int getViewportPosX() const;
247  int getViewportPosY() const;
249  int getViewportWidth() const;
251  int getViewportHeight() const;
252 
258  SphericalRegionP getViewportConvexPolygon(float marginX=0., float marginY=0.) const;
259 
261  const SphericalCap& getBoundingCap() const;
262 
264  float getPixelPerRadAtCenter() const;
265 
267  float getFov() const;
268 
270  bool flipFrontBackFace() const
271  {
272  // Moved to header for inlining
273  return (flipHorz*flipVert < 0.f);
274  }
275 
277  // Full projection methods
280  bool checkInViewport(const Vec3d& pos) const;
281 
284  bool checkInViewport(const Vec3f& pos) const;
285 
288  Vec3d viewPortIntersect(const Vec3d& p1, const Vec3d& p2) const;
289 
294  bool project(const Vec3d& v, Vec3d& win) const;
295 
300  bool project(const Vec3f& v, Vec3f& win) const;
301 
302  virtual void project(int n, const Vec3d* in, Vec3f* out);
303 
304  virtual void project(int n, const Vec3f* in, Vec3f* out);
305 
311  {
312  // No GLSL projection support by default.
313  return NULL;
314  }
315 
319  bool projectInPlace(Vec3d& vd) const;
320 
324  bool projectInPlace(Vec3f& v) const;
325 
330  bool projectCheck(const Vec3d& v, Vec3d& win) const;
331 
336  bool projectCheck(const Vec3f& v, Vec3f& win) const;
337 
342  bool unProject(const Vec3d& win, Vec3d& v) const;
343  bool unProject(double x, double y, Vec3d& v) const;
344 
351  bool projectLineCheck(const Vec3d& v1, Vec3d& win1, const Vec3d& v2, Vec3d& win2) const;
352 
355 
358  {
359  // Moved to header for inlining.
360  return Mat4f(2.f/viewportXywh[2], 0, 0, 0,
361  0, 2.f/viewportXywh[3], 0, 0,
362  0, 0, -1., 0.,
363  -(2.f*viewportXywh[0] + viewportXywh[2])/viewportXywh[2], -(2.f*viewportXywh[1] + viewportXywh[3])/viewportXywh[3], 0, 1);
364  }
365 
366 
369  static const QString maskTypeToString(StelProjectorMaskType type);
371  static StelProjectorMaskType stringToMaskType(const QString &s);
372 
375 
378  {
379  return viewportXywh;
380  }
381 
383  bool useGravityLabels() const
384  {
385  return gravityLabels;
386  }
387 
390  {
391  return defautAngleForGravityText;
392  }
393 
396  {
397  return viewportCenter;
398  }
399 
402  {
403  return viewportFovDiameter;
404  }
405 
406 protected:
408  StelProjector(ModelViewTranformP amodelViewTransform)
409  : modelViewTransform(amodelViewTransform)
410  , disableShaderProjection(false)
411  {;}
412 
414  virtual bool hasDiscontinuity() const =0;
418  virtual bool intersectViewportDiscontinuityInternal(const Vec3d& p1, const Vec3d& p2) const = 0;
419 
421  virtual bool intersectViewportDiscontinuityInternal(const Vec3d& capN, double capD) const = 0;
422 
424  virtual void computeBoundingCap();
425 
426  ModelViewTranformP modelViewTransform; // Operator to apply (if not NULL) before the modelview projection step
427 
428  float flipHorz,flipVert; // Whether to flip in horizontal or vertical directions
429  float pixelPerRad; // pixel per rad at the center of the viewport disk
430  StelProjectorMaskType maskType; // The current projector mask
431  float zNear, oneOverZNearMinusZFar; // Near and far clipping planes
432  Vec4i viewportXywh; // Viewport parameters
433  Vec2f viewportCenter; // Viewport center in screen pixel
434  float viewportFovDiameter; // diameter of the FOV disk in pixel
435  bool gravityLabels; // should label text align with the horizon?
436  float defautAngleForGravityText; // a rotation angle to apply to gravity text (only if gravityLabels is set to false)
437  SphericalCap boundingCap; // Bounding cap of the whole viewport
438 
443 
444 private:
446  void init(const StelProjectorParams& param);
447 };
448 
449 #endif // _STELPROJECTOR_HPP_