sculpto
gl_texture.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file gl_texture.cpp
3 * \brief OpenGL texture class implementation module.
4 *
5 * \author Sabitov Kirill
6 * \date 28 June 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10#include "gl_texture.h"
11
12void scl::gl_texture_2d::CreateColor(const image &Image, bool IsFloatingPoint)
13{
14 // Generate texture primitive
15 glGenTextures(1, &Id);
16 SCL_CORE_ASSERT(Id != 0, "Error in creation OpenGL texture primitive.");
17 glBindTexture(GL_TEXTURE_2D, Id);
18
19 // Create texture pixels storage
20 int w = Image.GetWidth(), h = Image.GetHeight();
21 int c = Image.GetComponentsCount();
22 GLenum internal_format = IsFloatingPoint ?
23 (c == 3 ? GL_RGB16F : c == 4 ? GL_RGBA16F : GL_R16F) :
24 (c == 3 ? GL_RGB8 : c == 4 ? GL_RGBA8 : GL_R8 );
25 GLenum format = c == 3 ? GL_RGB : c == 4 ? GL_RGBA : GL_RED;
26 GLenum type = IsFloatingPoint ? GL_FLOAT : GL_UNSIGNED_BYTE;
27 glTexStorage2D(GL_TEXTURE_2D, 1, internal_format, w, h);
28 if (Image.GetRawData() != nullptr)
29 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, format, type, Image.GetRawData());
30 glGenerateMipmap(GL_TEXTURE_2D);
31
32 // Configure texture sampling.
33 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
34 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
35 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
37
38 glBindTexture(GL_TEXTURE_2D, 0);
39}
40
41void scl::gl_texture_2d::CreateDepth(const image &Image)
42{
43 glCreateTextures(GL_TEXTURE_2D, 1, &Id);
44 glBindTexture(GL_TEXTURE_2D, Id);
45 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, Image.GetWidth(), Image.GetHeight(),
46 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
47
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
52 float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
53 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
54}
55
57{
58 this->Width = Image.GetWidth();
59 this->Height = Image.GetHeight();
60
61 switch (Type)
62 {
63 case scl::texture_type::COLOR: this->CreateColor(Image, false); SCL_CORE_SUCCES("OpenGL Color Texture with id {} created.", Id); return;
64 case scl::texture_type::COLOR_FLOATING_POINT: this->CreateColor(Image, true); SCL_CORE_SUCCES("OpenGL HDR Texture with id {} created.", Id); return;
65 case scl::texture_type::DEPTH: this->CreateDepth(Image); SCL_CORE_SUCCES("OpenGL Depth Texture with id {} created.", Id); return;
66 }
67
68 SCL_CORE_ASSERT(0, "Unknown texture type.");
69}
70
72{
73 Free();
74}
75
77{
78 if (Id == 0) return;
79
80 this->Slot = Slot;
81 glActiveTexture(GL_TEXTURE0 + Slot);
82 glBindTexture(GL_TEXTURE_2D, Id);
83}
84
86{
87 glActiveTexture(GL_TEXTURE0 + this->Slot);
88 glBindTexture(GL_TEXTURE_2D, 0);
89}
90
92{
93 if (Id == 0) return;
94
95 glDeleteTextures(1, &Id);
96 Id = 0;
97}
98
100{
101 // TODO: Load image raw data from GPU and create image container from it.
102 return image();
103}
#define SCL_CORE_ASSERT(expr,...)
Definition: assert.h:69
image GetImage() override
Definition: gl_texture.cpp:99
void Unbind() const override
Definition: gl_texture.cpp:85
gl_texture_2d(const image &Image, texture_type Type)
Definition: gl_texture.cpp:56
~gl_texture_2d() override
Definition: gl_texture.cpp:71
void Bind(u32 Slot) const override
Definition: gl_texture.cpp:76
void Free() override
Definition: gl_texture.cpp:91
int GetWidth() const
Definition: image.h:27
int GetHeight() const
Definition: image.h:29
OpenGL texture class definition module.
#define SCL_CORE_SUCCES(...)
Definition: log.h:42
uint32_t u32
Definition: math_common.h:21
texture_type
Definition: texture.h:18
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.