25 #define View_Resolution_X (Screen_Resolution_X / 2) 26 #define View_Resolution_Y Screen_Resolution_Y 33 #define View_Aspect_Ratio ((float)View_Resolution_X / (float)View_Resolution_Y) 34 #define Eye_Fb_Aspect_Ratio ((float)Eye_Fb_Resolution_X / (float)Eye_Fb_Resolution_Y) 40 #define Z_Near (Eye_Display_Distance) 41 #define Z_Far Meter(12.0f) 49 #define Warp_Mesh_Resolution_X 64 50 #define Warp_Mesh_Resolution_Y 64 61 PFNGLFRAMEBUFFERTEXTUREMULTISAMPLEMULTIVIEWOVR glFramebufferTextureMultisampleMultiviewOVR =
62 (PFNGLFRAMEBUFFERTEXTUREMULTISAMPLEMULTIVIEWOVR)eglGetProcAddress (
"glFramebufferTextureMultisampleMultiviewOVR");
63 if (!glFramebufferTextureMultiviewOVR)
65 LOGE(
"Did not have glFramebufferTextureMultiviewOVR\n");
68 if (!glFramebufferTextureMultisampleMultiviewOVR)
70 LOGE(
"Did not have glFramebufferTextureMultisampleMultiviewOVR\n");
72 bool have_multisampled_ext = glFramebufferTextureMultisampleMultiviewOVR != 0;
77 GL_CHECK(glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_DEPTH_COMPONENT16, width, height, num_views));
78 if (have_multisampled_ext)
84 GL_CHECK(glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, result.
depthbuffer, 0, 0, num_views));
88 GL_CHECK(glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, width, height, num_views));
89 GL_CHECK(glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
90 GL_CHECK(glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
93 GLint border_color[4] = {0, 0, 0, 0};
95 GL_CHECK(glBindTexture(GL_TEXTURE_2D_ARRAY, 0));
96 if (have_multisampled_ext)
102 GL_CHECK(glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, result.
colorbuffer, 0, 0, num_views));
104 GLenum status =
GL_CHECK(glCheckFramebufferStatus(GL_FRAMEBUFFER));
105 if (status != GL_FRAMEBUFFER_COMPLETE)
106 LOGE(
"Framebuffer not complete\n");
107 GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
114 -1.0f, -1.0f, +1.0f, 0.0f, 0.0f, +1.0f,
115 +1.0f, -1.0f, +1.0f, 0.0f, 0.0f, +1.0f,
116 +1.0f, +1.0f, +1.0f, 0.0f, 0.0f, +1.0f,
117 +1.0f, +1.0f, +1.0f, 0.0f, 0.0f, +1.0f,
118 -1.0f, +1.0f, +1.0f, 0.0f, 0.0f, +1.0f,
119 -1.0f, -1.0f, +1.0f, 0.0f, 0.0f, +1.0f,
121 -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
122 -1.0f, +1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
123 +1.0f, +1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
124 +1.0f, +1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
125 +1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
126 -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
128 -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
129 -1.0f, -1.0f, +1.0f, -1.0f, 0.0f, 0.0f,
130 -1.0f, +1.0f, +1.0f, -1.0f, 0.0f, 0.0f,
131 -1.0f, +1.0f, +1.0f, -1.0f, 0.0f, 0.0f,
132 -1.0f, +1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
133 -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
135 +1.0f, -1.0f, -1.0f, +1.0f, 0.0f, 0.0f,
136 +1.0f, +1.0f, -1.0f, +1.0f, 0.0f, 0.0f,
137 +1.0f, +1.0f, +1.0f, +1.0f, 0.0f, 0.0f,
138 +1.0f, +1.0f, +1.0f, +1.0f, 0.0f, 0.0f,
139 +1.0f, -1.0f, +1.0f, +1.0f, 0.0f, 0.0f,
140 +1.0f, -1.0f, -1.0f, +1.0f, 0.0f, 0.0f,
142 -1.0f, +1.0f, -1.0f, 0.0f, +1.0f, 0.0f,
143 -1.0f, +1.0f, +1.0f, 0.0f, +1.0f, 0.0f,
144 +1.0f, +1.0f, +1.0f, 0.0f, +1.0f, 0.0f,
145 +1.0f, +1.0f, +1.0f, 0.0f, +1.0f, 0.0f,
146 +1.0f, +1.0f, -1.0f, 0.0f, +1.0f, 0.0f,
147 -1.0f, +1.0f, -1.0f, 0.0f, +1.0f, 0.0f,
149 -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,
150 +1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,
151 +1.0f, -1.0f, +1.0f, 0.0f, -1.0f, 0.0f,
152 +1.0f, -1.0f, +1.0f, 0.0f, -1.0f, 0.0f,
153 -1.0f, -1.0f, +1.0f, 0.0f, -1.0f, 0.0f,
154 -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f
158 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, result));
159 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
sizeof(v), v, GL_STATIC_DRAW));
167 float tex_coord_factor)
169 float k1 = coefficients.
k1;
170 float k2 = coefficients.
k2;
171 float k3 = coefficients.
k3;
172 float p1 = coefficients.
p1;
173 float p2 = coefficients.
p2;
177 float dx = x - distort_centre.
x;
178 float dy = y - distort_centre.
y;
179 float r2 = dx * dx + dy * dy;
182 float radial_x = x * (k1 * r2 + k2 * r4 + k3 * r6);
183 float radial_y = y * (k1 * r2 + k2 * r4 + k3 * r6);
184 float tangential_x = p1 * (r2 + 2.0f*x*x) + 2.0f*p2*x*y;
185 float tangential_y = p2 * (r2 + 2.0f*y*y) + 2.0f*p1*x*y;
186 float distorted_x = x + radial_x + tangential_x;
187 float distorted_y = y + radial_y + tangential_y;
188 float result_x = 0.5f + tex_coord_factor * distorted_x;
190 return vec2(result_x, result_y);
198 vec2 uv_green_low_res;
199 vec2 uv_blue_low_res;
200 vec2 uv_red_high_res;
201 vec2 uv_green_high_res;
202 vec2 uv_blue_high_res;
229 Vertex v0 = v[(yi ) * (Warp_Mesh_Resolution_X + 1) + xi ];
230 Vertex v1 = v[(yi ) * (Warp_Mesh_Resolution_X + 1) + xi + 1];
231 Vertex v2 = v[(yi + 1) * (Warp_Mesh_Resolution_X + 1) + xi + 1];
232 Vertex v3 = v[(yi + 1) * (Warp_Mesh_Resolution_X + 1) + xi ];
242 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, result));
243 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
sizeof(f), f, GL_STATIC_DRAW));
259 float tt = tan(top_fov);
260 float tb = tan(bottom_fov);
261 float tl = tan(left_fov);
262 float tr = tan(right_fov);
263 result.
x.
x = 2.0f / (tl + tr);
264 result.
y.
y = 2.0f / (tt + tb);
265 result.
z.
x = (tl - tr) / (tl + tr);
266 result.
z.
y = (tt - tb) / (tt + tb);
267 result.
z.
z = (z_near + z_far) / (z_near - z_far);
269 result.
w.
z = 2.0f * z_near * z_far / (z_near - z_far);
285 result.
x.
x = 2.0f * eye_display_distance / (right - left);
286 result.
y.
y = 2.0f * eye_display_distance / (top - bottom);
287 result.
z.
x = (right + left) / (right - left);
288 result.
z.
y = (top + bottom) / (top - bottom);
289 result.
z.
z = (z_near + z_far) / (z_near - z_far);
291 result.
w.
z = 2.0f * z_near * z_far / (z_near - z_far);
297 const GLubyte* extensions = glGetString(GL_EXTENSIONS);
298 char * found_multiview2_extension = strstr ((
char*)extensions,
"GL_OVR_multiview2");
299 char * found_multisample_multiview_extension = strstr ((
char*)extensions,
"GL_OVR_multiview_multisampled_render_to_texture");
300 char * found_border_clamp_extension = strstr ((
char*)extensions,
"GL_EXT_texture_border_clamp");
301 if (found_multiview2_extension == NULL)
303 LOGI(
"OpenGL ES 3.0 implementation does not support GL_OVR_multiview2 extension.\n");
306 if (found_multisample_multiview_extension == NULL)
309 LOGI(
"OpenGL ES 3.0 implementation does not support GL_OVR_multiview_multisampled_render_to_texture extension.\n");
311 if (found_border_clamp_extension == NULL)
313 LOGI(
"OpenGL ES 3.0 implementation does not support GL_EXT_texture_border_clamp extension.\n");
404 for (
int zi = 0; zi <= n; zi++)
405 for (
int xi = 0; xi <= n; xi++)
415 GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36));
422 GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36));
431 GL_CHECK(glClearColor(0.15f, 0.17f, 0.2f, 1.0f));
481 GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
487 GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
490 GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));
491 GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
493 GL_CHECK(glActiveTexture(GL_TEXTURE0));
500 attribfv(distort, position, 2, 14, 0);
501 attribfv(distort, uv_red_low_res, 2, 14, 2);
502 attribfv(distort, uv_green_low_res, 2, 14, 4);
503 attribfv(distort, uv_blue_low_res, 2, 14, 6);
504 attribfv(distort, uv_red_high_res, 2, 14, 8);
505 attribfv(distort, uv_green_high_res, 2, 14, 10);
506 attribfv(distort, uv_blue_high_res, 2, 14, 12);
512 attribfv(distort, position, 2, 14, 0);
513 attribfv(distort, uv_red_low_res, 2, 14, 2);
514 attribfv(distort, uv_green_low_res, 2, 14, 4);
515 attribfv(distort, uv_blue_low_res, 2, 14, 6);
516 attribfv(distort, uv_red_high_res, 2, 14, 8);
517 attribfv(distort, uv_green_high_res, 2, 14, 10);
518 attribfv(distort, uv_blue_high_res, 2, 14, 12);
#define Warp_Mesh_Resolution_Y
void draw_scene(App *app)
#define Multisample_Samples
#define uniform1i(prog, name, value)
static mat4 scale(float x, float y, float z)
DistortionCoefficients coefficients_green
#define uniformm4(prog, name, value)
#define Eye_Display_Distance
#define get_attrib_location(prog, name)
mat4 make_frustum(float left_fov, float right_fov, float bottom_fov, float top_fov, float z_near, float z_far)
static mat4 rotateX(float rad)
static mat4 rotateY(float rad)
vec2 compute_distortion(float x, float y, vec2 distort_centre, DistortionCoefficients coefficients, float tex_coord_factor)
GLuint make_warp_mesh(LensConfig config)
GLuint warp_mesh[Num_Eyes]
#define attribfv(prog, name, n, stride, offset)
DistortionCoefficients coefficients_red
#define View_Aspect_Ratio
void(GL_APIENTRY * PFNGLFRAMEBUFFERTEXTUREMULTIVIEWOVR)(GLenum, GLenum, GLuint, GLint, GLint, GLsizei)
mat4 make_frustum_screen_viewer(float eye_display_distance, float left, float right, float bottom, float top, float z_near, float z_far)
static mat4 translate(float x, float y, float z)
Framebuffer make_eye_framebuffer(int width, int height, int num_views)
typedef void(GL_APIENTRY *PFNGLFRAMEBUFFERTEXTUREMULTISAMPLEMULTIVIEWOVR)(GLenum
#define Eye_Fb_Resolution_X
void app_initialize(App *app)
void app_update_and_render(App *app)
#define Warp_Mesh_Resolution_X
#define View_Resolution_X
#define get_uniform_location(prog, name)
#define uniformm4array(prog, name, value, arraySize)
#define GL_TEXTURE_BORDER_COLOR_EXT
#define View_Resolution_Y
DistortionCoefficients coefficients_blue
#define Eye_Fb_Resolution_Y
#define GL_CLAMP_TO_BORDER_EXT