20 #include "gl_lib_fwd.hpp" 23 #error GLLIB_GLES not defined 25 #ifndef GLLIB_OPENGL_VERSION 26 #error GLLIB_OPENGL_VERSION not defined 32 using GLenum =
unsigned int;
33 using GLuint =
unsigned int;
41 bool supports_mipmaps_for(
Size size);
63 void operator=(
Texture&& other) { this->swap(other); }
73 void set_data(
const void* data);
75 void set_mip_data(
const void* data,
Size size,
unsigned level);
77 const ImageFormat& format()
const {
return _format; }
79 const TexParams& params()
const {
return _params; }
82 bool has_data()
const {
return _has_data; }
84 bool is_power_of_two()
const;
86 const std::string& debug_name()
const {
return _debug_name; }
87 void set_debug_name(
const std::string& debug_name);
90 void bind(
unsigned tu = 0)
const;
92 unsigned width()
const {
return _size.x; }
93 unsigned height()
const {
return _size.y; }
94 const Size& size()
const {
return _size; }
98 unsigned bits_per_pixel()
const;
102 GLuint
id()
const {
return _id; }
103 bool has_id()
const {
return _id != 0; }
105 void generate_mipmaps();
108 void init(
const void* data);
110 void set_wrap_mode(WrapMode s, WrapMode t)
const;
111 void set_filtering(
TexFilter filter)
const;
116 std::string _debug_name;
119 bool _has_data =
false;
124 mutable bool _params_dirty =
false;
132 const void* data,
size_t num_bytes,
133 TexParams params, std::string debug_name);
156 using Uniforms = std::vector<Uniform>;
157 using Attributes = std::vector<Attribute>;
164 Program(
const std::string& vs,
const std::string&
fs, std::string debug_name);
171 const std::string& debug_name()
const {
return _debug_name; }
172 unsigned id()
const {
return _program; }
175 void validate()
const;
179 int get_attribute_loc(
const std::string& attrib_name)
const;
180 int get_uniform_loc(
const std::string& attrib_name)
const;
182 bool has_attribute(
const std::string& name)
const;
183 bool has_uniform(
const std::string& name)
const;
186 void set_uniform(
const std::string& name,
const T& value)
const 188 set_uniform(get_uniform_loc(name), value);
193 void set_uniform(
int location,
const T& value)
const;
199 unsigned _program = 0;
200 std::string _debug_name;
202 Attributes _attributes;
234 enum Normalize { DONT_NORMALIZE, NORMALIZE };
244 size_t sizeBytes()
const;
248 VertComp(
const char* name_,
unsigned num_comps_,
unsigned type_, Normalize normalize_)
249 : name(name_), num_comps(num_comps_), type(type_), normalize(normalize_) {}
252 static VertComp Float(
const char* name);
253 static VertComp Vec2(
const char* name, Normalize normalize = DONT_NORMALIZE);
260 using CompIter = std::vector<VertComp>::const_iterator;
262 VertexFormat(std::initializer_list<VertComp> components);
264 auto begin()
const {
return _comps.begin(); }
265 auto end()
const {
return _comps.end(); }
267 size_t stride()
const {
return _stride; }
271 std::vector<VertComp> _comps;
289 template<
typename ElementType>
290 const ElementType* data()
const {
return reinterpret_cast<const ElementType*
>(_buffer.data()); }
293 template<
typename ElementType>
297 _buffer.resize(count *
sizeof(ElementType));
299 return reinterpret_cast<ElementType*
>( _buffer.data() );
302 template<
typename ElementType>
303 void append(
const ElementType* elements,
size_t count)
306 auto elements_ptr =
reinterpret_cast<const char*
>(elements);
307 _buffer.insert(_buffer.end(), elements_ptr, elements_ptr + count *
sizeof(ElementType));
318 void invalidate() { _dirty =
true; }
320 bool empty()
const {
return _count == 0; }
321 size_t count()
const {
return _count; }
322 size_t size_bytes()
const {
return _buffer.size(); }
331 std::vector<char> _buffer;
364 const VertexFormat& vertex_format()
const {
return _vf; }
365 VBO& vert_vbo() {
return _vertices; }
366 const VBO& vert_vbo()
const {
return _vertices; }
369 template<
typename Vertex>
372 CHECK_F(
sizeof(Vertex) == _vf.stride(),
"Unexpected vertex size");
373 return _vertices.allocate<Vertex>(count);
376 template<
typename Vertex>
377 void set_verts(
const std::vector<Vertex>& vertices)
379 std::copy_n(vertices.data(), vertices.size(), allocate_vert<Vertex>(vertices.size()));
387 _indices.reset(
new VBO(VBO::Index, _usage));
389 return _indices->allocate<uint32_t>(count);
392 void set_indices(
const std::vector<uint32_t>& indices)
394 std::copy_n(indices.data(), indices.size(), allocate_indices(indices.size()));
400 void paint(
const Program& prog, GLenum mode);
402 void paint_strip(
const Program& prog);
404 void invalidate_verts() { _vertices.invalidate(); }
409 std::unique_ptr<VBO> _indices;
410 std::unique_ptr<VAO> _vao;
418 template<
typename Vertex>
424 CHECK_EQ_F(
sizeof(Vertex), _mesh_painter.vertex_format().stride());
427 bool empty() {
return _mesh_painter.vert_vbo().empty(); }
430 int count()
const {
return _mesh_painter.vert_vbo().count(); }
432 int size_bytes()
const {
return _mesh_painter.vert_vbo().size_bytes(); }
434 void clear() { _mesh_painter.vert_vbo().clear(); }
436 void add_strip(
const Vertex* ptr,
size_t n)
438 if (n == 0) {
return; }
440 VBO& vbo = _mesh_painter.vert_vbo();
443 Vertex last_vertex = vbo.data<Vertex>()[vbo.count() - 1u];
444 vbo.append<Vertex>(&last_vertex, 1u);
445 vbo.append<Vertex>(ptr, 1u);
447 vbo.append<Vertex>(ptr, n);
450 void add_strip(
const Vertex* start,
const Vertex* end) { add_strip(start, end - start); }
451 void add_strip(
const std::vector<Vertex>& verts) { add_strip(verts.data(), verts.size()); }
452 void add_strip(std::initializer_list<Vertex> verts) { add_strip(verts.begin(), verts.size()); }
454 void paint_strip(
const Program& prog) { _mesh_painter.paint_strip(prog); }
472 static void set_back_buffer(
Rectangle bb);
477 set_back_buffer(
Rectangle{0, 0, (int)size.x, (
int)size.y});
483 set_back_buffer(
Rectangle{0, 0, width, height});
487 static Size back_buffer_size();
511 Lock& operator=(
const Lock&)=
delete;
520 bool with_depth =
false;
521 #endif // !GLLIB_GLES 522 bool color_mipmap =
false;
528 FBO(
const std::string& debug_name,
Size size,
const Params& params);
529 FBO(
const std::string& debug_name,
Size size) :
FBO(debug_name, size,
Params()) {}
534 void operator=(
const FBO&) =
delete;
535 void operator=(
FBO&&) =
delete;
538 const Size& size()
const {
return _size; }
539 unsigned width()
const {
return _size.x; }
540 unsigned height()
const {
return _size.y; }
543 void generate_color_mipmap();
545 const gl::Texture& color_texture()
const {
return _color_tex; }
546 gl::Texture release_color_texture() {
return std::move(_color_tex); }
549 std::string _debug_name;
555 GLuint _depth_rbo_id = 0;
556 #endif // !GLLIB_GLES ElementType * allocate(size_t count)
Will re-use memory if same size.
Definition: gl_lib.hpp:294
Normalize normalize
If we normalize, values are rescaled to [0, 1].
Definition: gl_lib.hpp:241
Definition: gl_lib.hpp:419
size_t memory_usage() const
in bytes
Texture load_uncompressed_pvr2_from_memory(const void *data, size_t num_bytes, TexParams params, std::string debug_name)
uint32_t * allocate_indices(size_t count)
Definition: gl_lib.hpp:384
void set_bits_per_pixel(unsigned bpp)
Use to override when you know the format is compressed.
Bind/unbind FBO:
Definition: gl_lib.hpp:503
unsigned type
e.g. GL_FLOAT_VEC2
Definition: gl_lib.hpp:152
TexFilter
Definition: gl_lib_fwd.hpp:135
ImageFormat
Definition: gl_lib_fwd.hpp:103
static void set_back_buffer_size(int width, int height)
Call when we acquire context or resize window.
Definition: gl_lib.hpp:481
ProgramSource create_ff(int flags)
flags should be a combo if FF::FF_Flags
Definition: file_system.hpp:18
Definition: gl_lib.hpp:236
unsigned num_comps
1 for scalars, 2 for Vec2 etc
Definition: gl_lib.hpp:239
static void set_back_buffer_size(Size size)
Call when we acquire context or resize window.
Definition: gl_lib.hpp:475
int count() const
Number of vertices.
Definition: gl_lib.hpp:430
Vertex * allocate_vert(size_t count)
Will re-use memory if same size.
Definition: gl_lib.hpp:370
FF_Flags
Fixed function flags.
Definition: gl_lib.hpp:217
Definition: gl_lib_fwd.hpp:151
Definition: gl_lib.hpp:137
void bind(unsigned tu=0) const
We must have an id.
Definition: gl_lib.hpp:338
Definition: gl_lib.hpp:278
unsigned type
e.g. GL_FLOAT
Definition: gl_lib.hpp:240
Definition: gl_lib.hpp:48
Definition: gl_lib_fwd.hpp:89
int size
Mostly 1, maybe non-1 for arrays?
Definition: gl_lib.hpp:151
void free()
Free allocated texture, if any. sets id = 0.ß
GLuint id() const
0 if not generated
Definition: gl_lib.hpp:102
size_t offset
Byte offset, filled in by VertexFormat::VertexFormat.
Definition: gl_lib.hpp:242
Definition: gl_lib.hpp:517
Definition: gl_lib.hpp:357
Texture()
Will create an invalid texture!
Program compile_program(const std::string &vs, const std::string &fs, const std::string &debug_name)
Definition: gl_lib_fwd.hpp:48
Program compile_ff_program(int flags)
flags should be a combo if FF::FF_Flags
Definition: gl_lib.hpp:148
Usage
For VBO:s and the like.
Definition: gl_lib_fwd.hpp:82
An off-screen buffer you can draw onto.
Definition: gl_lib.hpp:499
Will set a viewport and restore the old viewport on death.
Definition: gl_lib.hpp:463
OpenGL wrapper classes.
Definition: gl_lib.hpp:30
Definition: gl_lib_fwd.hpp:94