TerraForge3D  2.3.1
3D Terrain And Landscape Generator
Texture2D.cpp
1
2#include "Texture2D.h"
3#include <iostream>
4#define STB_IMAGE_IMPLEMENTATION
5#include <stb/stb_image.h>
6
7#define STB_IMAGE_WRITE_IMPLEMENTATION
8#include "stb/stb_image_write.h"
9
10#include <avir/avir.h>
11
12Texture2D::Texture2D(uint32_t width, uint32_t height)
13 : m_Width(width), m_Height(height)
14{
15 m_InternalFormat = GL_RGB8;
16 m_DataFormat = GL_RGB;
17 m_Data = new unsigned char[width * height * 3];
18 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
19 glGenTextures(1, &m_RendererID);
20 glBindTexture(GL_TEXTURE_2D, m_RendererID);
21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
22 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
23 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
24 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
25 glGenerateMipmap(GL_TEXTURE_2D);
26}
27
28Texture2D::Texture2D(const std::string path, bool preserveData, bool readAlpha)
29 : m_Path(path)
30{
31 int width, height, channels;
32 stbi_set_flip_vertically_on_load(0);
33 unsigned char *data = nullptr;
34
35 if(readAlpha)
36 {
37 data = stbi_load(path.c_str(), &width, &height, &channels, 0);
38 }
39
40 else
41 {
42 data = stbi_load(path.c_str(), &width, &height, &channels, 3);
43 }
44
45 if (data)
46 {
47 m_IsLoaded = true;
48 m_Width = width;
49 m_Height = height;
50 glGenTextures(1, &m_RendererID);
51 glBindTexture(GL_TEXTURE_2D, m_RendererID);
52 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
53
54 if(readAlpha)
55 {
56 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
57 }
58
59 else
60 {
61 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
62 }
63
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
68 glGenerateMipmap(GL_TEXTURE_2D);
69
70 if (preserveData)
71 {
72 m_Data = data;
73 }
74
75 else
76 {
77 stbi_image_free(data);
78 }
79 }
80
81 else
82 {
83 std::cout << "Failed to load texture : " << path << std::endl;
84 }
85}
86
87Texture2D::~Texture2D()
88{
89 if (m_Data)
90 {
91 glDeleteTextures(1, &m_RendererID);
92 }
93}
94
95void Texture2D::SetData(void *data, uint32_t size, bool alpha)
96{
97 glBindTexture(GL_TEXTURE_2D, m_RendererID);
98
99 if(alpha)
100 {
101 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
102 }
103
104 else
105 {
106 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, m_Width, m_Height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
107 }
108
109 glGenerateMipmap(GL_TEXTURE_2D);
110}
111
112void Texture2D::DeleteData()
113{
114 if (m_Data)
115 {
116 stbi_image_free(m_Data);
117 }
118}
119
120void Texture2D::Bind(uint32_t slot) const
121{
122 glActiveTexture(GL_TEXTURE0 + slot);
123 glBindTexture(GL_TEXTURE_2D, m_RendererID);
124}
125
126void Texture2D::Resize(int width, int height, bool resetOpenGL)
127{
128 if (m_Width == width && m_Height == height)
129 {
130 return;
131 }
132
133 if (!m_Data)
134 {
135 return;
136 }
137
138 unsigned char *data = (unsigned char *)malloc(width * height * 3 * sizeof(unsigned char));
139 memset(data, 0, width * height * 3 * sizeof(unsigned char));
140 avir::CImageResizer<> ImageResizer(8);
141 ImageResizer.resizeImage(m_Data, m_Width, m_Height, 0, data, width, height, 3, 0);
142
143 if (data)
144 {
145 if (m_Data)
146 {
147 delete m_Data;
148 }
149
150 m_Data = data;
151 m_Width = width;
152 m_Height = height;
153
154 if (resetOpenGL)
155 {
156 glBindTexture(GL_TEXTURE_2D, m_RendererID);
157 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, m_Width, m_Height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
158 glGenerateMipmap(GL_TEXTURE_2D);
159 }
160 }
161}
162
163unsigned char *Texture2D::GetData()
164{
165 if (m_Data)
166 {
167 return m_Data;
168 }
169
170 else
171 {
172 return nullptr;
173 }
174}