File: Multiview.cpp

package info (click to toggle)
wpewebkit 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 421,720 kB
  • sloc: cpp: 3,670,389; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 85
file content (383 lines) | stat: -rw-r--r-- 14,718 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This sample shows basic usage of the GL_OVR_multiview2 extension.

#include "SampleApplication.h"

#include "util/geometry_utils.h"
#include "util/shader_utils.h"

#include <iostream>

namespace
{

void FillTranslationMatrix(float xOffset, float yOffset, float zOffset, float *matrix)
{
    matrix[0] = 1.0f;
    matrix[1] = 0.0f;
    matrix[2] = 0.0f;
    matrix[3] = xOffset;

    matrix[4] = 0.0f;
    matrix[5] = 1.0f;
    matrix[6] = 0.0f;
    matrix[7] = yOffset;

    matrix[8]  = 0.0f;
    matrix[9]  = 0.0f;
    matrix[10] = 1.0f;
    matrix[11] = zOffset;

    matrix[12] = 0.0f;
    matrix[13] = 0.0f;
    matrix[14] = 0.0f;
    matrix[15] = 1.0f;
}

}  // namespace

class MultiviewSample : public SampleApplication
{
  public:
    MultiviewSample(int argc, char **argv)
        : SampleApplication("Multiview", argc, argv, ClientType::ES3_0),
          mMultiviewProgram(0),
          mMultiviewPersperiveUniformLoc(-1),
          mMultiviewLeftEyeCameraUniformLoc(-1),
          mMultiviewRightEyeCameraUniformLoc(-1),
          mMultiviewTranslationUniformLoc(-1),
          mMultiviewFBO(0),
          mColorTexture(0),
          mDepthTexture(0),
          mQuadVAO(0),
          mQuadVBO(0),
          mCubeVAO(0),
          mCubePosVBO(0),
          mCubeNormalVBO(0),
          mCubeIBO(0),
          mCombineProgram(0)
    {}

    bool initialize() override
    {
        // Check whether the GL_OVR_multiview(2) extension is supported. If not, abort
        // initialization.
        const char *allExtensions = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS));
        const std::string paddedExtensions = std::string(" ") + allExtensions + std::string(" ");
        if ((paddedExtensions.find(std::string(" GL_OVR_multiview2 ")) == std::string::npos) &&
            (paddedExtensions.find(std::string(" GL_OVR_multiview ")) == std::string::npos))
        {
            std::cout << "GL_OVR_multiview(2) is not available." << std::endl;
            return false;
        }

        // A view covers horizontally half of the screen.
        int viewWidth  = getWindow()->getWidth() / 2;
        int viewHeight = getWindow()->getHeight();

        // Create color and depth texture arrays with two layers to which we render each view.
        glGenTextures(1, &mColorTexture);
        glBindTexture(GL_TEXTURE_2D_ARRAY, mColorTexture);
        glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, viewWidth, viewHeight, 2, 0, GL_RGBA,
                     GL_UNSIGNED_BYTE, nullptr);
        glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

        glGenTextures(1, &mDepthTexture);
        glBindTexture(GL_TEXTURE_2D_ARRAY, mDepthTexture);
        glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT32F, viewWidth, viewHeight, 2, 0,
                     GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);

        // Generate multiview framebuffer for layered rendering.
        glGenFramebuffers(1, &mMultiviewFBO);
        glBindFramebuffer(GL_FRAMEBUFFER, mMultiviewFBO);
        glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mColorTexture, 0, 0,
                                         2);
        glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, mDepthTexture, 0, 0,
                                         2);
        GLenum drawBuffer = GL_COLOR_ATTACHMENT0;
        glDrawBuffers(1, &drawBuffer);

        // Check that the framebuffer is complete. Abort initialization otherwise.
        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        {
            return false;
        }

        // Create multiview program and query the uniform locations.
        // The program has two code paths based on the gl_ViewID_OVR attribute which tells us which
        // view is currently being rendered to. Based on it we decide which eye's camera matrix to
        // use.
        constexpr char kMultiviewVS[] =
            "#version 300 es\n"
            "#extension GL_OVR_multiview2 : require\n"
            "layout(num_views = 2) in;\n"
            "layout(location=0) in vec3 posIn;\n"
            "layout(location=1) in vec3 normalIn;\n"
            "uniform mat4 uPerspective;\n"
            "uniform mat4 uCameraLeftEye;\n"
            "uniform mat4 uCameraRightEye;\n"
            "uniform mat4 uTranslation;\n"
            "out vec3 oNormal;\n"
            "void main()\n"
            "{\n"
            "   vec4 p = uTranslation * vec4(posIn,1.);\n"
            "   if (gl_ViewID_OVR == 0u) {\n"
            "       p = uCameraLeftEye * p;\n"
            "   } else {\n"
            "       p = uCameraRightEye * p;\n"
            "   }\n"
            "   oNormal = normalIn;\n"
            "   gl_Position = uPerspective * p;\n"
            "}\n";

        constexpr char kMultiviewFS[] =
            "#version 300 es\n"
            "#extension GL_OVR_multiview2 : require\n"
            "precision mediump float;\n"
            "out vec4 color;\n"
            "in vec3 oNormal;\n"
            "void main()\n"
            "{\n"
            "   vec3 col = 0.5 * oNormal + vec3(0.5);\n"
            "   color = vec4(col, 1.);\n"
            "}\n";

        mMultiviewProgram = CompileProgram(kMultiviewVS, kMultiviewFS);
        if (!mMultiviewProgram)
        {
            return false;
        }
        mMultiviewPersperiveUniformLoc = glGetUniformLocation(mMultiviewProgram, "uPerspective");
        mMultiviewLeftEyeCameraUniformLoc =
            glGetUniformLocation(mMultiviewProgram, "uCameraLeftEye");
        mMultiviewRightEyeCameraUniformLoc =
            glGetUniformLocation(mMultiviewProgram, "uCameraRightEye");
        mMultiviewTranslationUniformLoc = glGetUniformLocation(mMultiviewProgram, "uTranslation");

        // Create a normal program to combine both layers of the color array texture.
        constexpr char kCombineVS[] =
            "#version 300 es\n"
            "in vec2 vIn;\n"
            "out vec2 uv;\n"
            "void main()\n"
            "{\n"
            "   gl_Position = vec4(vIn, 0., 1.);\n"
            "   uv = vIn * .5 + vec2(.5);\n"
            "}\n";

        constexpr char kCombineFS[] =
            "#version 300 es\n"
            "precision mediump float;\n"
            "precision mediump sampler2DArray;\n"
            "uniform sampler2DArray uMultiviewTex;\n"
            "in vec2 uv;\n"
            "out vec4 color;\n"
            "void main()\n"
            "{\n"
            "   float scaledX = 2.0 * uv.x;\n"
            "   float layer = floor(scaledX);\n"
            "   vec2 adjustedUV = vec2(fract(scaledX), uv.y);\n"
            "   vec3 texColor = texture(uMultiviewTex, vec3(adjustedUV, layer)).rgb;\n"
            "   color = vec4(texColor, 1.);\n"
            "}\n";

        mCombineProgram = CompileProgram(kCombineVS, kCombineFS);
        if (!mCombineProgram)
        {
            return false;
        }

        // Generate a quad which covers the whole screen.
        glGenVertexArrays(1, &mQuadVAO);
        glBindVertexArray(mQuadVAO);

        glGenBuffers(1, &mQuadVBO);
        glBindBuffer(GL_ARRAY_BUFFER, mQuadVBO);
        const float kQuadPositionData[] = {1.f, -1.f, 1.f, 1.f, -1.f, -1.f, -1.f, 1.f};
        glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8, kQuadPositionData, GL_STATIC_DRAW);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
        glEnableVertexAttribArray(0);
        glBindVertexArray(0);

        // Generate a cube.
        GenerateCubeGeometry(1.0f, &mCube);
        glGenVertexArrays(1, &mCubeVAO);
        glBindVertexArray(mCubeVAO);

        glGenBuffers(1, &mCubePosVBO);
        glBindBuffer(GL_ARRAY_BUFFER, mCubePosVBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(angle::Vector3) * mCube.positions.size(),
                     mCube.positions.data(), GL_STATIC_DRAW);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
        glEnableVertexAttribArray(0);

        glGenBuffers(1, &mCubeNormalVBO);
        glBindBuffer(GL_ARRAY_BUFFER, mCubeNormalVBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(angle::Vector3) * mCube.normals.size(),
                     mCube.normals.data(), GL_STATIC_DRAW);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
        glEnableVertexAttribArray(1);

        glGenBuffers(1, &mCubeIBO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mCubeIBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * mCube.indices.size(),
                     mCube.indices.data(), GL_STATIC_DRAW);

        glBindVertexArray(0);

        glEnable(GL_DEPTH_TEST);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        return true;
    }

    void destroy() override
    {
        glDeleteProgram(mMultiviewProgram);
        glDeleteFramebuffers(1, &mMultiviewFBO);
        glDeleteTextures(1, &mColorTexture);
        glDeleteTextures(1, &mDepthTexture);
        glDeleteVertexArrays(1, &mQuadVAO);
        glDeleteBuffers(1, &mQuadVBO);
        glDeleteVertexArrays(1, &mCubeVAO);
        glDeleteBuffers(1, &mQuadVBO);
        glDeleteBuffers(1, &mCubePosVBO);
        glDeleteBuffers(1, &mCubeNormalVBO);
        glDeleteBuffers(1, &mCubeIBO);
        glDeleteProgram(mCombineProgram);
    }

    void draw() override
    {
        // Draw to multiview fbo.
        {
            // Generate the perspective projection matrix.
            const int viewWidth          = getWindow()->getWidth() / 2;
            const int viewHeight         = getWindow()->getHeight();
            const float kFOV             = 90.f;
            const float kNear            = 1.0f;
            const float kFar             = 100.0f;
            const float kPlaneDifference = kFar - kNear;
            const float kXYScale         = 1.f / (tanf(kFOV / 2.0f));
            const float kAspectRatio     = static_cast<float>(viewWidth) / viewHeight;
            float kPerspectiveProjectionMatrix[16];
            kPerspectiveProjectionMatrix[0] = kXYScale / kAspectRatio;
            kPerspectiveProjectionMatrix[1] = .0f;
            kPerspectiveProjectionMatrix[2] = .0f;
            kPerspectiveProjectionMatrix[3] = .0f;

            kPerspectiveProjectionMatrix[4] = .0f;
            kPerspectiveProjectionMatrix[5] = kXYScale;
            kPerspectiveProjectionMatrix[6] = .0f;
            kPerspectiveProjectionMatrix[7] = .0f;

            kPerspectiveProjectionMatrix[8]  = .0f;
            kPerspectiveProjectionMatrix[9]  = .0;
            kPerspectiveProjectionMatrix[10] = -kFar / kPlaneDifference;
            kPerspectiveProjectionMatrix[11] = -1.f;

            kPerspectiveProjectionMatrix[12] = .0f;
            kPerspectiveProjectionMatrix[13] = .0;
            kPerspectiveProjectionMatrix[14] = -kFar * kNear / kPlaneDifference;
            kPerspectiveProjectionMatrix[15] = .0;

            // Generate the camera matrices for the left and right eye.
            const float kXOffset = 1.5f;
            const float kYOffset = 1.5f;
            const float kZOffset = 5.0f;
            float kLeftCameraMatrix[16];
            FillTranslationMatrix(kXOffset, -kYOffset, -kZOffset, kLeftCameraMatrix);
            float kRightCameraMatrix[16];
            FillTranslationMatrix(-kXOffset, -kYOffset, -kZOffset, kRightCameraMatrix);

            // Bind and clear the multiview framebuffer.
            glBindFramebuffer(GL_FRAMEBUFFER, mMultiviewFBO);
            glClearColor(0, 0, 0, 1);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            // Set the viewport to be the size as one of the views.
            glViewport(0, 0, viewWidth, viewHeight);

            // Bind multiview program and set matrices.
            glUseProgram(mMultiviewProgram);
            glUniformMatrix4fv(mMultiviewPersperiveUniformLoc, 1, GL_TRUE,
                               kPerspectiveProjectionMatrix);
            glUniformMatrix4fv(mMultiviewLeftEyeCameraUniformLoc, 1, GL_TRUE, kLeftCameraMatrix);
            glUniformMatrix4fv(mMultiviewRightEyeCameraUniformLoc, 1, GL_TRUE, kRightCameraMatrix);

            glBindVertexArray(mCubeVAO);

            // Draw first cube.
            float kTranslationMatrix[16];
            FillTranslationMatrix(0.0f, 0.0f, 0.0f, kTranslationMatrix);
            glUniformMatrix4fv(mMultiviewTranslationUniformLoc, 1, GL_TRUE, kTranslationMatrix);
            glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mCube.indices.size()),
                           GL_UNSIGNED_SHORT, nullptr);

            // Draw second cube.
            FillTranslationMatrix(1.0f, 1.0f, -2.0f, kTranslationMatrix);
            glUniformMatrix4fv(mMultiviewTranslationUniformLoc, 1, GL_TRUE, kTranslationMatrix);
            glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mCube.indices.size()),
                           GL_UNSIGNED_SHORT, nullptr);

            glBindVertexArray(0);
        }

        // Combine both views.
        {
            // Bind the default framebuffer object and clear.
            glBindFramebuffer(GL_FRAMEBUFFER, 0);

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            // Set the viewport to cover the whole screen.
            glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());

            glUseProgram(mCombineProgram);

            // Bind the 2D array texture to be used as a sampler.
            glUniform1i(glGetUniformLocation(mCombineProgram, "uMultiviewTex"), 0);
            glBindTexture(GL_TEXTURE_2D_ARRAY, mMultiviewFBO);
            glActiveTexture(GL_TEXTURE0);

            // Draw a quad which covers the whole screen. Layer and texture coordinates are
            // calculated in the vertex shader based on the UV coordinates of the quad.
            glBindVertexArray(mQuadVAO);
            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
            glBindVertexArray(0);
        }
    }

  private:
    GLuint mMultiviewProgram;
    GLint mMultiviewPersperiveUniformLoc;
    GLint mMultiviewLeftEyeCameraUniformLoc;
    GLint mMultiviewRightEyeCameraUniformLoc;
    GLint mMultiviewTranslationUniformLoc;

    GLuint mMultiviewFBO;
    GLuint mColorTexture;
    GLuint mDepthTexture;

    GLuint mQuadVAO;
    GLuint mQuadVBO;

    CubeGeometry mCube;
    GLuint mCubeVAO;
    GLuint mCubePosVBO;
    GLuint mCubeNormalVBO;
    GLuint mCubeIBO;

    GLuint mCombineProgram;
};

int main(int argc, char **argv)
{
    MultiviewSample app(argc, argv);
    return app.run();
}