Texture.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 27 Dec 2023 01:50:38 +0100
branchv_0
changeset 29 dc3c102e1264
parent 28 4cbd9c0beb4c
child 30 02972f051744
permissions -rw-r--r--
derive OHP3D from ShaderShark
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     2
 * OHP3D
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <string>
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <stdexcept>
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include "opengl.h"
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include "Texture.h"
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
class Texture::Impl {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
public:
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
	GLuint id;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
	std::string fileName;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
	int width;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
};
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
Texture::Texture(
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
		int width,
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		int height,
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		const Buffer& img,
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		const std::string& fileName) : impl(new Impl()) {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	impl->fileName = fileName;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	glGenTextures(1, &impl->id);
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	update(width, height, img);
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
Texture::~Texture() {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	glDeleteTextures(1, &impl->id);
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	delete impl;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
GLuint Texture::getId() const {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	return impl->id;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
const std::string Texture::getFileName() const {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	return impl->fileName;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
int Texture::getWidth() const {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	return impl->width;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
int Texture::getHeight() const {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	return impl->height;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
GLfloat Texture::getRatio() const {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	return (GLfloat) impl->width / (GLfloat) impl->height;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
}
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
void Texture::update(int width, int height, const Buffer& img) {
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	impl->width = width;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	impl->height = height;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	if (img.getSize() != impl->width * impl->height * 4)
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		throw std::invalid_argument("invalid image size");
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	glBindTexture(GL_TEXTURE_2D, impl->id);
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	auto GLT2D = GL_TEXTURE_2D;
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	glTexImage2D(GLT2D, 0, GL_RGBA,
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
			impl->width, impl->height,
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
			0, GL_RGBA, GL_UNSIGNED_BYTE,
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
			img.getData());
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	glGenerateMipmap(GLT2D);
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	checkError(&std::cerr);
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
}