Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: examples/spinning_cube/spinning_cube.cc

Issue 1416133002: Use MGLGetProcAddress to access OpenGL functions (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Don't use inner class Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « examples/spinning_cube/spinning_cube.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 example program is based on Simple_VertexShader.c from: 5 // This example program is based on Simple_VertexShader.c from:
6 6
7 // 7 //
8 // Book: OpenGL(R) ES 2.0 Programming Guide 8 // Book: OpenGL(R) ES 2.0 Programming Guide
9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner 9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
10 // ISBN-10: 0321502795 10 // ISBN-10: 0321502795
11 // ISBN-13: 9780321502797 11 // ISBN-13: 9780321502797
12 // Publisher: Addison-Wesley Professional 12 // Publisher: Addison-Wesley Professional
13 // URLs: http://safari.informit.com/9780321563835 13 // URLs: http://safari.informit.com/9780321563835
14 // http://www.opengles-book.com 14 // http://www.opengles-book.com
15 // 15 //
16 16
17 #include "examples/spinning_cube/spinning_cube.h" 17 #include "examples/spinning_cube/spinning_cube.h"
18 18
19 #include <GLES2/gl2.h> 19 #include <GLES2/gl2.h>
20 #include <GLES2/gl2ext.h> 20 #include <GLES2/gl2ext.h>
21 #include <math.h> 21 #include <math.h>
22 #include <stdlib.h> 22 #include <stdlib.h>
23 #include <string.h> 23 #include <string.h>
24 24
25 #include "mojo/public/c/gpu/MGL/mgl.h"
25 #include "mojo/public/cpp/environment/logging.h" 26 #include "mojo/public/cpp/environment/logging.h"
26 27
27 namespace examples { 28 namespace examples {
28 29
30 #define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \
31 using Function##Type = ReturnType (*)PARAMETERS;
32 #include "mojo/public/platform/native/gles2/call_visitor_autogen.h"
33 #undef VISIT_GL_CALL
34
35 class GLInterface {
36 public:
37 GLInterface() {
38 #define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \
39 Function = nullptr;
40 #include "mojo/public/platform/native/gles2/call_visitor_autogen.h"
41 #undef VISIT_GL_CALL
42 }
43
44 void Init() {
45 #define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \
46 Function = reinterpret_cast<Function##Type>( \
47 MGLGetProcAddress("gl"#Function));
48 #include "mojo/public/platform/native/gles2/call_visitor_autogen.h"
49 #undef VISIT_GL_CALL
50 }
51
52 #define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \
53 Function##Type Function;
54 #include "mojo/public/platform/native/gles2/call_visitor_autogen.h"
55 #undef VISIT_GL_CALL
56 };
57
29 namespace { 58 namespace {
30 59
31 const float kPi = 3.14159265359f; 60 const float kPi = 3.14159265359f;
32 const int kNumVertices = 24; 61 const int kNumVertices = 24;
33 62
34 int GenerateCube(GLuint *vbo_vertices, 63 int GenerateCube(const scoped_ptr<GLInterface>& gl,
64 GLuint *vbo_vertices,
35 GLuint *vbo_indices) { 65 GLuint *vbo_indices) {
36 const int num_indices = 36; 66 const int num_indices = 36;
37 67
38 const GLfloat cube_vertices[kNumVertices * 3] = { 68 const GLfloat cube_vertices[kNumVertices * 3] = {
39 // -Y side. 69 // -Y side.
40 -0.5f, -0.5f, -0.5f, 70 -0.5f, -0.5f, -0.5f,
41 -0.5f, -0.5f, 0.5f, 71 -0.5f, -0.5f, 0.5f,
42 0.5f, -0.5f, 0.5f, 72 0.5f, -0.5f, 0.5f,
43 0.5f, -0.5f, -0.5f, 73 0.5f, -0.5f, -0.5f,
44 74
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 // -X side. 161 // -X side.
132 16, 17, 18, 162 16, 17, 18,
133 16, 18, 19, 163 16, 18, 19,
134 164
135 // +X side. 165 // +X side.
136 20, 23, 22, 166 20, 23, 22,
137 20, 22, 21 167 20, 22, 21
138 }; 168 };
139 169
140 if (vbo_vertices) { 170 if (vbo_vertices) {
141 glGenBuffers(1, vbo_vertices); 171 gl->GenBuffers(1, vbo_vertices);
142 glBindBuffer(GL_ARRAY_BUFFER, *vbo_vertices); 172 gl->BindBuffer(GL_ARRAY_BUFFER, *vbo_vertices);
143 glBufferData(GL_ARRAY_BUFFER, 173 gl->BufferData(GL_ARRAY_BUFFER,
144 sizeof(cube_vertices) + sizeof(vertex_normals), 174 sizeof(cube_vertices) + sizeof(vertex_normals),
145 nullptr, 175 nullptr,
146 GL_STATIC_DRAW); 176 GL_STATIC_DRAW);
147 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube_vertices), cube_vertices); 177 gl->BufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube_vertices), cube_vertices);
148 glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube_vertices), 178 gl->BufferSubData(GL_ARRAY_BUFFER, sizeof(cube_vertices),
149 sizeof(vertex_normals), vertex_normals); 179 sizeof(vertex_normals), vertex_normals);
150 glBindBuffer(GL_ARRAY_BUFFER, 0); 180 gl->BindBuffer(GL_ARRAY_BUFFER, 0);
151 } 181 }
152 182
153 if (vbo_indices) { 183 if (vbo_indices) {
154 glGenBuffers(1, vbo_indices); 184 gl->GenBuffers(1, vbo_indices);
155 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *vbo_indices); 185 gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, *vbo_indices);
156 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 186 gl->BufferData(GL_ELEMENT_ARRAY_BUFFER,
157 sizeof(cube_indices), 187 sizeof(cube_indices),
158 cube_indices, 188 cube_indices,
159 GL_STATIC_DRAW); 189 GL_STATIC_DRAW);
160 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 190 gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
161 } 191 }
162 192
163 return num_indices; 193 return num_indices;
164 } 194 }
165 195
166 GLuint LoadShader(GLenum type, 196 GLuint LoadShader(const scoped_ptr<GLInterface>& gl,
197 GLenum type,
167 const char* shader_source) { 198 const char* shader_source) {
168 GLuint shader = glCreateShader(type); 199 GLuint shader = gl->CreateShader(type);
169 glShaderSource(shader, 1, &shader_source, NULL); 200 gl->ShaderSource(shader, 1, &shader_source, NULL);
170 glCompileShader(shader); 201 gl->CompileShader(shader);
171 202
172 GLint compiled = 0; 203 GLint compiled = 0;
173 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); 204 gl->GetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
174 205
175 if (!compiled) { 206 if (!compiled) {
176 GLsizei expected_length = 0; 207 GLsizei expected_length = 0;
177 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &expected_length); 208 gl->GetShaderiv(shader, GL_INFO_LOG_LENGTH, &expected_length);
178 std::string log; 209 std::string log;
179 log.resize(expected_length); // Includes null terminator. 210 log.resize(expected_length); // Includes null terminator.
180 GLsizei actual_length = 0; 211 GLsizei actual_length = 0;
181 glGetShaderInfoLog(shader, expected_length, &actual_length, &log[0]); 212 gl->GetShaderInfoLog(shader, expected_length, &actual_length, &log[0]);
182 log.resize(actual_length); // Excludes null terminator. 213 log.resize(actual_length); // Excludes null terminator.
183 MOJO_LOG(FATAL) << "Compilation of shader failed: " << log; 214 MOJO_LOG(FATAL) << "Compilation of shader failed: " << log;
184 glDeleteShader(shader); 215 gl->DeleteShader(shader);
185 return 0; 216 return 0;
186 } 217 }
187 218
188 return shader; 219 return shader;
189 } 220 }
190 221
191 GLuint LoadProgram(const char* vertex_shader_source, 222 GLuint LoadProgram(const scoped_ptr<GLInterface>& gl,
223 const char* vertex_shader_source,
192 const char* fragment_shader_source) { 224 const char* fragment_shader_source) {
193 GLuint vertex_shader = LoadShader(GL_VERTEX_SHADER, 225 GLuint vertex_shader = LoadShader(gl,
226 GL_VERTEX_SHADER,
194 vertex_shader_source); 227 vertex_shader_source);
195 if (!vertex_shader) 228 if (!vertex_shader)
196 return 0; 229 return 0;
197 230
198 GLuint fragment_shader = LoadShader(GL_FRAGMENT_SHADER, 231 GLuint fragment_shader = LoadShader(gl,
232 GL_FRAGMENT_SHADER,
199 fragment_shader_source); 233 fragment_shader_source);
200 if (!fragment_shader) { 234 if (!fragment_shader) {
201 glDeleteShader(vertex_shader); 235 gl->DeleteShader(vertex_shader);
202 return 0; 236 return 0;
203 } 237 }
204 238
205 GLuint program_object = glCreateProgram(); 239 GLuint program_object = gl->CreateProgram();
206 glAttachShader(program_object, vertex_shader); 240 gl->AttachShader(program_object, vertex_shader);
207 glAttachShader(program_object, fragment_shader); 241 gl->AttachShader(program_object, fragment_shader);
208 glLinkProgram(program_object); 242 gl->LinkProgram(program_object);
209 glDeleteShader(vertex_shader); 243 gl->DeleteShader(vertex_shader);
210 glDeleteShader(fragment_shader); 244 gl->DeleteShader(fragment_shader);
211 245
212 GLint linked = 0; 246 GLint linked = 0;
213 glGetProgramiv(program_object, GL_LINK_STATUS, &linked); 247 gl->GetProgramiv(program_object, GL_LINK_STATUS, &linked);
214 if (!linked) { 248 if (!linked) {
215 GLsizei expected_length = 0; 249 GLsizei expected_length = 0;
216 glGetProgramiv(program_object, GL_INFO_LOG_LENGTH, &expected_length); 250 gl->GetProgramiv(program_object, GL_INFO_LOG_LENGTH, &expected_length);
217 std::string log; 251 std::string log;
218 log.resize(expected_length); // Includes null terminator. 252 log.resize(expected_length); // Includes null terminator.
219 GLsizei actual_length = 0; 253 GLsizei actual_length = 0;
220 glGetProgramInfoLog(program_object, expected_length, &actual_length, 254 gl->GetProgramInfoLog(program_object, expected_length, &actual_length,
221 &log[0]); 255 &log[0]);
222 log.resize(actual_length); // Excludes null terminator. 256 log.resize(actual_length); // Excludes null terminator.
223 MOJO_LOG(FATAL) << "Linking program failed: " << log; 257 MOJO_LOG(FATAL) << "Linking program failed: " << log;
224 glDeleteProgram(program_object); 258 gl->DeleteProgram(program_object);
225 return 0; 259 return 0;
226 } 260 }
227 261
228 return program_object; 262 return program_object;
229 } 263 }
230 264
231 class ESMatrix { 265 class ESMatrix {
232 public: 266 public:
233 GLfloat m[4][4]; 267 GLfloat m[4][4];
234 268
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 mvp_location_ = 0; 448 mvp_location_ = 0;
415 vbo_vertices_ = 0; 449 vbo_vertices_ = 0;
416 vbo_indices_ = 0; 450 vbo_indices_ = 0;
417 num_indices_ = 0; 451 num_indices_ = 0;
418 } 452 }
419 453
420 SpinningCube::SpinningCube() 454 SpinningCube::SpinningCube()
421 : initialized_(false), 455 : initialized_(false),
422 width_(0), 456 width_(0),
423 height_(0), 457 height_(0),
458 gl_(new GLInterface()),
424 state_(new GLState()), 459 state_(new GLState()),
425 fling_multiplier_(1.0f), 460 fling_multiplier_(1.0f),
426 direction_(1), 461 direction_(1),
427 color_() { 462 color_() {
428 state_->angle_ = 45.0f; 463 state_->angle_ = 45.0f;
429 set_color(1.0, 1.0, 1.0); 464 set_color(1.0, 1.0, 1.0);
430 } 465 }
431 466
432 SpinningCube::~SpinningCube() { 467 SpinningCube::~SpinningCube() {
433 if (!initialized_) 468 if (!initialized_)
434 return; 469 return;
435 if (state_->vbo_vertices_) 470 if (state_->vbo_vertices_)
436 glDeleteBuffers(1, &state_->vbo_vertices_); 471 gl_->DeleteBuffers(1, &state_->vbo_vertices_);
437 if (state_->vbo_indices_) 472 if (state_->vbo_indices_)
438 glDeleteBuffers(1, &state_->vbo_indices_); 473 gl_->DeleteBuffers(1, &state_->vbo_indices_);
439 if (state_->program_object_) 474 if (state_->program_object_)
440 glDeleteProgram(state_->program_object_); 475 gl_->DeleteProgram(state_->program_object_);
441 } 476 }
442 477
443 void SpinningCube::Init() { 478 void SpinningCube::Init() {
444 const char vertex_shader_source[] = 479 const char vertex_shader_source[] =
445 "uniform mat4 u_mvpMatrix; \n" 480 "uniform mat4 u_mvpMatrix; \n"
446 "attribute vec4 a_position; \n" 481 "attribute vec4 a_position; \n"
447 "attribute vec4 a_normal; \n" 482 "attribute vec4 a_normal; \n"
448 "uniform vec3 u_color; \n" 483 "uniform vec3 u_color; \n"
449 "varying vec4 v_color; \n" 484 "varying vec4 v_color; \n"
450 "void main() \n" 485 "void main() \n"
(...skipping 10 matching lines...) Expand all
461 "} \n"; 496 "} \n";
462 497
463 const char fragment_shader_source[] = 498 const char fragment_shader_source[] =
464 "precision mediump float; \n" 499 "precision mediump float; \n"
465 "varying vec4 v_color; \n" 500 "varying vec4 v_color; \n"
466 "void main() \n" 501 "void main() \n"
467 "{ \n" 502 "{ \n"
468 " gl_FragColor = v_color; \n" 503 " gl_FragColor = v_color; \n"
469 "} \n"; 504 "} \n";
470 505
506 gl_->Init();
471 state_->program_object_ = LoadProgram( 507 state_->program_object_ = LoadProgram(
472 vertex_shader_source, fragment_shader_source); 508 gl_, vertex_shader_source, fragment_shader_source);
473 state_->position_location_ = glGetAttribLocation( 509 state_->position_location_ = gl_->GetAttribLocation(
474 state_->program_object_, "a_position"); 510 state_->program_object_, "a_position");
475 state_->normal_location_ = glGetAttribLocation( 511 state_->normal_location_ = gl_->GetAttribLocation(
476 state_->program_object_, "a_normal"); 512 state_->program_object_, "a_normal");
477 state_->color_location_ = glGetUniformLocation( 513 state_->color_location_ = gl_->GetUniformLocation(
478 state_->program_object_, "u_color"); 514 state_->program_object_, "u_color");
479 state_->mvp_location_ = glGetUniformLocation( 515 state_->mvp_location_ = gl_->GetUniformLocation(
480 state_->program_object_, "u_mvpMatrix"); 516 state_->program_object_, "u_mvpMatrix");
481 state_->num_indices_ = GenerateCube( 517 state_->num_indices_ = GenerateCube(
482 &state_->vbo_vertices_, &state_->vbo_indices_); 518 gl_, &state_->vbo_vertices_, &state_->vbo_indices_);
483 519
484 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 520 gl_->ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
485 glEnable(GL_DEPTH_TEST); 521 gl_->Enable(GL_DEPTH_TEST);
486 initialized_ = true; 522 initialized_ = true;
487 } 523 }
488 524
489 void SpinningCube::OnGLContextLost() { 525 void SpinningCube::OnGLContextLost() {
490 initialized_ = false; 526 initialized_ = false;
491 state_->OnGLContextLost(); 527 state_->OnGLContextLost();
492 } 528 }
493 529
494 void SpinningCube::SetFlingMultiplier(float drag_distance, 530 void SpinningCube::SetFlingMultiplier(float drag_distance,
495 float drag_time) { 531 float drag_time) {
(...skipping 22 matching lines...) Expand all
518 554
519 void SpinningCube::UpdateForDragDistance(float distance) { 555 void SpinningCube::UpdateForDragDistance(float distance) {
520 state_->angle_ += RotationForDragDistance(distance); 556 state_->angle_ += RotationForDragDistance(distance);
521 if (state_->angle_ >= 360.0f ) 557 if (state_->angle_ >= 360.0f )
522 state_->angle_ -= 360.0f; 558 state_->angle_ -= 360.0f;
523 559
524 Update(); 560 Update();
525 } 561 }
526 562
527 void SpinningCube::Draw() { 563 void SpinningCube::Draw() {
528 glViewport(0, 0, width_, height_); 564 gl_->Viewport(0, 0, width_, height_);
529 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 565 gl_->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
530 glUseProgram(state_->program_object_); 566 gl_->UseProgram(state_->program_object_);
531 glBindBuffer(GL_ARRAY_BUFFER, state_->vbo_vertices_); 567 gl_->BindBuffer(GL_ARRAY_BUFFER, state_->vbo_vertices_);
532 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state_->vbo_indices_); 568 gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, state_->vbo_indices_);
533 glVertexAttribPointer(state_->position_location_, 3, GL_FLOAT, GL_FALSE, 569 gl_->VertexAttribPointer(state_->position_location_, 3, GL_FLOAT, GL_FALSE,
534 3 * sizeof(GLfloat), 0); 570 3 * sizeof(GLfloat), 0);
535 glVertexAttribPointer(state_->normal_location_, 3, GL_FLOAT, GL_FALSE, 571 gl_->VertexAttribPointer(state_->normal_location_, 3, GL_FLOAT, GL_FALSE,
536 3 * sizeof(GLfloat), 572 3 * sizeof(GLfloat),
537 reinterpret_cast<void*>(3 * sizeof(GLfloat) * 573 reinterpret_cast<void*>(3 * sizeof(GLfloat) *
538 kNumVertices)); 574 kNumVertices));
539 glEnableVertexAttribArray(state_->position_location_); 575 gl_->EnableVertexAttribArray(state_->position_location_);
540 glEnableVertexAttribArray(state_->normal_location_); 576 gl_->EnableVertexAttribArray(state_->normal_location_);
541 glUniformMatrix4fv(state_->mvp_location_, 1, GL_FALSE, 577 gl_->UniformMatrix4fv(state_->mvp_location_, 1, GL_FALSE,
542 static_cast<GLfloat*>(&state_->mvp_matrix_.m[0][0])); 578 static_cast<GLfloat*>(&state_->mvp_matrix_.m[0][0]));
543 glUniform3fv(state_->color_location_, 1, color_); 579 gl_->Uniform3fv(state_->color_location_, 1, color_);
544 glDrawElements(GL_TRIANGLES, state_->num_indices_, GL_UNSIGNED_SHORT, 0); 580 gl_->DrawElements(GL_TRIANGLES, state_->num_indices_, GL_UNSIGNED_SHORT, 0);
545 } 581 }
546 582
547 void SpinningCube::Update() { 583 void SpinningCube::Update() {
548 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); 584 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_);
549 585
550 ESMatrix perspective; 586 ESMatrix perspective;
551 perspective.LoadIdentity(); 587 perspective.LoadIdentity();
552 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); 588 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f );
553 589
554 ESMatrix modelview; 590 ESMatrix modelview;
555 modelview.LoadIdentity(); 591 modelview.LoadIdentity();
556 modelview.Translate(0.0, 0.0, -2.0); 592 modelview.Translate(0.0, 0.0, -2.0);
557 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); 593 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0);
558 594
559 state_->mvp_matrix_.Multiply(&modelview, &perspective); 595 state_->mvp_matrix_.Multiply(&modelview, &perspective);
560 } 596 }
561 597
562 } // namespace examples 598 } // namespace examples
OLDNEW
« no previous file with comments | « examples/spinning_cube/spinning_cube.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698