sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
texture_2d.cpp
1#include "texture.h"
2
3using namespace sgltk;
4
5Texture_2d::Texture_2d() : Texture() {
6 target = GL_TEXTURE_2D;
7}
8
9Texture_2d::Texture_2d(unsigned int res_x, unsigned int res_y, GLenum internal_format, GLenum type, GLenum format) : Texture() {
10 target = GL_TEXTURE_2D;
11 create_empty(res_x, res_y, internal_format, type, format);
12}
13
14Texture_2d::Texture_2d(const Image& image) : Texture() {
15 target = GL_TEXTURE_2D;
16 load(image);
17}
18
19Texture_2d::Texture_2d(const std::string& path) : Texture() {
20 target = GL_TEXTURE_2D;
21 load(path);
22}
23
24Texture_2d::~Texture_2d() {
25}
26
27void Texture_2d::create_empty(unsigned int res_x, unsigned int res_y, GLenum internal_format, GLenum type, GLenum format) {
28 width = res_x;
29 height = res_y;
30
31 bind();
32 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, res_x, res_y, 0,
33 format, type, nullptr);
34 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
35 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
38 unbind();
39}
40
41bool Texture_2d::load(const std::string& path) {
42 Image img;
43 if(!img.load(path))
44 return false;
45
46 width = img.width;
47 height = img.height;
48 return load(img);
49}
50
51bool Texture_2d::load(const Image& image) {
52 if(!image.image)
53 return false;
54
55 SDL_Surface *tmp = SDL_ConvertSurfaceFormat(image.image,
56 SDL_PIXELFORMAT_RGBA8888, 0);
57 if(!tmp) {
58 return false;
59 }
60
61 bind();
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
66 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0,
67 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, tmp->pixels);
68 glGenerateMipmap(GL_TEXTURE_2D);
69 unbind();
70 SDL_FreeSurface(tmp);
71 return true;
72}
73
74void Texture_2d::bind(unsigned int texture_unit) {
75 glActiveTexture(GL_TEXTURE0 + texture_unit);
76 glBindTexture(GL_TEXTURE_2D, texture);
77}
78
79void Texture_2d::unbind(unsigned int texture_unit) {
80 glActiveTexture(GL_TEXTURE0 + texture_unit);
81 glBindTexture(GL_TEXTURE_2D, 0);
82}
Manages images.
Definition image.h:12
unsigned int height
The width of the image surface.
Definition image.h:31
SDL_Surface * image
The image surface.
Definition image.h:44
bool load(const std::string &filename)
Loads a new image file.
Definition image.cpp:97
unsigned int width
The width of the image surface.
Definition image.h:27
void unbind(unsigned int texture_unit=0)
Unbind the texture.
void create_empty(unsigned int res_x, unsigned int res_y, GLenum internal_format, GLenum type, GLenum format)
Creates an empty texture.
bool load(const sgltk::Image &image)
Load a new image as texture.
void bind(unsigned int texture_unit=0)
Bind the texture to be used by the shader.
Manages textures.
Definition texture.h:13
unsigned int width
The width of the texture.
Definition texture.h:38
GLenum target
The texture target.
Definition texture.h:30
GLuint texture
The texture name.
Definition texture.h:34
unsigned int height
The height of the texture.
Definition texture.h:42