Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfUniverseRenderer.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
45#pragma once
46
47#include <vector>
48
49#include <Magnum/GL/Buffer.h>
50#include <Magnum/GL/Mesh.h>
51
52#include <tfUniverse.h>
53#include <tfSimulator.h>
54#include "tfRenderer.h"
55#include "tfGlfwWindow.h"
56#include <shaders/tfParticleSphereShader.h>
57
58#include <shaders/tfPhong.h>
59
60#include <Corrade/Containers/Pointer.h>
61
62#include <Corrade/Containers/Pointer.h>
63#include <Magnum/Math/Color.h>
64#include <Magnum/Primitives/Cube.h>
65#include <Magnum/Platform/GlfwApplication.h>
66#include <Magnum/SceneGraph/MatrixTransformation3D.h>
67#include <Magnum/Timeline.h>
68
69#include <Magnum/Shaders/Phong.h>
70#include <Magnum/Shaders/Flat.h>
71
72#include "tfGlfwWindow.h"
73
74#include <Magnum/Platform/GlfwApplication.h>
75
76#include "tfWindow.h"
77#include "tfArcBallCamera.h"
78
79#include "tfSubRenderer.h"
80
81
82namespace TissueForge {
83
84
85 struct Simulator;
86
87
88 namespace rendering {
89
90
91 class WireframeGrid;
92 class WireframeBox;
93
95 Magnum::Matrix4 transformationMatrix;
96 Magnum::Matrix3x3 normalMatrix;
97 Magnum::Color4 color;
98 };
99
101 Magnum::Vector3 position;
102 Magnum::Color4 color;
103 };
104
105
106 typedef enum SubRendererFlag {
107 SUBRENDERER_ANGLE = 1 << 0,
108 SUBRENDERER_ARROW = 1 << 1,
109 SUBRENDERER_BOND = 1 << 2,
110 SUBRENDERER_DIHEDRAL = 1 << 3,
111 SUBRENDERER_ORIENTATION = 1 << 4
112 } SubRendererFlag;
113
114
116
117
118 // TODO, implement the event system instead of hard coding window events.
119 UniverseRenderer(const Simulator::Config &conf, Window *win);
120
121 template<typename T>
122 UniverseRenderer& draw(T& camera, const iVector2& viewportSize);
123
124 bool& isDirty() { return _dirty; }
125
126 UniverseRenderer& setDirty() {
127 _dirty = true;
128 return *this;
129 }
130
131 shaders::ParticleSphereShader::ColorMode& colorMode() { return _colorMode; }
132
133 UniverseRenderer& setColorMode(shaders::ParticleSphereShader::ColorMode colorMode) {
134 _colorMode = colorMode;
135 return *this;
136 }
137
138 const Float lineWidth();
139
140 UniverseRenderer& setLineWidth(const Float &lw);
141
142 const Float lineWidthMin();
143
144 const Float lineWidthMax();
145
146 Color3& ambientColor() { return _ambientColor; }
147
148 UniverseRenderer& setAmbientColor(const Color3& color);
149
150 Color3& diffuseColor() { return _diffuseColor; }
151
152 UniverseRenderer& setDiffuseColor(const Color3& color);
153
154 Color3& specularColor() { return _specularColor; }
155
156 UniverseRenderer& setSpecularColor(const Color3& color);
157
158 Float& shininess() { return _shininess; }
159
160 UniverseRenderer& setShininess(float shininess);
161
162 Color3& gridColor() { return _gridColor; }
163
164 UniverseRenderer& setGridColor(const Color3 &color) {
165 _gridColor = color;
166 return *this;
167 }
168
169 Color3& sceneBoxColor() { return _sceneBoxColor; }
170
171 UniverseRenderer& setSceneBoxColor(const Color3 &color) {
172 _sceneBoxColor = color;
173 return *this;
174 }
175
176 fVector3& lightDirection() { return _lightDir; }
177
178 UniverseRenderer& setLightDirection(const fVector3& lightDir);
179
180 Color3& lightColor() { return _lightColor; }
181
182 UniverseRenderer& setLightColor(const Color3 &color);
183
184 Color3& backgroundColor() { return _clearColor; }
185
186 UniverseRenderer& setBackgroundColor(const Color3 &color);
187
188 UniverseRenderer& setModelViewTransform(const Magnum::Matrix4& mat) {
189 modelViewMat = mat;
190 return *this;
191 }
192
193 UniverseRenderer& setProjectionTransform(const Magnum::Matrix4& mat) {
194 projMat = mat;
195 return *this;
196 }
197
198 const bool showingDiscretizationGrid() const {
199 return _showDiscretizationGrid;
200 }
201
202 UniverseRenderer& showDiscretizationGrid(const bool &show) {
203 _showDiscretizationGrid = show;
204 return *this;
205 }
206
207 Color3& discretizationGridColor() {
208 return _discretizationGridColor;
209 }
210
211 UniverseRenderer& setDiscretizationGridColor(const Color3 &color);
212
213 const fVector3& defaultEye() const {
214 return _eye;
215 }
216
217 const fVector3& defaultCenter() const {
218 return _center;
219 }
220
221 const fVector3& defaultUp() const {
222 return _up;
223 }
224
225 bool renderUniverse = true;
226
227
228 void onCursorMove(double xpos, double ypos);
229
230 void onCursorEnter(int entered);
231
232 void onMouseButton(int button, int action, int mods);
233
234 void onRedraw();
235
236 void onWindowMove(int x, int y);
237
238 void onWindowSizeChange(int x, int y);
239
240 void onFramebufferSizeChange( int x, int y);
241
242 void viewportEvent(const int w, const int h);
243
244 void draw();
245
246 int clipPlaneCount() const;
247
248 static int maxClipPlaneCount();
249
250 const unsigned addClipPlaneEquation(const Magnum::Vector4& pe);
251
252 const unsigned removeClipPlaneEquation(const unsigned int &id);
253
254 void setClipPlaneEquation(unsigned id, const Magnum::Vector4& pe);
255
256 const Magnum::Vector4& getClipPlaneEquation(unsigned id);
257
258 const float getZoomRate();
259
260 void setZoomRate(const float &zoomRate);
261
262 const float getSpinRate();
263
264 void setSpinRate(const float &spinRate);
265
266 const float getMoveRate();
267
268 void setMoveRate(const float &moveRate);
269
271 const bool isLagging() const;
272
275
278
281
283 const float getLagging() const;
284
286 void setLagging(const float &lagging);
287
289 const bool getRendering3DBonds() const;
290
292 void setRendering3DBonds(const bool &_flag);
293
296
298 const bool getRendering3DAngles() const;
299
301 void setRendering3DAngles(const bool &_flag);
302
305
307 const bool getRendering3DDihedrals() const;
308
310 void setRendering3DDihedrals(const bool &_flag);
311
314
316 void setRendering3DAll(const bool &_flag);
317
320
321 void viewportEvent(Platform::GlfwApplication::ViewportEvent& event);
322
323 void cameraTranslateDown();
324
325 void cameraTranslateUp();
326
327 void cameraTranslateRight();
328
329 void cameraTranslateLeft();
330
331 void cameraTranslateForward();
332
333 void cameraTranslateBackward();
334
335 void cameraRotateDown();
336
337 void cameraRotateUp();
338
339 void cameraRotateLeft();
340
341 void cameraRotateRight();
342
343 void cameraRollLeft();
344
345 void cameraRollRight();
346
347 void cameraZoomIn();
348
349 void cameraZoomOut();
350
383 void keyPressEvent(Platform::GlfwApplication::KeyEvent& event);
384 void mousePressEvent(Platform::GlfwApplication::MouseEvent& event);
385 void mouseReleaseEvent(Platform::GlfwApplication::MouseEvent& event);
386
398 void mouseMoveEvent(Platform::GlfwApplication::MouseMoveEvent& event);
399 void mouseScrollEvent(Platform::GlfwApplication::MouseScrollEvent& event);
400
401 SubRenderer *getSubRenderer(const SubRendererFlag &flag);
402 HRESULT registerSubRenderer(SubRenderer *subrenderer);
403
404 bool _dirty = false;
405 bool _decorateScene = true;
406 bool _showDiscretizationGrid = false;
407 shaders::ParticleSphereShader::ColorMode _colorMode = shaders::ParticleSphereShader::ColorMode::ConsistentRandom;
408 Color3 _ambientColor{0.4f};
409 Color3 _diffuseColor{1.f};
410 Color3 _specularColor{0.2f};
411 Color3 _gridColor = {1.f, 1.f, 1.f};
412 Color3 _sceneBoxColor = {1.f, 1.f, 0.f};
413 Float _shininess = 100.0f;
414 fVector3 _lightDir{1.0f, 1.0f, 2.0f};
415 Color3 _lightColor = {0.9, 0.9, 0.9};
416 Color3 _clearColor{0.35f};
417 Color3 _discretizationGridColor{0.1, 0.1, 0.8};
418
419 fVector3 _eye, _center, _up;
420
421 std::vector<Magnum::Vector4> _clipPlanes;
422
431 Magnum::Matrix4 modelViewMat = Matrix4{Math::IdentityInit};
432 Magnum::Matrix4 projMat = Matrix4{Math::IdentityInit};
433
434 iVector2 _prevMousePosition;
435 fVector3 _rotationPoint, _translationPoint;
436 Float _lastDepth;
437
438 float sideLength;
439 float _zoomRate;
440 float _spinRate;
441 float _moveRate;
442
443 ArcBallCamera *_arcball;
444
445 /* ground grid */
446 GL::Mesh gridMesh{NoCreate};
447 Magnum::Matrix4 gridModelView;
448
449 GL::Mesh sceneBox{NoCreate};
450
451
452 /* Spheres rendering */
453
454 shaders::Phong sphereShader{NoCreate};
455
456 Shaders::Flat3D wireframeShader{NoCreate};
457
458 GL::Buffer sphereInstanceBuffer{NoCreate};
459
460 GL::Buffer largeSphereInstanceBuffer{NoCreate};
461
462 GL::Mesh sphereMesh{NoCreate};
463
464 GL::Mesh largeSphereMesh{NoCreate};
465
466 GL::Mesh discretizationGridMesh{NoCreate};
467
468 GL::Buffer discretizationGridBuffer{NoCreate};
469
470 std::vector<SubRenderer*> subRenderers;
471
472 fVector3 center;
473
474 Window *window;
475
481 void decorateScene(const bool &decorate);
482
489 bool sceneDecorated() const;
490
491 fVector3 unproject(const iVector2& windowPosition, float depth) const;
492
493 // todo: implement UniverseRenderer::setupCallbacks
494 void setupCallbacks();
495
497
498 protected:
499
500 float _lagging;
501 bool _bonds3d_flags[3] = {false, false, false}; // bonds, angles, dihedrals
502 };
503
504}};
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode.
Definition tfAngleConfig.h:26
HRESULT show()
Shows any windows that were specified in the config. Does not start the universe time propagation unl...
Definition tfSimulator.h:368
Definition tfUniverseRenderer.h:100
Definition tfRenderer.h:33
Definition tfUniverseRenderer.h:94
Definition tfSubRenderer.h:34
Definition tfUniverseRenderer.h:115
void keyPressEvent(Platform::GlfwApplication::KeyEvent &event)
Key press event handling.
void setRendering3DBonds(const bool &_flag)
bool sceneDecorated() const
Get scene decorator flag value.
void setLagging(const float &lagging)
void setRendering3DDihedrals(const bool &_flag)
void setRendering3DAngles(const bool &_flag)
void mouseMoveEvent(Platform::GlfwApplication::MouseMoveEvent &event)
Mouse move event handling.
void decorateScene(const bool &decorate)
Set flag to draw/not draw scene decorators (e.g., grid)
void setRendering3DAll(const bool &_flag)
Magnum::Matrix4 modelViewMat
Definition tfUniverseRenderer.h:431
Definition tfWindow.h:33
int32_t HRESULT
Definition tf_port.h:255