2
* Copyright (C) 2013 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "snapshotgenerator.h"
19
#include <camera_compatibility_layer.h>
21
#include <QGLFramebufferObject>
22
#include <QOpenGLShaderProgram>
23
#include <QOpenGLShader>
27
SnapshotGenerator::SnapshotGenerator()
39
* @brief SnapshotGenerator::snapshot
40
* @param textureId Texture to be stored as QImage
41
* @return Image containing the content of the last texture
43
QImage SnapshotGenerator::snapshot(GLuint textureId, const CameraControl *control)
45
Q_ASSERT(textureId > 0);
46
Q_ASSERT(control != NULL);
48
QGLFramebufferObject fbo(m_width, m_height);
53
QOpenGLShaderProgram program;
54
program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShader());
55
program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShader());
60
glViewport(0, 0, m_width, m_height);
62
position_loc = program.attributeLocation("a_position");
63
v_matrix_loc = program.uniformLocation("v_matrix");
64
sampler_loc = program.uniformLocation("s_texture");
65
tex_coord_loc = program.attributeLocation("a_texCoord");
66
tex_matrix_loc = program.uniformLocation("m_texMatrix");
68
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
69
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
70
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
71
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
73
GLfloat vVertices[] = {
74
0.0f, 00.0f, 0.0f, // Position 0
75
0.0f, 1.0f, 0.0f, // Position 1
76
1.0f, 1.0f, 0.0f, // Position 2
77
1.0f, 00.0f, 0.0f, // Position 3
79
vVertices[4] = m_height;
80
vVertices[6] = m_width;
81
vVertices[7] = m_height;
82
vVertices[9] = m_width;
84
GLfloat tVertices[] = {
85
0.0f, 1.0f, // TexCoord 1
86
0.0f, 0.0f, // TexCoord 0
87
1.0f, 0.0f, // TexCoord 3
88
1.0f, 1.0f // TexCoord 2
91
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
93
program.enableAttributeArray(position_loc);
94
program.setAttributeArray(position_loc, vVertices, 3);
96
pmvMatrix.ortho(QRect(0,0,m_width,m_height));
97
program.setUniformValue(v_matrix_loc, pmvMatrix);
99
program.enableAttributeArray(tex_coord_loc);
100
program.setAttributeArray(tex_coord_loc, tVertices, 2);
102
program.enableAttributeArray(sampler_loc);
103
program.setUniformValue(sampler_loc, 0);
104
program.enableAttributeArray(tex_matrix_loc);
106
GLfloat textureMatrix[16];
107
android_camera_get_preview_texture_transformation(const_cast<CameraControl*>(control), textureMatrix);
108
QMatrix4x4 texMat(textureMatrix);
109
texMat = texMat.transposed();
110
program.setUniformValue(tex_matrix_loc, texMat);
112
glBindTexture(GL_TEXTURE_2D, textureId);
113
glActiveTexture(GL_TEXTURE0);
115
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
118
return fbo.toImage();
121
void SnapshotGenerator::setSize(int width, int height)
127
const char *SnapshotGenerator::vertexShader() const
130
"#extension GL_OES_EGL_image_external : require \n"
131
"attribute vec4 a_position; \n"
132
"uniform highp mat4 v_matrix; \n"
133
"attribute vec2 a_texCoord; \n"
134
"uniform mat4 m_texMatrix; \n"
135
"varying vec2 v_texCoord; \n"
138
" gl_Position = v_matrix * a_position; \n"
139
" v_texCoord = (m_texMatrix * vec4(a_texCoord, 0.0, 1.0)).xy;\n"
143
const char *SnapshotGenerator::fragmentShader() const
146
"#extension GL_OES_EGL_image_external : require \n"
147
"precision mediump float; \n"
148
"varying vec2 v_texCoord; \n"
149
"uniform samplerExternalOES s_texture; \n"
152
" gl_FragColor = texture2D( s_texture, v_texCoord );\n"