|
| 1 | +#include "CameraSurfaceTexture.hpp" |
| 2 | + |
| 3 | +namespace camerakit { |
| 4 | + |
| 5 | + CameraSurfaceTexture::CameraSurfaceTexture(GLuint inputTexture, GLuint outputTexture) |
| 6 | + : width(0), |
| 7 | + height(0) { |
| 8 | + |
| 9 | + this->inputTexture = inputTexture; |
| 10 | + this->outputTexture = outputTexture; |
| 11 | + |
| 12 | + glBindTexture(GL_TEXTURE_EXTERNAL_OES, inputTexture); |
| 13 | + glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 14 | + glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 15 | + glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 16 | + glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 17 | + |
| 18 | + glGenBuffers(1, &vertexBuffer); |
| 19 | + glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); |
| 20 | + glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), VertexData(), GL_STATIC_DRAW); |
| 21 | + |
| 22 | + GLuint program = CreateProgram(VertexShaderCode(), FragmentShaderCode()); |
| 23 | + if (!program) { |
| 24 | + // TODO: throw here |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + glUseProgram(program); |
| 29 | + |
| 30 | + GLint aPosition = glGetAttribLocation(program, "aPosition"); |
| 31 | + GLint aTexCoord = glGetAttribLocation(program, "aTexCoord"); |
| 32 | + GLint uTransformMatrix = glGetUniformLocation(program, "uTransformMatrix"); |
| 33 | + GLint uRotationMatrix = glGetUniformLocation(program, "uRotationMatrix"); |
| 34 | + |
| 35 | + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 36 | + |
| 37 | + if (glGetError() != GL_NO_ERROR) { |
| 38 | + glDeleteProgram(program); |
| 39 | + |
| 40 | + // TODO: throw here |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + this->program = program; |
| 45 | + this->aPosition = aPosition; |
| 46 | + this->aTexCoord = aTexCoord; |
| 47 | + this->uTransformMatrix = uTransformMatrix; |
| 48 | + this->uRotationMatrix = uRotationMatrix; |
| 49 | + } |
| 50 | + |
| 51 | + CameraSurfaceTexture::~CameraSurfaceTexture() { |
| 52 | + if (vertexBuffer != 0) { |
| 53 | + glDeleteBuffers(1, &vertexBuffer); |
| 54 | + vertexBuffer = 0; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + void CameraSurfaceTexture::setSize(int width, int height) { |
| 59 | + this->width = width; |
| 60 | + this->height = height; |
| 61 | + |
| 62 | + if (glIsFramebuffer(framebuffer)) { |
| 63 | + glDeleteFramebuffers(1, &framebuffer); |
| 64 | + } |
| 65 | + |
| 66 | + glGenFramebuffers(1, &framebuffer); |
| 67 | + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 68 | + glBindTexture(GL_TEXTURE_2D, outputTexture); |
| 69 | + |
| 70 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 71 | + |
| 72 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 73 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 74 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 75 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 76 | + |
| 77 | + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, outputTexture, 0); |
| 78 | + glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 79 | + } |
| 80 | + |
| 81 | + void CameraSurfaceTexture::updateTexImage(float* transformMatrix, float* rotationMatrix) { |
| 82 | + glViewport(0, 0, width, height); |
| 83 | + |
| 84 | + glBindTexture(GL_TEXTURE_2D, 0); |
| 85 | + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 86 | + |
| 87 | + glDisable(GL_BLEND); |
| 88 | + glBindTexture(GL_TEXTURE_EXTERNAL_OES, inputTexture); |
| 89 | + |
| 90 | + glUseProgram(program); |
| 91 | + glUniformMatrix4fv(uTransformMatrix, 1, GL_FALSE, transformMatrix); |
| 92 | + glUniformMatrix4fv(uRotationMatrix, 1, GL_FALSE, rotationMatrix); |
| 93 | + glVertexAttribPointer(aPosition, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (const GLvoid*) (0 * sizeof(GLfloat))); |
| 94 | + glEnableVertexAttribArray(aPosition); |
| 95 | + glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (const GLvoid*) (4 * sizeof(GLfloat))); |
| 96 | + glEnableVertexAttribArray(aTexCoord); |
| 97 | + glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); |
| 98 | + glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, VertexIndices()); |
| 99 | + } |
| 100 | + |
| 101 | + const GLfloat* CameraSurfaceTexture::VertexData() { |
| 102 | + static const GLfloat vertexData[] = { |
| 103 | + -1.0f, -1.0f, 0.0, 1.0, 0.0f, 0.0f, |
| 104 | + +1.0f, -1.0f, 0.0, 1.0, 1.0f, 0.0f, |
| 105 | + -1.0f, +1.0f, 0.0, 1.0, 0.0f, 1.0f, |
| 106 | + +1.0f, +1.0f, 0.0, 1.0, 1.0f, 1.0f, |
| 107 | + }; |
| 108 | + |
| 109 | + return vertexData; |
| 110 | + } |
| 111 | + |
| 112 | + const GLushort* CameraSurfaceTexture::VertexIndices() { |
| 113 | + static const GLushort vertexIndices[] = { |
| 114 | + 0, 1, 2, 3 |
| 115 | + }; |
| 116 | + |
| 117 | + return vertexIndices; |
| 118 | + } |
| 119 | + |
| 120 | + const char* CameraSurfaceTexture::VertexShaderCode() { |
| 121 | + static const char vertexShader[] = |
| 122 | + "uniform mat4 uTransformMatrix;\n" |
| 123 | + "uniform mat4 uRotationMatrix;\n" |
| 124 | + "attribute vec4 aPosition;\n" |
| 125 | + "attribute vec4 aTexCoord;\n" |
| 126 | + "varying vec2 vTexCoord;\n" |
| 127 | + "void main() {\n" |
| 128 | + " gl_Position = uRotationMatrix * aPosition;\n" |
| 129 | + " vTexCoord = (uTransformMatrix * aTexCoord).xy;\n" |
| 130 | + "}\n"; |
| 131 | + |
| 132 | + return vertexShader; |
| 133 | + } |
| 134 | + |
| 135 | + const char* CameraSurfaceTexture::FragmentShaderCode() { |
| 136 | + static const char fragmentShader[] = |
| 137 | + "#extension GL_OES_EGL_image_external:require\n" |
| 138 | + "precision mediump float;\n" |
| 139 | + "uniform samplerExternalOES uTexture;\n" |
| 140 | + "varying vec2 vTexCoord;\n" |
| 141 | + "void main() {\n" |
| 142 | + " gl_FragColor = texture2D(uTexture, vTexCoord);\n" |
| 143 | + "}\n"; |
| 144 | + |
| 145 | + return fragmentShader; |
| 146 | + } |
| 147 | + |
| 148 | + GLuint CameraSurfaceTexture::LoadShader(GLenum shaderType, const char* shaderCode) { |
| 149 | + GLuint shader = glCreateShader(shaderType); |
| 150 | + if (shader) { |
| 151 | + glShaderSource(shader, 1, &shaderCode, NULL); |
| 152 | + glCompileShader(shader); |
| 153 | + GLint compileStatus = GL_FALSE; |
| 154 | + glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus); |
| 155 | + if (!compileStatus) { |
| 156 | + GLint infoLength = 0; |
| 157 | + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLength); |
| 158 | + if (infoLength) { |
| 159 | + char* infoBuffer = (char*) malloc((size_t) infoLength); |
| 160 | + if (infoBuffer) { |
| 161 | + glGetShaderInfoLog(shader, infoLength, NULL, infoBuffer); |
| 162 | + // todo: output log |
| 163 | + free(infoBuffer); |
| 164 | + } |
| 165 | + } |
| 166 | + glDeleteShader(shader); |
| 167 | + shader = 0; |
| 168 | + } |
| 169 | + } |
| 170 | + return shader; |
| 171 | + } |
| 172 | + |
| 173 | + GLuint CameraSurfaceTexture::CreateProgram(const char* vertexShaderCode, |
| 174 | + const char* fragmentShaderCode) { |
| 175 | + GLuint vertexShader = LoadShader(GL_VERTEX_SHADER, vertexShaderCode); |
| 176 | + if (!vertexShader) { |
| 177 | + return 0; |
| 178 | + } |
| 179 | + |
| 180 | + GLuint fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fragmentShaderCode); |
| 181 | + if (!fragmentShader) { |
| 182 | + return 0; |
| 183 | + } |
| 184 | + |
| 185 | + GLuint program = glCreateProgram(); |
| 186 | + if (program) { |
| 187 | + glAttachShader(program, vertexShader); |
| 188 | + // TODO: check error and throw if needed |
| 189 | + |
| 190 | + glAttachShader(program, fragmentShader); |
| 191 | + // TODO: check error and throw if needed |
| 192 | + |
| 193 | + glLinkProgram(program); |
| 194 | + GLint linkStatus = GL_FALSE; |
| 195 | + glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); |
| 196 | + if (!linkStatus) { |
| 197 | + GLint infoLength = 0; |
| 198 | + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLength); |
| 199 | + if (infoLength) { |
| 200 | + char* infoBuffer = (char*) malloc((size_t) infoLength); |
| 201 | + if (infoBuffer) { |
| 202 | + glGetProgramInfoLog(program, infoLength, NULL, infoBuffer); |
| 203 | + // todo: output log |
| 204 | + free(infoBuffer); |
| 205 | + } |
| 206 | + } |
| 207 | + glDeleteProgram(program); |
| 208 | + program = 0; |
| 209 | + } |
| 210 | + } |
| 211 | + return program; |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments