VR SDK for Android 0.1.1 ARM Developer Center
armvr.h
Go to the documentation of this file.
1 /* Copyright (c) 2015-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef _armvr_h_
22 #define _armvr_h_
23 #include <EGL/egl.h>
24 #include <EGL/eglext.h>
25 #include <GLES/gl.h>
26 #include <GLES3/gl3.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <matrix.h>
30 #include <iostream>
31 #define Meter(x) (x)
32 #define Centimeter(x) (Meter(x) / 100.0f)
33 #define Millimeter(x) (Meter(x) / 1000.0f)
34 
35 // The following compile-time constants are used to calibrate the
36 // VR experience for each device and each user. For the optimal
37 // experience, you will need to perform measurements on your own
38 // device, head-mounted-display and eyes. The default values are
39 // calibrated for a Samsung Note 4 for a Gear VR SM-R320 HMD.
40 
41 #define Num_Eyes 2
42 #define Num_Views Num_Eyes * 2
43 
44 // The dimensions of the device screen in pixels and meters.
45 #define Screen_Resolution_X 2560
46 #define Screen_Resolution_Y 1440
47 #define Screen_Size_X Meter(0.125f)
48 #define Screen_Size_Y Meter(0.072f)
49 
50 // The dimensions of the framebuffers used for both eyes when
51 // rendering the scene. The values for these will balance visual
52 // quality and performance. The framebuffers will be scaled down
53 // or up to fit inside the viewports.
54 #define Eye_Fb_Resolution_X 1280
55 #define Eye_Fb_Resolution_Y 1440
56 
57 // If multisampling is available on the device the framebuffers
58 // will be rendered to using multisampling.
59 #define Multisample_Samples 4
60 
61 // The interpupillary distance (IPD) is the distance between
62 // your pupils when looking straight ahead. The human average
63 // is about 64mm, which is the same as the distance between the
64 // lenses of the Gear VR headset. The user should set these to
65 // their own measured IPD for the most comfortable experience.
66 #define Eye_IPD Millimeter(61.0f)
67 
68 // This should be set equal to the distance between the lens
69 // centres in the head-mounted display.
70 #define Lens_IPD Millimeter(64.0f)
71 
72 // This should be set equal to the distance between the display
73 // and the center point of the viewer's eye.
74 #define Eye_Display_Distance Centimeter(8.0f)
75 
76 // Defining border color enums in case the headers are not up to date for using the android extension pack.
77 #ifndef GL_TEXTURE_BORDER_COLOR_EXT
78 #define GL_TEXTURE_BORDER_COLOR_EXT 0x1004
79 #endif
80 
81 #ifndef GL_CLAMP_TO_BORDER_EXT
82 #define GL_CLAMP_TO_BORDER_EXT 0x812D
83 #endif
84 
85 #include <android/log.h>
86 #define LOG_TAG "ARMVR"
87 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
88 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
89 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
90 
92 {
93  int width;
94  int height;
98 };
99 // These coefficients control the degree of distortion that
100 // is applied on the rendertargets, per channel. The notation
101 // is the same as in the original Brown-Conray distortion
102 // correction model.
104 {
105  // Radial distortion coefficients
106  float k1; // Central
107  float k2; // Edge
108  float k3; // Fine
109  // Tangential distortion coefficients
110  float p1; // Horizontal
111  float p2; // Vertical
112 };
114 {
115  // One set for each channel, to handle chromatic aberration.
119  // The viewer may not look through the lens centre. This means
120  // that we need to perform an asymmetrical barrel distortion,
121  // centered at an offset given by the difference between the
122  // viewer's eye seperation and the HMD lens seperation.
124  // Each eye should be looking at the centre of each produced
125  // framebuffer image. To do this we must shift the result of
126  // each framebuffer by an appropriate amount, such that when
127  // the viewer looks straight ahead, the left pupil is in the
128  // centre of the left image and the right pupil vice versa.
130  // The distorted image will appear smaller on the screen,
131  // depending on the values of the distortion coefficients.
132  // We apply a shape-preserving upscale to make the distorted
133  // image fit into the edges of the screen. The value of this
134  // scale is somewhat arbitrarily chosen.
135  float fill_scale;
136 };
137 struct HMDConfig
138 {
141 };
142 struct WarpMesh
143 {
147 };
148 struct App
149 {
152  float frame_time;
154  // Distortion shader
165  // Cube shader
172  // Geometry
175  GLuint warp_mesh[Num_Eyes];
178 };
179 void gl_check(const char *msg);
180 void app_initialize(App *app);
183 // Convenience macros
184 #define GL_CHECK(x) \
185  x; \
186  { \
187  GLenum glError = glGetError(); \
188  if(glError != GL_NO_ERROR) { \
189  LOGE("glGetError() = %i (0x%.8x) at %s:%i\n", glError, glError, __FILE__, __LINE__); \
190  exit(1); \
191  } \
192  }
193 #define get_attrib_location(prog, name) \
194  app->a_##prog##_##name = GL_CHECK(glGetAttribLocation(app->program_##prog, #name)); \
195  if (app->a_##prog##_##name < 0) { \
196  LOGE("Invalid or unused attribute %s\n", #name); \
197  }
198 #define get_uniform_location(prog, name) \
199  app->u_##prog##_##name = GL_CHECK(glGetUniformLocation(app->program_##prog, #name)); \
200  if (app->u_##prog##_##name < 0) { \
201  LOGE("Invalid or unused uniform %s\n", #name); \
202  }
203 #define attribfv(prog, name, n, stride, offset) \
204  GL_CHECK(glEnableVertexAttribArray(app->a_##prog##_##name)); \
205  GL_CHECK(glVertexAttribPointer(app->a_##prog##_##name, n, GL_FLOAT, GL_FALSE, \
206  stride * sizeof(GLfloat), (void*)(offset * sizeof(GLfloat))));
207 #define attribiv(prog, name, n, stride, offset) \
208  GL_CHECK(glEnableVertexAttribArray(app->a_##prog##_##name)); \
209  GL_CHECK(glVertexAttribPointer(app->a_##prog##_##name, n, GL_INT, GL_FALSE, \
210  stride * sizeof(GLint), (void*)(offset * sizeof(GLint)) ));
211 #define uniform1f(prog, name, value) GL_CHECK(glUniform1f(app->u_##prog##_##name, value));
212 #define uniform2f(prog, name, x, y) GL_CHECK(glUniform2f(app->u_##prog##_##name, x, y));
213 #define uniform2fv(prog, name, value) GL_CHECK(glUniform2fv(app->u_##prog##_##name, 1, &value[0]));
214 #define uniform3fv(prog, name, value) GL_CHECK(glUniform3fv(app->u_##prog##_##name, 1, &value[0]));
215 #define uniform1i(prog, name, value) GL_CHECK(glUniform1i(app->u_##prog##_##name, value));
216 #define uniformm4(prog, name, value) GL_CHECK(glUniformMatrix4fv(app->u_##prog##_##name, 1, GL_FALSE, value.value_ptr()));
217 #define uniformm4array(prog, name, value, arraySize) GL_CHECK(glUniformMatrix4fv(app->u_##prog##_##name, arraySize, GL_FALSE, value.value_ptr()));
218 #endif
vec2 image_centre
Definition: armvr.h:129
DistortionCoefficients coefficients_green
Definition: armvr.h:117
int height
Definition: armvr.h:94
GLuint framebuffer
Definition: armvr.h:95
int index_count
Definition: armvr.h:146
GLuint colorbuffer
Definition: armvr.h:97
GLuint u_distort_framebuffer
Definition: armvr.h:164
Definition: matrix.h:28
GLuint u_cube_projection
Definition: armvr.h:169
GLuint program_distort
Definition: armvr.h:155
float frame_time
Definition: armvr.h:152
int window_width
Definition: armvr.h:150
static App app
Definition: main.cpp:30
void gl_check(const char *msg)
DistortionCoefficients coefficients_red
Definition: armvr.h:116
GLuint u_cube_view
Definition: armvr.h:170
void app_update_and_render(App *app)
Definition: armvr.cpp:424
GLuint a_distort_position
Definition: armvr.h:156
GLuint a_distort_uv_green_high_res
Definition: armvr.h:161
float fill_scale
Definition: armvr.h:135
void app_initialize(App *app)
Definition: armvr.cpp:294
float elapsed_time
Definition: armvr.h:153
GLuint a_cube_normal
Definition: armvr.h:168
LensConfig right
Definition: armvr.h:140
int window_height
Definition: armvr.h:151
GLuint a_cube_position
Definition: armvr.h:167
GLuint vao
Definition: armvr.h:173
HMDConfig hmd
Definition: armvr.h:176
GLuint program_cube
Definition: armvr.h:166
GLuint a_distort_uv_green_low_res
Definition: armvr.h:158
typedef GLuint
Definition: armvr.cpp:53
Framebuffer fb
Definition: armvr.h:177
GLuint ibo
Definition: armvr.h:145
GLuint a_distort_uv_blue_low_res
Definition: armvr.h:159
GLuint vbo_cube
Definition: armvr.h:174
#define Num_Eyes
Definition: armvr.h:41
int width
Definition: armvr.h:93
GLuint u_distort_layer_index
Definition: armvr.h:163
Definition: armvr.h:148
GLuint depthbuffer
Definition: armvr.h:96
LensConfig left
Definition: armvr.h:139
DistortionCoefficients coefficients_blue
Definition: armvr.h:118
GLuint vbo
Definition: armvr.h:144
GLuint a_distort_uv_blue_high_res
Definition: armvr.h:162
GLuint a_distort_uv_red_low_res
Definition: armvr.h:157
GLuint u_cube_model
Definition: armvr.h:171
vec2 distort_centre
Definition: armvr.h:123
GLuint a_distort_uv_red_high_res
Definition: armvr.h:160