/
pivot.h
263 lines (216 loc) · 8.79 KB
/
pivot.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include "math/Matrix4.h"
#include "irenderable.h"
#include "render.h"
inline void billboard_viewplaneOriented(Matrix4& rotation, const Matrix4& world2screen)
{
#if 1
rotation = Matrix4::getIdentity();
Vector3 x(world2screen.x().getVector3().getNormalised());
Vector3 y(world2screen.y().getVector3().getNormalised());
Vector3 z(world2screen.z().getVector3().getNormalised());
rotation.y().getVector3() = Vector3(x.y(), y.y(), z.y());
rotation.z().getVector3() = -Vector3(x.z(), y.z(), z.z());
rotation.x().getVector3() = rotation.y().getVector3().crossProduct(rotation.z().getVector3()).getNormalised();
rotation.y().getVector3() = rotation.z().getVector3().crossProduct(rotation.x().getVector3());
#else
Matrix4 screen2world(matrix4_full_inverse(world2screen));
Vector3 near_(
matrix4_transformed_vector4(
screen2world,
Vector4(0, 0, -1, 1)
).getProjected()
);
Vector3 far_(
matrix4_transformed_vector4(
screen2world,
Vector4(0, 0, 1, 1)
).getProjected()
);
Vector3 up(
matrix4_transformed_vector4(
screen2world,
Vector4(0, 1, -1, 1)
).getProjected()
);
rotation = Matrix4::getIdentity();
rotation.y().getVector3() = (up - near_).getNormalised();
rotation.z().getVector3() = (near_ - far_).getNormalised();
rotation.x().getVector3() = rotation.y().getVector3().crossProduct(rotation.z().getVector3()).getNormalised();
rotation.y().getVector3() = rotation.z().getVector3().crossProduct(rotation.x().getVector3());
#endif
}
inline void billboard_viewpointOriented(Matrix4& rotation, const Matrix4& world2screen)
{
Matrix4 screen2world(world2screen.getFullInverse());
#if 1
rotation = Matrix4::getIdentity();
rotation.y().getVector3() = screen2world.y().getVector3().getNormalised();
rotation.z().getVector3() = -screen2world.z().getVector3().getNormalised();
rotation.x().getVector3() = rotation.y().getVector3().crossProduct(rotation.z().getVector3()).getNormalised();
rotation.y().getVector3() = rotation.z().getVector3().crossProduct(rotation.x().getVector3());
#else
Vector3 near_(
matrix4_transformed_vector4(
screen2world,
Vector4(world2screen[12] / world2screen[15], world2screen[13] / world2screen[15], -1, 1)
).getProjected()
);
Vector3 far_(
matrix4_transformed_vector4(
screen2world,
Vector4(world2screen[12] / world2screen[15], world2screen[13] / world2screen[15], 1, 1)
).getProjected()
);
Vector3 up(
matrix4_transformed_vector4(
screen2world,
Vector4(world2screen[12] / world2screen[15], world2screen[13] / world2screen[15] + 1, -1, 1)
).getProjected()
);
rotation = Matrix4::getIdentity();
rotation.y().getVector3() = (up - near_).getNormalised();
rotation.z().getVector3() = (near_ - far_).getNormalised();
rotation.x().getVector3() = rotation.y().getVector3().crossProduct(rotation.z().getVector3()).getNormalised();
rotation.y().getVector3() = rotation.z().getVector3().crossProduct(rotation.x().getVector3());
#endif
}
/**
* greebo: Returns a single matrix which transforms object coordinates to screen coordinates,
* which is basically the same pipeline a vertex is sent through by openGL.
* The 4 input matrices are just concatenated by multiplication, like this:
* object2screen = device2screen * view2device * world2view * object2world
*/
inline Matrix4 constructObject2Screen(const Matrix4& object2world, const Matrix4& world2view, const Matrix4& view2device, const Matrix4& device2screen)
{
return object2world.getPremultipliedBy(world2view).getPremultipliedBy(view2device).getPremultipliedBy(device2screen);
}
/**
* greebo: Returns a matrix that transforming object coordinates to device coordinates [-1..+1].
* The three input matrices are concatenated like this:
* object2device = view2device * world2view * object2world
*/
inline Matrix4 constructObject2Device(const Matrix4& object2world, const Matrix4& world2view, const Matrix4& view2device)
{
return object2world.getPremultipliedBy(world2view).getPremultipliedBy(view2device);
}
inline void ConstructDevice2Object(Matrix4& device2object, const Matrix4& object2world, const Matrix4& world2view, const Matrix4& view2device)
{
device2object = constructObject2Device(object2world, world2view, view2device);
device2object.invertFull();
}
//! S = ( Inverse(Object2Screen *post ScaleOf(Object2Screen) ) *post Object2Screen
inline void pivot_scale(Matrix4& scale, const Matrix4& pivot2screen)
{
Matrix4 pre_scale(Matrix4::getIdentity());
pre_scale[0] = static_cast<float>(pivot2screen.x().getVector3().getLength());
pre_scale[5] = static_cast<float>(pivot2screen.y().getVector3().getLength());
pre_scale[10] = static_cast<float>(pivot2screen.z().getVector3().getLength());
scale = pivot2screen;
scale.multiplyBy(pre_scale);
scale.invertFull();
scale.multiplyBy(pivot2screen);
}
// scale by (inverse) W
inline void pivot_perspective(Matrix4& scale, const Matrix4& pivot2screen)
{
scale = Matrix4::getIdentity();
scale[0] = scale[5] = scale[10] = pivot2screen[15];
}
inline void ConstructDevice2Manip(Matrix4& device2manip, const Matrix4& object2world, const Matrix4& world2view, const Matrix4& view2device, const Matrix4& device2screen)
{
Matrix4 pivot2screen = constructObject2Screen(object2world, world2view, view2device, device2screen);
device2manip = constructObject2Device(object2world, world2view, view2device);
Matrix4 scale;
pivot_scale(scale, pivot2screen);
device2manip.multiplyBy(scale);
pivot_perspective(scale, pivot2screen);
device2manip.multiplyBy(scale);
device2manip.invertFull();
}
const Colour4b g_colour_x(255, 0, 0, 255);
const Colour4b g_colour_y(0, 255, 0, 255);
const Colour4b g_colour_z(0, 0, 255, 255);
class RenderablePivot :
public OpenGLRenderable
{
private:
std::vector<VertexCb> _vertices;
const Vector3& _pivot;
ShaderPtr _shader;
public:
mutable Matrix4 m_localToWorld;
const ShaderPtr& getShader() const
{
return _shader;
}
RenderablePivot(const Vector3& pivot) :
_pivot(pivot)
{
_vertices.reserve(6);
_vertices.push_back(VertexCb(_pivot, g_colour_x));
_vertices.push_back(VertexCb(_pivot + Vector3(16,0,0), g_colour_x));
_vertices.push_back(VertexCb(_pivot, g_colour_y));
_vertices.push_back(VertexCb(_pivot + Vector3(0, 16, 0), g_colour_y));
_vertices.push_back(VertexCb(_pivot, g_colour_z));
_vertices.push_back(VertexCb(_pivot + Vector3(0, 0, 16), g_colour_z));
}
/** greebo: Updates the renderable vertex array to the given pivot point
*/
void updatePivot()
{
_vertices.clear();
_vertices.push_back(VertexCb(_pivot, g_colour_x));
_vertices.push_back(VertexCb(_pivot + Vector3(16,0,0), g_colour_x));
_vertices.push_back(VertexCb(_pivot, g_colour_y));
_vertices.push_back(VertexCb(_pivot + Vector3(0, 16, 0), g_colour_y));
_vertices.push_back(VertexCb(_pivot, g_colour_z));
_vertices.push_back(VertexCb(_pivot + Vector3(0, 0, 16), g_colour_z));
}
void setRenderSystem(const RenderSystemPtr& renderSystem)
{
if (renderSystem)
{
_shader = renderSystem->capture("$PIVOT");
}
else
{
_shader.reset();
}
}
void render(const RenderInfo& info) const
{
if (_vertices.empty()) return;
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_DOUBLE, sizeof(VertexCb), &_vertices.data()->vertex);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(VertexCb), &_vertices.data()->colour);
glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(_vertices.size()));
}
void render(RenderableCollector& collector, const VolumeTest& volume, const Matrix4& localToWorld) const
{
collector.PushState();
// greebo: Commented this out to avoid the point from being moved along with the view.
//Pivot2World_worldSpace(m_localToWorld, localToWorld, volume.GetModelview(), volume.GetProjection(), volume.GetViewport());
collector.setHighlightFlag(RenderableCollector::Highlight::Primitives, false);
collector.SetState(_shader, RenderableCollector::eWireframeOnly);
collector.SetState(_shader, RenderableCollector::eFullMaterials);
collector.addRenderable(*this, localToWorld);
collector.PopState();
}
};