Elevate Engine 1
Loading...
Searching...
No Matches
OpenGLTexture.cpp
Go to the documentation of this file.
1#include "eepch.h"
2
3#include "OpenGLTexture.h"
4
6
7#define STB_IMAGE_IMPLEMENTATION
8#include "stb/stb_image.h"
10
12
13namespace Elevate
14{
16 switch (format) {
17 case TextureFormat::GRAYSCALE: return GL_R8; // 8-bit single channel
18 case TextureFormat::RGB: return GL_RGB8; // 8-bit RGB
19 case TextureFormat::RGBA: return GL_RGBA8; // 8-bit RGBA
20 case TextureFormat::DEPTH: return GL_DEPTH_COMPONENT24;
21 default: return GL_RGBA8;
22 }
23 }
24
25 constexpr GLenum ToOpenGL(TextureFormat format) {
26 switch (format) {
27 case TextureFormat::EMPTY: return GL_NONE;
28 case TextureFormat::GRAYSCALE: return GL_RED;
29 case TextureFormat::RGB: return GL_RGB;
30 case TextureFormat::RGBA: return GL_RGBA;
31 case TextureFormat::DEPTH: return GL_DEPTH_COMPONENT;
32 //case TextureFormat::DEPTH16: return GL_DEPTH_COMPONENT16;
33 //case TextureFormat::DEPTH24: return GL_DEPTH_COMPONENT24;
34 //case TextureFormat::DEPTH32F: return GL_DEPTH_COMPONENT32F;
35 //case TextureFormat::DEPTH24_STENCIL8: return GL_DEPTH24_STENCIL8;
36 default: return GL_NONE;
37 }
38 }
39
40 constexpr GLenum ToOpenGLType(TextureFormat format) {
41 switch (format) {
42 case TextureFormat::DEPTH: return GL_FLOAT;
43 default: return GL_UNSIGNED_BYTE;
44 }
45 }
46
47 constexpr GLenum ToOpenGL(TextureFilter filter) {
48 switch (filter) {
49 case TextureFilter::Nearest: return GL_NEAREST;
50 case TextureFilter::Linear: return GL_LINEAR;
51 default: return GL_NEAREST;
52 }
53 }
54
55 constexpr GLenum ToOpenGL(TextureWrap wrap) {
56 switch (wrap) {
57 case TextureWrap::Repeat: return GL_REPEAT;
58 case TextureWrap::MirrorRepeat: return GL_MIRRORED_REPEAT;
59 case TextureWrap::ClampToEdge: return GL_CLAMP_TO_EDGE;
60 case TextureWrap::ClampToBorder:return GL_CLAMP_TO_BORDER;
61 default: return GL_REPEAT;
62 }
63 }
64
65 constexpr GLenum ToOpenGL(TextureType type) {
66 switch (type) {
67 case TextureType::Cubemap: return GL_TEXTURE_CUBE_MAP;
68 default: return GL_TEXTURE_2D;
69 }
70 }
71
73 : Texture(meta)
74 {
75 // todo get parameters for the textures
76 GLCheck(glGenTextures(1, &m_textureID));
77
78 Bind();
79 SetDataImpl(data);
80 }
81
82 void OpenGLTexture::Bind(uint32_t index)
83 {
84 GLCheck(glActiveTexture(GL_TEXTURE0 + index));
85 GLCheck(glBindTexture(ToOpenGL(m_meta.Usage), m_textureID));
86 }
87
88 void OpenGLTexture::Unbind()
89 {
90 GLCheck(glBindTexture(ToOpenGL(m_meta.Usage), 0));
91 }
92
93 void OpenGLTexture::SetDataImpl(unsigned char* data)
94 {
95 Bind();
96
97 // set the texture wrapping parameters
98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, ToOpenGL(m_meta.WrapS));
99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, ToOpenGL(m_meta.WrapT));
100 // set texture filtering parameters
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, ToOpenGL(m_meta.MinFilter));
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, ToOpenGL(m_meta.MagFilter));
103
104 // Swizzle if there is only a single channnel
105#ifdef EE_SUPPORTS_DSA
107 GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ONE };
108 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
109 }
110#endif
111
112 EE_CORE_INFO("Creating Texture: ID={}, Size={}x{}, Format={}", m_textureID, m_meta.Width, m_meta.Height, (int)m_meta.Format);
113
115 {
116 GLCheck(glTexImage2D(GL_TEXTURE_2D, 0, ToInternalFormat(m_meta.Format), m_meta.Width, m_meta.Height, 0, ToOpenGL(m_meta.Format), ToOpenGLType(m_meta.Format), data));
117
118 if (m_meta.Mipmaps)
119 GLCheck(glGenerateMipmap(GL_TEXTURE_2D));
120 }
121 }
122
124 {
125 return reinterpret_cast<void*>((intptr_t)m_textureID);
126 }
127}
#define GLCheck(x)
Definition GLDebug.h:11
virtual void SetDataImpl(unsigned char *data) override
OpenGLTexture(unsigned char *data, TextureMetadata &meta)
virtual void * GetNativeHandle() const override
TextureMetadata m_meta
Definition Texture.h:133
constexpr GLenum ToOpenGL(TextureFormat format)
TextureFormat
Definition Texture.h:21
TextureWrap
Definition Texture.h:50
unsigned int GLenum
TextureType
Definition Texture.h:29
constexpr GLenum ToInternalFormat(TextureFormat format)
TextureFilter
Definition Texture.h:49
constexpr GLenum ToOpenGLType(TextureFormat format)
TextureSource Source
Definition Texture.h:60
TextureFilter MagFilter
Definition Texture.h:65
TextureFilter MinFilter
Definition Texture.h:64
TextureFormat Format
Definition Texture.h:58