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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 const char kRTCVertexShaderSource[] = | 21 const char kRTCVertexShaderSource[] = |
22 SHADER_VERSION | 22 SHADER_VERSION |
23 VERTEX_SHADER_IN " vec2 position;\n" | 23 VERTEX_SHADER_IN " vec2 position;\n" |
24 VERTEX_SHADER_IN " vec2 texcoord;\n" | 24 VERTEX_SHADER_IN " vec2 texcoord;\n" |
25 VERTEX_SHADER_OUT " vec2 v_texcoord;\n" | 25 VERTEX_SHADER_OUT " vec2 v_texcoord;\n" |
26 "void main() {\n" | 26 "void main() {\n" |
27 " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" | 27 " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" |
28 " v_texcoord = texcoord;\n" | 28 " v_texcoord = texcoord;\n" |
29 "}\n"; | 29 "}\n"; |
30 | 30 |
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 | 31 // Compiles a shader of the given |type| with GLSL source |source| and returns |
47 // the shader handle or 0 on error. | 32 // the shader handle or 0 on error. |
48 GLuint RTCCreateShader(GLenum type, const GLchar *source) { | 33 GLuint RTCCreateShader(GLenum type, const GLchar *source) { |
49 GLuint shader = glCreateShader(type); | 34 GLuint shader = glCreateShader(type); |
50 if (!shader) { | 35 if (!shader) { |
51 return 0; | 36 return 0; |
52 } | 37 } |
53 glShaderSource(shader, 1, &source, NULL); | 38 glShaderSource(shader, 1, &source, NULL); |
54 glCompileShader(shader); | 39 glCompileShader(shader); |
55 GLint compileStatus = GL_FALSE; | 40 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. | 89 // Shaders are created only to generate program. |
105 if (vertexShader) { | 90 if (vertexShader) { |
106 glDeleteShader(vertexShader); | 91 glDeleteShader(vertexShader); |
107 } | 92 } |
108 if (fragmentShader) { | 93 if (fragmentShader) { |
109 glDeleteShader(fragmentShader); | 94 glDeleteShader(fragmentShader); |
110 } | 95 } |
111 return program; | 96 return program; |
112 } | 97 } |
113 | 98 |
114 // Set vertex shader variables 'position' and 'texcoord' in |program| to the | 99 // Set vertex shader variables 'position' and 'texcoord' in |program| to use |
115 // |gVertices| array above. It will use |vertexBuffer| and |vertexArray| to | 100 // |vertexBuffer| and |vertexArray| to store the vertex data. |
116 // store the vertex data. | |
117 BOOL RTCSetupVerticesForProgram(GLuint program, GLuint* vertexBuffer, GLuint* ve
rtexArray) { | 101 BOOL RTCSetupVerticesForProgram(GLuint program, GLuint* vertexBuffer, GLuint* ve
rtexArray) { |
118 GLint position = glGetAttribLocation(program, "position"); | 102 GLint position = glGetAttribLocation(program, "position"); |
119 GLint texcoord = glGetAttribLocation(program, "texcoord"); | 103 GLint texcoord = glGetAttribLocation(program, "texcoord"); |
120 if (position < 0 || texcoord < 0) { | 104 if (position < 0 || texcoord < 0) { |
121 return NO; | 105 return NO; |
122 } | 106 } |
123 #if !TARGET_OS_IPHONE | 107 #if !TARGET_OS_IPHONE |
124 glGenVertexArrays(1, vertexArray); | 108 glGenVertexArrays(1, vertexArray); |
125 if (*vertexArray == 0) { | 109 if (*vertexArray == 0) { |
126 return NO; | 110 return NO; |
127 } | 111 } |
128 glBindVertexArray(*vertexArray); | 112 glBindVertexArray(*vertexArray); |
129 #endif | 113 #endif |
130 glGenBuffers(1, vertexBuffer); | 114 glGenBuffers(1, vertexBuffer); |
131 if (*vertexBuffer == 0) { | 115 if (*vertexBuffer == 0) { |
132 return NO; | 116 return NO; |
133 } | 117 } |
134 glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer); | 118 glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer); |
135 glBufferData(GL_ARRAY_BUFFER, sizeof(gVertices), gVertices, GL_DYNAMIC_DRAW); | 119 glBufferData(GL_ARRAY_BUFFER, 4 * 4 * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW); |
136 | 120 |
137 // Read position attribute from |gVertices| with size of 2 and stride of 4 | 121 // 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 | 122 // start of the array. The last argument indicates offset of data within the |
139 // of data within |gVertices| as supplied to the vertex buffer. | 123 // vertex buffer. |
140 glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), | 124 glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), |
141 (void *)0); | 125 (void *)0); |
142 glEnableVertexAttribArray(position); | 126 glEnableVertexAttribArray(position); |
143 | 127 |
144 // Read texcoord attribute from |gVertices| with size of 2 and stride of 4 | 128 // 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 | 129 // beginning at the first texcoord in the array. The last argument indicates |
146 // offset of data within |gVertices| as supplied to the vertex buffer. | 130 // offset of data within |gVertices| as supplied to the vertex buffer. |
147 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), | 131 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), |
148 (void *)(2 * sizeof(GLfloat))); | 132 (void *)(2 * sizeof(GLfloat))); |
149 glEnableVertexAttribArray(texcoord); | 133 glEnableVertexAttribArray(texcoord); |
150 | 134 |
151 return YES; | 135 return YES; |
152 } | 136 } |
| 137 |
| 138 // Set vertex data to the currently bound vertex buffer. |
| 139 void RTCSetVertexData(int rotation) { |
| 140 // When modelview and projection matrices are identity (default) the world is |
| 141 // contained in the square around origin with unit size 2. Drawing to these |
| 142 // coordinates is equivalent to drawing to the entire screen. The texture is |
| 143 // stretched over that square using texture coordinates (u, v) that range |
| 144 // from (0, 0) to (1, 1) inclusive. Texture coordinates are flipped vertically |
| 145 // here because the incoming frame has origin in upper left hand corner but |
| 146 // OpenGL expects origin in bottom left corner. |
| 147 static const GLfloat UVCoords[4][2] = { |
| 148 {0, 1}, // Lower left. |
| 149 {1, 1}, // Lower right. |
| 150 {1, 0}, // Upper right. |
| 151 {0, 0}, // Upper left. |
| 152 }; |
| 153 |
| 154 const int offset = rotation / 90; |
| 155 const GLfloat gVertices[] = { |
| 156 // X, Y, U, V. |
| 157 -1, -1, UVCoords[(offset + 0) % 4][0], UVCoords[(offset + 0) % 4][1], |
| 158 1, -1, UVCoords[(offset + 1) % 4][0], UVCoords[(offset + 1) % 4][1], |
| 159 1, 1, UVCoords[(offset + 2) % 4][0], UVCoords[(offset + 2) % 4][1], |
| 160 -1, 1, UVCoords[(offset + 3) % 4][0], UVCoords[(offset + 3) % 4][1], |
| 161 }; |
| 162 |
| 163 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(gVertices), gVertices); |
| 164 } |
OLD | NEW |