Example #1
0
  public void setOrthographicView(int w, int h) {
    gl.glViewport(0, 0, w, h);

    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();

    glu.gluOrtho2D(0.0, w, 0.0, h);

    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }
Example #2
0
  public synchronized void bind(GL gl) {
    combine();

    if (id < 0) id = VisUtil.allocateID();

    int b[] = blocks.get(0);

    // the back-end special cases VBO_TYPE_COLOR, and uses gltype = unsigned byte.
    gl.glColor(Color.white);

    gl.gldBind(GL.VBO_TYPE_COLOR, id, b.length, 4, b);
  }
Example #3
0
  public static void shaderSource(final GL _gl, final int shader, final CharSequence[] source) {
    final GL2ES2 gl = _gl.getGL2ES2();
    if (!isShaderCompilerAvailable(_gl)) {
      throw new GLException("No compiler is available");
    }

    final int count = (null != source) ? source.length : 0;
    if (count == 0) {
      throw new GLException("No sources specified");
    }

    final IntBuffer lengths = Buffers.newDirectIntBuffer(count);
    for (int i = 0; i < count; i++) {
      lengths.put(i, source[i].length());
    }
    if (source instanceof String[]) {
      // rare case ..
      gl.glShaderSource(shader, count, (String[]) source, lengths);
    } else {
      final String[] tmp = new String[source.length];
      for (int i = source.length - 1; i >= 0; i--) {
        final CharSequence csq = source[i];
        if (csq instanceof String) {
          // if ShaderCode.create(.. mutableStringBuilder == false )
          tmp[i] = (String) csq;
        } else {
          // if ShaderCode.create(.. mutableStringBuilder == true )
          tmp[i] = source[i].toString();
        }
      }
      gl.glShaderSource(shader, count, tmp, lengths);
    }
  }
Example #4
0
  public GLState(GL gl) {
    if (INSTANCES_COUNT == 0) {
      GLState.gl = gl;

      glTextureObjects = new HashSet<Integer>();
      glVertexBuffers = new HashSet<Integer>();
      glFrameBuffers = new HashSet<Integer>();
      glRenderBuffers = new HashSet<Integer>();
      glslPrograms = new HashSet<Integer>();
      glslShaders = new HashSet<Integer>();
      cgContexts = new HashSet<Object>();
      cgPrograms = new HashSet<Object>();
      cgEffects = new HashSet<Object>();

      getVersionNumbers();
      getAvailableExtensions();

      int[] val = {0};
      gl.glGetIntegerv(GL.GL_MAX_COLOR_ATTACHMENTS_EXT, val, 0);
      glMaxColorAttach = val[0];

      glu = new GLU();
      FBO = new GLFramebufferObject(gl);

      fboStack = new Stack<GLFramebufferObject>();
      destTexStack = new Stack<GLTexture[]>();
      screenFBO = new GLFramebufferObject(gl, true);
      currentFBO = screenFBO;
      emptyDestTex = new GLTexture[0];
      currentDestTex = emptyDestTex;

      singleFBO = false;
    }
    INSTANCES_COUNT++;
  }
Example #5
0
  public static boolean createAndLoadShader(
      final GL _gl,
      final IntBuffer shader,
      final int shaderType,
      final int binFormat,
      final java.nio.Buffer bin,
      final PrintStream verboseOut) {
    final GL2ES2 gl = _gl.getGL2ES2();
    int err = gl.glGetError(); // flush previous errors ..
    if (err != GL.GL_NO_ERROR && null != verboseOut) {
      verboseOut.println("createAndLoadShader: Pre GL Error: 0x" + Integer.toHexString(err));
    }

    createShader(gl, shaderType, shader);
    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR) {
      throw new GLException(
          "createAndLoadShader: CreateShader failed, GL Error: 0x" + Integer.toHexString(err));
    }

    shaderBinary(gl, shader, binFormat, bin);

    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR && null != verboseOut) {
      verboseOut.println(
          "createAndLoadShader: ShaderBinary failed, GL Error: 0x" + Integer.toHexString(err));
    }
    return err == GL.GL_NO_ERROR;
  }
Example #6
0
  public static boolean isProgramStatusValid(final GL _gl, final int programObj, final int name) {
    final GL2ES2 gl = _gl.getGL2ES2();
    final int[] ires = new int[1];
    gl.glGetProgramiv(programObj, name, ires, 0);

    return ires[0] == 1;
  }
Example #7
0
 /** Returns true if a hader compiler is available, otherwise false. */
 public static boolean isShaderCompilerAvailable(final GL _gl) {
   final GL2ES2 gl = _gl.getGL2ES2();
   final ProfileInformation info = getProfileInformation(gl);
   if (null == info.shaderCompilerAvailable) {
     if (gl.isGLES2()) {
       boolean queryOK = false;
       try {
         final byte[] param = new byte[1];
         gl.glGetBooleanv(GL2ES2.GL_SHADER_COMPILER, param, 0);
         final int err = gl.glGetError();
         boolean v = GL.GL_NO_ERROR == err && param[0] != (byte) 0x00;
         if (!v) {
           final Set<Integer> bfs = getShaderBinaryFormats(gl);
           if (bfs.size() == 0) {
             // no supported binary formats, hence a compiler must be available!
             v = true;
           }
         }
         info.shaderCompilerAvailable = Boolean.valueOf(v);
         queryOK = true;
       } catch (final GLException gle) {
         System.err.println("Caught exception on thread " + Thread.currentThread().getName());
         gle.printStackTrace();
       }
       if (!queryOK) {
         info.shaderCompilerAvailable = Boolean.valueOf(true);
       }
     } else if (gl.isGL2ES2()) {
       info.shaderCompilerAvailable = new Boolean(true);
     } else {
       throw new GLException("Invalid OpenGL profile");
     }
   }
   return info.shaderCompilerAvailable.booleanValue();
 }
Example #8
0
 /**
  * If supported, queries the natively supported shader binary formats using {@link
  * GL2ES2#GL_NUM_SHADER_BINARY_FORMATS} and {@link GL2ES2#GL_SHADER_BINARY_FORMATS} via {@link
  * GL2ES2#glGetIntegerv(int, int[], int)}.
  */
 public static Set<Integer> getShaderBinaryFormats(final GL _gl) {
   final GL2ES2 gl = _gl.getGL2ES2();
   final ProfileInformation info = getProfileInformation(gl);
   if (null == info.shaderBinaryFormats) {
     info.shaderBinaryFormats = new HashSet<Integer>();
     if (gl.isGLES2Compatible()) {
       try {
         final int[] param = new int[1];
         gl.glGetIntegerv(GL2ES2.GL_NUM_SHADER_BINARY_FORMATS, param, 0);
         final int err = gl.glGetError();
         final int numFormats = GL.GL_NO_ERROR == err ? param[0] : 0;
         if (numFormats > 0) {
           final int[] formats = new int[numFormats];
           gl.glGetIntegerv(GL2ES2.GL_SHADER_BINARY_FORMATS, formats, 0);
           for (int i = 0; i < numFormats; i++) {
             info.shaderBinaryFormats.add(Integer.valueOf(formats[i]));
           }
         }
       } catch (final GLException gle) {
         System.err.println("Caught exception on thread " + Thread.currentThread().getName());
         gle.printStackTrace();
       }
     }
   }
   return info.shaderBinaryFormats;
 }
Example #9
0
  public void clearColorBuffer(int color) {
    float r, g, b, a;
    int ir, ig, ib, ia;

    ia = (color >> 24) & 0xff;
    ir = (color >> 16) & 0xff;
    ig = (color >> 8) & 0xff;
    ib = color & 0xff;

    a = ia / 255.0f;
    r = ir / 255.0f;
    g = ig / 255.0f;
    b = ib / 255.0f;

    gl.glClearColor(r, g, b, a);
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
  }
Example #10
0
  public void paintTex(int glid, int target, int w, int h, int color) {
    float r, g, b, a;
    int ir, ig, ib, ia;

    ia = (color >> 24) & 0xff;
    ir = (color >> 16) & 0xff;
    ig = (color >> 8) & 0xff;
    ib = color & 0xff;

    a = ia / 255.0f;
    r = ir / 255.0f;
    g = ig / 255.0f;
    b = ib / 255.0f;

    pushFramebuffer();
    setFramebuffer(FBO);
    FBO.setDrawBuffer(target, glid);

    saveView();
    setOrthographicView(w, h);
    gl.glColor4f(r, g, b, a);
    gl.glBegin(GL.GL_QUADS);
    gl.glVertex2f(0.0f, 0.0f);
    gl.glVertex2f(w, 0.0f);
    gl.glVertex2f(w, h);
    gl.glVertex2f(0.0f, h);
    gl.glEnd();
    restoreView();

    popFramebuffer();
  }
Example #11
0
 private static ProfileInformation getProfileInformation(final GL gl) {
   final GLContext context = gl.getContext();
   context.validateCurrent();
   ProfileInformation data = (ProfileInformation) context.getAttachedObject(implObjectKey);
   if (data == null) {
     data = new ProfileInformation();
     context.attachObject(implObjectKey, data);
   }
   return data;
 }
Example #12
0
  public static boolean isShaderStatusValid(
      final GL _gl, final int shaderObj, final int name, final PrintStream verboseOut) {
    final GL2ES2 gl = _gl.getGL2ES2();
    final int[] ires = new int[1];
    gl.glGetShaderiv(shaderObj, name, ires, 0);

    final boolean res = ires[0] == 1;
    if (!res && null != verboseOut) {
      verboseOut.println("Shader status invalid: " + getShaderInfoLog(gl, shaderObj));
    }
    return res;
  }
Example #13
0
  public static int createGLResource(int type, int param) {
    int id = 0;
    if (type == GL_TEXTURE_OBJECT) {
      int[] temp = new int[1];
      gl.glGenTextures(1, temp, 0);
      id = temp[0];
      glTextureObjects.add(id);
    } else if (type == GL_VERTEX_BUFFER) {
      int[] temp = new int[1];
      gl.glGenBuffersARB(1, temp, 0);
      id = temp[0];
      glVertexBuffers.add(id);
    } else if (type == GL_FRAME_BUFFER) {
      int[] temp = new int[1];
      gl.glGenFramebuffersEXT(1, temp, 0);
      id = temp[0];
      glFrameBuffers.add(id);
    } else if (type == GL_RENDER_BUFFER) {
      int[] temp = new int[1];
      gl.glGenRenderbuffersEXT(1, temp, 0);
      id = temp[0];
      glRenderBuffers.add(id);
    } else if (type == GLSL_PROGRAM) {
      id = gl.glCreateProgram();
      glslPrograms.add(id);
    } else if (type == GLSL_SHADER) {
      id = gl.glCreateShader(param);
      glslShaders.add(id);
    }

    return id;
  }
Example #14
0
  public void render3D(GL gl, GLDrawable glc) {
    if (pts2d.size() < 4) return;
    if (fill != null || texture != null) {
      if (texture == null) {
        setColor(gl, fill);
        gl.glDisable(GL.GL_TEXTURE_2D);
      } else {
        setColor(gl, Color.white);
        Texture gltexture = texture.getTexture(glc);
        gltexture.enable();
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
        gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
        gltexture.bind();
      }
      gl.glPushMatrix();

      // get 4 control points of portal
      Point2D p0 = pts2d.get(0);
      Point2D p1 = pts2d.get(1);
      Point2D p2 = pts2d.get(2);
      Point2D p3 = pts2d.get(3);

      // render 1st side of portal with height extra[0]
      renderOneside(p0, p1, gl, extra[0]);

      // render 1st side of portal with height extra[1]
      renderOneside(p2, p3, gl, extra[1]);

      gl.glPopMatrix();
    }
  }
Example #15
0
  public void clearTex(int glid, int target, int color) {
    float r, g, b, a;
    int ir, ig, ib, ia;

    ia = (color >> 24) & 0xff;
    ir = (color >> 16) & 0xff;
    ig = (color >> 8) & 0xff;
    ib = color & 0xff;

    a = ia / 255.0f;
    r = ir / 255.0f;
    g = ig / 255.0f;
    b = ib / 255.0f;

    pushFramebuffer();
    setFramebuffer(FBO);
    FBO.setDrawBuffer(target, glid);

    gl.glClearColor(r, g, b, a);
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);

    popFramebuffer();
  }
Example #16
0
  public static String getProgramInfoLog(final GL _gl, final int programObj) {
    final GL2ES2 gl = _gl.getGL2ES2();
    final int[] infoLogLength = new int[1];
    gl.glGetProgramiv(programObj, GL2ES2.GL_INFO_LOG_LENGTH, infoLogLength, 0);

    if (infoLogLength[0] == 0) {
      return "(no info log)";
    }
    final int[] charsWritten = new int[1];
    final byte[] infoLogBytes = new byte[infoLogLength[0]];
    gl.glGetProgramInfoLog(programObj, infoLogLength[0], charsWritten, 0, infoLogBytes, 0);

    return new String(infoLogBytes, 0, charsWritten[0]);
  }
Example #17
0
 /**
  * Performs {@link GL2ES2#glValidateProgram(int)}
  *
  * <p>One shall only call this method while debugging and only if all required resources by the
  * shader are set.
  *
  * <p>Note: It is possible that a working shader program will fail validation. This has been
  * experienced on NVidia APX2500 and Tegra2.
  *
  * @see GL2ES2#glValidateProgram(int)
  */
 public static boolean isProgramExecStatusValid(
     final GL _gl, final int programObj, final PrintStream verboseOut) {
   final GL2ES2 gl = _gl.getGL2ES2();
   gl.glValidateProgram(programObj);
   if (!isProgramStatusValid(gl, programObj, GL2ES2.GL_VALIDATE_STATUS)) {
     if (null != verboseOut) {
       verboseOut.println(
           "Program validation failed: "
               + programObj
               + "\n\t"
               + getProgramInfoLog(gl, programObj));
     }
     return false;
   }
   return true;
 }
Example #18
0
 public static boolean isProgramLinkStatusValid(
     final GL _gl, final int programObj, final PrintStream verboseOut) {
   final GL2ES2 gl = _gl.getGL2ES2();
   if (!gl.glIsProgram(programObj)) {
     if (null != verboseOut) {
       verboseOut.println("Program name invalid: " + programObj);
     }
     return false;
   }
   if (!isProgramStatusValid(gl, programObj, GL2ES2.GL_LINK_STATUS)) {
     if (null != verboseOut) {
       verboseOut.println(
           "Program link failed: " + programObj + "\n\t" + getProgramInfoLog(gl, programObj));
     }
     return false;
   }
   return true;
 }
Example #19
0
 @Override
 public void clear(final GL gl, final int sizeLimit, final boolean fading) {
   isFading = fading;
   if (sizeLimit == 0) {
     objects.clear();
   } else {
     int size = objects.size();
     for (int i = 0, n = size - sizeLimit; i < n; i++) {
       objects.poll();
     }
   }
   currentList = newCurrentList();
   objects.offer(currentList);
   Integer index = openGLListIndex;
   if (index != null) {
     gl.getGL2().glDeleteLists(index, 1);
     openGLListIndex = null;
   }
 }
Example #20
0
  public static void shaderBinary(
      final GL _gl, final IntBuffer shaders, final int binFormat, final java.nio.Buffer bin) {
    final GL2ES2 gl = _gl.getGL2ES2();
    if (getShaderBinaryFormats(gl).size() <= 0) {
      throw new GLException("No binary formats are supported");
    }

    final int shaderNum = shaders.remaining();
    if (shaderNum <= 0) {
      throw new GLException("No shaders specified");
    }
    if (null == bin) {
      throw new GLException("Null shader binary");
    }
    final int binLength = bin.remaining();
    if (0 >= binLength) {
      throw new GLException("Empty shader binary (remaining == 0)");
    }
    gl.glShaderBinary(shaderNum, shaders, binFormat, bin, binLength);
  }
Example #21
0
  public static boolean createAndCompileShader(
      final GL _gl,
      final IntBuffer shader,
      final int shaderType,
      final CharSequence[][] sources,
      final PrintStream verboseOut) {
    final GL2ES2 gl = _gl.getGL2ES2();
    int err = gl.glGetError(); // flush previous errors ..
    if (err != GL.GL_NO_ERROR && null != verboseOut) {
      verboseOut.println("createAndCompileShader: Pre GL Error: 0x" + Integer.toHexString(err));
    }

    createShader(gl, shaderType, shader);
    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR) {
      throw new GLException(
          "createAndCompileShader: CreateShader failed, GL Error: 0x" + Integer.toHexString(err));
    }

    shaderSource(gl, shader, sources);
    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR) {
      throw new GLException(
          "createAndCompileShader: ShaderSource failed, GL Error: 0x" + Integer.toHexString(err));
    }

    compileShader(gl, shader);
    err = gl.glGetError();
    if (err != GL.GL_NO_ERROR && null != verboseOut) {
      verboseOut.println(
          "createAndCompileShader: CompileShader failed, GL Error: 0x" + Integer.toHexString(err));
    }

    return isShaderStatusValid(gl, shader, GL2ES2.GL_COMPILE_STATUS, verboseOut)
        && err == GL.GL_NO_ERROR;
  }
Example #22
0
 public static void deleteGLResource(int id, int type) {
   if (type == GL_TEXTURE_OBJECT) {
     if (glTextureObjects.contains(id)) {
       int[] temp = {id};
       gl.glDeleteTextures(1, temp, 0);
       glTextureObjects.remove(id);
     }
   } else if (type == GL_VERTEX_BUFFER) {
     if (glVertexBuffers.contains(id)) {
       int[] temp = {id};
       gl.glDeleteBuffersARB(1, temp, 0);
       glVertexBuffers.remove(id);
     }
   } else if (type == GL_FRAME_BUFFER) {
     if (glFrameBuffers.contains(id)) {
       int[] temp = {id};
       gl.glDeleteFramebuffersEXT(1, temp, 0);
       glFrameBuffers.remove(id);
     }
   } else if (type == GL_RENDER_BUFFER) {
     if (glRenderBuffers.contains(id)) {
       int[] temp = {id};
       gl.glDeleteRenderbuffersEXT(1, temp, 0);
       glRenderBuffers.remove(id);
     }
   } else if (type == GLSL_PROGRAM) {
     if (glslPrograms.contains(id)) {
       gl.glDeleteProgram(id);
       glslPrograms.remove(id);
     }
   } else if (type == GLSL_SHADER) {
     if (glslShaders.contains(id)) {
       gl.glDeleteShader(id);
       glslShaders.remove(id);
     }
   }
 }
Example #23
0
  public synchronized void unbind(GL gl) {
    int b[] = blocks.get(0);

    gl.gldUnbind(GL.VBO_TYPE_COLOR, id);
  }
Example #24
0
 protected void setupDefaultBlending() {
   // Default blending mode in PGraphicsOpenGL.
   blendMode = BLEND;
   gl.glBlendEquation(GL.GL_FUNC_ADD);
   gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
 }
Example #25
0
 protected void getVersionNumbers() {
   glVersion = gl.glGetString(GL.GL_VERSION);
   glMajor = Character.getNumericValue(glVersion.charAt(0));
   glMinor = Character.getNumericValue(glVersion.charAt(2));
   glslVersion = gl.glGetString(GL.GL_SHADING_LANGUAGE_VERSION_ARB);
 }
Example #26
0
  protected void getAvailableExtensions() {
    // For a complete list of extensions, go to this sections in the openGL
    // registry:
    // http://www.opengl.org/registry/#arbextspecs
    // http://www.opengl.org/registry/#otherextspecs
    String extensions = gl.glGetString(GL.GL_EXTENSIONS);

    if (extensions.indexOf("GL_ARB_multitexture") == -1) {
      multiTexAvailable = false;
      System.out.println("GL_ARB_multitexture extension not available");
    }

    if (extensions.indexOf("GL_ARB_vertex_buffer_object") == -1) {
      vbosAvailable = false;
      System.out.println("GL_ARB_vertex_buffer_object extension not available");
    }

    if (extensions.indexOf("GL_EXT_framebuffer_object") == -1) {
      fbosAvailable = false;
      System.out.println("GL_EXT_framebuffer_object extension not available");
    }

    if (extensions.indexOf("GL_ARB_shader_objects") == -1) {
      shadersAvailable = false;
      System.out.println("GL_ARB_shader_objects extension not available");
    }

    if (extensions.indexOf("GL_EXT_geometry_shader4") == -1) {
      geoShadersAvailable = false;
      System.out.println("GL_ARB_geometry_shader4 extension not available");
    }

    if (extensions.indexOf("GL_ARB_vertex_shader") == -1) {
      vertShadersAvailable = false;
      System.out.println("GL_ARB_vertex_shader extension not available");
    }

    if (extensions.indexOf("GL_ARB_fragment_shader") == -1) {
      fragShadersAvailable = false;
      System.out.println("GL_ARB_fragment_shader extension not available");
    }

    if (extensions.indexOf("GL_ARB_shading_language_100") == -1) {
      glsl100Available = false;
      System.out.println("GL_ARB_shading_language_100 extension not available");
    }

    if (extensions.indexOf("GL_ARB_texture_float") == -1) {
      floatTexAvailable = false;
      System.out.println("GL_ARB_texture_float extension not available");
    }

    if (extensions.indexOf("GL_ARB_texture_non_power_of_two") == -1) {
      nonTwoPowTexAvailable = false;
      System.out.println("GL_ARB_texture_non_power_of_two extension not available");
    }

    if (extensions.indexOf("GL_EXT_framebuffer_multisample") == -1) {
      fboMultisampleAvailable = false;
      System.out.println("GL_EXT_framebuffer_multisample extension not available");
    }
  }
Example #27
0
 public static void deleteShader(final GL _gl, final IntBuffer shaders) {
   final GL2ES2 gl = _gl.getGL2ES2();
   for (int i = shaders.position(); i < shaders.limit(); i++) {
     gl.glDeleteShader(shaders.get(i));
   }
 }
Example #28
0
 public static void detachShader(final GL _gl, final int program, final IntBuffer shaders) {
   final GL2ES2 gl = _gl.getGL2ES2();
   for (int i = shaders.position(); i < shaders.limit(); i++) {
     gl.glDetachShader(program, shaders.get(i));
   }
 }
Example #29
0
 /**
  * Returns true if GeometryShader is supported, i.e. whether GLContext is &ge; 3.2 or
  * ARB_geometry_shader4 extension is available.
  */
 public static boolean isGeometryShaderSupported(final GL _gl) {
   final GLContext ctx = _gl.getContext();
   return ctx.getGLVersionNumber().compareTo(GLContext.Version320) >= 0
       || ctx.isExtensionAvailable(GLExtensions.ARB_geometry_shader4);
 }
Example #30
0
 public static void createShader(final GL _gl, final int type, final IntBuffer shaders) {
   final GL2ES2 gl = _gl.getGL2ES2();
   for (int i = shaders.position(); i < shaders.limit(); i++) {
     shaders.put(i, gl.glCreateShader(type));
   }
 }