interactiverenderer.cpp 4.09 KB
Newer Older
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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#include "interactiverenderer.hpp"

#include "texture.hpp"

#include <QMouseEvent>

#include <math.h>

#define RADIANS_TO_DEGREES (180.0 / M_PI)
#define DEFAULT_FOV 120
#define FOV_MAX 140
#define FOV_MIN 5

InteractiveRenderer::InteractiveRenderer()
    : Renderer(), latRot(0.0f), lngRot(0.0f), roll(0.0f), fov(90), rWidth(1), rHeight(1) {}

InteractiveRenderer::~InteractiveRenderer() {}

void InteractiveRenderer::drawSphere() {
  Texture::getLeft().latePanoramaDef();

  glEnable(GL_TEXTURE_2D);

  sphereProgram.bind();
  if (Texture::getLeft().getWidth() != 0) {
    glBindTexture(GL_TEXTURE_2D, Texture::getLeft().id);
    if (Texture::getLeft().pixelBuffer != 0) {
      glBindBuffer(GL_PIXEL_UNPACK_BUFFER, Texture::getLeft().pixelBuffer);
      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Texture::getLeft().getWidth(), Texture::getLeft().getHeight(), GL_RGBA,
                      GL_UNSIGNED_BYTE, nullptr);
      glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
    }
  } else {
    placeHolderTex.bind();
  }
  sphereProgram.setUniformValue("texture", 0);

  static const QVector3D defaultCenter(0.0f, 0.0f, 0.0f);
  static const QVector3D defaultLookedPoint(-1.0f, 0.0f, 0.0f);
  static const QVector3D defaultUp(0.0f, 0.0f, -1.0f);
  static const QVector3D yawAxis(0, 0, 1);
  static const QVector3D pitchAxis(0, -1, 0);
  static const QVector3D rollAxis(1, 0, 0);

  QMatrix4x4 viewMatrix;
  viewMatrix.lookAt(defaultCenter, defaultLookedPoint, defaultUp);
  viewMatrix.rotate(latRot * RADIANS_TO_DEGREES, pitchAxis);
  viewMatrix.rotate(roll * RADIANS_TO_DEGREES, rollAxis);
  viewMatrix.rotate(lngRot * RADIANS_TO_DEGREES, yawAxis);

  sphereProgram.setUniformValue("mvp_matrix", projection * viewMatrix);
  sphereProgram.release();

  Renderer::renderSphere();
  glBindTexture(GL_TEXTURE_2D, 0);

  glDisable(GL_TEXTURE_2D);
}

void InteractiveRenderer::drawSkybox(QGLShaderProgram& program) {
  Texture::getLeft().lateCubemapDef();

  glEnable(GL_TEXTURE_CUBE_MAP);

  program.bind();
  if (Texture::getLeft().getLength() != 0) {
    glBindTexture(GL_TEXTURE_CUBE_MAP, Texture::getLeft().id);
    for (int i = 0; i < 6; ++i) {
      glBindBuffer(GL_PIXEL_UNPACK_BUFFER, Texture::getLeft().pbo[i]);
      glTexSubImage2D(cube[i], 0, 0, 0, Texture::getLeft().getLength(), Texture::getLeft().getLength(), GL_RGBA,
                      GL_UNSIGNED_BYTE, nullptr);
    }
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

    program.setUniformValue("texture", 0);
  } else {
    // TODO
  }
  static const QVector3D defaultCenter(0.0f, 0.0f, 0.0f);
  static const QVector3D defaultLookedPoint(0.0f, 0.0f, 1.0f);
  static const QVector3D defaultUp(0.0f, -1.0f, 0.0f);
  static const QVector3D yawAxis(0, 1, 0);
  static const QVector3D pitchAxis(1, 0, 0);
  static const QVector3D rollAxis(0, 0, 1);

  QMatrix4x4 viewMatrix;
  viewMatrix.lookAt(defaultCenter, defaultLookedPoint, defaultUp);
  viewMatrix.rotate(latRot * RADIANS_TO_DEGREES, pitchAxis);
  viewMatrix.rotate(roll * RADIANS_TO_DEGREES, rollAxis);
  viewMatrix.rotate(lngRot * RADIANS_TO_DEGREES, yawAxis);

  program.setUniformValue("mvp_matrix", projection * viewMatrix);
  program.release();

  Renderer::renderSkybox(program);
  glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

  glDisable(GL_TEXTURE_CUBE_MAP);
}

void InteractiveRenderer::render() {
  glClearColor(0., 1., 0., 0.);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  std::lock_guard<std::mutex> textureLock(*(Texture::get().lock));
  Texture::Type textureType = Texture::getLeft().getType();
  switch (textureType) {
    case Texture::Type::PANORAMIC:
      return drawSphere();
    case Texture::Type::CUBEMAP:
      return drawSkybox(skyboxProgram);
    case Texture::Type::EQUIANGULAR_CUBEMAP:
      return drawSkybox(equiangularSkyboxProgram);
  }
}

void InteractiveRenderer::resize(int width, int height) {
  rWidth = width;
  rHeight = height;
  glViewport(0, 0, rWidth, rHeight);
  projection.setToIdentity();
  projection.perspective(fov, rWidth / (float)rHeight, 0.1f, 100.0f);  // origin = bottom left
}

void InteractiveRenderer::updateZoom() { resize(rWidth, rHeight); }