-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtexture.cpp
More file actions
167 lines (139 loc) · 5.87 KB
/
texture.cpp
File metadata and controls
167 lines (139 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <fea/rendering/texture.hpp>
#include <fea/assert.hpp>
#include <utility>
#include <string>
namespace fea
{
Texture::Texture() : mId(0), mSize(0, 0), mSmooth(false)
{
}
Texture::Texture(Texture&& other) : mId(0), mSize(0, 0), mSmooth(false)
{
std::swap(mId, other.mId);
std::swap(mSize, other.mSize);
std::swap(mSmooth, other.mSmooth);
mPixelData = std::move(other.mPixelData);
}
Texture& Texture::operator=(Texture&& other)
{
std::swap(mId, other.mId);
std::swap(mSize, other.mSize);
std::swap(mSmooth, other.mSmooth);
mPixelData = std::move(other.mPixelData);
return *this;
}
GLuint Texture::getId() const
{
return mId;
}
void Texture::create(const glm::ivec2& size, const uint8_t* imageData, bool smooth)
{
FEA_ASSERT(size.x > 0 && size.y > 0, "Cannot create a texture with a width or height smaller than zero! Given dimensions are " + std::to_string(size.x) + " " + std::to_string(size.y));
if(mId)
{
destroy();
}
mSize = size;
mSmooth = smooth;
glGenTextures(1, &mId);
FEA_ASSERT(mId != 0, "Failed to create texture. Make sure there is a valid OpenGL context available!");
glBindTexture(GL_TEXTURE_2D, mId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)size.x, (GLsizei)size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, smooth ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, smooth ? GL_LINEAR : GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
uint32_t byteAmount = mSize.x * mSize.y * 4;
mPixelData = std::vector<uint8_t>(byteAmount);
if(imageData)
std::copy(imageData, imageData + byteAmount, mPixelData.data());
}
void Texture::create(const glm::ivec2& size, const Color& color, bool smooth)
{
FEA_ASSERT(size.x > 0 && size.y > 0, "Cannot create a texture with a width or height smaller than zero! Given dimensions are " + std::to_string(size.x) + " " + std::to_string(size.y));
std::vector<uint8_t> pixels(size.x * size.y * 4);
for(uint32_t x = 0; x < size.x; x++)
{
for(uint32_t y = 0; y < size.y; y++)
{
pixels[(x + y * size.x) * 4 + 0] = color.r();
pixels[(x + y * size.x) * 4 + 1] = color.g();
pixels[(x + y * size.x) * 4 + 2] = color.b();
pixels[(x + y * size.x) * 4 + 3] = color.a();
}
}
create(size, pixels.data(), smooth);
}
const glm::ivec2& Texture::getSize() const
{
return mSize;
}
void Texture::resize(const glm::ivec2& newSize)
{
FEA_ASSERT(newSize.x > 0 && newSize.y > 0, "Size must be bigger than zero in both dimensions! Given size is " << newSize.x << " " << newSize.y);
std::vector<uint8_t> newPixels(newSize.x * newSize.y * 4, 0);
int32_t newWidth = newSize.x;
int32_t currentWidth = mSize.x;
for(uint32_t x = 0; x < mSize.x && x < newSize.x; x++)
{
for(uint32_t y = 0; y < mSize.y < newSize.y; y++)
{
newPixels[(x + y * newWidth) * 4 + 0] = mPixelData[(x + y * currentWidth) * 4 + 0];
newPixels[(x + y * newWidth) * 4 + 1] = mPixelData[(x + y * currentWidth) * 4 + 1];
newPixels[(x + y * newWidth) * 4 + 2] = mPixelData[(x + y * currentWidth) * 4 + 2];
newPixels[(x + y * newWidth) * 4 + 3] = mPixelData[(x + y * currentWidth) * 4 + 3];
}
}
create(newSize, newPixels.data(), mSmooth);
}
void Texture::destroy()
{
if(mId)
{
glDeleteTextures(1, &mId);
mId = 0;
mSize = {0, 0};
mSmooth = false;
mPixelData = std::vector<uint8_t>();
}
}
void Texture::setPixel(const glm::ivec2& pixel, const Color& color)
{
FEA_ASSERT(pixel.x >= 0 && pixel.y >= 0 && pixel.x < mSize.x && pixel.y < mSize.y, "Trying to set pixel outside of the bounds of the texture. Accessing at " + std::to_string(pixel.x) + " " + std::to_string(pixel.y) + " and texture dimensions are " + std::to_string(mSize.x) + " " + std::to_string(mSize.y));
uint32_t pixelIndex = (pixel.x + pixel.y * mSize.x) * 4;
mPixelData[pixelIndex ] = color.r();
mPixelData[pixelIndex + 1] = color.g();
mPixelData[pixelIndex + 2] = color.b();
mPixelData[pixelIndex + 3] = color.a();
}
Color Texture::getPixel(const glm::ivec2& pixel) const
{
FEA_ASSERT(pixel.x >= 0 && pixel.y >= 0 && pixel.x < mSize.x && pixel.y < mSize.y, "Trying to get pixel outside of the bounds of the texture. Accessing at " + std::to_string(pixel.x) + " " + std::to_string(pixel.y) + " and texture dimensions are " + std::to_string(mSize.x) + " " + std::to_string(mSize.y));
uint32_t pixelIndex = (pixel.x + pixel.y * mSize.x) * 4;
return Color(mPixelData[pixelIndex],
mPixelData[pixelIndex + 1],
mPixelData[pixelIndex + 2],
mPixelData[pixelIndex + 3]);
}
uint8_t* Texture::getPixelData()
{
return mPixelData.data();
}
const uint8_t* Texture::getPixelData() const
{
return mPixelData.data();
}
void Texture::update()
{
glBindTexture(GL_TEXTURE_2D, mId);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mSize.x, mSize.y, GL_RGBA, GL_UNSIGNED_BYTE, mPixelData.data());
}
Texture::~Texture()
{
if(mId)
{
destroy();
}
}
}