OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file is here so other GLES2 related files can have a common set of | 5 // This file is here so other GLES2 related files can have a common set of |
6 // includes where appropriate. | 6 // includes where appropriate. |
7 | 7 |
8 #include "../client/gles2_demo_cc.h" | 8 #include "../client/gles2_demo_cc.h" |
9 #include "../common/logging.h" | 9 #include "../common/logging.h" |
10 | 10 |
11 #include <math.h> | 11 #include <math.h> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 // This is here so we have at least some idea that the inline path is working. | 14 // This is here so we have at least some idea that the inline path is working. |
15 #define GLES2_INLINE_OPTIMIZATION | 15 #define GLES2_INLINE_OPTIMIZATION |
16 #include <GLES2/gl2.h> | 16 #include <GLES2/gl2.h> |
| 17 #include <GLES2/gl2ext.h> |
17 #include "../common/logging.h" | 18 #include "../common/logging.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 GLuint g_texture = 0; | 22 int g_frameCount = 0; |
| 23 int g_textureIndex = 0; |
| 24 int g_numTextures = 1; |
| 25 GLuint g_textures[5]; |
22 int g_textureLoc = -1; | 26 int g_textureLoc = -1; |
23 GLuint g_programObject = 0; | 27 GLuint g_programObject = 0; |
24 GLuint g_worldMatrixLoc = 0; | 28 GLuint g_worldMatrixLoc = 0; |
25 GLuint g_vbo = 0; | 29 GLuint g_vbo = 0; |
26 GLsizei g_texCoordOffset = 0; | 30 GLsizei g_texCoordOffset = 0; |
27 int g_angle = 0; | 31 int g_angle = 0; |
28 | 32 |
29 void CheckGLError(const char* func_name, int line_no) { | 33 void CheckGLError(const char* func_name, int line_no) { |
30 #ifndef NDEBUG | 34 #ifndef NDEBUG |
31 GLenum error = GL_NO_ERROR; | 35 GLenum error = GL_NO_ERROR; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
161 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | 165 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
162 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, | 166 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, |
163 pixels); | 167 pixels); |
164 CheckGLError("CreateCheckerboardTexture", __LINE__); | 168 CheckGLError("CreateCheckerboardTexture", __LINE__); |
165 return texture; | 169 return texture; |
166 } | 170 } |
167 | 171 |
| 172 GLuint CreateCompressedTexture( |
| 173 const void* data, |
| 174 GLsizeiptr size, |
| 175 GLenum format, |
| 176 GLint width, |
| 177 GLint height) { |
| 178 GLuint texture; |
| 179 glGenTextures(1, &texture); |
| 180 glBindTexture(GL_TEXTURE_2D, texture); |
| 181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 185 glCompressedTexImage2D( |
| 186 GL_TEXTURE_2D, 0, format, width, height, 0, size, data); |
| 187 CheckGLError("CreateCompressedTxture", __LINE__); |
| 188 return texture; |
| 189 } |
| 190 |
| 191 GLuint LoadDXT1RGBTexture() { |
| 192 static unsigned char data[] = { |
| 193 0x00, 0xd0, 0x00, 0xef, 0x00, 0xaa, 0x95, 0xd5, |
| 194 0x00, 0x90, 0x57, 0xff, 0x00, 0xaa, 0xff, 0x55, |
| 195 0x00, 0x88, 0x99, 0xff, 0x00, 0xaa, 0xff, 0x55, |
| 196 0x20, 0xb8, 0xe4, 0xf6, 0x00, 0xaa, 0x5b, 0x5d, |
| 197 0x21, 0x09, 0xe6, 0x27, 0x15, 0x15, 0x15, 0x15, |
| 198 0xd7, 0xbd, 0xff, 0xff, 0x56, 0x16, 0x16, 0x56, |
| 199 0x00, 0x00, 0xff, 0xff, 0x55, 0x00, 0x00, 0x55, |
| 200 0xe0, 0x08, 0xa9, 0x2f, 0x51, 0x58, 0x56, 0x54, |
| 201 0xff, 0x07, 0x15, 0x09, 0x40, 0x6a, 0xd5, 0xd5, |
| 202 0xd7, 0xbd, 0xff, 0xff, 0x56, 0x16, 0x16, 0x16, |
| 203 0x91, 0x08, 0xff, 0xff, 0x55, 0xff, 0x00, 0x03, |
| 204 0xfe, 0x07, 0x5b, 0x09, 0x01, 0xa9, 0x55, 0xff, |
| 205 0x0d, 0x10, 0x18, 0xe8, 0xea, 0x15, 0x95, 0x55, |
| 206 0x08, 0x88, 0x3c, 0xe7, 0x55, 0x55, 0xff, 0x00, |
| 207 0x10, 0x20, 0x18, 0xe8, 0xa8, 0x54, 0x56, 0x55, |
| 208 0x1f, 0x78, 0x15, 0xf8, 0x00, 0xaa, 0x55, 0x55, |
| 209 }; |
| 210 return CreateCompressedTexture( |
| 211 data, sizeof(data), GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 16, 16); |
| 212 } |
| 213 |
| 214 GLuint LoadDXT1RGBATexture() { |
| 215 static unsigned char data[] = { |
| 216 0xff, 0xff, 0x90, 0xee, 0x00, 0xaa, 0xff, 0x55, |
| 217 0x53, 0xde, 0xdd, 0xff, 0x55, 0x55, 0x00, 0xff, |
| 218 0xc9, 0xb4, 0xdd, 0xff, 0x55, 0x55, 0xaa, 0xff, |
| 219 0x6f, 0xee, 0xdd, 0xff, 0x55, 0x55, 0xa8, 0x03, |
| 220 0x60, 0xa3, 0xa5, 0xe5, 0x55, 0x55, 0xaa, 0x00, |
| 221 0x00, 0x00, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, |
| 222 0x80, 0xab, 0xc2, 0xc4, 0xff, 0x55, 0xaa, 0xff, |
| 223 0x60, 0x9b, 0xa5, 0xe5, 0x57, 0x56, 0xaa, 0x00, |
| 224 0x1f, 0xbf, 0xff, 0xf7, 0x55, 0x55, 0xaa, 0x00, |
| 225 0x00, 0x00, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, |
| 226 0xfe, 0xbe, 0x7e, 0xdf, 0xff, 0xaa, 0x55, 0x00, |
| 227 0xfe, 0xbe, 0xff, 0xf7, 0x54, 0x56, 0xaa, 0x00, |
| 228 0xfa, 0x4c, 0x7e, 0x9e, 0x55, 0xaa, 0x00, 0x00, |
| 229 0xbb, 0x2c, 0x98, 0x54, 0xff, 0xff, 0x55, 0xaa, |
| 230 0xfa, 0x4c, 0x7e, 0x9e, 0x55, 0xaa, 0x00, 0x00, |
| 231 0xfa, 0x4c, 0x7e, 0x9e, 0x55, 0xaa, 0x00, 0x00, |
| 232 }; |
| 233 return CreateCompressedTexture( |
| 234 data, sizeof(data), GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 16, 16); |
| 235 } |
| 236 |
| 237 GLuint LoadDXT3RGBATexture() { |
| 238 static unsigned char data[] = { |
| 239 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 240 0xa3, 0x22, 0x03, 0x03, 0x55, 0xff, 0xaa, 0x00, |
| 241 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 242 0xcf, 0x7c, 0xc3, 0x12, 0x55, 0x55, 0x55, 0x54, |
| 243 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 244 0x83, 0x1a, 0x03, 0x03, 0x55, 0xff, 0x00, 0x00, |
| 245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, |
| 246 0xcf, 0x7c, 0xc3, 0x12, 0x55, 0x55, 0x55, 0x54, |
| 247 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 248 0xc3, 0x59, 0x63, 0x32, 0x55, 0xff, 0xaa, 0x00, |
| 249 0x00, 0x00, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, |
| 250 0x8f, 0x94, 0x03, 0x4a, 0x54, 0x94, 0x94, 0x54, |
| 251 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 252 0xa3, 0x59, 0x43, 0x2a, 0x55, 0xff, 0xaa, 0x00, |
| 253 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, |
| 254 0xaf, 0x84, 0x03, 0x42, 0x54, 0x55, 0x55, 0x55, |
| 255 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 256 0xc3, 0xa0, 0x83, 0x71, 0x55, 0xff, 0xaa, 0x00, |
| 257 0x00, 0x00, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, |
| 258 0x0f, 0xb4, 0x43, 0x81, 0x54, 0x94, 0x94, 0x94, |
| 259 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 260 0xc3, 0xa0, 0x83, 0x69, 0x55, 0xff, 0xaa, 0x00, |
| 261 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 262 0xc3, 0xa0, 0x83, 0x69, 0x55, 0xfd, 0xaa, 0x00, |
| 263 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 264 0x23, 0xd0, 0xa3, 0xb0, 0x55, 0xff, 0xaa, 0x00, |
| 265 0x00, 0x40, 0x00, 0x40, 0xff, 0xff, 0xff, 0xff, |
| 266 0xf0, 0xc3, 0x43, 0xc0, 0x94, 0x94, 0x55, 0x55, |
| 267 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 268 0x23, 0xd0, 0xa3, 0xb0, 0x55, 0xff, 0xaa, 0x00, |
| 269 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 270 0x23, 0xd0, 0xa3, 0xb0, 0x55, 0xff, 0xaa, 0x00, |
| 271 }; |
| 272 return CreateCompressedTexture( |
| 273 data, sizeof(data), GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 16, 16); |
| 274 } |
| 275 |
| 276 GLuint LoadDXT5RGBATexture() { |
| 277 static unsigned char data[] = { |
| 278 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 279 0x72, 0xdd, 0x03, 0x92, 0x2d, 0x2d, 0x2d, 0x2d, |
| 280 0x04, 0xfb, 0xff, 0xff, 0xff, 0x49, 0x02, 0xdb, |
| 281 0x97, 0xf6, 0x32, 0xcd, 0xc0, 0xc0, 0x7b, 0xc0, |
| 282 0xfb, 0xff, 0x49, 0x92, 0x24, 0x00, 0x60, 0xdb, |
| 283 0x11, 0xcd, 0x67, 0x82, 0x78, 0x78, 0x5e, 0x78, |
| 284 0x04, 0xfb, 0xff, 0xff, 0xff, 0xf9, 0x8f, 0xff, |
| 285 0x2e, 0xac, 0xc4, 0x71, 0x15, 0x15, 0x15, 0x14, |
| 286 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 287 0x72, 0xdd, 0x03, 0x92, 0x2d, 0x2d, 0x2d, 0x2d, |
| 288 0x04, 0x43, 0xb0, 0x0d, 0x3b, 0xb0, 0x03, 0xdb, |
| 289 0xb8, 0xf6, 0xd5, 0xd5, 0x60, 0x60, 0x60, 0x60, |
| 290 0xfb, 0x00, 0x49, 0x02, 0x00, 0x00, 0x90, 0x24, |
| 291 0xb0, 0xbc, 0x26, 0x7a, 0x78, 0x78, 0x78, 0x78, |
| 292 0x04, 0xf7, 0xf8, 0x9f, 0xff, 0xf9, 0x9f, 0xff, |
| 293 0x0e, 0xac, 0x83, 0x69, 0x34, 0x35, 0x35, 0x35, |
| 294 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 295 0x72, 0xdd, 0x03, 0x92, 0x2d, 0x2d, 0x2d, 0x2d, |
| 296 0x04, 0x43, 0xb0, 0x0d, 0x3b, 0xb0, 0x03, 0x3b, |
| 297 0xb8, 0xf6, 0xd5, 0xd5, 0x60, 0x60, 0x60, 0x60, |
| 298 0xfb, 0xff, 0xb6, 0x0d, 0x00, 0x49, 0x92, 0x24, |
| 299 0x11, 0xcd, 0x67, 0x82, 0x78, 0x5e, 0x78, 0x78, |
| 300 0xea, 0xff, 0x4a, 0xd2, 0x24, 0x49, 0x92, 0x24, |
| 301 0x0e, 0xac, 0xc4, 0x71, 0x15, 0x15, 0x15, 0x15, |
| 302 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 303 0x72, 0xdd, 0x03, 0x92, 0x2d, 0x2d, 0x2d, 0x2d, |
| 304 0xfd, 0x00, 0x49, 0x9c, 0xc4, 0x00, 0x00, 0x00, |
| 305 0xb8, 0xf6, 0x53, 0xcd, 0xe0, 0xe0, 0x7f, 0xe0, |
| 306 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 307 0xf1, 0xcc, 0x46, 0x82, 0x78, 0x78, 0x78, 0x78, |
| 308 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 309 0x0e, 0xac, 0xc4, 0x71, 0x15, 0x15, 0x15, 0x15, |
| 310 }; |
| 311 return CreateCompressedTexture( |
| 312 data, sizeof(data), GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 16, 16); |
| 313 } |
| 314 |
168 } // anonymous namespace. | 315 } // anonymous namespace. |
169 | 316 |
170 void GLFromCPPInit() { | 317 void GLFromCPPInit() { |
171 CheckGLError("GLFromCPPInit", __LINE__); | 318 CheckGLError("GLFromCPPInit", __LINE__); |
| 319 g_textures[0] = CreateCheckerboardTexture(); |
172 glClearColor(0.f, 0.f, .7f, 1.f); | 320 glClearColor(0.f, 0.f, .7f, 1.f); |
173 g_texture = CreateCheckerboardTexture(); | 321 const char* extensions = |
| 322 reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); |
| 323 if (strstr(extensions, "GL_EXT_texture_compression_dxt1")) { |
| 324 g_textures[g_numTextures++] = LoadDXT1RGBTexture(); |
| 325 g_textures[g_numTextures++] = LoadDXT1RGBATexture(); |
| 326 } |
| 327 if (strstr(extensions, "GL_CHROMIUM_texture_compression_dxt3")) { |
| 328 g_textures[g_numTextures++] = LoadDXT3RGBATexture(); |
| 329 } |
| 330 if (strstr(extensions, "GL_CHROMIUM_texture_compression_dxt5")) { |
| 331 g_textures[g_numTextures++] = LoadDXT5RGBATexture(); |
| 332 } |
174 InitShaders(); | 333 InitShaders(); |
175 CheckGLError("GLFromCPPInit", __LINE__); | 334 CheckGLError("GLFromCPPInit", __LINE__); |
176 | 335 |
177 char buf[128]; | 336 char buf[128]; |
178 glReadPixels(0, 0, 1, 1, 0, 0, buf); | 337 glReadPixels(0, 0, 1, 1, 0, 0, buf); |
179 CheckGLError("GLFromCPPInit:ReadPixels", __LINE__); | 338 CheckGLError("GLFromCPPInit:ReadPixels", __LINE__); |
180 } | 339 } |
181 | 340 |
182 void GLFromCPPDraw() { | 341 void GLFromCPPDraw() { |
183 const float kPi = 3.1415926535897932384626433832795f; | 342 const float kPi = 3.1415926535897932384626433832795f; |
(...skipping 19 matching lines...) Expand all Loading... |
203 rot_matrix[8] = 0.0f; | 362 rot_matrix[8] = 0.0f; |
204 rot_matrix[9] = 0.0f; | 363 rot_matrix[9] = 0.0f; |
205 rot_matrix[10] = 1.0f; | 364 rot_matrix[10] = 1.0f; |
206 rot_matrix[11] = 0.0f; | 365 rot_matrix[11] = 0.0f; |
207 | 366 |
208 rot_matrix[12] = 0.0f; | 367 rot_matrix[12] = 0.0f; |
209 rot_matrix[13] = 0.0f; | 368 rot_matrix[13] = 0.0f; |
210 rot_matrix[14] = 0.0f; | 369 rot_matrix[14] = 0.0f; |
211 rot_matrix[15] = 1.0f; | 370 rot_matrix[15] = 1.0f; |
212 | 371 |
| 372 g_frameCount++; |
| 373 if (g_frameCount > 60) |
| 374 { |
| 375 g_frameCount = 0; |
| 376 g_textureIndex = (g_textureIndex + 1) % g_numTextures; |
| 377 } |
| 378 |
213 // Note: the viewport is automatically set up to cover the entire Canvas. | 379 // Note: the viewport is automatically set up to cover the entire Canvas. |
214 // Clear the color buffer | 380 // Clear the color buffer |
215 glClear(GL_COLOR_BUFFER_BIT); | 381 glClear(GL_COLOR_BUFFER_BIT); |
| 382 glEnable(GL_BLEND); |
| 383 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
216 CheckGLError("GLFromCPPDraw", __LINE__); | 384 CheckGLError("GLFromCPPDraw", __LINE__); |
217 // Use the program object | 385 // Use the program object |
218 glUseProgram(g_programObject); | 386 glUseProgram(g_programObject); |
219 CheckGLError("GLFromCPPDraw", __LINE__); | 387 CheckGLError("GLFromCPPDraw", __LINE__); |
220 // Set up the model matrix | 388 // Set up the model matrix |
221 glUniformMatrix4fv(g_worldMatrixLoc, 1, GL_FALSE, rot_matrix); | 389 glUniformMatrix4fv(g_worldMatrixLoc, 1, GL_FALSE, rot_matrix); |
222 | 390 |
223 // Load the vertex data | 391 // Load the vertex data |
224 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); | 392 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); |
225 glEnableVertexAttribArray(0); | 393 glEnableVertexAttribArray(0); |
226 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); | 394 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); |
227 glEnableVertexAttribArray(1); | 395 glEnableVertexAttribArray(1); |
228 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, | 396 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, |
229 reinterpret_cast<const void*>(g_texCoordOffset)); | 397 reinterpret_cast<const void*>(g_texCoordOffset)); |
230 CheckGLError("GLFromCPPDraw", __LINE__); | 398 CheckGLError("GLFromCPPDraw", __LINE__); |
231 // Bind the texture to texture unit 0 | 399 // Bind the texture to texture unit 0 |
232 glBindTexture(GL_TEXTURE_2D, g_texture); | 400 glBindTexture(GL_TEXTURE_2D, g_textures[g_textureIndex]); |
233 CheckGLError("GLFromCPPDraw", __LINE__); | 401 CheckGLError("GLFromCPPDraw", __LINE__); |
234 // Point the uniform sampler to texture unit 0 | 402 // Point the uniform sampler to texture unit 0 |
235 glUniform1i(g_textureLoc, 0); | 403 glUniform1i(g_textureLoc, 0); |
236 CheckGLError("GLFromCPPDraw", __LINE__); | 404 CheckGLError("GLFromCPPDraw", __LINE__); |
237 glDrawArrays(GL_TRIANGLES, 0, 6); | 405 glDrawArrays(GL_TRIANGLES, 0, 6); |
238 CheckGLError("GLFromCPPDraw", __LINE__); | 406 CheckGLError("GLFromCPPDraw", __LINE__); |
239 glFlush(); | 407 glFlush(); |
240 } | 408 } |
OLD | NEW |