forked from SurajSharma90/OpenGL-C---Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.h
More file actions
100 lines (79 loc) · 2.17 KB
/
Texture.h
File metadata and controls
100 lines (79 loc) · 2.17 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
#pragma once
#include<iostream>
#include<string>
#include<glew.h>
#include<glfw3.h>
#include<SOIL2.h>
class Texture
{
private:
GLuint id;
int width;
int height;
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);
glGenTextures(1, &this->id);
glBindTexture(type, this->id);
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
{
std::cout << "ERROR::TEXTURE::TEXTURE_LOADING_FAILED: " << fileName <<"\n";
}
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(this->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);
glGenTextures(1, &this->id);
glBindTexture(this->type, this->id);
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
{
std::cout << "ERROR::TEXTURE::LOADFROMFILE::TEXTURE_LOADING_FAILED: " << fileName << "\n";
}
glActiveTexture(0);
glBindTexture(this->type, 0);
SOIL_free_image_data(image);
}
};