Line data Source code
1 :
2 : /* Copyright (c) 2011-2013, Stefan Eilemann <eile@eyescale.ch>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #include "renderer.h"
19 :
20 : #include "application.h"
21 : #include "viewData.h"
22 : #include "detail/renderer.h"
23 :
24 : namespace seq
25 : {
26 2 : Renderer::Renderer( Application& application )
27 2 : : _impl( new detail::Renderer )
28 4 : , app_( application )
29 : {
30 2 : }
31 :
32 4 : Renderer::~Renderer()
33 : {
34 2 : delete _impl;
35 2 : }
36 :
37 4 : co::Object* Renderer::getFrameData()
38 : {
39 4 : return _impl->getFrameData();
40 : }
41 :
42 0 : const GLEWContext* Renderer::glewGetContext() const
43 : {
44 0 : return _impl->glewGetContext();
45 : }
46 :
47 1 : ViewData* Renderer::createViewData()
48 : {
49 1 : return new ViewData;
50 : }
51 :
52 1 : void Renderer::destroyViewData( ViewData* viewData )
53 : {
54 1 : delete viewData;
55 1 : }
56 :
57 0 : const Frustumf& Renderer::getFrustum() const
58 : {
59 0 : return _impl->getFrustum();
60 : }
61 :
62 0 : const Matrix4f& Renderer::getViewMatrix() const
63 : {
64 0 : return _impl->getViewMatrix();
65 : }
66 :
67 0 : const Matrix4f& Renderer::getModelMatrix() const
68 : {
69 0 : return _impl->getModelMatrix();
70 : }
71 :
72 2 : bool Renderer::initContext( co::Object* /*initData*/ )
73 : {
74 2 : return _impl->initContext();
75 : }
76 :
77 2 : bool Renderer::exitContext()
78 : {
79 2 : return _impl->exitContext();
80 : }
81 :
82 3 : void Renderer::clear( co::Object* /*frameData*/ )
83 : {
84 3 : _impl->clear();
85 3 : }
86 :
87 0 : void Renderer::updateNearFar( const Vector4f& boundingSphere )
88 : {
89 0 : const Matrix4f& view = getViewMatrix();
90 0 : Matrix4f viewInv;
91 0 : compute_inverse( view, viewInv );
92 :
93 0 : const Vector3f& zero = viewInv * Vector3f::ZERO;
94 0 : Vector3f front = viewInv * Vector3f( 0.0f, 0.0f, -1.0f );
95 0 : front -= zero;
96 0 : front.normalize();
97 0 : front *= boundingSphere.w();
98 :
99 0 : const Vector3f& translation = getModelMatrix().get_translation();
100 0 : const Vector3f& center = translation - boundingSphere.get_sub_vector< 3 >();
101 0 : const Vector3f& nearPoint = view * ( center - front );
102 0 : const Vector3f& farPoint = view * ( center + front );
103 :
104 0 : if( _impl->useOrtho( ))
105 : {
106 0 : LBASSERTINFO( fabs( farPoint.z() - nearPoint.z() ) >
107 : std::numeric_limits< float >::epsilon(),
108 : nearPoint << " == " << farPoint );
109 0 : setNearFar( -nearPoint.z(), -farPoint.z() );
110 : }
111 : else
112 : {
113 : // estimate minimal value of near plane based on frustum size
114 0 : const eq::Frustumf& frustum = _impl->getFrustum();
115 0 : const float width = fabs( frustum.right() - frustum.left() );
116 0 : const float height = fabs( frustum.top() - frustum.bottom() );
117 0 : const float size = LB_MIN( width, height );
118 0 : const float minNear = frustum.near_plane() / size * .001f;
119 :
120 0 : const float zNear = LB_MAX( minNear, -nearPoint.z() );
121 0 : const float zFar = LB_MAX( zNear * 2.f, -farPoint.z() );
122 :
123 0 : setNearFar( zNear, zFar );
124 : }
125 0 : }
126 :
127 0 : void Renderer::setNearFar( const float nearPlane, const float farPlane )
128 : {
129 0 : _impl->setNearFar( nearPlane, farPlane );
130 0 : }
131 :
132 0 : void Renderer::applyRenderContext()
133 : {
134 0 : _impl->applyRenderContext();
135 0 : }
136 :
137 0 : const RenderContext& Renderer::getRenderContext() const
138 : {
139 0 : return _impl->getRenderContext();
140 : }
141 :
142 0 : void Renderer::applyModelMatrix()
143 : {
144 0 : _impl->applyModelMatrix();
145 0 : }
146 :
147 0 : co::Object* Renderer::createObject( const uint32_t type )
148 : {
149 0 : return app_.createObject( type );
150 : }
151 :
152 0 : void Renderer::destroyObject( co::Object* object, const uint32_t type )
153 : {
154 0 : app_.destroyObject( object, type );
155 0 : }
156 3 : }
|