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
|
#include "texture.h"
#include <stdexcept>
#include "renderer.h"
#include "util.h"
// include stb_image
#define STBI_ONLY_PNG
#define STBI_ONLY_BMP
#include "stb_image.h"
Texture::Texture(
int width,
int height) :
width_(width),
height_(height)
{
if (!Renderer::isSingletonInitialized())
{
throw std::logic_error("Renderer needs to be initialized");
}
glBindTexture(GL_TEXTURE_2D, texture_.getId());
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
width_,
height_,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
}
Texture::Texture(const char* filename)
{
if (!Renderer::isSingletonInitialized())
{
throw std::logic_error("Renderer needs to be initialized");
}
glBindTexture(GL_TEXTURE_2D, texture_.getId());
unsigned char* data = stbi_load(filename, &width_, &height_, 0, 4);
flipImageData(data, width_, height_, 4);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
width_,
height_,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
data);
stbi_image_free(data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
Texture::Texture(
const Texture& tex) :
width_(tex.width_),
height_(tex.height_)
{
if (!Renderer::isSingletonInitialized())
{
throw std::logic_error("Renderer needs to be initialized");
}
unsigned char* data = new unsigned char[4 * width_ * height_];
glBindTexture(GL_TEXTURE_2D, tex.getId());
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glBindTexture(GL_TEXTURE_2D, texture_.getId());
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
width_,
height_,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
delete[] data;
}
Texture::Texture(Texture&& tex) : Texture(0, 0)
{
swap(*this, tex);
}
Texture& Texture::operator= (Texture tex)
{
swap(*this, tex);
return *this;
}
void swap(Texture& tex1, Texture& tex2)
{
std::swap(tex1.width_, tex2.width_);
std::swap(tex1.height_, tex2.height_);
std::swap(tex1.texture_, tex2.texture_);
}
Rectangle Texture::entirety() const
{
return {0, 0, width_, height_};
}
|