@@ -30,6 +30,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
30
30
#include " shaders/shader_pos_clr.hpp"
31
31
#include " shaders/shader_pos_tex.hpp"
32
32
33
+ #include " frame_buffer.hpp"
34
+ #include " index_buffer.hpp"
35
+ #include " texture_2d.hpp"
36
+ #include " texture_cube.hpp"
37
+ #include " texture_depth.hpp"
38
+ #include " vertex_array.hpp"
39
+ #include " vertex_buffer.hpp"
40
+
33
41
using namespace ruis ::render::opengl;
34
42
35
43
utki::shared_ref<ruis::render::context::shaders> context::create_shaders ()
@@ -50,3 +58,210 @@ utki::shared_ref<ruis::render::context::shaders> context::create_shaders()
50
58
ret.get ().color_pos_lum = std::make_unique<shader_color_pos_lum>(this ->get_shared_ref ());
51
59
return ret;
52
60
}
61
+
62
+ utki::shared_ref<ruis::render::texture_2d> context::create_texture_2d (
63
+ rasterimage::format format,
64
+ rasterimage::dimensioned::dimensions_type dims,
65
+ texture_2d_parameters params
66
+ )
67
+ {
68
+ return this ->create_texture_2d_internal (format, dims, {}, std::move (params));
69
+ }
70
+
71
+ utki::shared_ref<ruis::render::texture_2d> context::create_texture_2d (
72
+ const rasterimage::image_variant& imvar,
73
+ texture_2d_parameters params
74
+ )
75
+ {
76
+ auto imvar_copy = imvar;
77
+ return this ->create_texture_2d (std::move (imvar_copy), std::move (params));
78
+ }
79
+
80
+ utki::shared_ref<ruis::render::texture_2d> context::create_texture_2d (
81
+ rasterimage::image_variant&& imvar,
82
+ texture_2d_parameters params
83
+ )
84
+ {
85
+ auto iv = std::move (imvar);
86
+ return std::visit (
87
+ [this , &imvar = iv, ¶ms](auto && im) -> utki::shared_ref<ruis::render::texture_2d> {
88
+ if constexpr (sizeof (im.pixels ().front ().front ()) != 1 ) {
89
+ throw std::logic_error (
90
+ " context::create_texture_2d(): "
91
+ " non-8bit images are not supported"
92
+ );
93
+ } else {
94
+ im.span ().flip_vertical ();
95
+ auto data = im.pixels ();
96
+ return this ->create_texture_2d_internal (
97
+ imvar.get_format (),
98
+ im.dims (),
99
+ utki::make_span (data.front ().data (), data.size_bytes ()),
100
+ std::move (params)
101
+ );
102
+ }
103
+ },
104
+ iv.variant
105
+ );
106
+ }
107
+
108
+ utki::shared_ref<ruis::render::texture_2d> context::create_texture_2d_internal (
109
+ rasterimage::format type,
110
+ rasterimage::dimensioned::dimensions_type dims,
111
+ utki::span<const uint8_t > data,
112
+ texture_2d_parameters params
113
+ )
114
+ {
115
+ return utki::make_shared<texture_2d>(
116
+ this ->get_shared_ref (), //
117
+ type,
118
+ dims,
119
+ data,
120
+ params
121
+ );
122
+ }
123
+
124
+ utki::shared_ref<ruis::render::texture_depth> context::create_texture_depth (
125
+ rasterimage::dimensioned::dimensions_type dims
126
+ )
127
+ {
128
+ return utki::make_shared<texture_depth>(
129
+ this ->get_shared_ref (), //
130
+ dims
131
+ );
132
+ }
133
+
134
+ utki::shared_ref<ruis::render::texture_cube> context::create_texture_cube (
135
+ rasterimage::image_variant&& positive_x,
136
+ rasterimage::image_variant&& negative_x,
137
+ rasterimage::image_variant&& positive_y,
138
+ rasterimage::image_variant&& negative_y,
139
+ rasterimage::image_variant&& positive_z,
140
+ rasterimage::image_variant&& negative_z
141
+ )
142
+ {
143
+ constexpr auto num_cube_sides = 6 ;
144
+ std::array<rasterimage::image_variant, num_cube_sides> sides = {
145
+ std::move (positive_x),
146
+ std::move (negative_x),
147
+
148
+ // negative_y and positive_y are swapped due to texture coordinates y-axis going downwards
149
+ std::move (negative_y),
150
+ std::move (positive_y),
151
+
152
+ std::move (positive_z),
153
+ std::move (negative_z)
154
+ };
155
+ std::array<ruis::render::opengl::texture_cube::cube_face_image, num_cube_sides> faces;
156
+
157
+ auto face = faces.begin ();
158
+ for (auto & side : sides) {
159
+ ASSERT (face != faces.end ())
160
+ std::visit (
161
+ [&](auto & im) {
162
+ if constexpr (sizeof (im.pixels ().front ().front ()) != 1 ) {
163
+ throw std::logic_error (
164
+ " context::create_texture_cube(): "
165
+ " non-8bit images are not supported"
166
+ );
167
+ } else {
168
+ im.span ().flip_vertical ();
169
+ auto data = im.pixels ();
170
+
171
+ face->type = side.get_format ();
172
+ face->dims = im.dims ();
173
+ face->data = utki::make_span (data.front ().data (), data.size_bytes ());
174
+ }
175
+ },
176
+ side.variant
177
+ );
178
+ ++face;
179
+ }
180
+
181
+ return utki::make_shared<texture_cube>(
182
+ this ->get_shared_ref (), //
183
+ faces
184
+ );
185
+ }
186
+
187
+ utki::shared_ref<ruis::render::vertex_buffer> context::create_vertex_buffer (
188
+ utki::span<const r4::vector4<float >> vertices
189
+ )
190
+ {
191
+ return utki::make_shared<vertex_buffer>(
192
+ this ->get_shared_ref (), //
193
+ vertices
194
+ );
195
+ }
196
+
197
+ utki::shared_ref<ruis::render::vertex_buffer> context::create_vertex_buffer (
198
+ utki::span<const r4::vector3<float >> vertices
199
+ )
200
+ {
201
+ return utki::make_shared<vertex_buffer>(
202
+ this ->get_shared_ref (), //
203
+ vertices
204
+ );
205
+ }
206
+
207
+ utki::shared_ref<ruis::render::vertex_buffer> context::create_vertex_buffer (
208
+ utki::span<const r4::vector2<float >> vertices
209
+ )
210
+ {
211
+ return utki::make_shared<vertex_buffer>(
212
+ this ->get_shared_ref (), //
213
+ vertices
214
+ );
215
+ }
216
+
217
+ utki::shared_ref<ruis::render::vertex_buffer> context::create_vertex_buffer (utki::span<const float > vertices)
218
+ {
219
+ return utki::make_shared<vertex_buffer>(
220
+ this ->get_shared_ref (), //
221
+ vertices
222
+ );
223
+ }
224
+
225
+ utki::shared_ref<ruis::render::vertex_array> context::create_vertex_array (
226
+ std::vector<utki::shared_ref<const ruis::render::vertex_buffer>> buffers,
227
+ utki::shared_ref<const ruis::render::index_buffer> indices,
228
+ ruis::render::vertex_array::mode mode
229
+ )
230
+ {
231
+ return utki::make_shared<vertex_array>(
232
+ this ->get_shared_ref (), //
233
+ std::move (buffers),
234
+ std::move (indices),
235
+ mode
236
+ );
237
+ }
238
+
239
+ utki::shared_ref<ruis::render::index_buffer> context::create_index_buffer (utki::span<const uint16_t > indices)
240
+ {
241
+ return utki::make_shared<index_buffer>(
242
+ this ->get_shared_ref (), //
243
+ indices
244
+ );
245
+ }
246
+
247
+ utki::shared_ref<ruis::render::index_buffer> context::create_index_buffer (utki::span<const uint32_t > indices)
248
+ {
249
+ return utki::make_shared<index_buffer>(
250
+ this ->get_shared_ref (), //
251
+ indices
252
+ );
253
+ }
254
+
255
+ utki::shared_ref<ruis::render::frame_buffer> context::create_framebuffer ( //
256
+ std::shared_ptr<ruis::render::texture_2d> color,
257
+ std::shared_ptr<ruis::render::texture_depth> depth,
258
+ std::shared_ptr<ruis::render::texture_stencil> stencil
259
+ )
260
+ {
261
+ return utki::make_shared<frame_buffer>( //
262
+ this ->get_shared_ref (),
263
+ std::move (color),
264
+ std::move (depth),
265
+ std::move (stencil)
266
+ );
267
+ }
0 commit comments