2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_PIVOT_H )
23 #define INCLUDED_PIVOT_H
25 #include "math/matrix.h"
28 inline void billboard_viewplaneOriented( Matrix4& rotation, const Matrix4& world2screen ){
30 rotation = g_matrix4_identity;
31 Vector3 x( vector3_normalised( vector4_to_vector3( world2screen.x() ) ) );
32 Vector3 y( vector3_normalised( vector4_to_vector3( world2screen.y() ) ) );
33 Vector3 z( vector3_normalised( vector4_to_vector3( world2screen.z() ) ) );
34 vector4_to_vector3( rotation.y() ) = Vector3( x.y(), y.y(), z.y() );
35 vector4_to_vector3( rotation.z() ) = vector3_negated( Vector3( x.z(), y.z(), z.z() ) );
36 vector4_to_vector3( rotation.x() ) = vector3_normalised( vector3_cross( vector4_to_vector3( rotation.y() ), vector4_to_vector3( rotation.z() ) ) );
37 vector4_to_vector3( rotation.y() ) = vector3_cross( vector4_to_vector3( rotation.z() ), vector4_to_vector3( rotation.x() ) );
39 Matrix4 screen2world( matrix4_full_inverse( world2screen ) );
43 matrix4_transformed_vector4(
45 Vector4( 0, 0, -1, 1 )
52 matrix4_transformed_vector4(
61 matrix4_transformed_vector4(
63 Vector4( 0, 1, -1, 1 )
68 rotation = g_matrix4_identity;
69 vector4_to_vector3( rotation.y() ) = vector3_normalised( vector3_subtracted( up, near_ ) );
70 vector4_to_vector3( rotation.z() ) = vector3_normalised( vector3_subtracted( near_, far_ ) );
71 vector4_to_vector3( rotation.x() ) = vector3_normalised( vector3_cross( vector4_to_vector3( rotation.y() ), vector4_to_vector3( rotation.z() ) ) );
72 vector4_to_vector3( rotation.y() ) = vector3_cross( vector4_to_vector3( rotation.z() ), vector4_to_vector3( rotation.x() ) );
76 inline void billboard_viewpointOriented( Matrix4& rotation, const Matrix4& world2screen ){
77 Matrix4 screen2world( matrix4_full_inverse( world2screen ) );
80 rotation = g_matrix4_identity;
81 vector4_to_vector3( rotation.y() ) = vector3_normalised( vector4_to_vector3( screen2world.y() ) );
82 vector4_to_vector3( rotation.z() ) = vector3_negated( vector3_normalised( vector4_to_vector3( screen2world.z() ) ) );
83 vector4_to_vector3( rotation.x() ) = vector3_normalised( vector3_cross( vector4_to_vector3( rotation.y() ), vector4_to_vector3( rotation.z() ) ) );
84 vector4_to_vector3( rotation.y() ) = vector3_cross( vector4_to_vector3( rotation.z() ), vector4_to_vector3( rotation.x() ) );
88 matrix4_transformed_vector4(
90 Vector4( world2screen[12] / world2screen[15], world2screen[13] / world2screen[15], -1, 1 )
97 matrix4_transformed_vector4(
99 Vector4( world2screen[12] / world2screen[15], world2screen[13] / world2screen[15], 1, 1 )
106 matrix4_transformed_vector4(
108 Vector4( world2screen[12] / world2screen[15], world2screen[13] / world2screen[15] + 1, -1, 1 )
113 rotation = g_matrix4_identity;
114 vector4_to_vector3( rotation.y() ) = vector3_normalised( vector3_subtracted( up, near_ ) );
115 vector4_to_vector3( rotation.z() ) = vector3_normalised( vector3_subtracted( near_, far_ ) );
116 vector4_to_vector3( rotation.x() ) = vector3_normalised( vector3_cross( vector4_to_vector3( rotation.y() ), vector4_to_vector3( rotation.z() ) ) );
117 vector4_to_vector3( rotation.y() ) = vector3_cross( vector4_to_vector3( rotation.z() ), vector4_to_vector3( rotation.x() ) );
122 inline void ConstructObject2Screen( Matrix4& object2screen, const Matrix4& object2world, const Matrix4& world2view, const Matrix4& view2device, const Matrix4& device2screen ){
123 object2screen = device2screen;
124 matrix4_multiply_by_matrix4( object2screen, view2device );
125 matrix4_multiply_by_matrix4( object2screen, world2view );
126 matrix4_multiply_by_matrix4( object2screen, object2world );
129 inline void ConstructObject2Device( Matrix4& object2screen, const Matrix4& object2world, const Matrix4& world2view, const Matrix4& view2device ){
130 object2screen = view2device;
131 matrix4_multiply_by_matrix4( object2screen, world2view );
132 matrix4_multiply_by_matrix4( object2screen, object2world );
135 inline void ConstructDevice2Object( Matrix4& device2object, const Matrix4& object2world, const Matrix4& world2view, const Matrix4& view2device ){
136 ConstructObject2Device( device2object, object2world, world2view, view2device );
137 matrix4_full_invert( device2object );
140 //! S = ( Inverse(Object2Screen *post ScaleOf(Object2Screen) ) *post Object2Screen
141 inline void pivot_scale( Matrix4& scale, const Matrix4& pivot2screen ){
142 Matrix4 pre_scale( g_matrix4_identity );
143 pre_scale[0] = static_cast<float>( vector3_length( vector4_to_vector3( pivot2screen.x() ) ) );
144 pre_scale[5] = static_cast<float>( vector3_length( vector4_to_vector3( pivot2screen.y() ) ) );
145 pre_scale[10] = static_cast<float>( vector3_length( vector4_to_vector3( pivot2screen.z() ) ) );
147 scale = pivot2screen;
148 matrix4_multiply_by_matrix4( scale, pre_scale );
149 matrix4_full_invert( scale );
150 matrix4_multiply_by_matrix4( scale, pivot2screen );
153 // scale by (inverse) W
154 inline void pivot_perspective( Matrix4& scale, const Matrix4& pivot2screen ){
155 scale = g_matrix4_identity;
156 scale[0] = scale[5] = scale[10] = pivot2screen[15];
159 inline void ConstructDevice2Manip( Matrix4& device2manip, const Matrix4& object2world, const Matrix4& world2view, const Matrix4& view2device, const Matrix4& device2screen ){
160 Matrix4 pivot2screen;
161 ConstructObject2Screen( pivot2screen, object2world, world2view, view2device, device2screen );
163 ConstructObject2Device( device2manip, object2world, world2view, view2device );
166 pivot_scale( scale, pivot2screen );
167 matrix4_multiply_by_matrix4( device2manip, scale );
168 pivot_perspective( scale, pivot2screen );
169 matrix4_multiply_by_matrix4( device2manip, scale );
171 matrix4_full_invert( device2manip );
174 inline void Pivot2World_worldSpace( Matrix4& manip2world, const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection, const Matrix4& viewport ){
175 manip2world = pivot2world;
177 Matrix4 pivot2screen;
178 ConstructObject2Screen( pivot2screen, pivot2world, modelview, projection, viewport );
181 pivot_scale( scale, pivot2screen );
182 matrix4_multiply_by_matrix4( manip2world, scale );
183 pivot_perspective( scale, pivot2screen );
184 matrix4_multiply_by_matrix4( manip2world, scale );
187 inline void Pivot2World_viewpointSpace( Matrix4& manip2world, Vector3& axis, const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection, const Matrix4& viewport ){
188 manip2world = pivot2world;
190 Matrix4 pivot2screen;
191 ConstructObject2Screen( pivot2screen, pivot2world, modelview, projection, viewport );
194 pivot_scale( scale, pivot2screen );
195 matrix4_multiply_by_matrix4( manip2world, scale );
197 billboard_viewpointOriented( scale, pivot2screen );
198 axis = vector4_to_vector3( scale.z() );
199 matrix4_multiply_by_matrix4( manip2world, scale );
201 pivot_perspective( scale, pivot2screen );
202 matrix4_multiply_by_matrix4( manip2world, scale );
205 inline void Pivot2World_viewplaneSpace( Matrix4& manip2world, const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection, const Matrix4& viewport ){
206 manip2world = pivot2world;
208 Matrix4 pivot2screen;
209 ConstructObject2Screen( pivot2screen, pivot2world, modelview, projection, viewport );
212 pivot_scale( scale, pivot2screen );
213 matrix4_multiply_by_matrix4( manip2world, scale );
215 billboard_viewplaneOriented( scale, pivot2screen );
216 matrix4_multiply_by_matrix4( manip2world, scale );
218 pivot_perspective( scale, pivot2screen );
219 matrix4_multiply_by_matrix4( manip2world, scale );
223 #include "renderable.h"
224 #include "cullable.h"
227 const Colour4b g_colour_x( 255, 0, 0, 255 );
228 const Colour4b g_colour_y( 0, 255, 0, 255 );
229 const Colour4b g_colour_z( 0, 0, 255, 255 );
233 class RenderablePivot : public OpenGLRenderable
235 VertexBuffer<PointVertex> m_vertices;
237 mutable Matrix4 m_localToWorld;
238 typedef Static<Shader*> StaticShader;
239 static Shader* getShader(){
240 return StaticShader::instance();
244 m_vertices.reserve( 6 );
246 m_vertices.push_back( PointVertex( Vertex3f( 0, 0, 0 ), g_colour_x ) );
247 m_vertices.push_back( PointVertex( Vertex3f( 16, 0, 0 ), g_colour_x ) );
249 m_vertices.push_back( PointVertex( Vertex3f( 0, 0, 0 ), g_colour_y ) );
250 m_vertices.push_back( PointVertex( Vertex3f( 0, 16, 0 ), g_colour_y ) );
252 m_vertices.push_back( PointVertex( Vertex3f( 0, 0, 0 ), g_colour_z ) );
253 m_vertices.push_back( PointVertex( Vertex3f( 0, 0, 16 ), g_colour_z ) );
256 void render( RenderStateFlags state ) const {
257 if ( m_vertices.size() == 0 ) {
260 if ( m_vertices.data() == 0 ) {
263 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
264 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
265 glDrawArrays( GL_LINES, 0, m_vertices.size() );
268 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
269 renderer.PushState();
271 Pivot2World_worldSpace( m_localToWorld, localToWorld, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
273 renderer.Highlight( Renderer::ePrimitive, false );
274 renderer.SetState( getShader(), Renderer::eWireframeOnly );
275 renderer.SetState( getShader(), Renderer::eFullMaterials );
276 renderer.addRenderable( *this, m_localToWorld );