parent
7f74a78a82
commit
fa59c32e24
@ -1,8 +1,17 @@ |
||||
#ifndef TEXTURES_HPP |
||||
#define TEXTURES_HPP |
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION |
||||
#include "../extern/stb/stb_image.h" |
||||
|
||||
namespace Textures { |
||||
class Texture2D { |
||||
public: |
||||
unsigned int id; |
||||
Texture2D(const char* t_src); |
||||
void load(); |
||||
void use(); |
||||
private: |
||||
const char* texture_src; |
||||
int width, height, nrChannels; |
||||
}; |
||||
} |
||||
|
||||
#endif |
||||
|
@ -1,11 +1,16 @@ |
||||
#version 330 core |
||||
|
||||
out vec4 FragColor; |
||||
in vec4 vertexColor; |
||||
|
||||
in vec4 VertexColor; |
||||
in vec2 TexCoord; |
||||
|
||||
uniform sampler2D inpTexture; |
||||
|
||||
uniform float r; |
||||
uniform float g; |
||||
uniform float b; |
||||
|
||||
void main() { |
||||
FragColor = vertexColor + vec4(r, g, b, 1.0f); |
||||
FragColor = texture(inpTexture, TexCoord) * (VertexColor + vec4(r, g, b, 1.0f)); |
||||
} |
||||
|
@ -1,10 +1,13 @@ |
||||
#version 330 core |
||||
layout (location = 0) in vec3 aPos; |
||||
layout (location = 1) in vec3 aColor; |
||||
layout (location = 2) in vec2 aTexCoord; |
||||
|
||||
out vec4 vertexColor; |
||||
out vec4 VertexColor; |
||||
out vec2 TexCoord; |
||||
|
||||
void main() { |
||||
gl_Position = vec4(aPos, 1.0); |
||||
vertexColor = vec4(aColor, 1.0); |
||||
VertexColor = vec4(aColor, 1.0); |
||||
TexCoord = aTexCoord; |
||||
} |
||||
|
@ -0,0 +1,46 @@ |
||||
#include <stdio.h> |
||||
#include "../headers/textures.hpp" |
||||
#include <glad/glad.h> |
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION |
||||
#include "../extern/stb/stb_image.h" |
||||
|
||||
namespace Textures { |
||||
Texture2D::Texture2D(const char* t_src) { |
||||
texture_src = t_src; |
||||
|
||||
// Bind this texture
|
||||
glGenTextures(1, &id); |
||||
glBindTexture(GL_TEXTURE_2D, id); |
||||
|
||||
// Repeat/bounded etc
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); |
||||
|
||||
// Mipmap "blending"
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
||||
|
||||
// Load texture source etc
|
||||
load(); |
||||
} |
||||
|
||||
void Texture2D::load() { |
||||
printf("[%i] \"%s\" - ", id, texture_src); |
||||
unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0); |
||||
|
||||
if (data) { |
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); |
||||
glGenerateMipmap(GL_TEXTURE_2D); |
||||
printf("Loaded!\n"); |
||||
} else { |
||||
printf("[ERROR] Failed to load texture \"%s\"\n", texture_src); |
||||
} |
||||
|
||||
stbi_image_free(data); |
||||
} |
||||
|
||||
void Texture2D::use() { |
||||
glBindTexture(GL_TEXTURE_2D, id); |
||||
} |
||||
} |
Loading…
Reference in new issue