BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
buffer.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/view/buffer.cpp
6 //! @brief Implements Buffer 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/buffer.h"
17 
18 namespace {
19 
20 const float cx = 120; // multiplication scale for controlling how long the axes shall be in xy
21 const float cz = 100; // multiplication scale for controlling how long the axes shall be in z
22 
23 } // namespace
24 
25 namespace GUI::RealSpace {
26 
27 Buffer::Buffer(Geometry const& geometry)
28 {
29  initializeOpenGLFunctions();
30 
31  const auto& mesh = geometry.m_mesh;
32  m_vertexCount = mesh.count();
33 
34  QOpenGLVertexArrayObject::Binder dummy(&m_vao);
35 
36  m_glBuffer.create();
37  m_glBuffer.bind();
38  m_glBuffer.allocate(mesh.constData(), m_vertexCount * int(sizeof(Geometry::Vert_Normal)));
39 
40  glEnableVertexAttribArray(0); // vertices
41  glEnableVertexAttribArray(1); // normals
42 
43  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(Vector3D), nullptr);
44  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(Vector3D),
45  reinterpret_cast<void*>(sizeof(Vector3D)));
46 }
47 
49 {
50  QOpenGLVertexArrayObject::Binder dummy(&m_vao);
51  glDrawArrays(GL_TRIANGLES, 0, m_vertexCount);
52 }
53 
55 {
56  initializeOpenGLFunctions();
57 
58  QOpenGLVertexArrayObject::Binder dummy(&m_vao3DAxes);
59 
60  // vertices (xyz) and colors (rgb) for drawing each line (also arrows) in the 3D axes
61  const GLfloat vertices3DAxes[] = {
62  0.00f, 0.00f, 0.00f, 1.0f, 0.0f, 0.0f, // x-axis
63  cx * 1.00f, 0.00f, 0.00f, 1.0f, 0.0f, 0.0f, cx * 1.00f,
64  0.00f, 0.00f, 1.0f, 0.0f, 0.0f, cx * 0.95f, cz * 0.05f,
65  cz * 0.05f, 1.0f, 0.0f, 0.0f, cx * 1.00f, 0.00f, 0.00f,
66  1.0f, 0.0f, 0.0f, cx * 0.95f, cz * 0.05f, cz * -0.05f, 1.0f,
67  0.0f, 0.0f, cx * 1.00f, 0.00f, 0.00f, 1.0f, 0.0f,
68  0.0f, cx * 0.95f, cz * -0.05f, cz * 0.05f, 1.0f, 0.0f, 0.0f,
69  cx * 1.00f, 0.00f, 0.00f, 1.0f, 0.0f, 0.0f, cx * 0.95f,
70  cz * -0.05f, cz * -0.05f, 1.0f, 0.0f, 0.0f,
71 
72  0.00f, 0.00f, 0.00f, 0.0f, 1.0f, 0.0f, // y-axis
73  0.00f, cx * 1.00f, 0.00f, 0.0f, 1.0f, 0.0f, 0.00f,
74  cx * 1.00f, 0.00f, 0.0f, 1.0f, 0.0f, cz * 0.05f, cx * 0.95f,
75  cz * 0.05f, 0.0f, 1.0f, 0.0f, 0.00f, cx * 1.00f, 0.00f,
76  0.0f, 1.0f, 0.0f, cz * 0.05f, cx * 0.95f, cz * -0.05f, 0.0f,
77  1.0f, 0.0f, 0.00f, cx * 1.00f, 0.00f, 0.0f, 1.0f,
78  0.0f, cz * -0.05f, cx * 0.95f, cz * 0.05f, 0.0f, 1.0f, 0.0f,
79  0.00f, cx * 1.00f, 0.00f, 0.0f, 1.0f, 0.0f, cz * -0.05f,
80  cx * 0.95f, cz * -0.05f, 0.0f, 1.0f, 0.0f,
81 
82  0.00f, 0.00f, 0.00f, 0.0f, 0.0f, 1.0f, // z-axis
83  0.00f, 0.00f, cz * 1.00f, 0.0f, 0.0f, 1.0f, 0.00f,
84  0.00f, cz * 1.00f, 0.0f, 0.0f, 1.0f, cz * 0.05f, cz * 0.05f,
85  cz * 0.95f, 0.0f, 0.0f, 1.0f, 0.00f, 0.00f, cz * 1.00f,
86  0.0f, 0.0f, 1.0f, cz * 0.05f, cz * -0.05f, cz * 0.95f, 0.0f,
87  0.0f, 1.0f, 0.00f, 0.00f, cz * 1.00f, 0.0f, 0.0f,
88  1.0f, cz * -0.05f, cz * 0.05f, cz * 0.95f, 0.0f, 0.0f, 1.0f,
89  0.00f, 0.00f, cz * 1.00f, 0.0f, 0.0f, 1.0f, cz * -0.05f,
90  cz * -0.05f, cz * 0.95f, 0.0f, 0.0f, 1.0f,
91  };
92 
94 
95  m_glBuffer3DAxes.create();
96  m_glBuffer3DAxes.bind();
97  m_glBuffer3DAxes.allocate(vertices3DAxes, int(sizeof(vertices3DAxes)));
98 
99  glEnableVertexAttribArray(0); // 3D axes vertices
100  glEnableVertexAttribArray(2); // 3D axes colors
101 
102  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr);
103  glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
104  reinterpret_cast<void*>(3 * sizeof(float)));
105 }
106 
108 {
109  QOpenGLVertexArrayObject::Binder dummy(&m_vao3DAxes);
110  glLineWidth(1.4f);
111  glDrawArrays(GL_LINES, 0, m_vertexCount3DAxes);
112 }
113 
114 } // namespace GUI::RealSpace
Defines Buffer class.
QOpenGLVertexArrayObject m_vao3DAxes
Definition: buffer.h:47
QOpenGLBuffer m_glBuffer3DAxes
Definition: buffer.h:48
QOpenGLVertexArrayObject m_vao
Definition: buffer.h:35
Buffer(Geometry const &)
Definition: buffer.cpp:27
QOpenGLBuffer m_glBuffer
Definition: buffer.h:36
Defines Geometry class.