-
Notifications
You must be signed in to change notification settings - Fork 882
/
Copy pathCameraSurfaceView.cpp
204 lines (168 loc) · 6.44 KB
/
CameraSurfaceView.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "CameraSurfaceView.hpp"
namespace camerakit {
CameraSurfaceView::CameraSurfaceView()
: surfaceWidth(0),
surfaceHeight(0) {
}
CameraSurfaceView::~CameraSurfaceView() {
if (vertexBuffer != 0) {
glDeleteBuffers(1, &vertexBuffer);
vertexBuffer = 0;
}
}
void CameraSurfaceView::onSurfaceCreated() {
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), VertexData(), GL_STATIC_DRAW);
GLuint program = CreateProgram(VertexShaderCode(), FragmentShaderCode());
if (!program) {
// TODO: throw here
return;
}
glUseProgram(program);
GLint aPosition = glGetAttribLocation(program, "aPosition");
GLint aTexCoord = glGetAttribLocation(program, "aTexCoord");
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if (glGetError() != GL_NO_ERROR) {
glDeleteProgram(program);
// TODO: throw here
return;
}
this->program = program;
this->aPosition = aPosition;
this->aTexCoord = aTexCoord;
}
void CameraSurfaceView::onSurfaceChanged(int width, int height) {
this->surfaceWidth = width;
this->surfaceHeight = height;
}
void CameraSurfaceView::onDrawFrame() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void CameraSurfaceView::drawTexture(GLuint texture, int textureWidth, int textureHeight) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, texture);
int viewportX = 0;
int viewportY = 0;
int viewportWidth = surfaceWidth;
int viewportHeight = surfaceHeight;
int candidateWidth = (int) (((float) textureWidth / (float) textureHeight) * surfaceHeight);
int candidateHeight = (int) (((float) textureHeight / (float) textureWidth) * surfaceWidth);
if (candidateWidth > surfaceWidth) {
viewportX = -1 * (candidateWidth - surfaceWidth) / 2;
viewportWidth = candidateWidth;
} else if (candidateHeight > surfaceHeight) {
viewportY = -1 * (candidateHeight - surfaceHeight) / 2;
viewportHeight = candidateHeight;
}
glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
glUseProgram(program);
glVertexAttribPointer(aPosition, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (const GLvoid*) (0 * sizeof(GLfloat)));
glEnableVertexAttribArray(aPosition);
glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (const GLvoid*) (4 * sizeof(GLfloat)));
glEnableVertexAttribArray(aTexCoord);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, VertexIndices());
glFlush();
}
void CameraSurfaceView::abandon() {
}
const GLfloat* CameraSurfaceView::VertexData() {
static const GLfloat vertexData[] = {
-1.0f, -1.0f, 0.0, 1.0, 0.0f, 0.0f,
+1.0f, -1.0f, 0.0, 1.0, 1.0f, 0.0f,
-1.0f, +1.0f, 0.0, 1.0, 0.0f, 1.0f,
+1.0f, +1.0f, 0.0, 1.0, 1.0f, 1.0f,
};
return vertexData;
}
const GLushort* CameraSurfaceView::VertexIndices() {
static const GLushort vertexIndices[] = {
0, 1, 2, 3
};
return vertexIndices;
}
const char* CameraSurfaceView::VertexShaderCode() {
static const char vertexShader[] =
"attribute vec4 aPosition;\n"
"attribute vec4 aTexCoord;\n"
"varying vec2 vTexCoord;\n"
"void main() {\n"
" gl_Position = aPosition;\n"
" vTexCoord = aTexCoord.xy;\n"
"}\n";
return vertexShader;
}
const char* CameraSurfaceView::FragmentShaderCode() {
static const char fragmentShader[] =
"precision mediump float;\n"
"uniform sampler2D uTexture;\n"
"varying vec2 vTexCoord;\n"
"void main() {\n"
" vec4 color = texture2D(uTexture, vTexCoord);\n"
" gl_FragColor = color;\n"
"}\n";
return fragmentShader;
}
GLuint CameraSurfaceView::LoadShader(GLenum shaderType, const char* shaderCode) {
GLuint shader = glCreateShader(shaderType);
if (shader) {
glShaderSource(shader, 1, &shaderCode, NULL);
glCompileShader(shader);
GLint compileStatus = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
if (!compileStatus) {
GLint infoLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLength);
if (infoLength) {
char* infoBuffer = (char*) malloc((size_t) infoLength);
if (infoBuffer) {
glGetShaderInfoLog(shader, infoLength, NULL, infoBuffer);
// todo: output log
free(infoBuffer);
}
}
glDeleteShader(shader);
shader = 0;
}
}
return shader;
}
GLuint CameraSurfaceView::CreateProgram(const char* vertexShaderCode, const char* fragmentShaderCode) {
GLuint vertexShader = LoadShader(GL_VERTEX_SHADER, vertexShaderCode);
if (!vertexShader) {
return 0;
}
GLuint fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fragmentShaderCode);
if (!fragmentShader) {
return 0;
}
GLuint program = glCreateProgram();
if (program) {
glAttachShader(program, vertexShader);
// TODO: check error and throw if needed
glAttachShader(program, fragmentShader);
// TODO: check error and throw if needed
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (!linkStatus) {
GLint infoLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLength);
if (infoLength) {
char* infoBuffer = (char*) malloc((size_t) infoLength);
if (infoBuffer) {
glGetProgramInfoLog(program, infoLength, NULL, infoBuffer);
// todo: output log
free(infoBuffer);
}
}
glDeleteProgram(program);
program = 0;
}
}
return program;
}
}