NoJobEngine 2.0.7
Loading...
Searching...
No Matches
OpenGLTexture.cpp
Go to the documentation of this file.
2
3#include <glad/gl.h>
4#include <stb_image.h>
5
6#include <stdexcept>
7#include <vector>
8
9namespace NoJob
10{
11 namespace
12 {
13 void ConfigureTexture(std::uint32_t id)
14 {
15 glTextureParameteri(id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
16 glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
17 glTextureParameteri(id, GL_TEXTURE_WRAP_S, GL_REPEAT);
18 glTextureParameteri(id, GL_TEXTURE_WRAP_T, GL_REPEAT);
19 }
20 }
21
23 std::uint32_t width, std::uint32_t height)
24 {
25 std::vector<std::uint8_t> pixels(width * height * 4);
26 constexpr std::uint32_t cellSize = 32;
27
28 for (std::uint32_t y = 0; y < height; ++y)
29 {
30 for (std::uint32_t x = 0; x < width; ++x)
31 {
32 const bool light =
33 ((x / cellSize) + (y / cellSize)) % 2 == 0;
34 const std::uint8_t c = light ? 230 : 55;
35 const std::size_t i = (y * width + x) * 4;
36
37 pixels[i + 0] = c;
38 pixels[i + 1] = c;
39 pixels[i + 2] = c;
40 pixels[i + 3] = 255;
41 }
42 }
43
44 glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
45 glTextureStorage2D(m_RendererID, 1, GL_RGBA8, width, height);
46 glTextureSubImage2D(
47 m_RendererID, 0, 0, 0, width, height,
48 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
49
50 glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
51 glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
52 glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT);
53 glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT);
54 }
55
56 OpenGLTexture2D::OpenGLTexture2D(const std::string& path)
57 : m_Path(path)
58 {
59 stbi_set_flip_vertically_on_load(1);
60
61 int width = 0;
62 int height = 0;
63 int channels = 0;
64
65 stbi_uc* data = stbi_load(
66 path.c_str(),
67 &width,
68 &height,
69 &channels,
70 STBI_rgb_alpha);
71
72 if (!data || width <= 0 || height <= 0)
73 {
74 const std::string reason =
75 stbi_failure_reason() ? stbi_failure_reason() : "unknown error";
76 stbi_image_free(data);
77 throw std::runtime_error(
78 "NoJobEngine: could not load texture '" +
79 path + "': " + reason);
80 }
81
82 glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
83
84 int levels = 1;
85 int size = width > height ? width : height;
86 while (size > 1)
87 {
88 size /= 2;
89 ++levels;
90 }
91
92 glTextureStorage2D(
93 m_RendererID,
94 levels,
95 GL_RGBA8,
96 width,
97 height);
98
99 glTextureSubImage2D(
100 m_RendererID,
101 0,
102 0,
103 0,
104 width,
105 height,
106 GL_RGBA,
107 GL_UNSIGNED_BYTE,
108 data);
109
110 ConfigureTexture(m_RendererID);
111 glGenerateTextureMipmap(m_RendererID);
112
113 stbi_image_free(data);
114 }
115
117 {
118 glDeleteTextures(1, &m_RendererID);
119 }
120
121 void OpenGLTexture2D::Bind(std::uint32_t slot) const
122 {
123 glBindTextureUnit(slot, m_RendererID);
124 }
125}
OpenGLTexture declarations for the OpenGL Backend subsystem.
void Bind(std::uint32_t slot=0) const override
OpenGLTexture2D(std::uint32_t width, std::uint32_t height)