Anvil Engine
Loading...
Searching...
No Matches
ATexture.h
Go to the documentation of this file.
1#pragma once
2#include <glad/glad.h>
3#include <stb_image.h>
4#include <string>
5
7{
8 public:
9 unsigned int ID;
10 std::string type;
11 std::string path;
12
17 ATexture(const char* path)
18 {
19 // Generate a texture name and store it in ID
20 glGenTextures(1, &ID);
21
22
23
24 // Load image data from file using stb_image library
25 int width, height, nrComponents; // Variables to store image dimensions and components
26 unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
27 if (data)
28 {
29 // Determine texture format based on number of components (RGB or RGBA)
30 GLenum format = (nrComponents == 4) ? GL_RGBA : GL_RGB;
31
32 // Bind the texture to the GL_TEXTURE_2D target
33 glBindTexture(GL_TEXTURE_2D, ID);
34 // Create and initialize the texture image
35 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE,
36 data);
37 // Generate mipmaps for the texture
38 glGenerateMipmap(GL_TEXTURE_2D);
39
40
41
42
43 // Set texture wrapping parameters
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set wrapping to repeat on the S axis
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Set wrapping to repeat on the T axis
46
47
48 // Set texture filtering parameters
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Set minification filter
50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Set magnification filter
51
52 // Free the loaded image data
53 stbi_image_free(data);
54 }
55 }
56
61 void Bind(unsigned int unit = 0)
62 {
63 // Activate the specified texture unit
64 glActiveTexture(GL_TEXTURE0 + unit);
65 // Bind the texture to the currently active texture unit
66 glBindTexture(GL_TEXTURE_2D, ID);
67 }
68};
ATexture(const char *path)
Definition ATexture.h:17
unsigned int ID
Definition ATexture.h:9
std::string type
Definition ATexture.h:10
void Bind(unsigned int unit=0)
Definition ATexture.h:61
std::string path
Definition ATexture.h:11