20 #ifndef _STELPROJECTIONS_HPP_
21 #define _STELPROJECTIONS_HPP_
24 #include "StelProjector.hpp"
26 #include "renderer/StelGLSLShader.hpp"
37 const float r = std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
50 v[0] = std::numeric_limits<float>::max();
51 v[1] = std::numeric_limits<float>::max();
74 const float r = std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
75 const float f = std::sqrt(2.f/(r*(r-v[2])));
93 friend class StereographicGLSLProjectorShader;
108 if(projector.disableShaderProjection)
117 if(!projector.modelViewTransform->setupGLSLTransform(shader))
122 Q_ASSERT_X(
false, Q_FUNC_INFO,
123 "Failed to restore shader after failing to add a modelview "
124 "transform shader to it");
132 static const QString projectorSource(
133 "vec4 modelViewForward(in vec4 v);\n"
135 "uniform vec2 viewportCenter;\n"
136 "uniform float flipHorz;\n"
137 "uniform float flipVert;\n"
138 "uniform float pixelPerRad;\n"
139 "uniform float zNear;\n"
140 "uniform float oneOverZNearMinusZFar;\n"
142 "vec4 projectorForward(in vec4 v)\n"
144 " float r = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);\n"
145 " float h = 0.5 * (r - v.z);\n"
148 " return vec4(1000000.0, 1000000.0, -1000000.0, 1.0);\n"
150 " float f = 1.0 / h;\n"
151 " return vec4(v.x * f, v.y * f, r, 1.0);\n"
154 "vec4 project(in vec4 posIn)\n"
157 " v = modelViewForward(v);\n"
158 " v = projectorForward(v);\n"
159 " return vec4(viewportCenter.x + flipHorz * pixelPerRad * v.x,\n"
160 " viewportCenter.y + flipVert * pixelPerRad * v.y,\n"
161 " (v.z - zNear) * oneOverZNearMinusZFar, 1.0);\n"
164 if(!shader->
addVertexShader(
"StereographicProjector", projectorSource))
169 Q_ASSERT_X(
false, Q_FUNC_INFO,
170 "Failed to restore shader after failing to add a stereographic "
171 "projector shader to it");
173 projector.disableShaderProjection =
true;
176 qDebug() <<
"Build log after adding a stereographic projection shader: "
184 qDebug() <<
"Failed to build with a stereographic projector shader: "
186 projector.disableShaderProjection =
true;
191 Q_ASSERT_X(
false, Q_FUNC_INFO,
192 "Failed to restore default projector shader after failing to link "
193 "with a (succesfully compiled) stereographic projector shader");
202 projector.modelViewTransform->setGLSLUniforms(shader);
209 shader->
setUniformValue(
"oneOverZNearMinusZFar", projector.oneOverZNearMinusZFar);
215 projector.modelViewTransform->disableGLSLTransform(shader);
220 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Failed to disable projector shader");
227 : projector(projector)
235 , glslProjector(*
this)
242 const float r = std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
243 const float h = 0.5f*(r-v[2]);
245 v[0] = std::numeric_limits<float>::max();
246 v[1] = std::numeric_limits<float>::max();
247 v[2] = -std::numeric_limits<float>::min();
250 const float f = 1.f / h;
257 virtual void project(
int n,
const Vec3d* in,
Vec3f* out)
260 for (
int i = 0; i < n; ++i, ++out)
263 modelViewTransform->forward(v);
264 out->set(v[0], v[1], v[2]);
266 out->set(viewportCenter[0] + flipHorz * pixelPerRad * (*out)[0],
267 viewportCenter[1] + flipVert * pixelPerRad * (*out)[1],
268 ((*out)[2] - zNear) * oneOverZNearMinusZFar);
274 return &glslProjector;
288 StereographicGLSLProjector glslProjector;
300 const float rq1 = v[0]*v[0] + v[1]*v[1];
302 const float h = std::sqrt(rq1);
303 const float f = std::atan2(h,-v[2]) / h;
306 v[2] = std::sqrt(rq1 + v[2]*v[2]);
315 v[0] = std::numeric_limits<float>::max();
316 v[1] = std::numeric_limits<float>::max();
317 v[2] = std::numeric_limits<float>::min();
337 virtual void project(
int n,
const Vec3d* in,
Vec3f* out)
340 for (
int i = 0; i < n; ++i)
343 modelViewTransform->forward(v);
344 out[i].set(v[0], v[1], v[2]);
346 out[i][0] = viewportCenter[0] + flipHorz * pixelPerRad * out[i][0];
347 out[i][1] = viewportCenter[1] + flipVert * pixelPerRad * out[i][1];
348 out[i][2] = (out[i][2] - zNear) * oneOverZNearMinusZFar;
354 const float r = std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
355 const float alpha = std::atan2(v[0],-v[2]);
356 const float cosDelta = std::sqrt(1.f-v[1]*v[1]/(r*r));
357 float z = std::sqrt(1.+cosDelta*std::cos(alpha/2.f));
358 v[0] = 2.f*M_SQRT2*cosDelta*std::sin(alpha/2.f)/z;
359 v[1] = M_SQRT2*v[1]/r/z;
376 return cap.intersects(cap1) && cap.intersects(cap2) && cap.intersects(cap2);
386 virtual float getMaxFov()
const {
return 175.f * 4.f/3.f;}
396 return p1[0]*p2[0]<0 && !(p1[2]<0 && p2[2]<0);
404 return cap.intersects(cap1) && cap.intersects(cap2) && cap.intersects(cap2);
414 virtual float getMaxFov()
const {
return 175.f * 4.f/3.f;}
424 return p1[0]*p2[0]<0 && !(p1[2]<0 && p2[2]<0);
432 return cap.intersects(cap1) && cap.intersects(cap2) && cap.intersects(cap2);
473 #endif // _STELPROJECTIONS_HPP_