/
RGBAImage.h
98 lines (81 loc) · 2.55 KB
/
RGBAImage.h
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once
#include "igl.h"
#include "iimage.h"
#include "BasicTexture2D.h"
#include <memory>
#include "util/Noncopyable.h"
#include "debugging/gl.h"
/// A single pixel with 8-bit RGBA components
struct RGBAPixel
{
uint8_t red, green, blue, alpha;
};
/**
* An RGBA image represents a single-mipmap image with certain
* dimensions. The memory for the actual pixelmap is allocated
* and de-allocated automatically.
*/
class RGBAImage :
public Image,
public util::Noncopyable
{
std::size_t _width;
std::size_t _height;
public:
RGBAPixel* pixels;
/// Construct image and initialise internal storage
RGBAImage(std::size_t width, std::size_t height):
_width(width),
_height(height),
pixels(new RGBAPixel[_width * _height])
{}
~RGBAImage()
{
delete[] pixels;
}
/* Image implementation */
uint8_t* getPixels() const override
{
return reinterpret_cast<byte*>(pixels);
}
std::size_t getWidth(std::size_t = 0) const override { return _width; }
std::size_t getHeight(std::size_t = 0) const override { return _height; }
std::size_t getLevels() const override { return 1; }
GLenum getGLFormat() const override { return GL_RGBA; }
/* BindableTexture implementation */
TexturePtr bindTexture(const std::string& name, Role role) const
{
GLuint textureNum;
debug::assertNoGlErrors();
// Allocate a new texture number and store it into the Texture structure
glGenTextures(1, &textureNum);
glBindTexture(GL_TEXTURE_2D, textureNum);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Upload the image to OpenGL, choosing an internal format based on role
GLint format = GL_RGBA8;
if (role == Role::NORMAL_MAP) {
format = GL_RG8;
}
glTexImage2D(GL_TEXTURE_2D, 0, format, static_cast<GLint>(getWidth()),
static_cast<GLint>(getHeight()), 0, GL_RGBA,
GL_UNSIGNED_BYTE, getPixels());
if (GLEW_VERSION_3_0) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
}
// Un-bind the texture
glBindTexture(GL_TEXTURE_2D, 0);
// Construct texture object
BasicTexture2DPtr tex2DObject(new BasicTexture2D(textureNum, name));
tex2DObject->setWidth(getWidth());
tex2DObject->setHeight(getHeight());
debug::assertNoGlErrors();
return tex2DObject;
}
bool isPrecompressed() const
{
return false; // not compressed
}
};
typedef std::shared_ptr<RGBAImage> RGBAImagePtr;