-
Notifications
You must be signed in to change notification settings - Fork 1
/
GLTexture.cpp
executable file
·75 lines (66 loc) · 2.07 KB
/
GLTexture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "GLTexture.h"
#include <cstdint>
#include <utility>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
cGLTexture::cGLTexture(const std::string& textureName)
: mTextureName(textureName), mTextureId(0)
{
}
cGLTexture::cGLTexture(const std::string& textureName, GLint textureId)
: mTextureName(textureName), mTextureId(textureId)
{
}
cGLTexture::cGLTexture(cGLTexture&& other) noexcept
: mTextureName(std::move(other.mTextureName)),
mTextureId(other.mTextureId)
{
other.mTextureId = 0;
}
cGLTexture& cGLTexture::operator=(cGLTexture&& other) noexcept
{
mTextureName = std::move(other.mTextureName);
std::swap(mTextureId, other.mTextureId);
return *this;
}
cGLTexture::~cGLTexture() noexcept {
if (mTextureId)
glDeleteTextures(1, &mTextureId);
}
GLuint cGLTexture::texture() const
{
return mTextureId;
}
const std::string& cGLTexture::textureName() const
{
return mTextureName;
}
cGLTexture::handleType cGLTexture::load(const std::string& fileName)
{
if (mTextureId) {
glDeleteTextures(1, &mTextureId);
mTextureId = 0;
}
int force_channels = 0;
int w, h, n;
handleType textureData(stbi_load(fileName.c_str(), &w, &h, &n, force_channels), stbi_image_free);
if (!textureData)
throw std::invalid_argument("Could not load texture data from file " + fileName);
glGenTextures(1, &mTextureId);
glBindTexture(GL_TEXTURE_2D, mTextureId);
GLint internalFormat;
GLint format;
switch (n) {
case 1: internalFormat = GL_R8; format = GL_RED; break;
case 2: internalFormat = GL_RG8; format = GL_RG; break;
case 3: internalFormat = GL_RGB8; format = GL_RGB; break;
case 4: internalFormat = GL_RGBA8; format = GL_RGBA; break;
default: internalFormat = 0; format = 0; break;
}
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, format, GL_UNSIGNED_BYTE, textureData.get());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
return textureData;
}