-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathMouseRay.cpp
145 lines (100 loc) · 4.56 KB
/
MouseRay.cpp
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
////////////////////////////////////////////////////////////////////////////////////////////////////
// This file is part of CosmoScout VR //
////////////////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: German Aerospace Center (DLR) <[email protected]>
// SPDX-License-Identifier: MIT
#include "MouseRay.hpp"
#include "../cs-utils/utils.hpp"
#include <VistaKernel/GraphicsManager/VistaSceneGraph.h>
#include <VistaKernel/GraphicsManager/VistaTransformNode.h>
#include <VistaKernel/VistaSystem.h>
#include <VistaKernelOpenSGExt/VistaOpenSGMaterialTools.h>
#include <VistaMath/VistaBoundingBox.h>
#include <glm/gtc/type_ptr.hpp>
#include <array>
namespace cs::graphics {
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::array VERTICES{-0.5F, -0.5F, 0.F, -0.5F, 0.5F, 0.F, 0.5F, 0.5F, 0.F, 0.5F, -0.5F, 0.F,
-0.5F, -0.5F, -1.F, -0.5F, 0.5F, -1.F, 0.5F, 0.5F, -1.F, 0.5F, -0.5F, -1.F};
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::array INDICES{0U, 1U, 2U, 2U, 3U, 0U, 0U, 4U, 5U, 5U, 1U, 0U, 4U, 7U, 6U, 6U, 5U, 4U, 2U,
6U, 7U, 7U, 3U, 2U, 1U, 5U, 6U, 6U, 2U, 1U, 0U, 3U, 7U, 7U, 4U, 0U};
////////////////////////////////////////////////////////////////////////////////////////////////////
static const char* SHADER_VERT = R"(
#version 330
uniform mat4 uMatModelView;
uniform mat4 uMatProjection;
// inputs
layout(location = 0) in vec3 iPosition;
void main()
{
vec3 pos = (uMatModelView * vec4(iPosition, 1.0)).xyz;
gl_Position = uMatProjection * vec4(pos, 1);
}
)";
////////////////////////////////////////////////////////////////////////////////////////////////////
static const char* SHADER_FRAG = R"(
#version 330
// outputs
layout(location = 0) out vec4 oColor;
void main()
{
oColor = vec4(1, 1, 1, 0.3);
}
)";
////////////////////////////////////////////////////////////////////////////////////////////////////
MouseRay::MouseRay() {
auto* sceneGraph = GetVistaSystem()->GetGraphicsManager()->GetSceneGraph();
VistaTransformNode* intentionNode =
dynamic_cast<VistaTransformNode*>(sceneGraph->GetNode("SELECTION_NODE"));
mRayTransform.reset(sceneGraph->NewTransformNode(intentionNode));
mRayTransform->SetScale(0.001F, 0.001F, 30.F);
mMouseRayNode.reset(sceneGraph->NewOpenGLNode(mRayTransform.get(), this));
VistaOpenSGMaterialTools::SetSortKeyOnSubtree(
intentionNode, static_cast<int>(cs::utils::DrawOrder::eRay));
// Create box shader.
mShader.InitVertexShaderFromString(SHADER_VERT);
mShader.InitFragmentShaderFromString(SHADER_FRAG);
mShader.Link();
// Create box geometry.
mRayVAO.Bind();
mRayVBO.Bind(GL_ARRAY_BUFFER);
mRayVBO.BufferData(VERTICES.size() * sizeof(float), VERTICES.data(), GL_STATIC_DRAW);
mRayIBO.Bind(GL_ELEMENT_ARRAY_BUFFER);
mRayIBO.BufferData(INDICES.size() * sizeof(unsigned), INDICES.data(), GL_STATIC_DRAW);
mRayVAO.EnableAttributeArray(0);
mRayVAO.SpecifyAttributeArrayFloat(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0, &mRayVBO);
mRayVAO.Release();
mRayIBO.Release();
mRayVBO.Release();
mUniforms.modelViewMatrix = mShader.GetUniformLocation("uMatModelView");
mUniforms.projectionMatrix = mShader.GetUniformLocation("uMatProjection");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool MouseRay::Do() {
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
std::array<GLfloat, 16> glMatMV{};
std::array<GLfloat, 16> glMatP{};
glGetFloatv(GL_MODELVIEW_MATRIX, glMatMV.data());
glGetFloatv(GL_PROJECTION_MATRIX, glMatP.data());
mShader.Bind();
mRayVAO.Bind();
glUniformMatrix4fv(mUniforms.modelViewMatrix, 1, GL_FALSE, glMatMV.data());
glUniformMatrix4fv(mUniforms.projectionMatrix, 1, GL_FALSE, glMatP.data());
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(INDICES.size()), GL_UNSIGNED_INT, nullptr);
mRayVAO.Release();
mShader.Release();
glPopAttrib();
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool MouseRay::GetBoundingBox(VistaBoundingBox& bb) {
std::array fMin{-0.5F, -0.5F, -0.1F};
std::array fMax{0.5F, 0.5F, 0.0F};
bb.SetBounds(fMin.data(), fMax.data());
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace cs::graphics