Example #1
0
  /**
   * Reads an image from an archived file and return it as ByteBuffer object.
   *
   * @author Mike Butler, Kiet Le
   */
  private ByteBuffer readImage(String filename, Dimension dim) {
    if (dim == null) dim = new Dimension(0, 0);
    ByteBuffer bytes = null;
    try {
      DataInputStream dis =
          new DataInputStream(getClass().getClassLoader().getResourceAsStream(filename));
      dim.width = dis.readInt();
      dim.height = dis.readInt();
      System.out.println("Creating buffer, width: " + dim.width + " height: " + dim.height);
      // byte[] buf = new byte[3 * dim.height * dim.width];
      bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
      for (int i = 0; i < bytes.capacity(); i++) {
        bytes.put(dis.readByte());
      }
      dis.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
    bytes.rewind();
    return bytes;
  }
  protected void buildGrid(int w, int h) {
    int i, j, k, n;
    int numX, numY;

    if (built) {
      gl.glDeleteBuffersARB(1, vertCoordsVBO, 0);
      gl.glDeleteBuffersARB(numTextures, texCoordsVBO, 0);
    }

    numX = resX;
    numY = resY;

    if (numX <= 0) numX = w;
    if (numY <= 0) numY = h;

    numTextures = 100;

    for (k = 0; k < numLayers; k++) {
      if (points[k].s.length < numTextures) numTextures = points[k].s.length;
    }

    numVertices = numX * numY * numLayers;
    numTexCoords = numX * numY * numLayers;

    FloatBuffer vertCoordsBuffer;
    FloatBuffer[] texCoordsBuffer;

    vertCoordsBuffer = BufferUtil.newFloatBuffer(numVertices * 3);
    texCoordsBuffer = new FloatBuffer[numTextures];
    for (n = 0; n < numTextures; n++)
      texCoordsBuffer[n] = BufferUtil.newFloatBuffer(numTexCoords * 2);

    float x, y, s0, t0, s, t;
    float dx, dy, ds0, dt0, ds, dt;

    if ((mode == GL.GL_LINE_STRIP)
        || (mode == GL.GL_LINE_LOOP)
        || (mode == GL.GL_TRIANGLE_STRIP)
        || (mode == GL.GL_TRIANGLE_FAN)
        || (mode == GL.GL_QUAD_STRIP)) {
      if (1 < numX) ds0 = 1.0f / (numX - 1);
      else ds0 = 1.0f;
      if (1 < numY) dt0 = 1.0f / (numY - 1);
      else dt0 = 1.0f;
    } else {
      ds0 = 1.0f / numX;
      dt0 = 1.0f / numY;
    }

    for (j = 0; j < numY; j++)
      for (i = 0; i < numX; i++)
        for (k = 0; k < numLayers; k++) {
          s0 = (float) i * ds0;
          t0 = (float) j * dt0;

          x = points[k].x;
          y = points[k].y;
          dx = points[k].dx;
          dy = points[k].dy;

          if (x == -1.0f) x = s0 * w;
          if (y == -1.0f) y = t0 * h;
          if (dx == -1.0f) dx = ds0 * w;
          if (dy == -1.0f) dy = dt0 * h;

          vertCoordsBuffer.put(x + dx);
          vertCoordsBuffer.put(y + dy);
          vertCoordsBuffer.put(0.0f);

          for (n = 0; n < numTextures; n++) {
            s = points[k].s[n];
            t = points[k].t[n];
            ds = points[k].ds[n];
            dt = points[k].dt[n];

            if (s == -1.0f) s = s0;
            if (t == -1.0f) t = t0;
            if (ds == -1.0f) ds = ds0;
            if (dt == -1.0f) dt = dt0;

            texCoordsBuffer[n].put(s + ds);
            texCoordsBuffer[n].put(t + dt);
          }
        }

    vertCoordsBuffer.flip();
    for (n = 0; n < numTextures; n++) texCoordsBuffer[n].flip();

    texCoordsVBO = new int[numTextures];

    // Generate and bind The Vertex Buffer Object.
    gl.glGenBuffersARB(1, vertCoordsVBO, 0); // Get a valid OpenGL ID.
    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertCoordsVBO[0]); // Bind the buffer.
    gl.glBufferDataARB(
        GL.GL_ARRAY_BUFFER_ARB,
        numVertices * 3 * BufferUtil.SIZEOF_FLOAT,
        vertCoordsBuffer,
        GL.GL_STATIC_DRAW_ARB); // Load the data.

    // Generate and bind the Texture Coordinate Buffers
    gl.glGenBuffersARB(numTextures, texCoordsVBO, 0); // Get a valid OpenGL ID.
    for (n = 0; n < numTextures; n++) {
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, texCoordsVBO[n]); // Bind the buffer.
      gl.glBufferDataARB(
          GL.GL_ARRAY_BUFFER_ARB,
          numTexCoords * 2 * BufferUtil.SIZEOF_FLOAT,
          texCoordsBuffer[n],
          GL.GL_STATIC_DRAW_ARB); // Load the data.
    }

    built = true;
    width = w;
    height = h;
  }