stuff
This commit is contained in:
+36
-26
@@ -1,51 +1,61 @@
|
||||
#include "texture.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "stb_image.h"
|
||||
|
||||
Texture::Texture(const char* filename) : path(filename)
|
||||
{
|
||||
int widthImg, heightImg, nrComponents;
|
||||
int nrComponents;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
unsigned char* bytes = stbi_load(filename, &widthImg, &heightImg, &nrComponents, 0);
|
||||
unsigned char* bytes = stbi_load(filename, &width, &height, &nrComponents, 4);
|
||||
if (not bytes)
|
||||
{
|
||||
std::cout << "Failed to load texture" << std::endl;
|
||||
throw;
|
||||
}
|
||||
throw std::runtime_error("Failed to load texture: " + path);
|
||||
|
||||
pixels.assign(bytes, bytes + (size_t)width * height * 4);
|
||||
stbi_image_free(bytes);
|
||||
|
||||
CreateGLTexture();
|
||||
}
|
||||
|
||||
Texture::Texture(int newWidth, int newHeight) : width(newWidth), height(newHeight)
|
||||
{
|
||||
pixels.assign((size_t)width * height * 4, 255);
|
||||
CreateGLTexture();
|
||||
}
|
||||
|
||||
Texture::Texture(const Texture& source)
|
||||
: path(source.path), width(source.width), height(source.height), pixels(source.pixels)
|
||||
{
|
||||
CreateGLTexture();
|
||||
}
|
||||
|
||||
void Texture::CreateGLTexture()
|
||||
{
|
||||
glGenTextures(1, &ID);
|
||||
glBindTexture(GL_TEXTURE_2D, ID);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
// Mipmaps when shrunk, otherwise tiled textures shimmer in the distance
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
GLenum format;
|
||||
if (nrComponents == 1)
|
||||
format = GL_RED;
|
||||
else if (nrComponents == 3)
|
||||
format = GL_RGB;
|
||||
else if (nrComponents == 4)
|
||||
format = GL_RGBA;
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, widthImg, heightImg, 0, format, GL_UNSIGNED_BYTE, bytes);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
stbi_image_free(bytes);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void Texture::texUnit(Shader shader, const char* uniform, GLuint unit) {
|
||||
shader.SetInt(uniform,unit);
|
||||
}
|
||||
|
||||
void Texture::Bind() {
|
||||
void Texture::Upload()
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, ID);
|
||||
}
|
||||
|
||||
void Texture::Unbind() {
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void Texture::Delete() {
|
||||
glDeleteTextures(1, &ID);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user