1 // Copyright (c) 2011 The Native Client Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXAMPLES_TUMBLER_CUBE_H_
6 #define EXAMPLES_TUMBLER_CUBE_H_
10 #include "opengl_context.h"
11 #include "opengl_context_ptrs.h"
15 // The Cube class provides a place to implement 3D rendering. It has a
16 // frame that it occupies in a browser window.
19 explicit Cube(SharedOpenGLContext opengl_context);
22 // Called once when a new RenderContext is first bound to the view. The
23 // bound context is guaranteed to be current and valid before calling this
27 // Called whenever the size of the browser view changes. This method is
28 // called at least once when the view is first made visible. Clamps the
30 void Resize(int width, int height);
32 // Called every time the view need to be drawn. The bound context is
33 // guaranteed to be current and valid before this method is called. The
34 // visible portion of the context is flushed to the browser after this
38 // Accessor for width and height. To change these, call Resize.
39 const int width() const {
43 const int height() const {
47 // Accessor/mutator for the camera orientation.
48 void GetOrientation(std::vector<float>* orientation) const {
51 (*orientation)[0] = static_cast<float>(orientation_[0]);
52 (*orientation)[1] = static_cast<float>(orientation_[1]);
53 (*orientation)[2] = static_cast<float>(orientation_[2]);
54 (*orientation)[3] = static_cast<float>(orientation_[3]);
56 void SetOrientation(const std::vector<float>& orientation) {
57 orientation_[0] = static_cast<GLfloat>(orientation[0]);
58 orientation_[1] = static_cast<GLfloat>(orientation[1]);
59 orientation_[2] = static_cast<GLfloat>(orientation[2]);
60 orientation_[3] = static_cast<GLfloat>(orientation[3]);
64 // Create the shaders used to draw the cube, and link them into a program.
65 // Initializes |shader_progam_object_|, |position_loction_| and
69 // Generates a cube as a series of GL_TRIANGLE_STRIPs, and initializes
70 // |index_count_| to the number of indices in the index list used as a VBO.
71 // Creates the |vbo_ids_| required for the vertex and index data and uploads
75 // Build up the model-view transform from the eye and orienation properties.
76 // Assumes that |model_view| is a 4x4 matrix.
77 void ComputeModelViewTransform(GLfloat* model_view);
79 SharedOpenGLContext opengl_context_;
82 GLuint shader_program_object_; // The compiled shaders.
83 GLint position_location_; // The position attribute location.
84 GLint color_location_; // The color attribute location.
85 GLint mvp_location_; // The Model-View-Projection composite matrix.
87 GLfloat eye_[3]; // The eye point of the virtual camera.
88 // The orientation of the virtual camera stored as a quaternion. The
89 // quaternion is laid out as {{x, y, z}, w}.
90 GLfloat orientation_[4];
91 GLfloat perspective_proj_[16];
92 GLfloat mvp_matrix_[16];
95 } // namespace tumbler
97 #endif // EXAMPLES_TUMBLER_CUBE_H_