This repository was archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.h
More file actions
92 lines (74 loc) · 2.28 KB
/
Texture.h
File metadata and controls
92 lines (74 loc) · 2.28 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
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<glew.h>
#include<glfw3.h>
#include<SOIL2.h>
using namespace std;
class Texture {
private:
GLuint id;
int height;
int width;
unsigned int type;
public:
Texture(const char* fileName, GLenum type) {
this->type = type;
unsigned char* image = SOIL_load_image(fileName, &this->width, &this->height, NULL, SOIL_LOAD_RGBA);
GLuint texture0;
glGenTextures(1, &this->id);
glBindTexture(type, this->id);
// Opotions
glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (image) {
glTexImage2D(type, 0, GL_RGBA, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(type);
}
else {
cout << "ERROR::TEXTURE:COULD_NOT_LOAD_TEXTURE: " << fileName << endl;
}
glActiveTexture(0);
glBindTexture(type, 0);
SOIL_free_image_data(image);
}
~Texture() {
glDeleteTextures(1 , &this->id);
}
inline GLuint getID() const { return this->id;}
void bind(const GLint texture_unit) {
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(type, this->id);
}
void unbind() {
glActiveTexture(0);
glBindTexture(this->type, 0);
}
void loadFromFile(const char* fileName){
if (this->id) {
glDeleteTextures(1, &this->id);
}
unsigned char* image = SOIL_load_image(fileName, &this->width, &this->height, NULL, SOIL_LOAD_RGBA);
GLuint texture0;
glGenTextures(1, &this->id);
glBindTexture(this->type, this->id);
// Opotions
glTexParameteri(this->type, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(this->type, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(this->type, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(this->type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (image) {
glTexImage2D(this->type, 0, GL_RGBA, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(this->type);
}
else {
cout << "ERROR::TEXTURE:COULD_NOT_LOAD_TEXTURE: " << fileName << endl;
}
glActiveTexture(0);
glBindTexture(type, 0);
SOIL_free_image_data(image);
}
};