| 1 |
/* |
| 2 |
* This file is part of Bino, a 3D video player. |
| 3 |
* |
| 4 |
* Copyright (C) 2016, 2017, 2018, 2019, 2020, 2021, 2022 |
| 5 |
* Computer Graphics Group, University of Siegen |
| 6 |
* Written by Martin Lambers <[email protected]> |
| 7 |
* Copyright (C) 2022, 2023, 2024, 2025, 2026 |
| 8 |
* Martin Lambers <[email protected]> |
| 9 |
* |
| 10 |
* This program is free software; you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU General Public License as published by |
| 12 |
* the Free Software Foundation; either version 3 of the License, or |
| 13 |
* (at your option) any later version. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef WITH_QVR |
| 25 |
|
| 26 |
#include <qvr/manager.hpp> |
| 27 |
#include <qvr/device.hpp> |
| 28 |
|
| 29 |
#include "qvrapp.hpp" |
| 30 |
#include "bino.hpp" |
| 31 |
#include "tools.hpp" |
| 32 |
|
| 33 |
|
| 34 |
BinoQVRApp::BinoQVRApp(bool renderDevices) : _renderDevices(renderDevices) |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
unsigned int BinoQVRApp::setupTex(const QImage& img) |
| 39 |
{ |
| 40 |
unsigned int tex; |
| 41 |
glGenTextures(1, &tex); |
| 42 |
glBindTexture(GL_TEXTURE_2D, tex); |
| 43 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width(), img.height(), 0, |
| 44 |
GL_RGBA, GL_UNSIGNED_BYTE, img.constBits()); |
| 45 |
glGenerateMipmap(GL_TEXTURE_2D); |
| 46 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE); |
| 47 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN); |
| 48 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED); |
| 49 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA); |
| 50 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 51 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 52 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 53 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
| 54 |
if (_haveAnisotropicFiltering) |
| 55 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 4.0f); |
| 56 |
return tex; |
| 57 |
} |
| 58 |
|
| 59 |
unsigned int BinoQVRApp::setupVao(int vertexCount, |
| 60 |
const float* positions, const float* normals, const float* texcoords, |
| 61 |
int indexCount, const unsigned short* indices) |
| 62 |
{ |
| 63 |
GLuint vao; |
| 64 |
GLuint positionBuf, normalBuf, texcoordBuf, indexBuf; |
| 65 |
|
| 66 |
glGenVertexArrays(1, &vao); |
| 67 |
glBindVertexArray(vao); |
| 68 |
glGenBuffers(1, &positionBuf); |
| 69 |
glBindBuffer(GL_ARRAY_BUFFER, positionBuf); |
| 70 |
glBufferData(GL_ARRAY_BUFFER, vertexCount * 3 * sizeof(float), positions, GL_STATIC_DRAW); |
| 71 |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); |
| 72 |
glEnableVertexAttribArray(0); |
| 73 |
glGenBuffers(1, &normalBuf); |
| 74 |
glBindBuffer(GL_ARRAY_BUFFER, normalBuf); |
| 75 |
glBufferData(GL_ARRAY_BUFFER, vertexCount * 3 * sizeof(float), normals, GL_STATIC_DRAW); |
| 76 |
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0); |
| 77 |
glEnableVertexAttribArray(1); |
| 78 |
glGenBuffers(1, &texcoordBuf); |
| 79 |
glBindBuffer(GL_ARRAY_BUFFER, texcoordBuf); |
| 80 |
glBufferData(GL_ARRAY_BUFFER, vertexCount * 2 * sizeof(float), texcoords, GL_STATIC_DRAW); |
| 81 |
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 82 |
glEnableVertexAttribArray(2); |
| 83 |
glGenBuffers(1, &indexBuf); |
| 84 |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuf); |
| 85 |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(unsigned short), indices, GL_STATIC_DRAW); |
| 86 |
return vao; |
| 87 |
} |
| 88 |
|
| 89 |
void BinoQVRApp::serializeStaticData(QDataStream& ds) const |
| 90 |
{ |
| 91 |
Bino::instance()->serializeStaticData(ds); |
| 92 |
} |
| 93 |
|
| 94 |
void BinoQVRApp::deserializeStaticData(QDataStream& ds) |
| 95 |
{ |
| 96 |
Bino::instance()->deserializeStaticData(ds); |
| 97 |
} |
| 98 |
|
| 99 |
void BinoQVRApp::serializeDynamicData(QDataStream& ds) const |
| 100 |
{ |
| 101 |
Bino::instance()->serializeDynamicData(ds); |
| 102 |
} |
| 103 |
|
| 104 |
void BinoQVRApp::deserializeDynamicData(QDataStream& ds) |
| 105 |
{ |
| 106 |
Bino::instance()->deserializeDynamicData(ds); |
| 107 |
} |
| 108 |
|
| 109 |
bool BinoQVRApp::wantExit() |
| 110 |
{ |
| 111 |
return Bino::instance()->wantExit(); |
| 112 |
} |
| 113 |
|
| 114 |
bool BinoQVRApp::initProcess(QVRProcess*) |
| 115 |
{ |
| 116 |
initializeOpenGLFunctions(); |
| 117 |
_haveAnisotropicFiltering = checkTextureAnisotropicFilterAvailability(); |
| 118 |
// Shader program |
| 119 |
QString vrdeviceVS = readFile(":src/shader-vrdevice.vert.glsl"); |
| 120 |
QString vrdeviceFS = readFile(":src/shader-vrdevice.frag.glsl"); |
| 121 |
if (OpenGLType != OpenGL_Type_Desktop) { |
| 122 |
vrdeviceVS.prepend("#version 300 es\n"); |
| 123 |
vrdeviceFS.prepend("#version 300 es\n" |
| 124 |
"precision mediump float;\n"); |
| 125 |
} else { |
| 126 |
vrdeviceVS.prepend("#version 330\n"); |
| 127 |
vrdeviceFS.prepend("#version 330\n"); |
| 128 |
} |
| 129 |
_prg.addShaderFromSourceCode(QOpenGLShader::Vertex, vrdeviceVS); |
| 130 |
_prg.addShaderFromSourceCode(QOpenGLShader::Fragment, vrdeviceFS); |
| 131 |
_prg.link(); |
| 132 |
// Get device model data |
| 133 |
for (int i = 0; i < QVRManager::deviceModelVertexDataCount(); i++) { |
| 134 |
_devModelVaos.append(setupVao( |
| 135 |
QVRManager::deviceModelVertexCount(i), |
| 136 |
QVRManager::deviceModelVertexPositions(i), |
| 137 |
QVRManager::deviceModelVertexNormals(i), |
| 138 |
QVRManager::deviceModelVertexTexCoords(i), |
| 139 |
QVRManager::deviceModelVertexIndexCount(i), |
| 140 |
QVRManager::deviceModelVertexIndices(i))); |
| 141 |
_devModelVaoIndices.append(QVRManager::deviceModelVertexIndexCount(i)); |
| 142 |
} |
| 143 |
for (int i = 0; i < QVRManager::deviceModelTextureCount(); i++) { |
| 144 |
_devModelTextures.append(setupTex(QVRManager::deviceModelTexture(i))); |
| 145 |
} |
| 146 |
|
| 147 |
return Bino::instance()->initProcess(); |
| 148 |
} |
| 149 |
|
| 150 |
void BinoQVRApp::preRenderProcess(QVRProcess*) |
| 151 |
{ |
| 152 |
Bino::instance()->preRenderProcess(); |
| 153 |
} |
| 154 |
|
| 155 |
void BinoQVRApp::render(QVRWindow*, const QVRRenderContext& context, const unsigned int* textures) |
| 156 |
{ |
| 157 |
for (int view = 0; view < context.viewCount(); view++) { |
| 158 |
// Render Bino view |
| 159 |
QMatrix4x4 projectionMatrix = context.frustum(view).toMatrix4x4(); |
| 160 |
QMatrix4x4 orientationMatrix; |
| 161 |
orientationMatrix.rotate(context.navigationOrientation().inverted()); |
| 162 |
orientationMatrix.rotate(context.trackingOrientation(view).inverted()); |
| 163 |
QMatrix4x4 viewMatrix = context.viewMatrix(view); |
| 164 |
QMatrix4x4 viewMatrixPure = context.viewMatrixPure(view); |
| 165 |
int v = (context.eye(view) == QVR_Eye_Right ? 1 : 0); |
| 166 |
int texWidth = context.textureSize(view).width(); |
| 167 |
int texHeight = context.textureSize(view).height(); |
| 168 |
Bino::instance()->render( |
| 169 |
context.unitedScreenWallBottomLeft(), context.unitedScreenWallBottomRight(), context.unitedScreenWallTopLeft(), |
| 170 |
context.intersectedScreenWallBottomLeft(), context.intersectedScreenWallBottomRight(), context.intersectedScreenWallTopLeft(), |
| 171 |
projectionMatrix, orientationMatrix, viewMatrix, v, texWidth, texHeight, textures[view]); |
| 172 |
// Render VR device models (optional) |
| 173 |
if (_renderDevices) { |
| 174 |
glUseProgram(_prg.programId()); |
| 175 |
for (int i = 0; i < QVRManager::deviceCount(); i++) { |
| 176 |
const QVRDevice& device = QVRManager::device(i); |
| 177 |
for (int j = 0; j < device.modelNodeCount(); j++) { |
| 178 |
QMatrix4x4 nodeMatrix = device.matrix(); |
| 179 |
nodeMatrix.translate(device.modelNodePosition(j)); |
| 180 |
nodeMatrix.rotate(device.modelNodeOrientation(j)); |
| 181 |
QMatrix4x4 modelViewMatrix = viewMatrixPure * nodeMatrix; |
| 182 |
int vertexDataIndex = device.modelNodeVertexDataIndex(j); |
| 183 |
int textureIndex = device.modelNodeTextureIndex(j); |
| 184 |
_prg.setUniformValue("modelViewMatrix", modelViewMatrix); |
| 185 |
_prg.setUniformValue("projectionModelViewMatrix", projectionMatrix * modelViewMatrix); |
| 186 |
_prg.setUniformValue("normalMatrix", modelViewMatrix.normalMatrix()); |
| 187 |
_prg.setUniformValue("hasDiffTex", _devModelTextures[textureIndex] == 0 ? 0 : 1); |
| 188 |
_prg.setUniformValue("diffTex", 0); |
| 189 |
glActiveTexture(GL_TEXTURE0); |
| 190 |
glBindTexture(GL_TEXTURE_2D, _devModelTextures[textureIndex]); |
| 191 |
glBindVertexArray(_devModelVaos[vertexDataIndex]); |
| 192 |
glDrawElements(GL_TRIANGLES, _devModelVaoIndices[vertexDataIndex], GL_UNSIGNED_SHORT, 0); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
// Invalidate depth attachment (to help OpenGL ES performance) |
| 198 |
const GLenum fboInvalidations[] = { GL_DEPTH_ATTACHMENT }; |
| 199 |
glInvalidateFramebuffer(GL_FRAMEBUFFER, 1, fboInvalidations); |
| 200 |
} |
| 201 |
|
| 202 |
void BinoQVRApp::keyPressEvent(const QVRRenderContext&, QKeyEvent* event) |
| 203 |
{ |
| 204 |
Bino::instance()->keyPressEvent(event); |
| 205 |
} |
| 206 |
|
| 207 |
#endif |