Texture.cpp
branchv_0
changeset 19 262828ae9682
child 22 12262f2420de
equal deleted inserted replaced
18:c4384f43c739 19:262828ae9682
       
     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;
       
    30 };
       
    31 
       
    32 Texture::Texture(
       
    33 		int width,
       
    34 		int height,
       
    35 		const Buffer& img,
       
    36 		const std::string& fileName) : impl(new Impl()) {
       
    37 	impl->fileName = fileName;
       
    38 	glGenTextures(1, &impl->id);
       
    39 	update(width, height, img);
       
    40 }
       
    41 
       
    42 Texture::~Texture() {
       
    43 	glDeleteTextures(1, &impl->id);
       
    44 	delete impl;
       
    45 }
       
    46 
       
    47 GLuint Texture::getId() const {
       
    48 	return impl->id;
       
    49 }
       
    50 
       
    51 const std::string Texture::getFileName() const {
       
    52 	return impl->fileName;
       
    53 }
       
    54 
       
    55 int Texture::getWidth() const {
       
    56 	return impl->width;
       
    57 }
       
    58 
       
    59 int Texture::getHeight() const {
       
    60 	return impl->height;
       
    61 }
       
    62 
       
    63 GLfloat Texture::getRatio() const {
       
    64 	return (GLfloat) impl->width / (GLfloat) impl->height;
       
    65 }
       
    66 
       
    67 void Texture::update(int width, int height, const Buffer& img) {
       
    68 	impl->width = width;
       
    69 	impl->height = height;
       
    70 
       
    71 	if (img.getSize() != impl->width * impl->height * 4)
       
    72 		throw std::invalid_argument("invalid image size");
       
    73 
       
    74 	glBindTexture(GL_TEXTURE_2D, impl->id);
       
    75 	auto GLT2D = GL_TEXTURE_2D;
       
    76 	glTexImage2D(GLT2D, 0, GL_RGBA,
       
    77 			impl->width, impl->height,
       
    78 			0, GL_RGBA, GL_UNSIGNED_BYTE,
       
    79 			img.getData());
       
    80 	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
       
    81 	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
       
    82 	glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
    83 	glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
       
    84 	glGenerateMipmap(GLT2D);
       
    85 	checkError(&std::cerr);
       
    86 }