forked from a1928370421/Bongobs-Cat-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAppTextureManager.cpp
More file actions
140 lines (120 loc) · 3.38 KB
/
Copy pathLAppTextureManager.cpp
File metadata and controls
140 lines (120 loc) · 3.38 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
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
131
132
133
134
135
136
137
138
139
140
/**
* Copyright(c) Live2D Inc. All rights reserved.
*
* Use of this source code is governed by the Live2D Open Software license
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
*/
#include "LAppTextureManager.hpp"
#include <iostream>
#define STBI_NO_STDIO
#define STBI_ONLY_PNG
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "Pal.hpp"
LAppTextureManager::LAppTextureManager()
{
}
LAppTextureManager::~LAppTextureManager()
{
ReleaseTextures();
}
LAppTextureManager::TextureInfo* LAppTextureManager::CreateTextureFromPngFile(std::string fileName)
{
//search loaded texture already.
for (Csm::csmUint32 i = 0; i < _textures.GetSize(); i++)
{
if (_textures[i]->fileName == fileName)
{
return _textures[i];
}
}
GLuint textureId;
int width, height, channels;
unsigned int size;
unsigned char* png;
unsigned char* address;
address = Pal::LoadFileAsBytes(fileName, &size);
// png情報を取得する
png = stbi_load_from_memory(
address,
static_cast<int>(size),
&width,
&height,
&channels,
STBI_rgb_alpha);
{
#ifdef PREMULTIPLIED_ALPHA_ENABLE
unsigned int* fourBytes = reinterpret_cast<unsigned int*>(png);
for (int i = 0; i < width * height; i++)
{
unsigned char* p = png + i * 4;
fourBytes[i] = Premultiply(p[0], p[1], p[2], p[3]);
}
#endif
}
// OpenGL用のテクスチャを生成する
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, png);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
// 解放処理
stbi_image_free(png);
Pal::ReleaseBytes(address);
LAppTextureManager::TextureInfo* textureInfo = new LAppTextureManager::TextureInfo();
if (textureInfo != NULL)
{
textureInfo->fileName = fileName;
textureInfo->width = width;
textureInfo->height = height;
textureInfo->id = textureId;
_textures.PushBack(textureInfo);
}
return textureInfo;
}
void LAppTextureManager::ReleaseTextures()
{
for (Csm::csmUint32 i = 0; i < _textures.GetSize(); i++)
{
delete _textures[i];
}
_textures.Clear();
}
void LAppTextureManager::ReleaseTexture(Csm::csmUint32 textureId)
{
for (Csm::csmUint32 i = 0; i < _textures.GetSize(); i++)
{
if (_textures[i]->id != textureId)
{
continue;
}
delete _textures[i];
_textures.Remove(i);
break;
}
}
void LAppTextureManager::ReleaseTexture(std::string fileName)
{
for (Csm::csmUint32 i = 0; i < _textures.GetSize(); i++)
{
if (_textures[i]->fileName == fileName)
{
delete _textures[i];
_textures.Remove(i);
break;
}
}
}
LAppTextureManager::TextureInfo* LAppTextureManager::GetTextureInfoById(GLuint textureId) const
{
for (Csm::csmUint32 i = 0; i < _textures.GetSize(); i++)
{
if (_textures[i]->id == textureId)
{
return _textures[i];
}
}
return NULL;
}