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