galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
LuaGraphics.cpp
Go to the documentation of this file.
1
7
8#include <entt/locator/locator.hpp>
9#include <sol/sol.hpp>
10
18
21
22#include "../Lua.hpp"
23
24namespace galaxy
25{
26 void Lua::inject_graphics() noexcept
27 {
28 auto& lua = entt::locator<sol::state>::value();
29
30 lua.set_function("gen_quad_vertices", &graphics::gen_quad_vertices);
31 lua.set_function("gen_default_indices", &graphics::gen_default_indices);
32 lua.set_function("map_x_texel", &graphics::map_x_texel<float>);
33 lua.set_function("map_y_texel", &graphics::map_y_texel<float>);
34
35 // clang-format off
36 lua.new_enum<GLDrawHint>("GLDrawHint",
37 {
38 {"STATIC_DRAW", GLDrawHint::STATIC_DRAW},
39 {"DYNAMIC_DRAW", GLDrawHint::DYNAMIC_DRAW},
40 {"STREAM_DRAW", GLDrawHint::STREAM_DRAW}
41 });
42
43 lua.new_enum<GLAttributeBinding>("GLAttributeBinding",
44 {
45 {"POSITION_POINT", GLAttributeBinding::POSITION_POINT},
46 {"TEXEL_POINT", GLAttributeBinding::TEXEL_POINT},
47 {"OFFSET_POINT", GLAttributeBinding::OFFSET_POINT}
48 });
49
50 lua.new_enum<GLBufferBinding>("GLBufferBinding",
51 {
52 {"VERTEX_BUFFER_POINT", GLBufferBinding::VERTEX_BUFFER_POINT},
53 {"INSTANCE_BUFFER_POINT", GLBufferBinding::INSTANCE_BUFFER_POINT}
54 });
55
56 lua.new_enum<GLTextureMode>("GLTextureMode",
57 {
58 {"REPEAT", GLTextureMode::REPEAT},
59 {"MIRRORED_REPEAT", GLTextureMode::MIRRORED_REPEAT},
60 {"CLAMP_TO_EDGE", GLTextureMode::CLAMP_TO_EDGE},
61 {"CLAMP_TO_BORDER", GLTextureMode::CLAMP_TO_BORDER}
62 });
63
64 lua.new_enum<GLTextureFilter>("GLTextureFilter",
65 {
66 {"NEAREST", GLTextureFilter::NEAREST},
67 {"TRILINEAR", GLTextureFilter::TRILINEAR}
68 });
69 // clang-format on
70
71 auto colour_type =
72 lua.new_usertype<Colour>("Colour", sol::constructors<Colour(), Colour(const std::uint8_t, const std::uint8_t, const std::uint8_t, const std::uint8_t)>());
73 colour_type["array"] = &Colour::array;
74 colour_type["normalize"] = &Colour::normalize;
75 colour_type["set_from_norm"] = &Colour::set_from_norm;
76 // colour_type["OPAQUE"] = &Colour::OPAQUE;
77 // colour_type["TRANSPARENT"] = &Colour::TRANSPARENT;
78 colour_type["set_r"] = sol::resolve<void(const std::uint8_t)>(&Colour::r);
79 colour_type["set_g"] = sol::resolve<void(const std::uint8_t)>(&Colour::g);
80 colour_type["set_b"] = sol::resolve<void(const std::uint8_t)>(&Colour::b);
81 colour_type["set_a"] = sol::resolve<void(const std::uint8_t)>(&Colour::a);
82 colour_type["get_r"] = sol::resolve<std::uint8_t() const>(&Colour::r);
83 colour_type["get_g"] = sol::resolve<std::uint8_t() const>(&Colour::g);
84 colour_type["get_b"] = sol::resolve<std::uint8_t() const>(&Colour::b);
85 colour_type["get_a"] = sol::resolve<std::uint8_t() const>(&Colour::a);
86
87 auto shader_type = lua.new_usertype<Shader>("Shader", sol::constructors<Shader()>());
88 shader_type["bind"] = &Shader::bind;
89 shader_type["compile"] = &Shader::compile;
90 shader_type["destroy"] = &Shader::destroy;
91 shader_type["id"] = &Shader::id;
92 shader_type["load"] = sol::resolve<bool(const std::string&, const std::string&)>(&Shader::load);
93 shader_type["load_combined"] = sol::resolve<bool(const std::string&)>(&Shader::load);
94 shader_type["parse"] = sol::resolve<bool(const std::string&, const std::string&)>(&Shader::parse);
95 shader_type["parse_combined"] = sol::resolve<bool(const std::string&)>(&Shader::parse);
96 shader_type["unbind"] = &Shader::unbind;
97
98 auto vertex_type = lua.new_usertype<Vertex>("Vertex", sol::constructors<Vertex()>());
99 vertex_type["pos"] = &Vertex::m_pos;
100 vertex_type["texels"] = &Vertex::m_texels;
101
102 auto vertexbuffer_type = lua.new_usertype<VertexBuffer>("VertexBuffer", sol::constructors<VertexBuffer()>());
103 vertexbuffer_type["buffer"] = sol::resolve<void(std::span<Vertex>, std::span<unsigned int>)>(&VertexBuffer::buffer);
104 vertexbuffer_type["buffer_no_upload"] = sol::resolve<void(const int, std::span<unsigned int>)>(&VertexBuffer::buffer);
105 vertexbuffer_type["clear"] = &VertexBuffer::clear;
106 vertexbuffer_type["count"] = &VertexBuffer::count;
107 vertexbuffer_type["id"] = &VertexBuffer::id;
108 vertexbuffer_type["offset"] = &VertexBuffer::offset;
109 vertexbuffer_type["sub_buffer"] = &VertexBuffer::sub_buffer;
110
111 auto vertexarray_type = lua.new_usertype<VertexArray>("VertexArray", sol::constructors<VertexArray()>());
112 vertexarray_type["bind"] = &VertexArray::bind;
113 vertexarray_type["buffer"] = sol::resolve<void(std::span<Vertex>, std::span<unsigned int>)>(&VertexArray::buffer);
114 vertexarray_type["buffer_no_upload"] = sol::resolve<void(const int, std::span<unsigned int>)>(&VertexArray::buffer);
115 vertexarray_type["count"] = &VertexArray::count;
116 vertexarray_type["id"] = &VertexArray::id;
117 vertexarray_type["offset"] = &VertexArray::offset;
118 vertexarray_type["sub_buffer"] = &VertexArray::sub_buffer;
119 vertexarray_type["unbind"] = &VertexArray::unbind;
120 vertexarray_type["vbo"] = &VertexArray::vbo;
121
122 auto texture_type = lua.new_usertype<Texture>("Texture", sol::constructors<Texture()>());
123 texture_type["bind"] = &Texture::bind;
124 texture_type["destroy"] = &Texture::destroy;
125 texture_type["handle"] = &Texture::handle;
126 texture_type["height"] = &Texture::height;
127 texture_type["id"] = &Texture::id;
128 texture_type["load"] = &Texture::load;
129 texture_type["load_mem"] = &Texture::load_mem;
130 texture_type["recreate"] = &Texture::recreate;
131 texture_type["save"] = &Texture::save;
132 texture_type["unbind"] = &Texture::unbind;
133 texture_type["width"] = &Texture::width;
134
135 auto ssbo_type = lua.new_usertype<ShaderStorageBuffer>("ShaderStorageBuffer", sol::constructors<ShaderStorageBuffer(const int)>());
136 // ssbo_type[""] = &ShaderStorageBuffer::buffer;
137 // ssbo_type[""] = &ShaderStorageBuffer::sub_buffer;
138 ssbo_type[""] = &ShaderStorageBuffer::bind;
139 ssbo_type[""] = &ShaderStorageBuffer::clear;
140 ssbo_type[""] = &ShaderStorageBuffer::destroy;
141 ssbo_type[""] = &ShaderStorageBuffer::id;
142 ssbo_type[""] = &ShaderStorageBuffer::unbind;
143
144 // auto animation_type = lua.new_usertype<graphics::Animation>("Animation", sol::constructors<graphics::Animation()>());
145 // animation_type["current"] = &graphics::Animation::current;
146 // animation_type["frames"] = &graphics::Animation::frames;
147 // animation_type["index"] = &graphics::Animation::index;
148 // animation_type["load"] = &graphics::Animation::load;
149 // animation_type["name"] = &graphics::Animation::m_name;
150 // animation_type["speed"] = &graphics::Animation::m_speed;
151 // animation_type["next"] = &graphics::Animation::next;
152 // animation_type["prev"] = &graphics::Animation::prev;
153 // animation_type["restart"] = &graphics::Animation::restart;
155 // animation_type["total"] = &graphics::Animation::total;
156
157 // auto frame_type = lua.new_usertype<graphics::Frame>("Frame", sol::constructors<graphics::Frame()>());
158 // frame_type["bounds"] = &graphics::Frame::m_bounds;
159 // frame_type["duration"] = &graphics::Frame::m_duration;
160
161 // auto cameradata_type = lua.new_usertype<graphics::Camera::Data>("CameraData", sol::no_constructor);
162 // cameradata_type["model_view"] = &graphics::Camera::Data::m_model_view;
163 // cameradata_type["projection"] = &graphics::Camera::Data::m_projection;
164
165 // auto camera_type = lua.new_usertype<graphics::Camera>("Camera", sol::constructors<graphics::Camera()>());
166 // camera_type["get_data"] = &graphics::Camera::get_data;
167 // camera_type["get_model_view"] = &graphics::Camera::get_model_view;
168 // camera_type["get_origin"] = &graphics::Camera::get_origin;
169 // camera_type["get_pos"] = &graphics::Camera::get_pos;
170 // camera_type["get_proj"] = &graphics::Camera::get_proj;
171 // camera_type["get_rotation"] = &graphics::Camera::get_rotation;
172 // camera_type["get_scale"] = &graphics::Camera::get_scale;
173 // camera_type["get_transform"] = &graphics::Camera::get_transform;
174 // camera_type["get_viewport"] = &graphics::Camera::get_viewport;
175 // camera_type["rotation_speed"] = &graphics::Camera::m_rotation_speed;
176 // camera_type["translation_speed"] = &graphics::Camera::m_translation_speed;
177 // camera_type["allow_rotation"] = &graphics::Camera::m_allow_rotation;
178 // camera_type["reset"] = &graphics::Camera::reset;
179 // camera_type["rotate"] = &graphics::Camera::rotate;
180 // camera_type["scale"] = &graphics::Camera::scale;
181 // camera_type["set_viewport"] = &graphics::Camera::set_viewport;
182 // camera_type["set_pos"] = &graphics::Camera::set_pos;
183 // camera_type["set_rotation"] = &graphics::Camera::set_rotation;
184 // camera_type["set_origin"] = &graphics::Camera::set_origin;
185 // camera_type["set_scale_horizontal"] = &graphics::Camera::set_scale_horizontal;
186 // camera_type["set_scale_vertical"] = &graphics::Camera::set_scale_vertical;
187 // camera_type["translate"] = &graphics::Camera::translate;
188
189 // lua.set("galaxy_colour_opaque", graphics::Colour::OPAQUE);
190 // lua.set("galaxy_colour_transparent", graphics::Colour::TRANSPARENT);
191
192 // auto rendercommand_type = lua.new_usertype<graphics::RenderCommand>("RenderCommand", sol::constructors<graphics::RenderCommand()>());
193 // rendercommand_type["count"] = &graphics::RenderCommand::count;
194 // rendercommand_type["instances"] = &graphics::RenderCommand::instances;
195 // rendercommand_type["layer"] = &graphics::RenderCommand::layer;
196 // rendercommand_type["mode"] = &graphics::RenderCommand::mode;
197 // rendercommand_type["offset"] = &graphics::RenderCommand::offset;
198 // rendercommand_type["uniforms"] = &graphics::RenderCommand::uniforms;
199 // rendercommand_type["vao"] = &graphics::RenderCommand::vao;
200 // rendercommand_type["texture"] = &graphics::RenderCommand::texture;
201
202 // auto renderdata_type = lua.new_usertype<graphics::RenderData>("RenderData", sol::constructors<graphics::RenderData()>());
203 // renderdata_type["colour"] = &graphics::RenderData::colour;
204 // renderdata_type["entity"] = &graphics::RenderData::entity;
205 // renderdata_type["point"] = &graphics::RenderData::point;
206 // renderdata_type["textured"] = &graphics::RenderData::textured;
207 // renderdata_type["transform"] = &graphics::RenderData::transform;
208
209 // auto transform_type = lua.new_usertype<graphics::Transform>("Transform", sol::constructors<graphics::Transform()>());
210 // transform_type["get_origin"] = &graphics::Transform::get_origin;
211 // transform_type["get_pos"] = &graphics::Transform::get_pos;
212 // transform_type["get_rotation"] = &graphics::Transform::get_rotation;
213 // transform_type["get_scale"] = &graphics::Transform::get_scale;
214 // transform_type["get_transform"] = &graphics::Transform::get_transform;
215 // transform_type["reset"] = &graphics::Transform::reset;
216 // transform_type["rotate"] = &graphics::Transform::rotate;
217 // transform_type["scale"] = &graphics::Transform::scale;
218 // transform_type["set_origin"] = &graphics::Transform::set_origin;
219 // transform_type["set_pos"] = &graphics::Transform::set_pos;
220 // transform_type["set_rotation"] = &graphics::Transform::set_rotation;
221 // transform_type["set_scale_horizontal"] = &graphics::Transform::set_scale_horizontal;
222 // transform_type["set_scale_vertical"] = &graphics::Transform::set_scale_vertical;
223 // transform_type["translate"] = &graphics::Transform::translate;
224
226 // lua.new_enum<graphics::Text::Alignment>("TextAlignment",
227 //{
228 // {"CENTER", graphics::Text::Alignment::CENTER},
229 // {"LEFT", graphics::Text::Alignment::LEFT},
230 // {"RIGHT", graphics::Text::Alignment::RIGHT}
231 // });
233
234 // auto font_type = lua.new_usertype<graphics::Font>("Font", sol::constructors<graphics::Font()>());
235 // font_type["build"] = &graphics::Font::build;
236 // font_type["get_text_size"] = &graphics::Font::get_text_size;
237 // font_type["load_file"] = sol::resolve<bool(const std::string&)>(&graphics::Font::load);
238 // font_type["load_mem"] = sol::resolve<bool(unsigned char*, const unsigned int)>(&graphics::Font::load);
239 // font_type["vertical_advance"] = &graphics::Font::vertical_advance;
240
241 // auto fc_type = lua.new_usertype<graphics::FontContext>("Font", sol::no_constructor);
242 // fc_type["set_dpi"] = &graphics::FontContext::set_dpi;
243
244 // auto text_type = lua.new_usertype<graphics::Text>("Text", sol::constructors<graphics::Font()>());
245 // text_type["create"] = &graphics::Text::create;
246 // text_type["get_alignment"] = &graphics::Text::get_alignment;
247 // text_type["get_font"] = &graphics::Text::get_font;
248 // text_type["get_size"] = &graphics::Text::get_size;
249 // text_type["get_text"] = &graphics::Text::get_text;
250 // text_type["height"] = &graphics::Text::height;
251 // text_type["colour"] = &graphics::Text::m_colour;
252 // text_type["update"] = sol::resolve<void(std::string_view)>(&graphics::Text::update);
253 // text_type["update_size"] = sol::resolve<void(const float)>(&graphics::Text::update);
254 // text_type["update_alignment"] = sol::resolve<void(const graphics::Text::Alignment)>(&graphics::Text::update);
255 // text_type["set_font"] = &graphics::Text::set_font;
256 // text_type["width"] = &graphics::Text::width;
257
258 // auto circle_type = lua.new_usertype<graphics::Circle>("Circle", sol::constructors<graphics::Circle()>());
259 // circle_type["create"] = &graphics::Circle::create;
260 // circle_type["fragments"] = &graphics::Circle::fragments;
261 // circle_type["height"] = &graphics::Circle::height;
262 // circle_type["mode"] = &graphics::Circle::mode;
263 // circle_type["colour"] = &graphics::Circle::m_colour;
264 // circle_type["radius"] = &graphics::Circle::radius;
265 // circle_type["width"] = &graphics::Circle::width;
266
267 // auto ellipse_type = lua.new_usertype<graphics::Ellipse>("Ellipse", sol::constructors<graphics::Ellipse()>());
268 // ellipse_type["create"] = &graphics::Ellipse::create;
269 // ellipse_type["fragments"] = &graphics::Ellipse::fragments;
270 // ellipse_type["height"] = &graphics::Ellipse::height;
271 // ellipse_type["mode"] = &graphics::Ellipse::mode;
272 // ellipse_type["colour"] = &graphics::Ellipse::m_colour;
273 // ellipse_type["radii"] = &graphics::Ellipse::radii;
274 // ellipse_type["width"] = &graphics::Ellipse::width;
275
276 // auto point_type = lua.new_usertype<graphics::Point>("Point", sol::constructors<graphics::Point()>());
277 // point_type["create"] = &graphics::Point::create;
278 // point_type["height"] = &graphics::Point::height;
279 // point_type["mode"] = &graphics::Point::mode;
280 // point_type["colour"] = &graphics::Point::m_colour;
281 // point_type["pos"] = &graphics::Point::pos;
282 // point_type["width"] = &graphics::Point::width;
283
284 // auto polygon_type = lua.new_usertype<graphics::Polygon>("Polygon", sol::constructors<graphics::Polygon()>());
285 // polygon_type["create"] = &graphics::Polygon::create;
286 // polygon_type["height"] = &graphics::Polygon::height;
287 // polygon_type["mode"] = &graphics::Polygon::mode;
288 // polygon_type["colour"] = &graphics::Polygon::m_colour;
289 // polygon_type["points"] = &graphics::Polygon::points;
290 // polygon_type["width"] = &graphics::Polygon::width;
291
292 // auto polyline_type = lua.new_usertype<graphics::Polyline>("Polyline", sol::constructors<graphics::Polyline()>());
293 // polyline_type["create"] = &graphics::Polyline::create;
294 // polyline_type["height"] = &graphics::Polyline::height;
295 // polyline_type["mode"] = &graphics::Polyline::mode;
296 // polyline_type["colour"] = &graphics::Polyline::m_colour;
297 // polyline_type["points"] = &graphics::Polyline::points;
298 // polyline_type["width"] = &graphics::Polyline::width;
299 }
300} // namespace galaxy
Represents an RGBA colour.
Definition Colour.hpp:24
std::array< std::uint8_t, 4 > array() noexcept
Get as array.
Definition Colour.cpp:138
std::uint8_t a() const noexcept
Get alpha.
Definition Colour.cpp:113
glm::vec4 normalize() noexcept
Get normalized rgba vec4.
Definition Colour.cpp:126
void set_from_norm(const glm::vec4 &rgba) noexcept
Set RGBA from normalizaed values.
Definition Colour.cpp:118
std::uint8_t b() const noexcept
Get blue.
Definition Colour.cpp:108
std::uint8_t r() const noexcept
Get red.
Definition Colour.cpp:98
std::uint8_t g() const noexcept
Get green.
Definition Colour.cpp:103
static void inject_graphics() noexcept
Injects graphics module into Lua.
void bind() const
Bind buffer.
void clear() const
Clears data from buffer.
void unbind() const
Unbind buffer.
unsigned int id() const noexcept
Get OpenGL handle.
OpenGL Shader Program.
Definition Shader.hpp:28
unsigned int id() const noexcept
Get program id.
Definition Shader.cpp:221
void compile()
Compiles shader into GPU mem.
Definition Shader.cpp:113
void unbind() const
Unbind.
Definition Shader.cpp:216
bool parse(const std::string &src) noexcept
Loads a combined raw shader.
Definition Shader.cpp:83
bool load(const std::string &file) noexcept
Loads a combined shader.
Definition Shader.cpp:65
void bind() const
Make active shader.
Definition Shader.cpp:211
void destroy()
Destroys shader program.
Definition Shader.cpp:202
Bindless OpenGL 2D Texture.
Definition Texture.hpp:22
void unbind() const noexcept
Deactivate sampler.
Definition Texture.cpp:162
bool load(const std::string &file)
Load a texture from vfs.
Definition Texture.cpp:73
bool load_mem(std::span< std::uint8_t > buffer)
Loads texture from memory.
Definition Texture.cpp:81
void save(std::string_view file)
Saves texture to file on disk.
Definition Texture.cpp:119
void bind() const noexcept
Bind to sampler.
Definition Texture.cpp:157
void destroy()
Destroy texture.
Definition Texture.cpp:167
float height() const noexcept
Get texture height.
Definition Texture.cpp:193
unsigned int id() const noexcept
Get OpenGL texture id.
Definition Texture.cpp:198
void recreate()
Deletes texture data and configuration in OpenGL.
Definition Texture.cpp:182
std::uint64_t handle() const noexcept
Get OpenGL bindless handle.
Definition Texture.cpp:203
float width() const noexcept
Get texture width.
Definition Texture.cpp:188
Abstraction for OpenGL vertex array objects.
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex array.
void bind() const
Bind this vertex array.
VertexBuffer & vbo() noexcept
Get vertex buffer.
int count() const noexcept
Get the index count.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex array object.
void unbind() const
Unbind this vertex array.
unsigned int id() const noexcept
Get vertex array handle.
void * offset() noexcept
Gets index offset.
Abstraction for OpenGL vertex buffer objects.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex buffer object.
int count() const noexcept
Get the index count.
void * offset() noexcept
Gets index offset.
void clear()
Clear buffer data.
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex object.
unsigned int id() const noexcept
Get OpenGL handle.
float map_y_texel(const Type y, const float height) noexcept
Takes in a y positon texture coord and maps it to a texel.
Definition Vertex.hpp:83
std::array< Vertex, 4 > gen_quad_vertices(const float width, const float height) noexcept
Generate some default verticies.
Definition Vertex.cpp:14
std::array< unsigned int, 6 > gen_default_indices() noexcept
Generate some default indicies.
Definition Vertex.cpp:41
float map_x_texel(const Type x, const float width) noexcept
Takes in a x positon texture coord and maps it to a texel.
Definition Vertex.hpp:66
Animated.cpp galaxy.
Definition Animated.cpp:16
GLAttributeBinding
Stores the location of shader attribute binding point for the VAO.
Definition Enums.hpp:17
@ OFFSET_POINT
Instance offset data.
@ POSITION_POINT
Position data.
GLTextureFilter
Mipmap filtering.
Definition Enums.hpp:54
@ NEAREST
Nearest-neighbour.
GLDrawHint
OpenGL driver data buffer storage flags.
Definition GLEnums.hpp:19
GLBufferBinding
Stores the location of a buffer's binding point for the VAO.
Definition Enums.hpp:38
@ INSTANCE_BUFFER_POINT
Instance offsets.
@ VERTEX_BUFFER_POINT
Normal VBO.
GLTextureMode
Texture wrapping modes.
Definition GLEnums.hpp:29
@ CLAMP_TO_EDGE
GL_CLAMP_TO_EDGE.
@ MIRRORED_REPEAT
GL_MIRRORED_REPEAT.
@ CLAMP_TO_BORDER
GL_CLAMP_TO_BORDER.
Represents a single vertex point.
Definition Vertex.hpp:23
glm::vec2 m_pos
Position.
Definition Vertex.hpp:27
glm::vec2 m_texels
Texture coords (uv).
Definition Vertex.hpp:32