CoriEngine
Loading...
Searching...
No Matches
GL_Texture.cpp
Go to the documentation of this file.
1#include "GL_Texture.hpp"
2#include <glad/gl.h>
3
4namespace Cori {
5 namespace Graphics {
6 namespace Internal {
7 void OpenGLTexture2D::Upload(const void* pixelData, const uint32_t width, const uint32_t height, const Params& params) {
9 m_Width = width;
10 m_Height = height;
11 m_HasSemiTransparency = params.m_HasSemiTransparency;
13
14 GLint previousAlignment = 0;
15
16 if (params.m_UnpackAlignment != 0) {
17 glGetIntegerv(GL_UNPACK_ALIGNMENT, &previousAlignment);
18 glPixelStorei(GL_UNPACK_ALIGNMENT, params.m_UnpackAlignment);
19 }
20
21 glCreateTextures(GL_TEXTURE_2D, 1, &m_ID);
22
23 switch (params.m_PixelFormat) {
24 case RGBA8888:
25 glTextureStorage2D(m_ID, 1, GL_RGBA8, static_cast<GLsizei>(m_Width), static_cast<GLsizei>(m_Height));
26 break;
27 case RGB888:
28 glTextureStorage2D(m_ID, 1, GL_RGB8, static_cast<GLsizei>(m_Width), static_cast<GLsizei>(m_Height));
29 m_HasSemiTransparency = false;
30 break;
31 }
32
33 switch (params.m_Filter) {
34 case LINEAR:
35 glTextureParameteri(m_ID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
36 glTextureParameteri(m_ID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
37 break;
38 case NEAREST:
39 glTextureParameteri(m_ID, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
40 glTextureParameteri(m_ID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
41 break;
42 }
43
44 switch (params.m_WrapMode) {
45 case CLAMP_TO_EDGE:
46 glTextureParameteri(m_ID, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
47 glTextureParameteri(m_ID, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
48 break;
49 case CLAMP_TO_BORDER:
50 glTextureParameteri(m_ID, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
51 glTextureParameteri(m_ID, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
52 break;
53 case REPEAT:
54 glTextureParameteri(m_ID, GL_TEXTURE_WRAP_S, GL_REPEAT);
55 glTextureParameteri(m_ID, GL_TEXTURE_WRAP_T, GL_REPEAT);
56 break;
57 }
58
59 switch (params.m_PixelFormat) {
60 case RGBA8888:
61 glTextureSubImage2D(m_ID, 0, 0, 0, static_cast<GLsizei>(m_Width), static_cast<GLsizei>(m_Height), GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
62 break;
63 case RGB888:
64 glTextureSubImage2D(m_ID, 0, 0, 0, static_cast<GLsizei>(m_Width), static_cast<GLsizei>(m_Height), GL_RGB, GL_UNSIGNED_BYTE, pixelData);
65 break;
66 }
67
68 if (previousAlignment != 0) {
69 glPixelStorei(GL_UNPACK_ALIGNMENT, previousAlignment);
70 }
71
73 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Graphics::Self , Logger::Tags::Graphics::OpenGL, Logger::Tags::Graphics::Texture2D }, "(GL_RuntimeID; {}): Successfully created texture from preloaded image.", m_ID);
74 }
75
79
82 glDeleteTextures(1, &m_ID);
83 }
84
85 void OpenGLTexture2D::Bind(const uint32_t slot) const {
87 glBindTextureUnit(slot, m_ID);
88 }
89
91 return m_HasSemiTransparency;
92 }
93 }
94 }
95}
#define CORI_CORE_DEBUG_TAGGED(...)
Definition Logger.hpp:1026
#define CORI_PROFILE_FUNCTION()
Definition Profiler.hpp:9
void Bind(uint32_t slot) const override
Binds the texture to the slot.
void Upload(const void *pixelData, const uint32_t width, const uint32_t height, const Params &params) override
Definition GL_Texture.cpp:7
bool HasSemiTransparency() const override
Checks if texture has semi transparency.
Almost everything connected to graphics is in this namespace.
Definition Window.hpp:7
Global engine namespace.
Params to use when creating a texture.
Definition Texture.hpp:45
int32_t m_UnpackAlignment
Generally you want to leave this by default if you're creating a texture from an image,...
Definition Texture.hpp:53
bool m_HasSemiTransparency
Whether or no to flag the texture as semi transparent.
Definition Texture.hpp:58
static constexpr char OpenGL[]
Definition Logger.hpp:49
static constexpr char Texture2D[]
Definition Logger.hpp:60
static constexpr char Self[]
Definition Logger.hpp:47