author | František Kučera <franta-hg@frantovo.cz> |
Tue, 02 Jan 2024 17:50:43 +0100 | |
branch | v_0 |
changeset 32 | 74111f4e67cd |
parent 29 | 69903e30b00d |
permissions | -rw-r--r-- |
19 | 1 |
/** |
2 |
* ShaderShark |
|
3 |
* Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
||
18 |
#include <string> |
|
19 |
#include <stdexcept> |
|
20 |
||
21 |
#include "opengl.h" |
|
22 |
#include "Texture.h" |
|
23 |
||
24 |
class Texture::Impl { |
|
25 |
public: |
|
26 |
GLuint id; |
|
27 |
std::string fileName; |
|
28 |
int width; |
|
29 |
int height; |
|
28
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
30 |
GLfloat scale = 1.; |
19 | 31 |
}; |
32 |
||
33 |
Texture::Texture( |
|
34 |
int width, |
|
35 |
int height, |
|
36 |
const Buffer& img, |
|
37 |
const std::string& fileName) : impl(new Impl()) { |
|
38 |
impl->fileName = fileName; |
|
39 |
glGenTextures(1, &impl->id); |
|
40 |
update(width, height, img); |
|
41 |
} |
|
42 |
||
43 |
Texture::~Texture() { |
|
44 |
glDeleteTextures(1, &impl->id); |
|
45 |
delete impl; |
|
46 |
} |
|
47 |
||
48 |
GLuint Texture::getId() const { |
|
49 |
return impl->id; |
|
50 |
} |
|
51 |
||
52 |
const std::string Texture::getFileName() const { |
|
53 |
return impl->fileName; |
|
54 |
} |
|
55 |
||
56 |
int Texture::getWidth() const { |
|
57 |
return impl->width; |
|
58 |
} |
|
59 |
||
60 |
int Texture::getHeight() const { |
|
61 |
return impl->height; |
|
62 |
} |
|
63 |
||
64 |
GLfloat Texture::getRatio() const { |
|
65 |
return (GLfloat) impl->width / (GLfloat) impl->height; |
|
66 |
} |
|
67 |
||
28
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
68 |
GLfloat Texture::getScale() const { |
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
69 |
return impl->scale; |
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
70 |
} |
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
71 |
|
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
72 |
void Texture::setScale(GLfloat scale) { |
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
73 |
impl->scale = scale; |
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
74 |
} |
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
75 |
|
19 | 76 |
void Texture::update(int width, int height, const Buffer& img) { |
77 |
impl->width = width; |
|
78 |
impl->height = height; |
|
79 |
||
80 |
if (img.getSize() != impl->width * impl->height * 4) |
|
81 |
throw std::invalid_argument("invalid image size"); |
|
82 |
||
83 |
glBindTexture(GL_TEXTURE_2D, impl->id); |
|
84 |
auto GLT2D = GL_TEXTURE_2D; |
|
85 |
glTexImage2D(GLT2D, 0, GL_RGBA, |
|
86 |
impl->width, impl->height, |
|
87 |
0, GL_RGBA, GL_UNSIGNED_BYTE, |
|
88 |
img.getData()); |
|
89 |
glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
|
90 |
glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
|
22
12262f2420de
texture magnification filter: linear (blurry) vs. nearest (sharp/pixelatex)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
91 |
glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // blurry upscale |
12262f2420de
texture magnification filter: linear (blurry) vs. nearest (sharp/pixelatex)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
92 |
glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // sharp upscale |
19 | 93 |
glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
94 |
glGenerateMipmap(GLT2D); |
|
95 |
checkError(&std::cerr); |
|
96 |
} |
|
29
69903e30b00d
add Texture::bind() method
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
97 |
|
69903e30b00d
add Texture::bind() method
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
98 |
void Texture::bind() { |
69903e30b00d
add Texture::bind() method
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
99 |
glBindTexture(GL_TEXTURE_2D, impl->id); |
69903e30b00d
add Texture::bind() method
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
100 |
} |