#include <cmath>
-#include "meshbuilder.h"
+#include <msp/gl/meshbuilder.h>
+#include <msp/gl/texture2d.h>
#include "oculusriftcombiner.h"
-#include "texture2d.h"
using namespace std;
const char fs_source[] =
"uniform sampler2D texture;\n"
"uniform vec4 distortion;\n"
+ "uniform vec4 chromatic;\n"
+ "uniform vec2 lens_center;\n"
"uniform vec2 eye_center;\n"
"uniform vec3 scale;\n"
"varying vec2 texcoord;\n"
- "vec2 distort(vec2 coord)\n"
- "{\n"
- " float r_sq = dot(coord, coord);\n"
- " return coord*dot(distortion, vec4(1.0, r_sq, r_sq*r_sq, r_sq*r_sq*r_sq));\n"
- "}\n"
"void main()\n"
"{\n"
- " vec2 dtc = (distort(texcoord)-eye_center)/(scale.xy*scale.z)+0.5;\n"
+ " float r_sq = dot(texcoord, texcoord);\n"
+ " float f = dot(distortion, vec4(1.0, r_sq, r_sq*r_sq, r_sq*r_sq*r_sq));\n"
+ " vec2 dtc = (texcoord*f-eye_center)/(scale.xy*scale.z)+lens_center;\n"
" if(dtc.x<0.0 || dtc.y<0.0 || dtc.x>1.0 || dtc.y>1.0)\n"
" gl_FragColor = vec4(0.0);\n"
" else\n"
- " gl_FragColor = texture2D(texture, dtc);\n"
+ " {\n"
+ " vec2 red_dtc = (texcoord*f*(chromatic[0]+chromatic[1]*r_sq)-eye_center)/(scale.xy*scale.z)+lens_center;\n"
+ " vec2 blue_dtc = (texcoord*f*(chromatic[2]+chromatic[3]*r_sq)-eye_center)/(scale.xy*scale.z)+lens_center;\n"
+ " gl_FragColor = vec4(texture2D(texture, red_dtc).r, texture2D(texture, dtc).g, texture2D(texture, blue_dtc).b, 1.0);\n"
+ " }\n"
"}\n";
}
namespace Msp {
-namespace GL {
+namespace VR {
OculusRiftCombiner::OculusRiftCombiner():
- mesh(VERTEX2),
+ mesh(GL::VERTEX2),
shprog(vs_source, fs_source),
// Default values copied from the SDK
view_distance(0.438f),
right_shdata.uniform("texture", 0);
right_shdata.uniform("offset", 0.5f);
+ chromatic[0] = 1.0f;
+ chromatic[1] = 0.0f;
+ chromatic[2] = 1.0f;
+ chromatic[3] = 0.0f;
+
// This will also call update_parameters
set_distortion(1.0f, 0.22f, 0.24f);
- MeshBuilder bld(mesh);
- bld.begin(TRIANGLE_STRIP);
+ GL::MeshBuilder bld(mesh);
+ bld.begin(GL::TRIANGLE_STRIP);
bld.vertex(-1, 1);
bld.vertex(-1, -1);
bld.vertex(1, 1);
update_parameters();
}
+void OculusRiftCombiner::set_red_aberration(float c0, float c1)
+{
+ chromatic[0] = c0;
+ chromatic[1] = c1;
+
+ update_parameters();
+}
+
+void OculusRiftCombiner::set_blue_aberration(float c2, float c3)
+{
+ chromatic[2] = c2;
+ chromatic[3] = c3;
+
+ update_parameters();
+}
+
void OculusRiftCombiner::set_fill_factor(float f)
{
fill_factor = f;
void OculusRiftCombiner::update_parameters()
{
left_shdata.uniform4("distortion", distortion);
+ left_shdata.uniform4("chromatic", chromatic);
right_shdata.uniform4("distortion", distortion);
+ right_shdata.uniform4("chromatic", chromatic);
// Set lens center positions, in output texture coordinates
left_shdata.uniform("lens_center", 1.0f-lens_separation, 0.5);
/* Determine the necessary scaling factor to avoid quality degradation in
the center of the screen. */
- float horiz_oversize = distort((fill_factor-lens_separation)*2)-eye_offset;
+ float horiz_oversize = distort((fill_factor-lens_separation)*2)/((fill_factor-lens_separation)*2)-eye_offset;
float vert_oversize = distort(1.25f*fill_factor)/(1.25f*fill_factor);
oversize = min(horiz_oversize, vert_oversize);
right_shdata.uniform("scale", 2.0f, 2.5f, oversize);
fov = Geometry::atan(oversize*0.625f/view_distance)*2.0f;
+ frustum_skew = lens_separation*2-1;
}
float OculusRiftCombiner::distort(float r) const
}
}
-void OculusRiftCombiner::render(const Texture2D &left, const Texture2D &right) const
+void OculusRiftCombiner::render(const GL::Texture2D &left, const GL::Texture2D &right) const
{
- Bind bind_shprog(shprog);
+ GL::Bind bind_shprog(shprog);
- Bind bind_tex(left);
+ GL::Bind bind_tex(left);
left_shdata.apply();
mesh.draw();
mesh.draw();
}
-} // namespace GL
+} // namespace VR
} // namespace Msp