forked from tapetums/include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Texture.hpp
130 lines (104 loc) · 3.62 KB
/
Texture.hpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#pragma once
//---------------------------------------------------------------------------//
//
// テクスチャオブジェクト
// Texture.hpp
// Copyright (C) 2013-2016 tapetums
//
//---------------------------------------------------------------------------//
#include <cstdint>
#include <vector>
#include <gl/gl.h>
#include <gl/glext.h>
#pragma comment(lib, "opengl32.lib")
//---------------------------------------------------------------------------//
using float64_t = double;
//---------------------------------------------------------------------------//
// 前方宣言
//---------------------------------------------------------------------------//
namespace tapetums
{
struct TextureDesc;
class OpenGLTexture;
}
//---------------------------------------------------------------------------//
// 構造体
//---------------------------------------------------------------------------//
// テクスチャ情報を保持する構造体
struct tapetums::TextureDesc
{
int32_t width;
int32_t height;
float64_t dpiX;
float64_t dpiY;
int32_t interpolation_min;
int32_t interpolation_max;
int32_t repeat_s;
int32_t repeat_t;
int32_t envi;
};
//---------------------------------------------------------------------------//
// クラス
//---------------------------------------------------------------------------//
class tapetums::OpenGLTexture
{
private:
TextureDesc m_desc;
std::vector<uint8_t> m_buffer;
uint32_t m_texture { 0 };
public:
OpenGLTexture() = default;
~OpenGLTexture() { Uninit(); }
OpenGLTexture(const TextureDesc& desc, const void* const buffer, size_t size)
{ Init(desc, buffer, size); }
public:
const TextureDesc& desc() const noexcept { return m_desc; }
const uint8_t* buffer() const noexcept { return m_buffer.data(); }
size_t size() const noexcept { return m_buffer.size(); }
uint32_t texture() const noexcept { return m_texture; }
public:
void Init(const TextureDesc& desc, const void* const buffer, size_t size);
void Uninit();
};
//---------------------------------------------------------------------------//
inline void tapetums::OpenGLTexture::Init
(
const TextureDesc& desc, const void* const buffer, size_t size
)
{
m_desc = desc;
m_buffer.resize(size);
::memcpy(m_buffer.data(), buffer, size);
// テクスチャを設定
::glGenTextures(1, &m_texture);
if ( m_texture == 0 )
{
printf("glGenTextures error: 0x%04x", glGetError());
}
::glBindTexture (GL_TEXTURE_2D, m_texture);
::glPixelStorei (GL_UNPACK_ALIGNMENT, 32 / 8);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_desc.interpolation_min);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_desc.interpolation_min);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_desc.repeat_s);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_desc.repeat_t);
::glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, m_desc.envi);
::glTexImage2D
(
GL_TEXTURE_2D, 0, GL_RGBA,
m_desc.width, m_desc.height, 0,
GL_BGRA, GL_UNSIGNED_BYTE, m_buffer.data()
);
::glBindTexture(GL_TEXTURE_2D, 0);
}
//---------------------------------------------------------------------------//
inline void tapetums::OpenGLTexture::Uninit()
{
if ( m_texture )
{
::glDeleteTextures(1, &m_texture);
m_texture = 0;
}
m_buffer.resize(0);
}
//---------------------------------------------------------------------------//
// Texture.hpp