-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.cpp
More file actions
51 lines (40 loc) · 1.59 KB
/
texture.cpp
File metadata and controls
51 lines (40 loc) · 1.59 KB
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
#include "texture.h"
#include <QImage>
#include <QOpenGLWidget>
Texture::Texture(OpenGLContext *context)
: context(context), m_textureHandle(-1), m_textureImage(nullptr)
{}
Texture::~Texture()
{}
void Texture::create(const char *texturePath)
{
context->printGLErrorLog();
QImage img(texturePath);
img.convertToFormat(QImage::Format_ARGB32);
img = img.mirrored();
m_textureImage = std::make_shared<QImage>(img);
context->glGenTextures(1, &m_textureHandle);
context->printGLErrorLog();
}
void Texture::load(int texSlot = 0)
{
context->printGLErrorLog();
context->glActiveTexture(GL_TEXTURE0 + texSlot);
context->glBindTexture(GL_TEXTURE_2D, m_textureHandle);
// These parameters need to be set for EVERY texture you create
// They don't always have to be set to the values given here, but they do need
// to be set
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
context->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
m_textureImage->width(), m_textureImage->height(),
0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, m_textureImage->bits());
context->printGLErrorLog();
}
void Texture::bind(int texSlot = 0)
{
context->glActiveTexture(GL_TEXTURE0 + texSlot);
context->glBindTexture(GL_TEXTURE_2D, m_textureHandle);
}