-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTexture.cpp
executable file
·107 lines (79 loc) · 2.66 KB
/
Texture.cpp
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
#include "Texture.h"
#include <iostream>
#include <cassert>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
namespace g3d{
TextureBuilder::TextureBuilder()
: texture{new Texture()}, loadedFromMemory{false},
generateMipMaps{false}, wrap{GL_REPEAT}, filter{GL_LINEAR} {}
TextureBuilder::~TextureBuilder() {}
TextureBuilder &TextureBuilder::setWrap(GLint value) {
wrap = value;
return *this;
}
TextureBuilder &TextureBuilder::setFilter(GLint value) {
filter = value;
return *this;
}
TextureBuilder &TextureBuilder::generateMipmaps(bool value) {
generateMipMaps = value;
return *this;
}
TextureBuilder &TextureBuilder::fromFile(std::string fileName) {
stbi_set_flip_vertically_on_load(GL_TRUE);
int components;
raw = stbi_load(fileName.c_str(), &w, &h, &components, STBI_rgb_alpha);
if (!raw) {
std::cerr << "[ERROR] Cannot load {" << fileName << "}\n";
exit(0);
}
return *this;
}
TextureBuilder &TextureBuilder::fromRawRGBA(uint8_t *raw, int w, int h) {
this->raw = raw;
this->w = w;
this->h = h;
loadedFromMemory = true;
return *this;
}
Texture *TextureBuilder::get() { return texture; }
Texture *TextureBuilder::build() {
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
if (generateMipMaps) {
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
}
if (!loadedFromMemory)
stbi_image_free(raw);
glBindTexture(GL_TEXTURE_2D, 0);
texture = new Texture(textureID);
return texture;
}
Texture::Texture(GLuint tex) { this->set(tex); }
Texture::Texture() : textureID(0) {}
Texture::~Texture() { glDeleteTextures(1, &textureID); }
TextureBuilder Texture::create(){
return TextureBuilder();
}
void Texture::set(GLuint tex){
textureID=tex;
}
void Texture::bind(GLuint texUnit) {
glActiveTexture(GL_TEXTURE0 + texUnit);
glBindTexture(GL_TEXTURE_2D, textureID);
}
void Texture::unbind(GLuint texUnit) {
glActiveTexture(GL_TEXTURE0 + texUnit);
glBindTexture(GL_TEXTURE_2D, 0);
}
};