Debian Codegraph @

libs/opengl/src/CSkyBox.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, Individual contributors, see AUTHORS file |
6 | See: https://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See: https://www.mrpt.org/License |
8 +------------------------------------------------------------------------+ */
9
10 #include "opengl-precomp.h" // Precompiled header
11 //
12 #include <mrpt/opengl/CSkyBox.h>
13 #include <mrpt/opengl/opengl_api.h>
14 #include <mrpt/serialization/CArchive.h>
15 #include <mrpt/serialization/CSchemeArchiveBase.h>
16 #include <mrpt/serialization/stl_serialization.h>
17
18 #include <memory>
19
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::math;
23 using namespace std;
24
25 IMPLEMENTS_SERIALIZABLE(CSkyBox, CRenderizable, mrpt::opengl)
26
27 void CSkyBox::renderUpdateBuffers() const
28 {
29 // vertices:
30 constexpr static float skyboxVertices[] = {
31 // Positions
32 -1.0f, 1.0f, -1.0f, //
33 -1.0f, -1.0f, -1.0f, //
34 1.0f, -1.0f, -1.0f, //
35 1.0f, -1.0f, -1.0f, //
36 1.0f, 1.0f, -1.0f, //
37 -1.0f, 1.0f, -1.0f, //
38
39 -1.0f, -1.0f, 1.0f, //
40 -1.0f, -1.0f, -1.0f, //
41 -1.0f, 1.0f, -1.0f, //
42 -1.0f, 1.0f, -1.0f, //
43 -1.0f, 1.0f, 1.0f, //
44 -1.0f, -1.0f, 1.0f, //
45
46 1.0f, -1.0f, -1.0f, //
47 1.0f, -1.0f, 1.0f, //
48 1.0f, 1.0f, 1.0f, //
49 1.0f, 1.0f, 1.0f, //
50 1.0f, 1.0f, -1.0f, //
51 1.0f, -1.0f, -1.0f, //
52
53 -1.0f, -1.0f, 1.0f, //
54 -1.0f, 1.0f, 1.0f, //
55 1.0f, 1.0f, 1.0f, //
56 1.0f, 1.0f, 1.0f, //
57 1.0f, -1.0f, 1.0f, //
58 -1.0f, -1.0f, 1.0f, //
59
60 -1.0f, 1.0f, -1.0f, //
61 1.0f, 1.0f, -1.0f, //
62 1.0f, 1.0f, 1.0f, //
63 1.0f, 1.0f, 1.0f, //
64 -1.0f, 1.0f, 1.0f, //
65 -1.0f, 1.0f, -1.0f, //
66
67 -1.0f, -1.0f, -1.0f, //
68 -1.0f, -1.0f, 1.0f, //
69 1.0f, -1.0f, -1.0f, //
70 1.0f, -1.0f, -1.0f, //
71 -1.0f, -1.0f, 1.0f, //
72 1.0f, -1.0f, 1.0f //
73 };
74
75 // Define OpenGL buffers:
76 (*m_vbo)->createOnce();
77 (*m_vbo)->bind();
78 (*m_vbo)->allocate(skyboxVertices, sizeof(skyboxVertices));
79
80 // VAO: required to use glEnableVertexAttribArray()
81 (*m_vao)->createOnce();
82 }
83
84 void CSkyBox::render(const RenderContext& rc) const
85 {
86 #if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL
87
88 // This will load and/or select our texture, only once:
89 initializeTextures();
90 ASSERT_(m_cubeTexture.initialized());
91
92 // Needed to draw only "free" pixels:
93 glDepthFunc(GL_LEQUAL);
94
95 // Disable cull:
96 rc.activeCullFace = TCullFace::NONE;
97 glDisable(GL_CULL_FACE);
98
99 {
100 const Program& s = *rc.shader;
101 // bound to GL_TEXTURE0 + "i":
102 glUniform1i(s.uniformId("skybox"), m_cubeTexture.textureUnit());
103 }
104
105 // Set up the vertex array:
106 GLuint attr_position;
107 {
108 attr_position = rc.shader->attributeId("position");
109 (*m_vao)->bind();
110 glEnableVertexAttribArray(attr_position);
111 (*m_vbo)->bind();
112 glVertexAttribPointer(
113 attr_position, // attribute
114 3, // size
115 GL_FLOAT, // type
116 GL_FALSE, // normalized?
117 0, // stride
118 nullptr // offset
119 );
120 CHECK_OPENGL_ERROR_IN_DEBUG();
121 }
122
123 // Draw:
124 glDrawArrays(GL_TRIANGLES, 0, 36);
125 CHECK_OPENGL_ERROR_IN_DEBUG();
126
127 glDisableVertexAttribArray(attr_position);
128 glDepthFunc(GL_LESS); // restore
129
130 #endif
131 }
132
133 uint8_t CSkyBox::serializeGetVersion() const { return 0; }
134 void CSkyBox::serializeTo(mrpt::serialization::CArchive& out) const
135 {
136 writeToStreamRender(out);
137 out << m_textureImages; // <mrpt/serialization/stl_serialization.h>
138 }
139
140 void CSkyBox::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version)
141 {
142 switch (version)
143 {
144 case 0:
145 {
146 readFromStreamRender(in);
147 in >> m_textureImages;
148 }
149 break;
150 default:
151 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
152 };
153 CRenderizable::notifyChange();
154 }
155
156 /** In this class, returns a fixed box (max,max,max), (-max,-max,-max). */
157 auto CSkyBox::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf { return {}; }
158
159 void CSkyBox::assignImage(const CUBE_TEXTURE_FACE face, const mrpt::img::CImage& img)
160 {
161 const int faceIdx = static_cast<int>(face);
162 ASSERT_GE_(faceIdx, 0);
163 ASSERT_LT_(faceIdx, 6);
164
165 m_textureImages[faceIdx] = img;
166 CRenderizable::notifyChange();
167 }
168
169 void CSkyBox::assignImage(const CUBE_TEXTURE_FACE face, mrpt::img::CImage&& img)
170 {
171 const int faceIdx = static_cast<int>(face);
172 ASSERT_GE_(faceIdx, 0);
173 ASSERT_LT_(faceIdx, 6);
174
175 m_textureImages[faceIdx] = std::move(img);
176 CRenderizable::notifyChange();
177 }
178
179 void CSkyBox::initializeTextures() const
180 {
181 #if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL
182
183 if (m_cubeTexture.initialized())
184 {
185 m_cubeTexture.bindAsCubeTexture();
186 return;
187 }
188
189 // Reserve the new one --------------------------
190 m_cubeTexture.assignCubeImages(m_textureImages);
191
192 #endif
193 }
194
195 CSkyBox::~CSkyBox()
196 {
197 try
198 {
199 m_cubeTexture.unloadTexture();
200 }
201 catch (const std::exception& e)
202 {
203 std::cerr << "[~CSkyBox] Ignoring exception: " << mrpt::exception_to_str(e);
204 }
205 }