-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.cpp
More file actions
30 lines (27 loc) · 1.16 KB
/
texture.cpp
File metadata and controls
30 lines (27 loc) · 1.16 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
#include "texture.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "vender/stb/stb_image.h"
#include <GL/glew.h>
Image::Image(std::string const& path, unsigned int spot): spot(spot) {
int throwit;
stbi_set_flip_vertically_on_load(true);
data = stbi_load(path.c_str(), &width, &height, &throwit, 3);
assert(data);
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0+spot);
glBindTexture(GL_TEXTURE_2D, texture);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
void set_texture(unsigned int program, Image const& image) {
glActiveTexture(GL_TEXTURE0+image.spot);
glBindTexture(GL_TEXTURE_2D, image.texture);
glUseProgram(program);
auto location = glGetUniformLocation(program, "tex");
glUniform1i(location, image.spot); // Shader Location 0
}