-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextureArray.cpp
More file actions
85 lines (61 loc) · 2.35 KB
/
Copy pathTextureArray.cpp
File metadata and controls
85 lines (61 loc) · 2.35 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
#include "TextureArray.hpp"
#include <stb/stb_image.h> // Used for textures / image processing
#include <iostream>
TextureArray::TextureArray()
{
// Generate a texture ID
glGenTextures(1, &textureID);
// Activate the nth texture unit slot
glActiveTexture(GL_TEXTURE0 + textureSlotIndex);
// Bind our texture ID to our 2D texture array and initialize it
glBindTexture(GL_TEXTURE_2D_ARRAY, textureID);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, image_size.x, image_size.y, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Settings for 2D texture array
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Increase the texture slot index, so the next time we create a texture array it does not
// use the previous texture slot and override it
textureUnitID = textureSlotIndex;
textureSlotIndex++;
}
void TextureArray::AddTextureToArray(const GLchar *filePath)
{
// Load and generate the texture
GLint width, height, nrChannels;
unsigned char *data = stbi_load(filePath, &width, &height, &nrChannels, STBI_rgb_alpha); // Loading as rgb_alpha so transparent pixels are transparent
// Flip textures on load
stbi_set_flip_vertically_on_load(false);
if (data)
{
// Put data into the 2D texture array
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, image_index, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
}
else
{
std::cout << "Failed to load texture " << filePath << std::endl;
}
image_index++;
}
void TextureArray::ActivateShaderArray(GLuint shaderProgramID)
{
// Have to use our program before setting a uniform
glUseProgram(shaderProgramID);
glUniform1i(glGetUniformLocation(shaderProgramID, textureArrayName), textureUnitID);
}
void TextureArray::Bind()
{
// Bind our texture to the GL_TEXTURE_2D target
glBindTexture(GL_TEXTURE_2D_ARRAY, textureID);
}
void TextureArray::Unbind()
{
// Unbind the texture
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
}
void TextureArray::Delete()
{
glDeleteTextures(1, &textureID);
}