-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
120 lines (104 loc) · 4.4 KB
/
Texture.cpp
File metadata and controls
120 lines (104 loc) · 4.4 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
#include "Texture.h"
Texture::Texture() //Inititaizer list
:Width{ 0 }, Height{ 0 }, TextureID{ 0 }, NumberOfChannels{ 0 }, FileLocation{ (char*)("") }
/*Empty function*/ {}
Texture::Texture(char* Path) //Inititaizer list
: Width{ 0 }, Height{ 0 }, TextureID{ 0 }, NumberOfChannels{ 0 }, FileLocation{ Path }
/*Empty function*/ {}
bool Texture::LoadTexture()
{
unsigned char* TextureData = stbi_load(FileLocation, &Width, &Height, &NumberOfChannels, 0);
if (!TextureData)
{
std::cout << "Texture not found\n";
return false;
}
//Generates texture and binds to ID
glGenTextures(1, &TextureID);
// Textures have to be bound to apply operations to them.
//Since images are 2D arrays of pixels, it will be bound to the GL_TEXTURE_2D target of the OpenGL state machine.
glBindTexture(GL_TEXTURE_2D, TextureID);
//std::cout << "TextureID:" << TextureID << std::endl;
//Texture coordinates are between 0 and 1
// refer https://stackoverflow.com/questions/10568390/difference-between-uv-and-st-texture-coordinates
//Wrapping handles how the texture should be sampled when a coordinate outside the range of 0 to 1 is given.
//The wrapping can be set per coordinate, where the equivalent of (x,y,z) in texture coordinates is called (s,t,r).
//GL_Repeat repeats the texture when coordinate outside 0-1 is given
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//Texture filtering
//Linear is more clean, nearest is more blocky and looks like PS1 graphics
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Allows elements of an image array to be read by shaders.
//Binds the image to target currently set in GL_TEXTURE_2D
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureData);
//Generates mipmap
glGenerateMipmap(GL_TEXTURE_2D);
//Unbinds the GL_TEXTURE_2D target of OpenGL state machine
glBindTexture(GL_TEXTURE_2D, 0);
//Frees the image from memory
stbi_image_free(TextureData);
return true;
}
bool Texture::LoadTextureWithAlpha()
{
unsigned char* TextureData = stbi_load(FileLocation, &Width, &Height, &NumberOfChannels,0);
std::cout << NumberOfChannels << std::endl;
//std::cout << TextureData << std::endl;
if (!TextureData)
{
std::cout << "Texture not found\n";
return false;
}
//Generates texture and binds to ID
glGenTextures(1, &TextureID);
// Textures have to be bound to apply operations to them.
//Since images are 2D arrays of pixels, it will be bound to the GL_TEXTURE_2D target of the OpenGL state machine.
glBindTexture(GL_TEXTURE_2D, TextureID);
//std::cout << "TextureID:" << TextureID << std::endl;
//Texture coordinates are between 0 and 1
// refer https://stackoverflow.com/questions/10568390/difference-between-uv-and-st-texture-coordinates
//Wrapping handles how the texture should be sampled when a coordinate outside the range of 0 to 1 is given.
//The wrapping can be set per coordinate, where the equivalent of (x,y,z) in texture coordinates is called (s,t,r).
//GL_Repeat repeats the texture when coordinate outside 0-1 is given
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//Texture filtering
//Linear is more clean, nearest is more blocky and looks like PS1 graphics
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Allows elements of an image array to be read by shaders.
//Binds the image to target currently set in GL_TEXTURE_2D
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, TextureData);
//Generates mipmap
glGenerateMipmap(GL_TEXTURE_2D);
//Unbinds the GL_TEXTURE_2D target of OpenGL state machine
glBindTexture(GL_TEXTURE_2D, 0);
//Frees the image from memory
stbi_image_free(TextureData);
}
void Texture::UseTexture()
{
//glActiveTexture selects which texture unit of the state machine subsequent texture state calls will affect.
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, TextureID);
}
void Texture::ClearTexture()
{
glDeleteTextures(1, &TextureID);
Width = 0;
Height = 0;
NumberOfChannels = 0;
TextureID = 0;
FileLocation = (char*)("");
}
void Texture::setPath(char* Path)
{
//Set path to texture
FileLocation = Path;
}
Texture::~Texture()
{
ClearTexture();
}