texture loading

main
Brett 2024-01-01 22:15:30 -05:00
parent 8c86faf1ce
commit e37fc6b321
7 changed files with 75 additions and 49 deletions

View File

@ -19,10 +19,29 @@
#ifndef BLT_WITH_GRAPHICS_BATCH_2D_RENDERER_H #ifndef BLT_WITH_GRAPHICS_BATCH_2D_RENDERER_H
#define BLT_WITH_GRAPHICS_BATCH_2D_RENDERER_H #define BLT_WITH_GRAPHICS_BATCH_2D_RENDERER_H
#include "blt/gfx/model.h"
#include "blt/gfx/shader.h"
namespace blt::gfx namespace blt::gfx
{ {
struct rectangle
{
float x, y, width, height;
};
class batch_renderer_2d
{
private:
vertex_array* square_vao;
shader_t* shader;
public:
/**
* @param use_view_matrix should we use the view matrix when rendering? Defaults to false as the assumption is the view matrix is 3d
*/
void create(bool use_view_matrix = false);
void cleanup();
};
} }

View File

@ -16,8 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef BLT_WITH_GRAPHICS_TEXTURE_MANAGER_H #ifndef BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H
#define BLT_WITH_GRAPHICS_TEXTURE_MANAGER_H #define BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H
#include <blt/std/hashmap.h> #include <blt/std/hashmap.h>
#include <blt/gfx/texture.h> #include <blt/gfx/texture.h>
@ -38,7 +38,7 @@ namespace blt::gfx
{} {}
}; };
class texture_resource_manager class resource_manager
{ {
private: private:
std::mutex load_lock; std::mutex load_lock;
@ -47,15 +47,20 @@ namespace blt::gfx
std::vector<texture_file*> loaded_textures; std::vector<texture_file*> loaded_textures;
HASHMAP<std::string, texture_gl2D*> textures_2d; HASHMAP<std::string, texture_gl2D*> textures_2d;
public: public:
texture_resource_manager() = default; resource_manager() = default;
void enqueue(std::string path, std::string name = ""); void enqueue(std::string path, std::string name = "");
void load_resources(std::size_t threads = 8); void load_resources(std::size_t threads = 8);
void delete_resources(); inline texture_gl2D* get(const std::string& name)
{
return textures_2d[name];
}
void cleanup();
}; };
} }
#endif //BLT_WITH_GRAPHICS_TEXTURE_MANAGER_H #endif //BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H

@ -1 +1 @@
Subproject commit d913e959658d9194be031b356bf815d766e06d80 Subproject commit d882b76d83749273b05e71fb0361d8778cc6ad94

View File

@ -17,7 +17,28 @@
*/ */
#include <blt/gfx/renderer/batch_2d_renderer.h> #include <blt/gfx/renderer/batch_2d_renderer.h>
float square_vertices[20] = {
// positions // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
};
const unsigned int square_indices[6] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
namespace blt::gfx namespace blt::gfx
{ {
void batch_renderer_2d::create(bool use_view_matrix)
{
}
void batch_renderer_2d::cleanup()
{
}
} }

View File

@ -15,39 +15,41 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <blt/gfx/renderer/texture_manager.h> #include <blt/gfx/renderer/resource_manager.h>
#include <blt/std/logging.h> #include <blt/std/logging.h>
namespace blt::gfx namespace blt::gfx
{ {
void texture_resource_manager::delete_resources() void resource_manager::cleanup()
{ {
for (const auto& p : textures_2d) for (const auto& p : textures_2d)
delete p.second; delete p.second;
} }
void texture_resource_manager::enqueue(std::string path, std::string name) void resource_manager::enqueue(std::string path, std::string name)
{ {
textures_to_load.emplace_back(std::move(path), std::move(name)); textures_to_load.emplace_back(std::move(path), std::move(name));
} }
void texture_resource_manager::load_resources(std::size_t threads) void resource_manager::load_resources(std::size_t threads)
{ {
blt::thread_pool<false> pool; blt::thread_pool<false> pool{threads, [&]() {
pool.execute([&]() {
loadable_texture texture; loadable_texture texture;
{ {
std::scoped_lock lock(load_lock); std::scoped_lock lock(load_lock);
if (textures_to_load.empty())
return;
texture = textures_to_load.back(); texture = textures_to_load.back();
textures_to_load.pop_back(); textures_to_load.pop_back();
} }
BLT_DEBUG("Loading texture file %s", texture.path.c_str());
auto* file = new texture_file(texture.path, texture.name); auto* file = new texture_file(texture.path, texture.name);
{ {
std::scoped_lock lock(save_lock); std::scoped_lock lock(save_lock);
loaded_textures.push_back(file); loaded_textures.push_back(file);
} }
}); }};
do do
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
@ -62,7 +64,7 @@ namespace blt::gfx
{ {
auto* back = loaded_textures.back(); auto* back = loaded_textures.back();
textures_2d.insert({back->getName(), new texture_gl2D(back->texture())}); textures_2d.insert({back->getName(), new texture_gl2D(back->texture())});
BLT_DEBUG("Loaded Texture %s", back->getName().c_str()); BLT_DEBUG("Loaded Texture '%s'", back->getName().c_str());
delete back; delete back;
loaded_textures.pop_back(); loaded_textures.pop_back();
} }
@ -74,6 +76,7 @@ namespace blt::gfx
{ {
auto* back = loaded_textures.back(); auto* back = loaded_textures.back();
textures_2d.insert({back->getName(), new texture_gl2D(back->texture())}); textures_2d.insert({back->getName(), new texture_gl2D(back->texture())});
BLT_DEBUG("Loaded texture '%s'", back->getName().c_str());
loaded_textures.pop_back(); loaded_textures.pop_back();
} }
} }

View File

@ -23,7 +23,7 @@ uniform mat4 model;
void main() { void main() {
//gl_Position = projection * view * vec4(vertex.x, vertex.y, vertex.z, 1.0); //gl_Position = projection * view * vec4(vertex.x, vertex.y, vertex.z, 1.0);
gl_Position = ovm * model * vec4(vertex, 1.0); gl_Position = ortho * model * vec4(vertex, 1.0);
pos = vertex.xy; pos = vertex.xy;
uv = uv_in; uv = uv_in;
} }

View File

@ -9,36 +9,13 @@
#include <shaders/test.vert> #include <shaders/test.vert>
#include <shaders/test.frag> #include <shaders/test.frag>
#include "blt/gfx/renderer/resource_manager.h"
const float raw_vertices[18] = {
// first triangle
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, 0.5f, 0.0f, // top left
// second triangle
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
float vertices[20] = {
// positions // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
};
const unsigned int indices[6] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
blt::gfx::vertex_array* vao; blt::gfx::vertex_array* vao;
blt::gfx::shader_t* shader; blt::gfx::shader_t* shader;
blt::gfx::texture_gl2D* texture;
blt::gfx::texture_gl2D* parker_texture;
blt::gfx::matrix_state_manager global_matrices; blt::gfx::matrix_state_manager global_matrices;
blt::gfx::resource_manager resources;
float x = 0, y = 0, z = 0; float x = 0, y = 0, z = 0;
float bx = 500, by = 500; float bx = 500, by = 500;
float mx = 0, my = -9.8; float mx = 0, my = -9.8;
@ -104,10 +81,11 @@ void init()
shader->bindAttribute(0, "vertex"); shader->bindAttribute(0, "vertex");
shader->bindAttribute(1, "uv_in"); shader->bindAttribute(1, "uv_in");
texture = new texture_gl2D(texture_file("../resources/textures/cumdollar.jpg").texture()); resources.enqueue("../resources/textures/cumdollar.jpg", "ibuythat");
parker_texture = new texture_gl2D(texture_file("../resources/textures/dfoedbi-28157978-1555-45c3-b2f4-d5e5fe25b253.png").texture()); resources.enqueue("../resources/textures/dfoedbi-28157978-1555-45c3-b2f4-d5e5fe25b253.png", "niko");
global_matrices.create_internals(); global_matrices.create_internals();
resources.load_resources();
} }
void update(std::int32_t width, std::int32_t height) void update(std::int32_t width, std::int32_t height)
@ -128,10 +106,10 @@ void update(std::int32_t width, std::int32_t height)
const float w = 120, h = 120, cf = 30, rf = 15, crf = 10; const float w = 120, h = 120, cf = 30, rf = 15, crf = 10;
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
parker_texture->bind(); resources.get("ibuythat")->bind();
draw(width / 2.0, height / 2.0, width, height, 90); draw(width / 2.0, height / 2.0, width, height, 90);
texture->bind(); resources.get("niko")->bind();
draw(bx, by, w, h, 0); draw(bx, by, w, h, 0);
bx += mx * blt::gfx::getFrameDeltaSeconds() * cf; bx += mx * blt::gfx::getFrameDeltaSeconds() * cf;
@ -160,8 +138,8 @@ int main()
blt::gfx::init(blt::gfx::window_data{"My Sexy Window", init, update}.setSyncInterval(1)); blt::gfx::init(blt::gfx::window_data{"My Sexy Window", init, update}.setSyncInterval(1));
delete vao; delete vao;
delete shader; delete shader;
delete texture;
global_matrices.cleanup(); global_matrices.cleanup();
resources.cleanup();
blt::gfx::cleanup(); blt::gfx::cleanup();
return 0; return 0;
} }