forked from a1928370421/Bongobs-Cat-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.cpp
More file actions
156 lines (125 loc) · 4.32 KB
/
Sprite.cpp
File metadata and controls
156 lines (125 loc) · 4.32 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "Sprite.hpp"
#include "VtuberDelegate.hpp"
Sprite::Sprite(float x, float y, float width, float height,
GLuint textureId, GLuint programId)
: _rect()
{
_rect.left = (x - width);
_rect.right = (x + width );
_rect.up = (y + height );
_rect.down = (y - height );
_textureId = textureId;
// 何番目のattribute変数か
_positionLocation = glGetAttribLocation(programId, "position");
_uvLocation = glGetAttribLocation(programId, "uv");
_textureLocation = glGetUniformLocation(programId, "texture");
_colorLocation = glGetUniformLocation(programId, "baseColor");
_spriteColor[0] = 1.0f;
_spriteColor[1] = 1.0f;
_spriteColor[2] = 1.0f;
_spriteColor[3] = 1.0f;
_width = width;
_height = height;
}
Sprite::~Sprite() {}
void Sprite::Render(int i) const
{
// 画面サイズを取得する
int maxWidth, maxHeight;
maxWidth=VtuberDelegate::GetInstance()->getBufferWidth(i);
maxHeight=VtuberDelegate::GetInstance()->getBufferHeight(i);
if (maxWidth == 0 || maxHeight == 0) {
return; // この際は描画できず
}
//mix texture
glEnable(GL_BLEND);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
glEnable(GL_TEXTURE_2D);
const GLfloat uvVertex[] = {
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};
// attribute属性を有効にする
glEnableVertexAttribArray(_positionLocation);
glEnableVertexAttribArray(_uvLocation);
// uniform属性の登録
glUniform1i(_textureLocation, 0);
// 頂点データ
float positionVertex[] = {
(_rect.right - maxWidth * 0.5f) / (maxWidth * 0.5f),
(_rect.up - maxHeight * 0.5f) / (maxHeight * 0.5f),
(_rect.left - maxWidth * 0.5f) / (maxWidth * 0.5f),
(_rect.up - maxHeight * 0.5f) / (maxHeight * 0.5f),
(_rect.left - maxWidth * 0.5f) / (maxWidth * 0.5f),
(_rect.down - maxHeight * 0.5f) / (maxHeight * 0.5f),
(_rect.right - maxWidth * 0.5f) / (maxWidth * 0.5f),
(_rect.down - maxHeight * 0.5f) / (maxHeight * 0.5f)};
// attribute属性を登録
glVertexAttribPointer(_positionLocation, 2, GL_FLOAT, false, 0, positionVertex);
glVertexAttribPointer(_uvLocation, 2, GL_FLOAT, false, 0, uvVertex);
glUniform4f(_colorLocation, _spriteColor[0], _spriteColor[1],
_spriteColor[2], _spriteColor[3]);
// モデルの描画
glBindTexture(GL_TEXTURE_2D, _textureId);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
void Sprite::RenderImmidiate(int id,GLuint textureId,
const GLfloat uvVertex[8]) const
{
// 画面サイズを取得する
int maxWidth, maxHeight;
maxWidth = VtuberDelegate::GetInstance()->getBufferWidth(id);
maxHeight = VtuberDelegate::GetInstance()->getBufferHeight(id);
if (maxWidth == 0 || maxHeight == 0) {
return; // この際は描画できず
}
glEnable(GL_TEXTURE_2D);
// attribute属性を有効にする
glEnableVertexAttribArray(_positionLocation);
glEnableVertexAttribArray(_uvLocation);
// uniform属性の登録
glUniform1i(_textureLocation, 0);
// 頂点データ
float positionVertex[] = {
(_rect.right - maxWidth * 0.5f) / (maxWidth * 0.5f),
(_rect.up - maxHeight * 0.5f) / (maxHeight * 0.5f),
(_rect.left - maxWidth * 0.5f) / (maxWidth * 0.5f),
(_rect.up - maxHeight * 0.5f) / (maxHeight * 0.5f),
(_rect.left - maxWidth * 0.5f) / (maxWidth * 0.5f),
(_rect.down - maxHeight * 0.5f) / (maxHeight * 0.5f),
(_rect.right - maxWidth * 0.5f) / (maxWidth * 0.5f),
(_rect.down - maxHeight * 0.5f) / (maxHeight * 0.5f)};
// attribute属性を登録
glVertexAttribPointer(_positionLocation, 2, GL_FLOAT, false, 0,
positionVertex);
glVertexAttribPointer(_uvLocation, 2, GL_FLOAT, false, 0, uvVertex);
glUniform4f(_colorLocation, _spriteColor[0], _spriteColor[1],
_spriteColor[2], _spriteColor[3]);
// モデルの描画
glBindTexture(GL_TEXTURE_2D, textureId);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
void Sprite::SetColor(float r, float g, float b, float a)
{
_spriteColor[0] = r;
_spriteColor[1] = g;
_spriteColor[2] = b;
_spriteColor[3] = a;
}
void Sprite::ResetRect(float x, float y, float width, float height)
{
_rect.left = (x - width * 0.5f);
_rect.right = (x + width * 0.5f);
_rect.up = (y + height * 0.5f);
_rect.down = (y - height * 0.5f);
_width = width;
_height = height;
}
void Sprite::ResetLocation(float x, float y) {
_rect.left = (x - _width * 0.5f);
_rect.right = (x + _width * 0.5f);
_rect.up = (y + _height * 0.5f);
_rect.down = (y - _height * 0.5f);
}