-
Notifications
You must be signed in to change notification settings - Fork 1
/
video_overlay.cc
191 lines (159 loc) · 6.45 KB
/
video_overlay.cc
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
/*
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdlib>
#include "tango-gl/video_overlay.h"
#include "tango-gl/shaders.h"
namespace {
const GLfloat kVertices[] = {-1.0, 1.0, 0.0, -1.0, -1.0, 0.0,
1.0, 1.0, 0.0, 1.0, -1.0, 0.0};
const GLushort kIndices[] = {0, 1, 2, 2, 1, 3};
} // annonymous namespace
namespace tango_gl {
VideoOverlay::VideoOverlay()
: texture_type_(GL_TEXTURE_2D),
display_rotation_(TangoSupportRotation::ROTATION_IGNORED),
u_offset_(0.0f),
v_offset_(0.0f) {
Initialize();
}
VideoOverlay::VideoOverlay(GLuint texture_type)
: texture_type_(texture_type),
display_rotation_(TangoSupportRotation::ROTATION_IGNORED),
u_offset_(0.0f),
v_offset_(0.0f) {
Initialize();
}
VideoOverlay::VideoOverlay(TangoSupportRotation display_rotation)
: texture_type_(GL_TEXTURE_2D),
display_rotation_(display_rotation),
u_offset_(0.0f),
v_offset_(0.0f) {
Initialize();
}
VideoOverlay::VideoOverlay(GLuint texture_type,
TangoSupportRotation display_rotation)
: texture_type_(texture_type),
display_rotation_(display_rotation),
u_offset_(0.0f),
v_offset_(0.0f) {
Initialize();
}
void VideoOverlay::Initialize() {
SetDisplayRotation(display_rotation_);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
if (texture_type_ == GL_TEXTURE_2D) {
shader_program_ =
util::CreateProgram(shaders::GetVideoOverlayVertexShader().c_str(),
shaders::GetVideoOverlayFragmentShader().c_str());
} else {
shader_program_ = util::CreateProgram(
shaders::GetVideoOverlayVertexShader().c_str(),
shaders::GetVideoOverlayTexture2DFragmentShader().c_str());
}
if (!shader_program_) {
LOGE("Could not create program.");
}
glGenTextures(1, &texture_id_);
glBindTexture(texture_type_, texture_id_);
glTexParameteri(texture_type_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(texture_type_, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
uniform_texture_ = glGetUniformLocation(shader_program_, "texture");
glGenBuffers(2, vertex_buffers_);
// Allocate vertices buffer.
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 4, kVertices,
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Allocate triangle indices buffer.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertex_buffers_[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * 6, kIndices,
GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// Assign the vertices attribute data.
attrib_vertices_ = glGetAttribLocation(shader_program_, "vertex");
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
glEnableVertexAttribArray(attrib_vertices_);
glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Assign the texture coordinates attribute data.
attrib_texture_coords_ =
glGetAttribLocation(shader_program_, "textureCoords");
uniform_mvp_mat_ = glGetUniformLocation(shader_program_, "mvp");
}
void VideoOverlay::SetDisplayRotation(
TangoSupportRotation display_rotation) {
display_rotation_ = display_rotation;
float texture_coords_0[8] = {
0.0f + u_offset_, 0.0f + v_offset_, 0.0f + u_offset_, 1.0f - v_offset_,
1.0f - u_offset_, 0.0f + v_offset_, 1.0f - u_offset_, 1.0f - v_offset_};
float output_texture_coords[8];
TangoErrorType r = TangoSupport_getVideoOverlayUVBasedOnDisplayRotation(
texture_coords_0, display_rotation, output_texture_coords);
if (r == TANGO_SUCCESS) {
std::copy(std::begin(output_texture_coords),
std::end(output_texture_coords), std::begin(texture_coords_));
}
}
void VideoOverlay::SetTextureOffset(float screen_width, float screen_height,
float image_width, float image_height) {
if ((screen_width / screen_height > 1.0f) !=
(image_width / image_height > 1.0f)) {
// if image ratio and screen ratio don't comply to each other, we always
// aligned things to screen ratio.
float tmp = image_width;
image_width = image_height;
image_height = tmp;
}
float screen_ratio = screen_width / screen_height;
float image_ratio = image_width / image_height;
float zoom_factor = 1.0f;
if (image_ratio > screen_ratio) {
zoom_factor = screen_height / image_height;
} else {
zoom_factor = screen_width / image_width;
}
float render_width = image_width * zoom_factor;
float render_height = image_height * zoom_factor;
u_offset_ = ((render_width - screen_width) / 2.0f) / render_width;
v_offset_ = ((render_height - screen_height) / 2.0f) / render_height;
SetDisplayRotation(display_rotation_);
}
void VideoOverlay::Render(const glm::mat4& projection_mat,
const glm::mat4& view_mat) const {
glUseProgram(shader_program_);
glUniform1i(uniform_texture_, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(texture_type_, texture_id_);
glm::mat4 model_mat = GetTransformationMatrix();
glm::mat4 mvp_mat = projection_mat * view_mat * model_mat;
glUniformMatrix4fv(uniform_mvp_mat_, 1, GL_FALSE, glm::value_ptr(mvp_mat));
// Bind vertices buffer.
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
glEnableVertexAttribArray(attrib_vertices_);
glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(attrib_texture_coords_);
glVertexAttribPointer(attrib_texture_coords_, 2, GL_FLOAT, GL_FALSE, 0,
texture_coords_.data());
// Bind element array buffer.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertex_buffers_[1]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
util::CheckGlError("glDrawElements");
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glUseProgram(0);
util::CheckGlError("glUseProgram()");
}
} // namespace tango_gl