HPC_Voxel_Engine 0.2.0
High-Performance C++ Voxel Engine
Loading...
Searching...
No Matches
Texture.h
Go to the documentation of this file.
1#pragma once
2#include <glad/glad.h>
3#include <stb_image.h>
4#include <iostream>
5#include <string>
6
7namespace Renderer {
8class Texture {
9public:
10 unsigned int ID;
12 Texture(const char* cPath) {
13 glCreateTextures(GL_TEXTURE_2D, 1, &ID);
14
15 glTextureParameteri(ID, GL_TEXTURE_WRAP_S, GL_REPEAT);
16 glTextureParameteri(ID, GL_TEXTURE_WRAP_T, GL_REPEAT);
17
18 glTextureParameteri(ID, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
19 glTextureParameteri(ID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
20
21 stbi_set_flip_vertically_on_load(false);
22
23 unsigned char* cData = stbi_load(cPath, &width, &height, &nrChannels, 0);
24 if (cData) {
25 GLenum iInternalFormat = (nrChannels == 4) ? GL_RGBA8 : GL_RGB8;
26 GLenum iDataFormat = (nrChannels == 4) ? GL_RGBA : GL_RGB;
27
28 glTextureStorage2D(ID, 1, iInternalFormat, width, height);
29 glTextureSubImage2D(ID, 0, 0, 0, width, height, iDataFormat, GL_UNSIGNED_BYTE, cData);
30 glGenerateTextureMipmap(ID);
31 stbi_image_free(cData);
32
33 } else {
34 std::cout << "ERROR::TEXTURE::FAILED_TO_LOAD_PATH" << std::endl;
35 glTextureStorage2D(ID, 1, GL_RGBA8, 1, 1);
36 unsigned char debugColor[] = {255, 0, 255, 255}; // Bright Pink
37 glTextureSubImage2D(ID, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, debugColor);
38 }
39 }
41 glDeleteTextures(1, &ID);
42 }
43
44 void Bind(unsigned int iSlot) const { glBindTextureUnit(iSlot, ID); }
45};
46} // namespace Renderer
Definition Texture.h:8
int nrChannels
Definition Texture.h:11
Texture(const char *cPath)
Definition Texture.h:12
unsigned int ID
Definition Texture.h:10
int height
Definition Texture.h:11
~Texture()
Definition Texture.h:40
int width
Definition Texture.h:11
void Bind(unsigned int iSlot) const
Definition Texture.h:44
Definition Buffer.h:4