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