Пример #1
0
  public void init(final GLAutoDrawable drawable) {
    GL _gl = drawable.getGL();

    _gl.glGetError(); // flush error ..

    if (glDebug) {
      try {
        _gl =
            _gl.getContext()
                .setGL(GLPipelineFactory.create("javax.media.opengl.Debug", null, _gl, null));
      } catch (final Exception e) {
        throw new RuntimeException("can not set debug pipeline", e);
      }
    }

    if (glTrace) {
      try {
        _gl =
            _gl.getContext()
                .setGL(
                    GLPipelineFactory.create(
                        "javax.media.opengl.Trace", null, _gl, new Object[] {System.err}));
      } catch (final Exception e) {
        throw new RuntimeException("can not set trace pipeline", e);
      }
    }

    System.out.println(_gl);

    _gl.getContext().setGLReadDrawable(externalRead);
    if (_gl.isGL2GL3()) {
      _gl.getGL2GL3().glReadBuffer(GL.GL_FRONT);
    }
    System.out.println("---------------------------");
    System.out.println(_gl.getContext());
    System.out.println("---------------------------");
  }
  public void applyRenderState(RenderState state) {
    GL gl = GLContext.getCurrentGL();
    if (state.isWireframe() && !context.wireframe) {
      gl.getGL2GL3().glPolygonMode(GL.GL_FRONT_AND_BACK, GL2GL3.GL_LINE);
      context.wireframe = true;
    } else if (!state.isWireframe() && context.wireframe) {
      gl.getGL2GL3().glPolygonMode(GL.GL_FRONT_AND_BACK, GL2GL3.GL_FILL);
      context.wireframe = false;
    }

    if (state.isDepthTest() && !context.depthTestEnabled) {
      gl.glEnable(GL.GL_DEPTH_TEST);
      gl.glDepthFunc(GL.GL_LEQUAL);
      context.depthTestEnabled = true;
    } else if (!state.isDepthTest() && context.depthTestEnabled) {
      gl.glDisable(GL.GL_DEPTH_TEST);
      context.depthTestEnabled = false;
    }

    if (state.isAlphaTest()) {
      setFixedFuncBinding(FixedFuncBinding.AlphaTestFallOff, state.getAlphaFallOff());
    } else {
      setFixedFuncBinding(FixedFuncBinding.AlphaTestFallOff, 0f); // disable it
    }

    if (state.isDepthWrite() && !context.depthWriteEnabled) {
      gl.glDepthMask(true);
      context.depthWriteEnabled = true;
    } else if (!state.isDepthWrite() && context.depthWriteEnabled) {
      gl.glDepthMask(false);
      context.depthWriteEnabled = false;
    }

    if (state.isColorWrite() && !context.colorWriteEnabled) {
      gl.glColorMask(true, true, true, true);
      context.colorWriteEnabled = true;
    } else if (!state.isColorWrite() && context.colorWriteEnabled) {
      gl.glColorMask(false, false, false, false);
      context.colorWriteEnabled = false;
    }

    if (state.isPointSprite()) {
      logger.log(Level.WARNING, "Point Sprite unsupported!");
    }

    if (state.isPolyOffset()) {
      if (!context.polyOffsetEnabled) {
        gl.glEnable(GL.GL_POLYGON_OFFSET_FILL);
        gl.glPolygonOffset(state.getPolyOffsetFactor(), state.getPolyOffsetUnits());
        context.polyOffsetEnabled = true;
        context.polyOffsetFactor = state.getPolyOffsetFactor();
        context.polyOffsetUnits = state.getPolyOffsetUnits();
      } else {
        if (state.getPolyOffsetFactor() != context.polyOffsetFactor
            || state.getPolyOffsetUnits() != context.polyOffsetUnits) {
          gl.glPolygonOffset(state.getPolyOffsetFactor(), state.getPolyOffsetUnits());
          context.polyOffsetFactor = state.getPolyOffsetFactor();
          context.polyOffsetUnits = state.getPolyOffsetUnits();
        }
      }
    } else {
      if (context.polyOffsetEnabled) {
        gl.glDisable(GL.GL_POLYGON_OFFSET_FILL);
        context.polyOffsetEnabled = false;
        context.polyOffsetFactor = 0;
        context.polyOffsetUnits = 0;
      }
    }
    if (state.getFaceCullMode() != context.cullMode) {
      if (state.getFaceCullMode() == RenderState.FaceCullMode.Off) {
        gl.glDisable(GL.GL_CULL_FACE);
      } else {
        gl.glEnable(GL.GL_CULL_FACE);
      }

      switch (state.getFaceCullMode()) {
        case Off:
          break;
        case Back:
          gl.glCullFace(GL.GL_BACK);
          break;
        case Front:
          gl.glCullFace(GL.GL_FRONT);
          break;
        case FrontAndBack:
          gl.glCullFace(GL.GL_FRONT_AND_BACK);
          break;
        default:
          throw new UnsupportedOperationException(
              "Unrecognized face cull mode: " + state.getFaceCullMode());
      }

      context.cullMode = state.getFaceCullMode();
    }

    if (state.getBlendMode() != context.blendMode) {
      if (state.getBlendMode() == RenderState.BlendMode.Off) {
        gl.glDisable(GL.GL_BLEND);
      } else {
        gl.glEnable(GL.GL_BLEND);
        switch (state.getBlendMode()) {
          case Off:
            break;
          case Additive:
            gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
            break;
          case AlphaAdditive:
            gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
            break;
          case Color:
            gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_COLOR);
            break;
          case Alpha:
            gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
            break;
          case PremultAlpha:
            gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
            break;
          case Modulate:
            gl.glBlendFunc(GL.GL_DST_COLOR, GL.GL_ZERO);
            break;
          case ModulateX2:
            gl.glBlendFunc(GL.GL_DST_COLOR, GL.GL_SRC_COLOR);
            break;
          default:
            throw new UnsupportedOperationException(
                "Unrecognized blend mode: " + state.getBlendMode());
        }
      }

      context.blendMode = state.getBlendMode();
    }

    if (state.isStencilTest()) {
      throw new UnsupportedOperationException(
          "OpenGL 1.1 doesn't support two sided stencil operations.");
    }
  }
  public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
    if (vb.getBufferType() == VertexBuffer.Type.Color && !context.useVertexColor) {
      // Ignore vertex color buffer if vertex color is disabled.
      return;
    }

    int arrayType = convertArrayType(vb.getBufferType());
    if (arrayType == -1) {
      return; // unsupported
    }
    GL gl = GLContext.getCurrentGL();
    gl.getGL2GL3().glEnableClientState(arrayType);
    context.boundAttribs[vb.getBufferType().ordinal()] = vb;

    if (vb.getBufferType() == Type.Normal) {
      // normalize if requested
      if (vb.isNormalized() && !context.normalizeEnabled) {
        gl.glEnable(GLLightingFunc.GL_NORMALIZE);
        context.normalizeEnabled = true;
      } else if (!vb.isNormalized() && context.normalizeEnabled) {
        gl.glDisable(GLLightingFunc.GL_NORMALIZE);
        context.normalizeEnabled = false;
      }
    }

    // NOTE: Use data from interleaved buffer if specified
    Buffer data = idb != null ? idb.getData() : vb.getData();
    int comps = vb.getNumComponents();
    int type = convertVertexFormat(vb.getFormat());

    data.rewind();

    switch (vb.getBufferType()) {
      case Position:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        gl.getGL2().glVertexPointer(comps, type, vb.getStride(), (FloatBuffer) data);
        break;
      case Normal:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        gl.getGL2().glNormalPointer(type, vb.getStride(), (FloatBuffer) data);
        break;
      case Color:
        if (data instanceof FloatBuffer) {
          gl.getGL2().glColorPointer(comps, type, vb.getStride(), (FloatBuffer) data);
        } else if (data instanceof ByteBuffer) {
          gl.getGL2().glColorPointer(comps, type, vb.getStride(), (ByteBuffer) data);
        } else {
          throw new UnsupportedOperationException();
        }
        break;
      case TexCoord:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        gl.getGL2().glTexCoordPointer(comps, type, vb.getStride(), (FloatBuffer) data);
        break;
      default:
        // Ignore, this is an unsupported attribute for OpenGL1.
        break;
    }
  }
Пример #4
0
  public static void closestFit3D(
      GL gl,
      int target,
      int width,
      int height,
      int depth,
      int internalFormat,
      int format,
      int type,
      int[] newWidth,
      int[] newHeight,
      int[] newDepth) {
    int widthPowerOf2 = nearestPower(width);
    int heightPowerOf2 = nearestPower(height);
    int depthPowerOf2 = nearestPower(depth);
    int[] proxyWidth = new int[1];

    do {
      // compute level 1 width & height & depth, clamping each at 1
      int widthAtLevelOne = (widthPowerOf2 > 1) ? widthPowerOf2 >> 1 : widthPowerOf2;
      int heightAtLevelOne = (heightPowerOf2 > 1) ? heightPowerOf2 >> 1 : heightPowerOf2;
      int depthAtLevelOne = (depthPowerOf2 > 1) ? depthPowerOf2 >> 1 : depthPowerOf2;
      int proxyTarget = 0;
      assert (widthAtLevelOne > 0);
      assert (heightAtLevelOne > 0);
      assert (depthAtLevelOne > 0);

      // does width x height x depth at level 1 & all their mipmaps fit?
      if (target == GL2GL3.GL_TEXTURE_3D || target == GL2GL3.GL_PROXY_TEXTURE_3D) {
        proxyTarget = GL2GL3.GL_PROXY_TEXTURE_3D;
        gl.getGL2GL3()
            .glTexImage3D(
                proxyTarget,
                1,
                internalFormat,
                widthAtLevelOne,
                heightAtLevelOne,
                depthAtLevelOne,
                0,
                format,
                type,
                null);
      }
      if (gl.isGL2GL3()) {
        gl.getGL2GL3()
            .glGetTexLevelParameteriv(proxyTarget, 1, GL2GL3.GL_TEXTURE_WIDTH, proxyWidth, 0);
      } else {
        proxyWidth[0] = 0;
      }
      // does it fit
      if (proxyWidth[0] == 0) {
        if (widthPowerOf2 == 1 && heightPowerOf2 == 1 && depthPowerOf2 == 1) {
          newWidth[0] = newHeight[0] = newDepth[0] = 1;
          return;
        }
        widthPowerOf2 = widthAtLevelOne;
        heightPowerOf2 = heightAtLevelOne;
        depthPowerOf2 = depthAtLevelOne;
      }
    } while (proxyWidth[0] == 0);
    // loop must terminate

    // return the width & height at level 0 that fits
    newWidth[0] = widthPowerOf2;
    newHeight[0] = heightPowerOf2;
    newDepth[0] = depthPowerOf2;
  }
Пример #5
0
  /* Given user requested textures size, determine if it fits. If it doesn't then
   * halve both sides and make the determination again until it does fit ( for
   * IR only ).
   * Note that proxy textures are not implemented in RE* even though they
   * advertise the texture extension.
   * Note that proxy textures are implemented but not according to spec in IMPACT*
   */
  public static void closestFit(
      GL gl,
      int target,
      int width,
      int height,
      int internalFormat,
      int format,
      int type,
      int[] newWidth,
      int[] newHeight) {
    // Use proxy textures if OpenGL version >= 1.1
    if (Double.parseDouble(gl.glGetString(GL.GL_VERSION).trim().substring(0, 3)) >= 1.1) {
      int widthPowerOf2 = nearestPower(width);
      int heightPowerOf2 = nearestPower(height);
      int[] proxyWidth = new int[1];
      boolean noProxyTextures = false;

      // Some drivers (in particular, ATI's) seem to set a GL error
      // when proxy textures are used even though this is in violation
      // of the spec. Guard against this and interactions with the
      // DebugGL by watching for GLException.
      try {
        do {
          // compute level 1 width & height, clamping each at 1
          int widthAtLevelOne = ((width > 1) ? (widthPowerOf2 >> 1) : widthPowerOf2);
          int heightAtLevelOne = ((height > 1) ? (heightPowerOf2 >> 1) : heightPowerOf2);
          int proxyTarget;

          assert (widthAtLevelOne > 0);
          assert (heightAtLevelOne > 0);

          // does width x height at level 1 & all their mipmaps fit?
          if (target == GL2GL3.GL_TEXTURE_2D || target == GL2GL3.GL_PROXY_TEXTURE_2D) {
            proxyTarget = GL2GL3.GL_PROXY_TEXTURE_2D;
            gl.glTexImage2D(
                proxyTarget,
                1,
                internalFormat,
                widthAtLevelOne,
                heightAtLevelOne,
                0,
                format,
                type,
                null);
          } else if ((target == GL2GL3.GL_TEXTURE_CUBE_MAP_POSITIVE_X)
              || (target == GL2GL3.GL_TEXTURE_CUBE_MAP_NEGATIVE_X)
              || (target == GL2GL3.GL_TEXTURE_CUBE_MAP_POSITIVE_Y)
              || (target == GL2GL3.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y)
              || (target == GL2GL3.GL_TEXTURE_CUBE_MAP_POSITIVE_Z)
              || (target == GL2GL3.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)) {
            proxyTarget = GL2GL3.GL_PROXY_TEXTURE_CUBE_MAP;
            gl.glTexImage2D(
                proxyTarget,
                1,
                internalFormat,
                widthAtLevelOne,
                heightAtLevelOne,
                0,
                format,
                type,
                null);
          } else {
            assert (target == GL2GL3.GL_TEXTURE_1D || target == GL2GL3.GL_PROXY_TEXTURE_1D);
            proxyTarget = GL2GL3.GL_PROXY_TEXTURE_1D;
            gl.getGL2GL3()
                .glTexImage1D(
                    proxyTarget, 1, internalFormat, widthAtLevelOne, 0, format, type, null);
          }
          if (gl.isGL2GL3()) {
            gl.getGL2GL3()
                .glGetTexLevelParameteriv(proxyTarget, 1, GL2GL3.GL_TEXTURE_WIDTH, proxyWidth, 0);
          } else {
            proxyWidth[0] = 0;
          }
          // does it fit?
          if (proxyWidth[0] == 0) { // nope, so try again with theses sizes
            if (widthPowerOf2 == 1 && heightPowerOf2 == 1) {
              /* A 1x1 texture couldn't fit for some reason so break out.  This
               * should never happen.  But things happen.  The disadvantage with
               * this if-statement is that we will never be aware of when this
               * happens since it will silently branch out.
               */
              noProxyTextures = true;
              break;
            }
            widthPowerOf2 = widthAtLevelOne;
            heightPowerOf2 = heightAtLevelOne;
          }
          // else it does fit
        } while (proxyWidth[0] == 0);
      } catch (GLException e) {
        noProxyTextures = true;
      }
      // loop must terminate
      // return the width & height at level 0 that fits
      if (!noProxyTextures) {
        newWidth[0] = widthPowerOf2;
        newHeight[0] = heightPowerOf2;
        return;
      }
    }
    int[] maxsize = new int[1];
    gl.glGetIntegerv(GL2GL3.GL_MAX_TEXTURE_SIZE, maxsize, 0);
    // clamp user's texture sizes to maximum sizes, if necessary
    newWidth[0] = nearestPower(width);
    if (newWidth[0] > maxsize[0]) {
      newWidth[0] = maxsize[0];
    }
    newHeight[0] = nearestPower(height);
    if (newHeight[0] > maxsize[0]) {
      newHeight[0] = maxsize[0];
    }
  }