OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #import "RTCShader.h" | 11 #import "RTCShader.h" |
12 | 12 |
| 13 #include <algorithm> |
| 14 #include <array> |
13 #include <memory> | 15 #include <memory> |
14 | 16 |
15 #import "RTCShader+Private.h" | 17 #import "RTCShader+Private.h" |
16 | 18 |
17 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
18 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
19 | 21 |
20 // Vertex shader doesn't do anything except pass coordinates through. | 22 // Vertex shader doesn't do anything except pass coordinates through. |
21 const char kRTCVertexShaderSource[] = | 23 const char kRTCVertexShaderSource[] = |
22 SHADER_VERSION | 24 SHADER_VERSION |
23 VERTEX_SHADER_IN " vec2 position;\n" | 25 VERTEX_SHADER_IN " vec2 position;\n" |
24 VERTEX_SHADER_IN " vec2 texcoord;\n" | 26 VERTEX_SHADER_IN " vec2 texcoord;\n" |
25 VERTEX_SHADER_OUT " vec2 v_texcoord;\n" | 27 VERTEX_SHADER_OUT " vec2 v_texcoord;\n" |
26 "void main() {\n" | 28 "void main() {\n" |
27 " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" | 29 " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" |
28 " v_texcoord = texcoord;\n" | 30 " v_texcoord = texcoord;\n" |
29 "}\n"; | 31 "}\n"; |
30 | 32 |
31 // When modelview and projection matrices are identity (default) the world is | |
32 // contained in the square around origin with unit size 2. Drawing to these | |
33 // coordinates is equivalent to drawing to the entire screen. The texture is | |
34 // stretched over that square using texture coordinates (u, v) that range | |
35 // from (0, 0) to (1, 1) inclusive. Texture coordinates are flipped vertically | |
36 // here because the incoming frame has origin in upper left hand corner but | |
37 // OpenGL expects origin in bottom left corner. | |
38 static const GLfloat gVertices[] = { | |
39 // X, Y, U, V. | |
40 -1, -1, 0, 1, // Bottom left. | |
41 1, -1, 1, 1, // Bottom right. | |
42 1, 1, 1, 0, // Top right. | |
43 -1, 1, 0, 0, // Top left. | |
44 }; | |
45 | |
46 // Compiles a shader of the given |type| with GLSL source |source| and returns | 33 // Compiles a shader of the given |type| with GLSL source |source| and returns |
47 // the shader handle or 0 on error. | 34 // the shader handle or 0 on error. |
48 GLuint RTCCreateShader(GLenum type, const GLchar *source) { | 35 GLuint RTCCreateShader(GLenum type, const GLchar *source) { |
49 GLuint shader = glCreateShader(type); | 36 GLuint shader = glCreateShader(type); |
50 if (!shader) { | 37 if (!shader) { |
51 return 0; | 38 return 0; |
52 } | 39 } |
53 glShaderSource(shader, 1, &source, NULL); | 40 glShaderSource(shader, 1, &source, NULL); |
54 glCompileShader(shader); | 41 glCompileShader(shader); |
55 GLint compileStatus = GL_FALSE; | 42 GLint compileStatus = GL_FALSE; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // Shaders are created only to generate program. | 91 // Shaders are created only to generate program. |
105 if (vertexShader) { | 92 if (vertexShader) { |
106 glDeleteShader(vertexShader); | 93 glDeleteShader(vertexShader); |
107 } | 94 } |
108 if (fragmentShader) { | 95 if (fragmentShader) { |
109 glDeleteShader(fragmentShader); | 96 glDeleteShader(fragmentShader); |
110 } | 97 } |
111 return program; | 98 return program; |
112 } | 99 } |
113 | 100 |
114 // Set vertex shader variables 'position' and 'texcoord' in |program| to the | 101 // Set vertex shader variables 'position' and 'texcoord' in |program| to use |
115 // |gVertices| array above. It will use |vertexBuffer| and |vertexArray| to | 102 // |vertexBuffer| and |vertexArray| to store the vertex data. |
116 // store the vertex data. | |
117 BOOL RTCSetupVerticesForProgram(GLuint program, GLuint* vertexBuffer, GLuint* ve
rtexArray) { | 103 BOOL RTCSetupVerticesForProgram(GLuint program, GLuint* vertexBuffer, GLuint* ve
rtexArray) { |
118 GLint position = glGetAttribLocation(program, "position"); | 104 GLint position = glGetAttribLocation(program, "position"); |
119 GLint texcoord = glGetAttribLocation(program, "texcoord"); | 105 GLint texcoord = glGetAttribLocation(program, "texcoord"); |
120 if (position < 0 || texcoord < 0) { | 106 if (position < 0 || texcoord < 0) { |
121 return NO; | 107 return NO; |
122 } | 108 } |
123 #if !TARGET_OS_IPHONE | 109 #if !TARGET_OS_IPHONE |
124 glGenVertexArrays(1, vertexArray); | 110 glGenVertexArrays(1, vertexArray); |
125 if (*vertexArray == 0) { | 111 if (*vertexArray == 0) { |
126 return NO; | 112 return NO; |
127 } | 113 } |
128 glBindVertexArray(*vertexArray); | 114 glBindVertexArray(*vertexArray); |
129 #endif | 115 #endif |
130 glGenBuffers(1, vertexBuffer); | 116 glGenBuffers(1, vertexBuffer); |
131 if (*vertexBuffer == 0) { | 117 if (*vertexBuffer == 0) { |
132 return NO; | 118 return NO; |
133 } | 119 } |
134 glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer); | 120 glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer); |
135 glBufferData(GL_ARRAY_BUFFER, sizeof(gVertices), gVertices, GL_DYNAMIC_DRAW); | 121 glBufferData(GL_ARRAY_BUFFER, 4 * 4 * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW); |
136 | 122 |
137 // Read position attribute from |gVertices| with size of 2 and stride of 4 | 123 // Read position attribute with size of 2 and stride of 4 beginning at the |
138 // beginning at the start of the array. The last argument indicates offset | 124 // start of the array. The last argument indicates offset of data within the |
139 // of data within |gVertices| as supplied to the vertex buffer. | 125 // vertex buffer. |
140 glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), | 126 glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), |
141 (void *)0); | 127 (void *)0); |
142 glEnableVertexAttribArray(position); | 128 glEnableVertexAttribArray(position); |
143 | 129 |
144 // Read texcoord attribute from |gVertices| with size of 2 and stride of 4 | 130 // Read texcoord attribute from |gVertices| with size of 2 and stride of 4 |
145 // beginning at the first texcoord in the array. The last argument indicates | 131 // beginning at the first texcoord in the array. The last argument indicates |
146 // offset of data within |gVertices| as supplied to the vertex buffer. | 132 // offset of data within |gVertices| as supplied to the vertex buffer. |
147 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), | 133 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), |
148 (void *)(2 * sizeof(GLfloat))); | 134 (void *)(2 * sizeof(GLfloat))); |
149 glEnableVertexAttribArray(texcoord); | 135 glEnableVertexAttribArray(texcoord); |
150 | 136 |
151 return YES; | 137 return YES; |
152 } | 138 } |
| 139 |
| 140 // Set vertex data to the currently bound vertex buffer. |
| 141 void RTCSetVertexData(webrtc::VideoRotation rotation) { |
| 142 // When modelview and projection matrices are identity (default) the world is |
| 143 // contained in the square around origin with unit size 2. Drawing to these |
| 144 // coordinates is equivalent to drawing to the entire screen. The texture is |
| 145 // stretched over that square using texture coordinates (u, v) that range |
| 146 // from (0, 0) to (1, 1) inclusive. Texture coordinates are flipped vertically |
| 147 // here because the incoming frame has origin in upper left hand corner but |
| 148 // OpenGL expects origin in bottom left corner. |
| 149 std::array<std::array<GLfloat, 2>, 4> UVCoords = {{ |
| 150 {{0, 1}}, // Lower left. |
| 151 {{1, 1}}, // Lower right. |
| 152 {{1, 0}}, // Upper right. |
| 153 {{0, 0}}, // Upper left. |
| 154 }}; |
| 155 |
| 156 // Rotate the UV coordinates. |
| 157 int rotation_offset; |
| 158 switch (rotation) { |
| 159 case webrtc::kVideoRotation_0: |
| 160 rotation_offset = 0; |
| 161 break; |
| 162 case webrtc::kVideoRotation_90: |
| 163 rotation_offset = 1; |
| 164 break; |
| 165 case webrtc::kVideoRotation_180: |
| 166 rotation_offset = 2; |
| 167 break; |
| 168 case webrtc::kVideoRotation_270: |
| 169 rotation_offset = 3; |
| 170 break; |
| 171 } |
| 172 std::rotate(UVCoords.begin(), UVCoords.begin() + rotation_offset, |
| 173 UVCoords.end()); |
| 174 |
| 175 const GLfloat gVertices[] = { |
| 176 // X, Y, U, V. |
| 177 -1, -1, UVCoords[0][0], UVCoords[0][1], |
| 178 1, -1, UVCoords[1][0], UVCoords[1][1], |
| 179 1, 1, UVCoords[2][0], UVCoords[2][1], |
| 180 -1, 1, UVCoords[3][0], UVCoords[3][1], |
| 181 }; |
| 182 |
| 183 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(gVertices), gVertices); |
| 184 } |
OLD | NEW |