Пример #1
0
    /* pp */ GenericEye(
        final GenericStereoDevice device,
        final int distortionBits,
        final float[] eyePositionOffset,
        final EyeParameter eyeParam,
        final DimensionImmutable textureSize,
        final RectangleImmutable eyeViewport) {
      this.eyeName = eyeParam.number;
      this.distortionBits = distortionBits;
      this.viewport = eyeViewport;

      final boolean usePP = null != device.config.distortionMeshProducer && 0 != distortionBits;

      final boolean usesTimewarp = usePP && StereoUtil.usesTimewarpDistortion(distortionBits);
      final FloatBuffer fstash = Buffers.newDirectFloatBuffer(2 + 2 + (usesTimewarp ? 16 + 16 : 0));

      if (usePP) {
        eyeToSourceUVScale =
            new GLUniformData("svr_EyeToSourceUVScale", 2, Buffers.slice2Float(fstash, 0, 2));
        eyeToSourceUVOffset =
            new GLUniformData("svr_EyeToSourceUVOffset", 2, Buffers.slice2Float(fstash, 2, 2));
      } else {
        eyeToSourceUVScale = null;
        eyeToSourceUVOffset = null;
      }

      if (usesTimewarp) {
        eyeRotationStart =
            new GLUniformData("svr_EyeRotationStart", 4, 4, Buffers.slice2Float(fstash, 4, 16));
        eyeRotationEnd =
            new GLUniformData("svr_EyeRotationEnd", 4, 4, Buffers.slice2Float(fstash, 20, 16));
      } else {
        eyeRotationStart = null;
        eyeRotationEnd = null;
      }

      this.eyeParameter = eyeParam;

      // Setup: eyeToSourceUVScale, eyeToSourceUVOffset
      if (usePP) {
        final ScaleAndOffset2D textureScaleAndOffset =
            new ScaleAndOffset2D(eyeParam.fovhv, textureSize, eyeViewport);
        if (StereoDevice.DEBUG) {
          System.err.println("XXX." + eyeName + ": eyeParam      " + eyeParam);
          System.err.println("XXX." + eyeName + ": uvScaleOffset " + textureScaleAndOffset);
          System.err.println("XXX." + eyeName + ": textureSize   " + textureSize);
          System.err.println("XXX." + eyeName + ": viewport      " + eyeViewport);
        }
        final FloatBuffer eyeToSourceUVScaleFB = eyeToSourceUVScale.floatBufferValue();
        eyeToSourceUVScaleFB.put(0, textureScaleAndOffset.scale[0]);
        eyeToSourceUVScaleFB.put(1, textureScaleAndOffset.scale[1]);
        final FloatBuffer eyeToSourceUVOffsetFB = eyeToSourceUVOffset.floatBufferValue();
        eyeToSourceUVOffsetFB.put(0, textureScaleAndOffset.offset[0]);
        eyeToSourceUVOffsetFB.put(1, textureScaleAndOffset.offset[1]);
      } else {
        vertexCount = 0;
        indexCount = 0;
        iVBO = null;
        vboPos = null;
        vboParams = null;
        vboTexCoordsR = null;
        vboTexCoordsG = null;
        vboTexCoordsB = null;
        indices = null;
        if (StereoDevice.DEBUG) {
          System.err.println("XXX." + eyeName + ": " + this);
        }
        return;
      }
      final DistortionMesh meshData =
          device.config.distortionMeshProducer.create(eyeParam, distortionBits);
      if (null == meshData) {
        throw new GLException(
            "Failed to create meshData for eye "
                + eyeParam
                + ", and "
                + StereoUtil.distortionBitsToString(distortionBits));
      }

      vertexCount = meshData.vertexCount;
      indexCount = meshData.indexCount;

      /**
       * 2+2+2+2+2: { vec2 position, vec2 color, vec2 texCoordR, vec2 texCoordG, vec2 texCoordB }
       */
      final boolean useChromatic = StereoUtil.usesChromaticDistortion(distortionBits);
      final boolean useVignette = StereoUtil.usesVignetteDistortion(distortionBits);

      final int compsPerElement =
          2 + 2 + 2 + (useChromatic ? 2 + 2 /* texCoordG + texCoordB */ : 0);
      iVBO =
          GLArrayDataServer.createGLSLInterleaved(
              compsPerElement, GL.GL_FLOAT, false, vertexCount, GL.GL_STATIC_DRAW);
      vboPos = iVBO.addGLSLSubArray("svr_Position", 2, GL.GL_ARRAY_BUFFER);
      vboParams = iVBO.addGLSLSubArray("svr_Params", 2, GL.GL_ARRAY_BUFFER);
      vboTexCoordsR = iVBO.addGLSLSubArray("svr_TexCoordR", 2, GL.GL_ARRAY_BUFFER);
      if (useChromatic) {
        vboTexCoordsG = iVBO.addGLSLSubArray("svr_TexCoordG", 2, GL.GL_ARRAY_BUFFER);
        vboTexCoordsB = iVBO.addGLSLSubArray("svr_TexCoordB", 2, GL.GL_ARRAY_BUFFER);
      } else {
        vboTexCoordsG = null;
        vboTexCoordsB = null;
      }
      indices =
          GLArrayDataServer.createData(
              1, GL.GL_SHORT, indexCount, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER);

      /**
       * 2+2+2+2+2: { vec2 position, vec2 color, vec2 texCoordR, vec2 texCoordG, vec2 texCoordB }
       */
      final FloatBuffer iVBOFB = (FloatBuffer) iVBO.getBuffer();

      for (int vertNum = 0; vertNum < vertexCount; vertNum++) {
        final DistortionMesh.DistortionVertex v = meshData.vertices[vertNum];
        int dataIdx = 0;

        if (StereoDevice.DUMP_DATA) {
          System.err.println("XXX." + eyeName + ": START VERTEX " + vertNum + " / " + vertexCount);
        }
        // pos
        if (v.pos_size >= 2) {
          if (StereoDevice.DUMP_DATA) {
            System.err.println(
                "XXX." + eyeName + ": pos [" + v.data[dataIdx] + ", " + v.data[dataIdx + 1] + "]");
          }
          iVBOFB.put(v.data[dataIdx]);
          iVBOFB.put(v.data[dataIdx + 1]);
        } else {
          iVBOFB.put(0f);
          iVBOFB.put(0f);
        }
        dataIdx += v.pos_size;

        // params
        if (v.vignetteFactor_size >= 1 && useVignette) {
          if (StereoDevice.DUMP_DATA) {
            System.err.println("XXX." + eyeName + ": vignette " + v.data[dataIdx]);
          }
          iVBOFB.put(v.data[dataIdx]);
        } else {
          iVBOFB.put(1.0f);
        }
        dataIdx += v.vignetteFactor_size;

        if (v.timewarpFactor_size >= 1) {
          if (StereoDevice.DUMP_DATA) {
            System.err.println("XXX." + eyeName + ": timewarp " + v.data[dataIdx]);
          }
          iVBOFB.put(v.data[dataIdx]);
        } else {
          iVBOFB.put(1.0f);
        }
        dataIdx += v.timewarpFactor_size;

        // texCoordR
        if (v.texR_size >= 2) {
          if (StereoDevice.DUMP_DATA) {
            System.err.println(
                "XXX." + eyeName + ": texR [" + v.data[dataIdx] + ", " + v.data[dataIdx + 1] + "]");
          }
          iVBOFB.put(v.data[dataIdx]);
          iVBOFB.put(v.data[dataIdx + 1]);
        } else {
          iVBOFB.put(1f);
          iVBOFB.put(1f);
        }
        dataIdx += v.texR_size;

        if (useChromatic) {
          // texCoordG
          if (v.texG_size >= 2) {
            if (StereoDevice.DUMP_DATA) {
              System.err.println(
                  "XXX."
                      + eyeName
                      + ": texG ["
                      + v.data[dataIdx]
                      + ", "
                      + v.data[dataIdx + 1]
                      + "]");
            }
            iVBOFB.put(v.data[dataIdx]);
            iVBOFB.put(v.data[dataIdx + 1]);
          } else {
            iVBOFB.put(1f);
            iVBOFB.put(1f);
          }
          dataIdx += v.texG_size;

          // texCoordB
          if (v.texB_size >= 2) {
            if (StereoDevice.DUMP_DATA) {
              System.err.println(
                  "XXX."
                      + eyeName
                      + ": texB ["
                      + v.data[dataIdx]
                      + ", "
                      + v.data[dataIdx + 1]
                      + "]");
            }
            iVBOFB.put(v.data[dataIdx]);
            iVBOFB.put(v.data[dataIdx + 1]);
          } else {
            iVBOFB.put(1f);
            iVBOFB.put(1f);
          }
          dataIdx += v.texB_size;
        } else {
          dataIdx += v.texG_size;
          dataIdx += v.texB_size;
        }
      }
      if (StereoDevice.DUMP_DATA) {
        System.err.println("XXX." + eyeName + ": iVBO " + iVBO);
      }
      {
        if (StereoDevice.DUMP_DATA) {
          System.err.println("XXX." + eyeName + ": idx " + indices + ", count " + indexCount);
          for (int i = 0; i < indexCount; i++) {
            if (0 == i % 16) {
              System.err.printf("%n%5d: ", i);
            }
            System.err.printf("%5d, ", (int) meshData.indices[i]);
          }
          System.err.println();
        }
        final ShortBuffer out = (ShortBuffer) indices.getBuffer();
        out.put(meshData.indices, 0, meshData.indexCount);
      }
      if (StereoDevice.DEBUG) {
        System.err.println("XXX." + eyeName + ": " + this);
      }
    }
Пример #2
0
  @Override
  public void init(final GLAutoDrawable glad) {
    if (null != textureData) {
      texture = TextureIO.newTexture(glad.getGL(), textureData);
    }
    final GL2ES2 gl = glad.getGL().getGL2ES2();

    initShader(gl, true);

    // setup mgl_PMVMatrix
    pmvMatrix = new PMVMatrix();
    pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
    pmvMatrix.glLoadIdentity();
    pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    pmvMatrix.glLoadIdentity();
    pmvMatrixUniform =
        new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); // P, Mv

    st.ownUniform(pmvMatrixUniform);
    if (!st.uniform(gl, pmvMatrixUniform)) {
      throw new GLException("Error setting PMVMatrix in shader: " + st);
    }
    if (!st.uniform(gl, new GLUniformData("mgl_ActiveTexture", textureUnit))) {
      throw new GLException("Error setting mgl_ActiveTexture in shader: " + st);
    }

    if (null != texture) {
      // fetch the flipped texture coordinates
      texture.getImageTexCoords().getST_LB_RB_LT_RT(s_quadTexCoords, 0, 1f, 1f);
    }

    interleavedVBO =
        GLArrayDataServer.createGLSLInterleaved(
            3 + 4 + 2, GL.GL_FLOAT, false, 3 * 4, GL.GL_STATIC_DRAW);
    {
      interleavedVBO.addGLSLSubArray("mgl_Vertex", 3, GL.GL_ARRAY_BUFFER);
      interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER);
      // interleavedVBO.addGLSLSubArray("mgl_Normal",        3, GL.GL_ARRAY_BUFFER);
      interleavedVBO.addGLSLSubArray("mgl_MultiTexCoord", 2, GL.GL_ARRAY_BUFFER);

      final FloatBuffer ib = (FloatBuffer) interleavedVBO.getBuffer();

      for (int i = 0; i < 4; i++) {
        ib.put(s_quadVertices, i * 3, 3);
        ib.put(s_quadColors, i * 4, 4);
        // ib.put(s_cubeNormals,   i*3, 3);
        ib.put(s_quadTexCoords, i * 2, 2);
      }
    }
    interleavedVBO.seal(gl, true);
    interleavedVBO.enableBuffer(gl, false);
    st.ownAttribute(interleavedVBO, true);

    // OpenGL Render Settings
    gl.glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
    gl.glEnable(GL.GL_DEPTH_TEST);

    if (keepTextureBound && null != texture) {
      gl.glActiveTexture(GL.GL_TEXTURE0 + textureUnit);
      texture.enable(gl);
      texture.bind(gl);
    }
    st.useProgram(gl, false);
  }