Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / NativeClient / cube.h
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.
4
5 #ifndef EXAMPLES_TUMBLER_CUBE_H_
6 #define EXAMPLES_TUMBLER_CUBE_H_
7
8 #include <GLES2/gl2.h>
9 #include <vector>
10 #include "opengl_context.h"
11 #include "opengl_context_ptrs.h"
12
13 namespace tumbler {
14
15 // The Cube class provides a place to implement 3D rendering.  It has a
16 // frame that it occupies in a browser window.
17 class Cube {
18  public:
19   explicit Cube(SharedOpenGLContext opengl_context);
20   ~Cube();
21
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
24   // method.
25   void PrepareOpenGL();
26
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
29   // sizes to 1.
30   void Resize(int width, int height);
31
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
35   // method returns.
36   void Draw();
37
38   // Accessor for width and height.  To change these, call Resize.
39   const int width() const {
40     return width_;
41   }
42
43   const int height() const {
44     return height_;
45   }
46
47   // Accessor/mutator for the camera orientation.
48   void GetOrientation(std::vector<float>* orientation) const {
49     if (!orientation)
50       return;
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]);
55   }
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]);
61   }
62
63  private:
64   // Create the shaders used to draw the cube, and link them into a program.
65   // Initializes |shader_progam_object_|, |position_loction_| and
66   // |mvp_location_|.
67   bool CreateShaders();
68
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
72   // the the VBO data.
73   void CreateCube();
74
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);
78
79   SharedOpenGLContext opengl_context_;
80   int width_;
81   int height_;
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.
86   GLuint cube_vbos_[3];
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];
93 };
94
95 }  // namespace tumbler
96
97 #endif  // EXAMPLES_TUMBLER_CUBE_H_