BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
program.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/view/program.cpp
6 //! @brief Implements Program class
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "GUI/ba3d/view/program.h"
16 #include "GUI/ba3d/view/camera.h"
17 #include <QMatrix4x4>
18 #include <stdexcept>
19 
20 // The macro call has to be in the global namespace
21 inline void InitShaderResources()
22 {
23  Q_INIT_RESOURCE(shaders);
24 }
25 
26 static constexpr float AMBIENT = 0.4f;
27 
28 namespace GUI::RealSpace {
29 
31 {
32  // make sure our resource file gets initialized
34 
35  needsInit();
36 }
37 
39 {
40  doInit = true;
41 }
42 
44 {
45  if (!doInit)
46  return;
47  doInit = false;
48 
49  auto shader_found =
50  addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vertex_shader.vert");
51  if (!shader_found)
52  throw std::runtime_error("Vertex shader not loaded");
53 
54  shader_found =
55  addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fragment_shader.frag");
56  if (!shader_found)
57  throw std::runtime_error("Fragment shader not loaded");
58 
59  bindAttributeLocation("vertex", 0);
60  bindAttributeLocation("normal", 1);
61  bindAttributeLocation("axiscolor", 2);
62 
63  link();
64 
65  bind();
66  locMatProj = uniformLocation("matProj");
67  locMatModel = uniformLocation("matModel");
68  locMatObject = uniformLocation("matObject");
69  locLightPos1 = uniformLocation("lightPos1");
70  locColor = uniformLocation("color");
71  ambient = uniformLocation("ambient");
72  eye = uniformLocation("eye");
73  locAxis = uniformLocation("axis");
74  release();
75 }
76 
77 void Program::set(Camera const& camera)
78 {
79  setUniformValue(ambient, AMBIENT);
80  setUniformValue(locMatProj, camera.matProj);
81  setUniformValue(locMatModel, camera.matModel);
82  setUniformValue(locLightPos1, camera.lightPosRotated1);
83  setUniformValue(eye, camera.getPos().eye);
84 }
85 
86 void Program::set(QColor const& color)
87 {
88  setUniformValue(locColor, color);
89 }
90 
91 void Program::set(QMatrix4x4 const& mat)
92 {
93  setUniformValue(locMatObject, mat);
94 }
95 
96 void Program::setMatModel(QMatrix4x4 const& mat)
97 {
98  setUniformValue(locMatModel, mat);
99 }
100 
101 void Program::setAxis(bool const& axis_)
102 {
103  setUniformValue(locAxis, axis_);
104 }
105 
106 } // namespace GUI::RealSpace
Defines Camera class.
QMatrix4x4 matModel
Definition: camera.h:78
Vector3D lightPosRotated1
Definition: camera.h:74
const Position & getPos() const
Definition: camera.h:50
QMatrix4x4 matProj
Definition: camera.h:78
void set(Camera const &)
Definition: program.cpp:77
void setAxis(bool const &)
Definition: program.cpp:101
void setMatModel(QMatrix4x4 const &)
Definition: program.cpp:96
static constexpr float AMBIENT
Definition: program.cpp:26
void InitShaderResources()
Definition: program.cpp:21
Defines Program class.