Renderer.cc 3.77 KB
Newer Older
1
2
3
4

#include "Renderer.hh"


Jan Möbius's avatar
Jan Möbius committed
5
6
7
#include <OpenFlipper/common/GlobalOptions.hh>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <QGLFormat>
8
9
10
11
12
13
14
15
16
17
18
19


// =================================================

Renderer::Renderer()
{
}


Renderer::~Renderer()
{
}
Jan Möbius's avatar
Jan Möbius committed
20
21


22
23
24
25
26
27
28
29
30
void Renderer::initializePlugin()
{
  ACG::ShaderProgGenerator::setShaderDir(OpenFlipper::Options::shaderDirStr());
}



void Renderer::render(ACG::GLState* _glState, Viewer::ViewerProperties& _properties)
{
Jan Möbius's avatar
Jan Möbius committed
31
32
  // collect renderobjects + prepare OpenGL state
  prepareRenderingPipeline(_glState, _properties.drawMode(), PluginFunctions::getSceneGraphRootNode());
33

34
  // render every object
35

36
37
38
39
40
41
42
43
44
  if (!_properties.stereo())
  {
    for (int i = 0; i < getNumRenderObjects(); ++i)
      renderObject(sortedObjects_[i]);
  }
  else
  {
    ACG::GLMatrixd projLeft, projRight;
    computeProjStereo(_glState, _properties, &projLeft, &projRight);
45

46
    // draw red channel for left eye
47

48
49
50
51
52
53
54
55
56
57
58
59
    for (int i = 0; i < getNumRenderObjects(); ++i)
    {
      ACG::GLMatrixd prev = sortedObjects_[i]->proj;

      sortedObjects_[i]->proj = projLeft;
      sortedObjects_[i]->glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
      renderObject(sortedObjects_[i]);
     
      sortedObjects_[i]->proj = prev;
    }

    // draw green and blue channel for right eye
60

61
62
63
64
65
66
67
68
69
    glClear(GL_DEPTH_BUFFER_BIT);

    for (int i = 0; i < getNumRenderObjects(); ++i)
    {
      sortedObjects_[i]->proj = projRight;
      sortedObjects_[i]->glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
      renderObject(sortedObjects_[i]);
    }
  }
70
71


Jan Möbius's avatar
Jan Möbius committed
72
73
74
  // restore common opengl state
  // log window remains hidden otherwise
  finishRenderingPipeline();
75

76
//  dumpRenderObjectsToFile("../../dump_ro.txt", &sortedObjects_[0]);
77
78
79
80
81
}


QString Renderer::checkOpenGL()
{
Jan Möbius's avatar
Jan Möbius committed
82
83
  // Get version and check
  QGLFormat::OpenGLVersionFlags flags = QGLFormat::openGLVersionFlags();
84
85
86
87
88
89
90
91
92
93
94
95
96
  if ( !flags.testFlag(QGLFormat::OpenGL_Version_3_2) )
    return QString("Insufficient OpenGL Version! OpenGL 3.2 or higher required");

  // Check extensions
  QString glExtensions = QString((const char*)glGetString(GL_EXTENSIONS));
  QString missing("");
  if ( !glExtensions.contains("GL_ARB_vertex_buffer_object") )
    missing += "GL_ARB_vertex_buffer_object extension missing\n";

#ifndef __APPLE__
  if ( !glExtensions.contains("GL_ARB_vertex_program") )
    missing += "GL_ARB_vertex_program extension missing\n";
#endif
97

98
  return missing;
99
100
}

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
void Renderer::computeProjStereo( ACG::GLState* _glState, Viewer::ViewerProperties& _properties, ACG::GLMatrixd* _outLeft, ACG::GLMatrixd* _outRight )
{
  double l, r, t, b, w, h, a, radians, wd2, ndfl, zerop, xrange;

  w = _glState->viewport_width();
  h = _glState->viewport_height();
  a = w / h;

  double fovy = OpenFlipperSettings().value("Core/Projection/FOVY", 45.0).toDouble();
  radians = fovy * 0.5 / 180.0 * M_PI;
  wd2     = _properties.nearPlane() * tan(radians);
  zerop   = _properties.nearPlane() + ((_properties.farPlane() - _properties.nearPlane()) * OpenFlipperSettings().value("Core/Stereo/FocalDistance", 0.5).toDouble() );
  ndfl    = _properties.nearPlane() / zerop ;
  xrange  = a * wd2 * 2 * zerop / _properties.nearPlane();

  l = -a*wd2;
  r =  a*wd2;
  t =  wd2;
  b = -wd2;

  double offset  = 0.5 * OpenFlipperSettings().value("Core/Stereo/EyeDistance", 0.07).toDouble() * xrange;
  double offset2 = offset * ndfl;

  if (_outLeft)
  {
    _outLeft->identity();
    _outLeft->frustum(l+offset2, r+offset2, b, t, _properties.nearPlane(), _properties.farPlane());
    _outLeft->translate(offset, 0.0, 0.0);
  }

  if (_outRight)
  {
    _outRight->identity();
    _outRight->frustum(l-offset2, r-offset2, b, t, _properties.nearPlane(), _properties.farPlane());
    _outRight->translate(-offset, 0.0, 0.0);
  }
}

Matthias Möller's avatar
Matthias Möller committed
139
140
141
142
#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( shaderrenderer , Renderer );
#endif

143