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

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: 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 typedef ReturnType (*Function##Type) PARAMETERS;
jamesr 2015/10/21 19:58:53 nit: i find 'using' easier to read for function ty
Petr Hosek 2015/10/21 21:12:03 Done.
32 #include "mojo/public/platform/native/gles2/call_visitor_autogen.h"
33 #undef VISIT_GL_CALL
34
35 class SpinningCube::GLInterface {
36 private:
jamesr 2015/10/21 19:58:53 style guide has an order for public / private, thi
Petr Hosek 2015/10/21 21:12:03 I need this to be defined before I declare the mem
37 // This is a wrapper class that initializes pointer to nullptr.
38 template <typename T> class GLProcPtr {
39 public:
40 GLProcPtr() : ptr_(nullptr) {}
41 GLProcPtr operator=(T ptr) { ptr_ = ptr; return *this; }
42 operator T() const { return ptr_; }
43 private:
44 T ptr_;
45 };
46
47 public:
48 GLInterface() {}
49
50 void Init() {
51 #define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \
52 Function = reinterpret_cast<Function##Type>( \
53 MGLGetProcAddress("gl"#Function));
54 #include "mojo/public/platform/native/gles2/call_visitor_autogen.h"
55 #undef VISIT_GL_CALL
56 }
57
58 #define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \
59 GLProcPtr<Function##Type> Function;
60 #include "mojo/public/platform/native/gles2/call_visitor_autogen.h"
61 #undef VISIT_GL_CALL
62 };
63
29 namespace { 64 namespace {
30 65
31 const float kPi = 3.14159265359f; 66 const float kPi = 3.14159265359f;
32 const int kNumVertices = 24; 67 const int kNumVertices = 24;
33 68
34 int GenerateCube(GLuint *vbo_vertices, 69 int GenerateCube(const scoped_ptr<SpinningCube::GLInterface>& gl,
70 GLuint *vbo_vertices,
35 GLuint *vbo_indices) { 71 GLuint *vbo_indices) {
36 const int num_indices = 36; 72 const int num_indices = 36;
37 73
38 const GLfloat cube_vertices[kNumVertices * 3] = { 74 const GLfloat cube_vertices[kNumVertices * 3] = {
39 // -Y side. 75 // -Y side.
40 -0.5f, -0.5f, -0.5f, 76 -0.5f, -0.5f, -0.5f,
41 -0.5f, -0.5f, 0.5f, 77 -0.5f, -0.5f, 0.5f,
42 0.5f, -0.5f, 0.5f, 78 0.5f, -0.5f, 0.5f,
43 0.5f, -0.5f, -0.5f, 79 0.5f, -0.5f, -0.5f,
44 80
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 // -X side. 167 // -X side.
132 16, 17, 18, 168 16, 17, 18,
133 16, 18, 19, 169 16, 18, 19,
134 170
135 // +X side. 171 // +X side.
136 20, 23, 22, 172 20, 23, 22,
137 20, 22, 21 173 20, 22, 21
138 }; 174 };
139 175
140 if (vbo_vertices) { 176 if (vbo_vertices) {
141 glGenBuffers(1, vbo_vertices); 177 gl->GenBuffers(1, vbo_vertices);
142 glBindBuffer(GL_ARRAY_BUFFER, *vbo_vertices); 178 gl->BindBuffer(GL_ARRAY_BUFFER, *vbo_vertices);
143 glBufferData(GL_ARRAY_BUFFER, 179 gl->BufferData(GL_ARRAY_BUFFER,
144 sizeof(cube_vertices) + sizeof(vertex_normals), 180 sizeof(cube_vertices) + sizeof(vertex_normals),
145 nullptr, 181 nullptr,
146 GL_STATIC_DRAW); 182 GL_STATIC_DRAW);
147 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube_vertices), cube_vertices); 183 gl->BufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube_vertices), cube_vertices);
148 glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube_vertices), 184 gl->BufferSubData(GL_ARRAY_BUFFER, sizeof(cube_vertices),
149 sizeof(vertex_normals), vertex_normals); 185 sizeof(vertex_normals), vertex_normals);
150 glBindBuffer(GL_ARRAY_BUFFER, 0); 186 gl->BindBuffer(GL_ARRAY_BUFFER, 0);
151 } 187 }
152 188
153 if (vbo_indices) { 189 if (vbo_indices) {
154 glGenBuffers(1, vbo_indices); 190 gl->GenBuffers(1, vbo_indices);
155 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *vbo_indices); 191 gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, *vbo_indices);
156 glBufferData(GL_ELEMENT_ARRAY_BUFFER, 192 gl->BufferData(GL_ELEMENT_ARRAY_BUFFER,
157 sizeof(cube_indices), 193 sizeof(cube_indices),
158 cube_indices, 194 cube_indices,
159 GL_STATIC_DRAW); 195 GL_STATIC_DRAW);
160 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 196 gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
161 } 197 }
162 198
163 return num_indices; 199 return num_indices;
164 } 200 }
165 201
166 GLuint LoadShader(GLenum type, 202 GLuint LoadShader(const scoped_ptr<SpinningCube::GLInterface>& gl,
203 GLenum type,
167 const char* shader_source) { 204 const char* shader_source) {
168 GLuint shader = glCreateShader(type); 205 GLuint shader = gl->CreateShader(type);
169 glShaderSource(shader, 1, &shader_source, NULL); 206 gl->ShaderSource(shader, 1, &shader_source, NULL);
170 glCompileShader(shader); 207 gl->CompileShader(shader);
171 208
172 GLint compiled = 0; 209 GLint compiled = 0;
173 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); 210 gl->GetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
174 211
175 if (!compiled) { 212 if (!compiled) {
176 GLsizei expected_length = 0; 213 GLsizei expected_length = 0;
177 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &expected_length); 214 gl->GetShaderiv(shader, GL_INFO_LOG_LENGTH, &expected_length);
178 std::string log; 215 std::string log;
179 log.resize(expected_length); // Includes null terminator. 216 log.resize(expected_length); // Includes null terminator.
180 GLsizei actual_length = 0; 217 GLsizei actual_length = 0;
181 glGetShaderInfoLog(shader, expected_length, &actual_length, &log[0]); 218 gl->GetShaderInfoLog(shader, expected_length, &actual_length, &log[0]);
182 log.resize(actual_length); // Excludes null terminator. 219 log.resize(actual_length); // Excludes null terminator.
183 MOJO_LOG(FATAL) << "Compilation of shader failed: " << log; 220 MOJO_LOG(FATAL) << "Compilation of shader failed: " << log;
184 glDeleteShader(shader); 221 gl->DeleteShader(shader);
185 return 0; 222 return 0;
186 } 223 }
187 224
188 return shader; 225 return shader;
189 } 226 }
190 227
191 GLuint LoadProgram(const char* vertex_shader_source, 228 GLuint LoadProgram(const scoped_ptr<SpinningCube::GLInterface>& gl,
229 const char* vertex_shader_source,
192 const char* fragment_shader_source) { 230 const char* fragment_shader_source) {
193 GLuint vertex_shader = LoadShader(GL_VERTEX_SHADER, 231 GLuint vertex_shader = LoadShader(gl,
232 GL_VERTEX_SHADER,
194 vertex_shader_source); 233 vertex_shader_source);
195 if (!vertex_shader) 234 if (!vertex_shader)
196 return 0; 235 return 0;
197 236
198 GLuint fragment_shader = LoadShader(GL_FRAGMENT_SHADER, 237 GLuint fragment_shader = LoadShader(gl,
238 GL_FRAGMENT_SHADER,
199 fragment_shader_source); 239 fragment_shader_source);
200 if (!fragment_shader) { 240 if (!fragment_shader) {
201 glDeleteShader(vertex_shader); 241 gl->DeleteShader(vertex_shader);
202 return 0; 242 return 0;
203 } 243 }
204 244
205 GLuint program_object = glCreateProgram(); 245 GLuint program_object = gl->CreateProgram();
206 glAttachShader(program_object, vertex_shader); 246 gl->AttachShader(program_object, vertex_shader);
207 glAttachShader(program_object, fragment_shader); 247 gl->AttachShader(program_object, fragment_shader);
208 glLinkProgram(program_object); 248 gl->LinkProgram(program_object);
209 glDeleteShader(vertex_shader); 249 gl->DeleteShader(vertex_shader);
210 glDeleteShader(fragment_shader); 250 gl->DeleteShader(fragment_shader);
211 251
212 GLint linked = 0; 252 GLint linked = 0;
213 glGetProgramiv(program_object, GL_LINK_STATUS, &linked); 253 gl->GetProgramiv(program_object, GL_LINK_STATUS, &linked);
214 if (!linked) { 254 if (!linked) {
215 GLsizei expected_length = 0; 255 GLsizei expected_length = 0;
216 glGetProgramiv(program_object, GL_INFO_LOG_LENGTH, &expected_length); 256 gl->GetProgramiv(program_object, GL_INFO_LOG_LENGTH, &expected_length);
217 std::string log; 257 std::string log;
218 log.resize(expected_length); // Includes null terminator. 258 log.resize(expected_length); // Includes null terminator.
219 GLsizei actual_length = 0; 259 GLsizei actual_length = 0;
220 glGetProgramInfoLog(program_object, expected_length, &actual_length, 260 gl->GetProgramInfoLog(program_object, expected_length, &actual_length,
221 &log[0]); 261 &log[0]);
222 log.resize(actual_length); // Excludes null terminator. 262 log.resize(actual_length); // Excludes null terminator.
223 MOJO_LOG(FATAL) << "Linking program failed: " << log; 263 MOJO_LOG(FATAL) << "Linking program failed: " << log;
224 glDeleteProgram(program_object); 264 gl->DeleteProgram(program_object);
225 return 0; 265 return 0;
226 } 266 }
227 267
228 return program_object; 268 return program_object;
229 } 269 }
230 270
231 class ESMatrix { 271 class ESMatrix {
232 public: 272 public:
233 GLfloat m[4][4]; 273 GLfloat m[4][4];
234 274
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 mvp_location_ = 0; 454 mvp_location_ = 0;
415 vbo_vertices_ = 0; 455 vbo_vertices_ = 0;
416 vbo_indices_ = 0; 456 vbo_indices_ = 0;
417 num_indices_ = 0; 457 num_indices_ = 0;
418 } 458 }
419 459
420 SpinningCube::SpinningCube() 460 SpinningCube::SpinningCube()
421 : initialized_(false), 461 : initialized_(false),
422 width_(0), 462 width_(0),
423 height_(0), 463 height_(0),
464 gl_(new GLInterface()),
424 state_(new GLState()), 465 state_(new GLState()),
425 fling_multiplier_(1.0f), 466 fling_multiplier_(1.0f),
426 direction_(1), 467 direction_(1),
427 color_() { 468 color_() {
428 state_->angle_ = 45.0f; 469 state_->angle_ = 45.0f;
429 set_color(1.0, 1.0, 1.0); 470 set_color(1.0, 1.0, 1.0);
430 } 471 }
431 472
432 SpinningCube::~SpinningCube() { 473 SpinningCube::~SpinningCube() {
433 if (!initialized_) 474 if (!initialized_)
434 return; 475 return;
435 if (state_->vbo_vertices_) 476 if (state_->vbo_vertices_)
436 glDeleteBuffers(1, &state_->vbo_vertices_); 477 gl_->DeleteBuffers(1, &state_->vbo_vertices_);
437 if (state_->vbo_indices_) 478 if (state_->vbo_indices_)
438 glDeleteBuffers(1, &state_->vbo_indices_); 479 gl_->DeleteBuffers(1, &state_->vbo_indices_);
439 if (state_->program_object_) 480 if (state_->program_object_)
440 glDeleteProgram(state_->program_object_); 481 gl_->DeleteProgram(state_->program_object_);
441 } 482 }
442 483
443 void SpinningCube::Init() { 484 void SpinningCube::Init() {
444 const char vertex_shader_source[] = 485 const char vertex_shader_source[] =
445 "uniform mat4 u_mvpMatrix; \n" 486 "uniform mat4 u_mvpMatrix; \n"
446 "attribute vec4 a_position; \n" 487 "attribute vec4 a_position; \n"
447 "attribute vec4 a_normal; \n" 488 "attribute vec4 a_normal; \n"
448 "uniform vec3 u_color; \n" 489 "uniform vec3 u_color; \n"
449 "varying vec4 v_color; \n" 490 "varying vec4 v_color; \n"
450 "void main() \n" 491 "void main() \n"
(...skipping 10 matching lines...) Expand all
461 "} \n"; 502 "} \n";
462 503
463 const char fragment_shader_source[] = 504 const char fragment_shader_source[] =
464 "precision mediump float; \n" 505 "precision mediump float; \n"
465 "varying vec4 v_color; \n" 506 "varying vec4 v_color; \n"
466 "void main() \n" 507 "void main() \n"
467 "{ \n" 508 "{ \n"
468 " gl_FragColor = v_color; \n" 509 " gl_FragColor = v_color; \n"
469 "} \n"; 510 "} \n";
470 511
512 gl_->Init();
471 state_->program_object_ = LoadProgram( 513 state_->program_object_ = LoadProgram(
472 vertex_shader_source, fragment_shader_source); 514 gl_, vertex_shader_source, fragment_shader_source);
473 state_->position_location_ = glGetAttribLocation( 515 state_->position_location_ = gl_->GetAttribLocation(
474 state_->program_object_, "a_position"); 516 state_->program_object_, "a_position");
475 state_->normal_location_ = glGetAttribLocation( 517 state_->normal_location_ = gl_->GetAttribLocation(
476 state_->program_object_, "a_normal"); 518 state_->program_object_, "a_normal");
477 state_->color_location_ = glGetUniformLocation( 519 state_->color_location_ = gl_->GetUniformLocation(
478 state_->program_object_, "u_color"); 520 state_->program_object_, "u_color");
479 state_->mvp_location_ = glGetUniformLocation( 521 state_->mvp_location_ = gl_->GetUniformLocation(
480 state_->program_object_, "u_mvpMatrix"); 522 state_->program_object_, "u_mvpMatrix");
481 state_->num_indices_ = GenerateCube( 523 state_->num_indices_ = GenerateCube(
482 &state_->vbo_vertices_, &state_->vbo_indices_); 524 gl_, &state_->vbo_vertices_, &state_->vbo_indices_);
483 525
484 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 526 gl_->ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
485 glEnable(GL_DEPTH_TEST); 527 gl_->Enable(GL_DEPTH_TEST);
486 initialized_ = true; 528 initialized_ = true;
487 } 529 }
488 530
489 void SpinningCube::OnGLContextLost() { 531 void SpinningCube::OnGLContextLost() {
490 initialized_ = false; 532 initialized_ = false;
491 state_->OnGLContextLost(); 533 state_->OnGLContextLost();
492 } 534 }
493 535
494 void SpinningCube::SetFlingMultiplier(float drag_distance, 536 void SpinningCube::SetFlingMultiplier(float drag_distance,
495 float drag_time) { 537 float drag_time) {
(...skipping 22 matching lines...) Expand all
518 560
519 void SpinningCube::UpdateForDragDistance(float distance) { 561 void SpinningCube::UpdateForDragDistance(float distance) {
520 state_->angle_ += RotationForDragDistance(distance); 562 state_->angle_ += RotationForDragDistance(distance);
521 if (state_->angle_ >= 360.0f ) 563 if (state_->angle_ >= 360.0f )
522 state_->angle_ -= 360.0f; 564 state_->angle_ -= 360.0f;
523 565
524 Update(); 566 Update();
525 } 567 }
526 568
527 void SpinningCube::Draw() { 569 void SpinningCube::Draw() {
528 glViewport(0, 0, width_, height_); 570 gl_->Viewport(0, 0, width_, height_);
529 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 571 gl_->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
530 glUseProgram(state_->program_object_); 572 gl_->UseProgram(state_->program_object_);
531 glBindBuffer(GL_ARRAY_BUFFER, state_->vbo_vertices_); 573 gl_->BindBuffer(GL_ARRAY_BUFFER, state_->vbo_vertices_);
532 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state_->vbo_indices_); 574 gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, state_->vbo_indices_);
533 glVertexAttribPointer(state_->position_location_, 3, GL_FLOAT, GL_FALSE, 575 gl_->VertexAttribPointer(state_->position_location_, 3, GL_FLOAT, GL_FALSE,
534 3 * sizeof(GLfloat), 0); 576 3 * sizeof(GLfloat), 0);
535 glVertexAttribPointer(state_->normal_location_, 3, GL_FLOAT, GL_FALSE, 577 gl_->VertexAttribPointer(state_->normal_location_, 3, GL_FLOAT, GL_FALSE,
536 3 * sizeof(GLfloat), 578 3 * sizeof(GLfloat),
537 reinterpret_cast<void*>(3 * sizeof(GLfloat) * 579 reinterpret_cast<void*>(3 * sizeof(GLfloat) *
538 kNumVertices)); 580 kNumVertices));
539 glEnableVertexAttribArray(state_->position_location_); 581 gl_->EnableVertexAttribArray(state_->position_location_);
540 glEnableVertexAttribArray(state_->normal_location_); 582 gl_->EnableVertexAttribArray(state_->normal_location_);
541 glUniformMatrix4fv(state_->mvp_location_, 1, GL_FALSE, 583 gl_->UniformMatrix4fv(state_->mvp_location_, 1, GL_FALSE,
542 static_cast<GLfloat*>(&state_->mvp_matrix_.m[0][0])); 584 static_cast<GLfloat*>(&state_->mvp_matrix_.m[0][0]));
543 glUniform3fv(state_->color_location_, 1, color_); 585 gl_->Uniform3fv(state_->color_location_, 1, color_);
544 glDrawElements(GL_TRIANGLES, state_->num_indices_, GL_UNSIGNED_SHORT, 0); 586 gl_->DrawElements(GL_TRIANGLES, state_->num_indices_, GL_UNSIGNED_SHORT, 0);
545 } 587 }
546 588
547 void SpinningCube::Update() { 589 void SpinningCube::Update() {
548 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); 590 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_);
549 591
550 ESMatrix perspective; 592 ESMatrix perspective;
551 perspective.LoadIdentity(); 593 perspective.LoadIdentity();
552 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); 594 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f );
553 595
554 ESMatrix modelview; 596 ESMatrix modelview;
555 modelview.LoadIdentity(); 597 modelview.LoadIdentity();
556 modelview.Translate(0.0, 0.0, -2.0); 598 modelview.Translate(0.0, 0.0, -2.0);
557 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); 599 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0);
558 600
559 state_->mvp_matrix_.Multiply(&modelview, &perspective); 601 state_->mvp_matrix_.Multiply(&modelview, &perspective);
560 } 602 }
561 603
562 } // namespace examples 604 } // 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