Exemple #1
0
  private void newVertexBufferToDraw() {
    // (number of points) * (number of coordinate values) * 4 bytes per float)
    ByteBuffer bb = ByteBuffer.allocateDirect(verticesCoords.size() * COORDS_PER_VERTEX * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());
    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();
    // add the coordinates to the FloatBuffer
    vertexBuffer.put(Utils.pointVectorToArray(verticesCoords));
    // set the buffer to read the first coordinate
    vertexBuffer.position(0);
    vertexCount = verticesCoords.size();
    // Log.d(GAlg.DEBUG_TAG, "Scene coords to draw: " + verticesCoords.toString());
    GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, vertexCount, vertexBuffer);

    if (drawLines) {
      bb = ByteBuffer.allocateDirect(linesCoords.size() * COORDS_PER_VERTEX * 4);
      bb.order(ByteOrder.nativeOrder());
      linesVertexBuffer = bb.asFloatBuffer();
      linesVertexBuffer.put(Utils.pointVectorToArray(linesCoords));
      linesVertexBuffer.position(0);
      linesVertexCount = linesCoords.size();
      Log.d(GAlg.DEBUG_TAG, "Drawing lines between: " + linesCoords.toString());
      GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, linesVertexCount, linesVertexBuffer);
    }
  }
 // 初始化顶点坐标与着色数据的方法
 public void initVertexData() {
   // 顶点坐标数据的初始化================begin============================
   vCount = 6; // 每个格子两个三角形,每个三角形3个顶点
   float vertices[] = {
     -width / 2, height / 2, 0,
     -width / 2, -height / 2, 0,
     width / 2, height / 2, 0,
     -width / 2, -height / 2, 0,
     width / 2, -height / 2, 0,
     width / 2, height / 2, 0
   };
   // 创建顶点坐标数据缓冲
   // vertices.length*4是因为一个整数四个字节
   ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
   vbb.order(ByteOrder.nativeOrder()); // 设置字节顺序
   mVertexBuffer = vbb.asFloatBuffer(); // 转换为int型缓冲
   mVertexBuffer.put(vertices); // 向缓冲区中放入顶点坐标数据
   mVertexBuffer.position(0); // 设置缓冲区起始位置
   float textures[] = {0f, 0f, 0f, 1, 1, 0f, 0f, 1, 1, 1, 1, 0f};
   // 创建顶点纹理数据缓冲
   ByteBuffer tbb = ByteBuffer.allocateDirect(textures.length * 4);
   tbb.order(ByteOrder.nativeOrder()); // 设置字节顺序
   mTextureBuffer = tbb.asFloatBuffer(); // 转换为Float型缓冲
   mTextureBuffer.put(textures); // 向缓冲区中放入顶点着色数据
   mTextureBuffer.position(0); // 设置缓冲区起始位置
   // 特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
   // 转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
   // 顶点纹理数据的初始化================end============================
 }
  public PointParticleSystem(int maxParticleNum, int resId, boolean sameSize, boolean sameColor) {
    this.sameSize = sameSize;
    this.sameColor = sameColor;

    texture = new BitmapTexture(resId);

    ByteBuffer vfb = ByteBuffer.allocateDirect(BYTES_PER_FLOAT * 2 * maxParticleNum);
    vfb.order(ByteOrder.nativeOrder());
    vertexBuffer = vfb.asFloatBuffer();

    if (!sameSize) {
      vfb = ByteBuffer.allocateDirect(BYTES_PER_FLOAT * 1 * maxParticleNum);
      vfb.order(ByteOrder.nativeOrder());
      sizeBuffer = vfb.asFloatBuffer();
    }

    if (!sameColor) {
      vfb = ByteBuffer.allocateDirect(BYTES_PER_FLOAT * 4 * maxParticleNum);
      vfb.order(ByteOrder.nativeOrder());
      colorBuffer = vfb.asFloatBuffer();
    }

    particles = new Particle[maxParticleNum];

    for (int i = 0; i < particles.length; i++) particles[i] = new Particle();

    onInit(particles);

    addGlStatusController(texture);
  }
Exemple #4
0
  private void drawMesh(int x[], int y[], float u[], float v[], int nx, int ny) {
    /*
     * Given a 3x3 nine-patch image, the vertex order is defined as the
     * following graph:
     *
     * (0) (1) (2) (3)
     *  |  /|  /|  /|
     *  | / | / | / |
     * (4) (5) (6) (7)
     *  | \ | \ | \ |
     *  |  \|  \|  \|
     * (8) (9) (A) (B)
     *  |  /|  /|  /|
     *  | / | / | / |
     * (C) (D) (E) (F)
     *
     * And we draw the triangle strip in the following index order:
     *
     * index: 04152637B6A5948C9DAEBF
     */
    int pntCount = 0;
    float xy[] = mXyBuffer;
    float uv[] = mUvBuffer;
    for (int j = 0; j < ny; ++j) {
      for (int i = 0; i < nx; ++i) {
        int xIndex = (pntCount++) << 1;
        int yIndex = xIndex + 1;
        xy[xIndex] = x[i];
        xy[yIndex] = y[j];
        uv[xIndex] = u[i];
        uv[yIndex] = v[j];
      }
    }
    mUvPointer.asFloatBuffer().put(uv, 0, pntCount << 1).position(0);
    mXyPointer.asFloatBuffer().put(xy, 0, pntCount << 1).position(0);

    int idxCount = 1;
    byte index[] = mIndexBuffer;
    for (int i = 0, bound = nx * (ny - 1); true; ) {
      // normal direction
      --idxCount;
      for (int j = 0; j < nx; ++j, ++i) {
        index[idxCount++] = (byte) i;
        index[idxCount++] = (byte) (i + nx);
      }
      if (i >= bound) break;

      // reverse direction
      int sum = i + i + nx - 1;
      --idxCount;
      for (int j = 0; j < nx; ++j, ++i) {
        index[idxCount++] = (byte) (sum - i);
        index[idxCount++] = (byte) (sum - i + nx);
      }
      if (i >= bound) break;
    }
    mIndexPointer.put(index, 0, idxCount).position(0);

    mGL.glDrawElements(GL11.GL_TRIANGLE_STRIP, idxCount, GL11.GL_UNSIGNED_BYTE, mIndexPointer);
  }
Exemple #5
0
  public MyRenderer(Context ctx) {
    super(ctx);

    parser = new OBJParser(ctx);
    model = parser.parseOBJ("/sdcard/windmill.obj");
    Debug.stopMethodTracing();
    this.setRenderer(this);
    this.requestFocus();
    this.setFocusableInTouchMode(true);

    ByteBuffer byteBuf = ByteBuffer.allocateDirect(lightAmbient.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    lightAmbientBuffer = byteBuf.asFloatBuffer();
    lightAmbientBuffer.put(lightAmbient);
    lightAmbientBuffer.position(0);

    byteBuf = ByteBuffer.allocateDirect(lightDiffuse.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    lightDiffuseBuffer = byteBuf.asFloatBuffer();
    lightDiffuseBuffer.put(lightDiffuse);
    lightDiffuseBuffer.position(0);

    byteBuf = ByteBuffer.allocateDirect(lightPosition.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    lightPositionBuffer = byteBuf.asFloatBuffer();
    lightPositionBuffer.put(lightPosition);
    lightPositionBuffer.position(0);
  }
Exemple #6
0
  public Gracz(float x, float y) {

    this.x = x;
    this.y = y;

    float[] tablicaWierzcholkow = {
      -x, y, -x, -y, x, -y, x, y,
    };

    float[] tablicaTekstur = {
      0.0f, 0.0f,
      0.0f, 1.0f,
      1.0f, 1.0f,
      1.0f, 0.0f,
    };

    byte[] tablicaIndeksow = {
      0, 1, 2,
      0, 2, 3,
    };

    ByteBuffer byteBufferW = ByteBuffer.allocateDirect(tablicaWierzcholkow.length * 4);
    byteBufferW.order(ByteOrder.nativeOrder());
    buforWierzcholka = byteBufferW.asFloatBuffer();
    buforWierzcholka.put(tablicaWierzcholkow).position(0);

    ByteBuffer byteBufferT = ByteBuffer.allocateDirect(tablicaTekstur.length * 4);
    byteBufferT.order(ByteOrder.nativeOrder());
    buforTekstury = byteBufferT.asFloatBuffer();
    buforTekstury.put(tablicaTekstur).position(0);

    buforIndeksow = ByteBuffer.allocateDirect(tablicaIndeksow.length);
    buforIndeksow.order(ByteOrder.nativeOrder());
    buforIndeksow.put(tablicaIndeksow).position(0);
  }
Exemple #7
0
  public static void glGetFloatv(int pname, float[] params) {
    ByteBuffer bytes = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder());

    glGetFloat(pname, (FloatBuffer) bytes.asFloatBuffer().put(params).flip());

    bytes.asFloatBuffer().get(params);
  }
Exemple #8
0
    // 初始化坐标数据的方法
    public void initVertexData(float R, float angle_span, float height) {
      List<Float> tempList = new ArrayList<Float>();
      // 将交通锥的上侧顶点添加到List集合中
      tempList.add(0f);
      tempList.add(height);
      tempList.add(0f);
      for (float vAngle = 0; vAngle <= 360; vAngle = vAngle + angle_span) {
        float x = (float) (R * Math.cos(Math.toRadians(vAngle)));
        float y = -height;
        float z = (float) (-R * Math.sin(Math.toRadians(vAngle)));

        tempList.add(x);
        tempList.add(y);
        tempList.add(z);
      }
      vCount = tempList.size() / 3; // 顶点数量
      float[] vertex = new float[tempList.size()];
      for (int i = 0; i < tempList.size(); i++) {
        vertex[i] = tempList.get(i);
      }
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertex.length * 4);
      vbb.order(ByteOrder.nativeOrder());
      mVertexBuffer = vbb.asFloatBuffer();
      mVertexBuffer.put(vertex);
      mVertexBuffer.position(0);

      float[] texcoor = generateTexCoor((int) (360 / angle_span + 1), 1, 1);
      ByteBuffer tbb = ByteBuffer.allocateDirect(texcoor.length * 4);
      tbb.order(ByteOrder.nativeOrder());
      mTexCoorBuffer = tbb.asFloatBuffer();
      mTexCoorBuffer.put(texcoor);
      mTexCoorBuffer.position(0);
    }
Exemple #9
0
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      ByteBuffer byteBuf = ByteBuffer.allocateDirect(48);
      byteBuf.order(ByteOrder.nativeOrder());
      m_verticesBuffer = byteBuf.asFloatBuffer();

      byteBuf = ByteBuffer.allocateDirect(32);
      byteBuf.order(ByteOrder.nativeOrder());
      m_coordinatesBuffer = byteBuf.asFloatBuffer();

      gl.glEnable(GL10.GL_TEXTURE_2D);
      gl.glGenTextures(1, m_glTexture, 0);
      gl.glBindTexture(GL10.GL_TEXTURE_2D, m_glTexture[0]);
      gl.glTexImage2D(
          GL10.GL_TEXTURE_2D,
          0,
          mRgb,
          m_glTextureSize,
          m_glTextureSize,
          0,
          mRgb,
          GL10.GL_UNSIGNED_BYTE,
          m_rgbSource);
      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    }
  /**
   * Creates the buffers we use to store information about the 3D world. OpenGL doesn't use Java
   * arrays, but rather needs data in a format it can understand. Hence we use ByteBuffers.
   *
   * @param config The EGL configuration used when creating the surface.
   */
  @Override
  public void onSurfaceCreated(EGLConfig config) {
    Log.i(TAG, "In onSurfaceCreated");
    GLES20.glClearColor(0.1f, 0.1f, 0.1f, 0.5f); // Dark background so camera stuff show up.

    ByteBuffer bb = ByteBuffer.allocateDirect(squareVertices.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareVertices);
    vertexBuffer.position(0);

    ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    ByteBuffer bb2 = ByteBuffer.allocateDirect(textureVertices.length * 4);
    bb2.order(ByteOrder.nativeOrder());
    textureVerticesBuffer = bb2.asFloatBuffer();
    textureVerticesBuffer.put(textureVertices);
    textureVerticesBuffer.position(0);

    int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
    GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);

    // Create a texture and start mirroring the camera on the texture:
    texture = createTexture();
    startCamera(texture);
  }
Exemple #11
0
    // 初始化坐标数据的方法
    public void initVertexData(float R, float r, float angle_span, float height) {
      List<Float> tempList = new ArrayList<Float>();
      for (float vAngle = 0; vAngle < 360; vAngle = vAngle + angle_span) {
        float x0 = (float) (r * Math.cos(Math.toRadians(vAngle)));
        float y0 = height;
        float z0 = (float) (-r * Math.sin(Math.toRadians(vAngle)));

        float x1 = (float) (R * Math.cos(Math.toRadians(vAngle)));
        float y1 = -height;
        float z1 = (float) (-R * Math.sin(Math.toRadians(vAngle)));

        float x2 = (float) (R * Math.cos(Math.toRadians(vAngle + angle_span)));
        float y2 = -height;
        float z2 = (float) (-R * Math.sin(Math.toRadians(vAngle + angle_span)));

        float x3 = (float) (r * Math.cos(Math.toRadians(vAngle + angle_span)));
        float y3 = height;
        float z3 = (float) (-r * Math.sin(Math.toRadians(vAngle + angle_span)));

        tempList.add(x0);
        tempList.add(y0);
        tempList.add(z0);
        tempList.add(x1);
        tempList.add(y1);
        tempList.add(z1);
        tempList.add(x3);
        tempList.add(y3);
        tempList.add(z3);

        tempList.add(x3);
        tempList.add(y3);
        tempList.add(z3);
        tempList.add(x1);
        tempList.add(y1);
        tempList.add(z1);
        tempList.add(x2);
        tempList.add(y2);
        tempList.add(z2);
      }
      vCount = tempList.size() / 3; // 顶点数量
      float[] vertex = new float[tempList.size()];
      for (int i = 0; i < tempList.size(); i++) {
        vertex[i] = tempList.get(i);
      }
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertex.length * 4);
      vbb.order(ByteOrder.nativeOrder());
      mVertexBuffer = vbb.asFloatBuffer();
      mVertexBuffer.put(vertex);
      mVertexBuffer.position(0);

      float[] texcoor = generateTexCoor((int) (360 / angle_span), 1, 1, 1);
      ByteBuffer tbb = ByteBuffer.allocateDirect(texcoor.length * 4);
      tbb.order(ByteOrder.nativeOrder());
      mTexCoorBuffer = tbb.asFloatBuffer();
      mTexCoorBuffer.put(texcoor);
      mTexCoorBuffer.position(0);
    }
  public RockOnCover() {
    //  public RockOnCover(int[] textureId, int[] textureAlphabetId) {
    /** cover coordinates */
    float[] coords = {
      // X, Y, Z
      -1.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f, -1.f, 0.f, -1.f, -1.f, 0.f
    };

    /** Texture coordinates */
    float[] textCoords = {
      0.f, 1.f,
      1.f, 1.f,
      1.f, 0.f,
      0.f, 0.f
    };

    /**
     * Generate our openGL buffers with the vertice and texture coordinates and drawing indexes
     * VERTICAL
     */
    // Buffers to be passed to gl*Pointer() functions
    // must be direct, i.e., they must be placed on the
    // native heap where the garbage collector cannot
    // move them.
    //
    // Buffers with multi-byte datatypes (e.g., short, int, float)
    // must have their byte order set to native order

    ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4); // verts * ncoords * bytes per vert??
    vbb.order(ByteOrder.nativeOrder());
    mFVertexBuffer = vbb.asFloatBuffer();

    ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
    tbb.order(ByteOrder.nativeOrder());
    mTexBuffer = tbb.asFloatBuffer();

    ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
    ibb.order(ByteOrder.nativeOrder());
    mIndexBuffer = ibb.asShortBuffer();

    for (int i = 0; i < VERTS; i++) {
      for (int j = 0; j < 3; j++) {
        mFVertexBuffer.put(coords[i * 3 + j]);
      }
    }

    mTexBuffer.put(textCoords);

    for (int i = 0; i < VERTS; i++) {
      mIndexBuffer.put((short) i);
    }

    mFVertexBuffer.position(0);
    mTexBuffer.position(0);
    mIndexBuffer.position(0);
  }
Exemple #13
0
 @Override
 public FloatBuffer asNioFloat() {
   if (wrappedBuffer == null) {
     if (offset() == 0) {
       return FloatBuffer.wrap(floatData);
     } else return (FloatBuffer) FloatBuffer.wrap(floatData).position(offset());
   }
   if (offset() == 0) {
     return wrappedBuffer.asFloatBuffer();
   } else return (FloatBuffer) wrappedBuffer.asFloatBuffer().position(offset());
 }
  public void initGL(GL10 gl, Context context) {

    Bitmap texture = Util.getTextureFromBitmapResource(context, _textureResource);

    _width = texture.getWidth();
    _height = texture.getHeight();

    // scale the vertex buffer coords
    for (int i = 0; i < _vertices.length; i += 3) {
      _vertices[i] *= _width;
      _vertices[i + 1] *= _height;
    }
    // a float is 4 bytes, therefore we multiply the number if
    // vertices with 4.
    ByteBuffer vbb = ByteBuffer.allocateDirect(_vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    _vertexBuffer = vbb.asFloatBuffer();
    _vertexBuffer.put(_vertices);
    _vertexBuffer.position(0);

    // short is 2 bytes, therefore we multiply the number if
    // vertices with 2.
    ByteBuffer ibb = ByteBuffer.allocateDirect(_indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    _indexBuffer = ibb.asShortBuffer();
    _indexBuffer.put(_indices);
    _indexBuffer.position(0);

    // a float is 4 bytes, therefore we multiply the number if
    // vertices with 4.
    ByteBuffer tbb = ByteBuffer.allocateDirect(_textureCoords.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    _textureCoordsBuffer = tbb.asFloatBuffer();
    _textureCoordsBuffer.put(_textureCoords);
    _textureCoordsBuffer.position(0);

    // create texture
    gl.glEnable(GL10.GL_TEXTURE_2D);
    _texturesBuffer = IntBuffer.allocate(1);
    gl.glGenTextures(1, _texturesBuffer);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, _texturesBuffer.get(0));

    // set the texture
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);

    texture.recycle();
  }
Exemple #15
0
  public Floor() {
    // make a floor
    ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(FLOOR_COORDS.length * 4);
    bbFloorVertices.order(ByteOrder.nativeOrder());
    floorVertices = bbFloorVertices.asFloatBuffer();
    floorVertices.put(FLOOR_COORDS);
    floorVertices.position(0);

    ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(FLOOR_NORMALS.length * 4);
    bbFloorNormals.order(ByteOrder.nativeOrder());
    floorNormals = bbFloorNormals.asFloatBuffer();
    floorNormals.put(FLOOR_NORMALS);
    floorNormals.position(0);

    ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(FLOOR_COLORS.length * 4);
    bbFloorColors.order(ByteOrder.nativeOrder());
    floorColors = bbFloorColors.asFloatBuffer();
    floorColors.put(FLOOR_COLORS);
    floorColors.position(0);

    // now setup the shaders and program object
    int vertexShader = myStereoRenderer.LoadShader(GLES30.GL_VERTEX_SHADER, light_vertex);
    int gridShader = myStereoRenderer.LoadShader(GLES30.GL_FRAGMENT_SHADER, grid_fragment);
    int programObject;
    int[] linked = new int[1];

    // Create the program object
    programObject = GLES30.glCreateProgram();

    if (programObject == 0) {
      Log.e(TAG, "So some kind of error, but what?");
      return;
    }

    GLES30.glAttachShader(programObject, vertexShader);
    GLES30.glAttachShader(programObject, gridShader);

    // Link the program
    GLES30.glLinkProgram(programObject);

    // Check the link status
    GLES30.glGetProgramiv(programObject, GLES30.GL_LINK_STATUS, linked, 0);

    if (linked[0] == 0) {
      Log.e(TAG, "Error linking program:");
      Log.e(TAG, GLES30.glGetProgramInfoLog(programObject));
      GLES30.glDeleteProgram(programObject);
      return;
    }

    // Store the program object
    mProgramObject = programObject;
  }
Exemple #16
0
  @Override
  public FloatBuffer asNioFloat() {
    if (offset() >= Integer.MAX_VALUE)
      throw new IllegalStateException("Index out of bounds " + offset());

    if (wrappedBuffer == null) {
      return pointer.asByteBuffer().asFloatBuffer();
    } else if (offset() == 0) {
      return wrappedBuffer.asFloatBuffer();
    } else {
      return (FloatBuffer) wrappedBuffer.asFloatBuffer().position((int) (offset()));
    }
  }
Exemple #17
0
  public Cube(Bitmap b) {
    // initialize buffer for vertex coordinates
    ByteBuffer vpb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            faceCoords.length * 4);
    vpb.order(ByteOrder.nativeOrder());
    vertexBuffer = vpb.asFloatBuffer();
    vertexBuffer.put(faceCoords);
    vertexBuffer.position(0);

    // initialize buffer for vertex normals
    ByteBuffer vnb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            normals.length * 4);
    vnb.order(ByteOrder.nativeOrder());
    normalBuffer = vnb.asFloatBuffer();
    normalBuffer.put(normals);
    normalBuffer.position(0);

    // initialize buffer for UV Coordinates
    ByteBuffer uvb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            uvs.length * 4);
    uvb.order(ByteOrder.nativeOrder());
    uvBuffer = uvb.asFloatBuffer();
    uvBuffer.put(uvs);
    uvBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    shaderProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(shaderProgram, vertexShader);
    GLES20.glAttachShader(shaderProgram, fragmentShader);
    GLES20.glLinkProgram(shaderProgram);

    loadTexture(b);
  }
  public void render(TnMapSkyDome skyDome, Matrixf matrix) {
    // TnMapCheckGL("TnMapES1SkyDomeRender::Render start");

    if (skyDome != null) {
      if (skyDome.getTexture() != null && skyDome.getTexture().isLoaded()) {
        // TnMapCheckGL("TnMapSkyDome::Draw begin");
        skyDome.getTexture().startUsing();
        gl10.glDisableClientState(TnGL10.GL_COLOR_ARRAY);

        // Draw
        float[] matrixArray = matrix.getArray();
        gl10.glLoadMatrixf(matrixArray, 0);

        if (skyDome.getVertices() != null) {
          dataBuffer =
              TnNioManager.getInstance().allocateDirect(skyDome.getVertices().length * 3 * 4);
          FloatBuffer data = dataBuffer.asFloatBuffer();

          texCoordBuffer =
              TnNioManager.getInstance().allocateDirect(skyDome.getVertices().length * 2 * 4);
          FloatBuffer data2 = texCoordBuffer.asFloatBuffer();
          for (int n = 0; n < skyDome.getVertices().length; n++) {
            TnMapVertex5f vertex = skyDome.getVertices()[n];
            data.put(vertex.x);
            data.put(vertex.y);
            data.put(vertex.z);
            data2.put(vertex.s);
            data2.put(vertex.t);
          }
          skyDome.destroy();
        }

        if (dataBuffer != null && texCoordBuffer != null) {
          gl10.glVertexPointer(3, TnGL10.GL_FLOAT, 0, dataBuffer);
          gl10.glTexCoordPointer(2, TnGL10.GL_FLOAT, 0, texCoordBuffer);

          gl10.glDrawArrays(TnGL10.GL_TRIANGLE_FAN, 0, skyDome.getFanVertexCount());
          gl10.glDrawArrays(
              TnGL10.GL_TRIANGLE_STRIP, skyDome.getFanVertexCount(), skyDome.getStripVertexCount());
        }
        // Shutdown
        gl10.glEnableClientState(TnGL10.GL_COLOR_ARRAY);
        // TnMapCheckGL("TnMapSkyDome::Draw end");

        skyDome.getTexture().stopUsing();
      }
    }

    // TnMapCheckGL("TnMapES1ScaleRender::Render end");
  }
  public Entity() {
    // Allocate 4 bytes per float.
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuffer.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    textureBuffer = byteBuffer.asFloatBuffer();
    textureBuffer.put(texture);
    textureBuffer.position(0);
  }
Exemple #20
0
  public MarkerTile(float mx, float my, float sx, float sy, int texture) {
    midx = mx;
    midy = my;
    sizx = sx;
    sizy = sy;

    midOx = mx;
    midOy = my;
    sizOx = sx;
    sizOy = sy;

    midTx = mx;
    midTy = my;
    sizTx = sx;
    sizTy = sy;

    textureRef = texture;

    int vertexInfoShader =
        XQGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexInfoTileShaderCode);
    int fragmentInfoShader =
        XQGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentInfoTileShaderCode);

    mProgram = GLES20.glCreateProgram();

    // add the vertex shader to program
    GLES20.glAttachShader(mProgram, vertexInfoShader);

    // add the fragment shader to program
    GLES20.glAttachShader(mProgram, fragmentInfoShader);

    // creates OpenGL ES program executables
    GLES20.glLinkProgram(mProgram);

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(tileCoords.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuffer.asFloatBuffer();
    vertexBuffer.put(tileCoords);
    vertexBuffer.position(0);
    byteBuffer = ByteBuffer.allocateDirect(drawOrder.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    indexBuffer = byteBuffer.asShortBuffer();
    indexBuffer.put(drawOrder);
    indexBuffer.position(0);
    byteBuffer = ByteBuffer.allocateDirect(TextureCoords.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    textureBuffer = byteBuffer.asFloatBuffer();
    textureBuffer.put(TextureCoords);
    textureBuffer.position(0);
  }
 public static void SetupBuffers() {
   byte tModel[] = new byte[FaceArray.length];
   for (int i = 0; i < FaceArray.length; i++) {
     tModel[i] = (byte) (FaceArray[i]);
   }
   for (int i = 0; i < TextArray.length; i += 2) {
     TextArray[i + 1] = TextArray[i + 1] * -1;
   }
   byte tModelB[] = new byte[FaceArrayB.length];
   for (int i = 0; i < FaceArrayB.length; i++) {
     tModelB[i] = (byte) (FaceArrayB[i]);
   }
   for (int i = 0; i < TextArrayB.length; i += 2) {
     TextArrayB[i + 1] = TextArrayB[i + 1] * -1;
   }
   ByteBuffer vbb = ByteBuffer.allocateDirect(VertArray.length * 4);
   vbb.order(ByteOrder.nativeOrder());
   mFVertexBuffer = vbb.asFloatBuffer();
   mFVertexBuffer.put(VertArray);
   mFVertexBuffer.position(0);
   ByteBuffer tbb = ByteBuffer.allocateDirect(TextArray.length * 4);
   tbb.order(ByteOrder.nativeOrder());
   mFTextureBuffer = tbb.asFloatBuffer();
   mFTextureBuffer.put(TextArray);
   mFTextureBuffer.position(0);
   mModel = ByteBuffer.allocateDirect(FaceArray.length);
   mModel.put(tModel);
   mModel.position(0);
   Faces = FaceArray.length;
   VertArray = null;
   TextArray = null;
   FaceArray = null;
   ByteBuffer vBbb = ByteBuffer.allocateDirect(VertArrayB.length * 4);
   vBbb.order(ByteOrder.nativeOrder());
   mFVertexBufferB = vBbb.asFloatBuffer();
   mFVertexBufferB.put(VertArrayB);
   mFVertexBufferB.position(0);
   ByteBuffer tBbb = ByteBuffer.allocateDirect(TextArrayB.length * 4);
   tBbb.order(ByteOrder.nativeOrder());
   mFTextureBufferB = tBbb.asFloatBuffer();
   mFTextureBufferB.put(TextArrayB);
   mFTextureBufferB.position(0);
   mModelB = ByteBuffer.allocateDirect(FaceArrayB.length);
   mModelB.put(tModelB);
   mModelB.position(0);
   FacesB = FaceArrayB.length;
   VertArrayB = null;
   TextArrayB = null;
   FaceArrayB = null;
 }
  /** Sets up the drawing object data for use in an OpenGL ES context. */
  public Square(Bitmap leftEyeImage, Bitmap rightEyeImage) {

    mLeftEyeImage = leftEyeImage;
    mRightEyeImage = rightEyeImage;

    GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
    mRightEyeTextureId = loadTexture(mRightEyeImage);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    mLeftEyeTextureTextureId = loadTexture(mLeftEyeImage);

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            squareCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareCoords);
    vertexBuffer.position(0);

    ByteBuffer bbUv =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            squareCoords.length * 4);
    bbUv.order(ByteOrder.nativeOrder());
    uvBuffer = bbUv.asFloatBuffer();
    uvBuffer.put(squareUVCoords);
    uvBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    // prepare shaders and OpenGL program
    int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
    GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram); // create OpenGL program executables
  }
Exemple #23
0
  public static Matrix4f getInverseModelViewMatrix() {
    ByteBuffer buf = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder());

    glGetFloat(GL_MODELVIEW_MATRIX, buf.asFloatBuffer());

    Matrix4f mat = new Matrix4f();

    mat.load(buf.asFloatBuffer());

    mat.m32 = -mat.m32;

    mat.invert();

    return mat;
  }
  public MyGL20ImageRenderer(Context context) {

    mcontext = context;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuffer.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    textureBuffer = byteBuffer.asFloatBuffer();
    textureBuffer.put(texture);
    textureBuffer.position(0);
  }
    public Triangle() {

      // Buffers to be passed to gl*Pointer() functions
      // must be direct, i.e., they must be placed on the
      // native heap where the garbage collector cannot
      // move them.
      //
      // Buffers with multi-byte datatypes (e.g., short, int, float)
      // must have their byte order set to native order

      ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
      vbb.order(ByteOrder.nativeOrder());
      mFVertexBuffer = vbb.asFloatBuffer();

      ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
      tbb.order(ByteOrder.nativeOrder());
      mTexBuffer = tbb.asFloatBuffer();

      ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
      ibb.order(ByteOrder.nativeOrder());
      mIndexBuffer = ibb.asShortBuffer();

      // A unit-sided equilateral triangle centered on the origin.
      float[] coords = {
        // X, Y, Z
        -0.5f, -0.25f, 0, 0.5f, -0.25f, 0, 0.0f, 0.559016994f, 0
      };

      for (int i = 0; i < VERTS; i++) {
        for (int j = 0; j < 3; j++) {
          mFVertexBuffer.put(coords[i * 3 + j] * 2.0f);
        }
      }

      for (int i = 0; i < VERTS; i++) {
        for (int j = 0; j < 2; j++) {
          mTexBuffer.put(coords[i * 3 + j] * 2.0f + 0.5f);
        }
      }

      for (int i = 0; i < VERTS; i++) {
        mIndexBuffer.put((short) i);
      }

      mFVertexBuffer.position(0);
      mTexBuffer.position(0);
      mIndexBuffer.position(0);
    }
    void generate(int w, int h) {
      textureBuffers = new FloatBuffer[cols * rows];

      for (int row = 0; row < rows; row++)
        for (int col = 0; col < cols; col++) {
          final float x1 = (startX + frameWidth * (float) col) / (float) w;
          final float x2 = (startX + frameWidth * (float) (col + 1)) / (float) w;
          final float y1 = (startY + frameHeight * (float) row) / (float) h;
          final float y2 = (startY + frameHeight * (float) (row + 1)) / (float) h;

          final float texture[] = {
            x1, y1,
            x2, y1,
            x1, y2,
            x2, y2,
          };

          final ByteBuffer ibb = ByteBuffer.allocateDirect(texture.length * 4);
          ibb.order(ByteOrder.nativeOrder());
          final FloatBuffer textureBuffer = ibb.asFloatBuffer();
          textureBuffer.put(texture);
          textureBuffer.position(0);
          textureBuffers[row * cols + col] = textureBuffer;
        }
    }
  /*
   * Reads FLOAT image data organized as separate image planes.
   *
   */
  public float[][] readPlanarFloat32(
      int width,
      int height,
      int samplesPerPixel,
      long[] stripOffsets,
      long[] stripCounts,
      long rowsPerStrip)
      throws IOException {
    float[][] data = new float[samplesPerPixel][width * height];
    int band = 0;
    int numRows = 0;

    ByteBuffer buff = ByteBuffer.allocateDirect(width * height * FLOAT_SIZEOF);
    buff.order(this.getByteOrder());

    for (int i = 0; i < stripOffsets.length; i++) {
      this.theChannel.position(stripOffsets[i]);
      int len = (int) stripCounts[i];
      if ((buff.position() + len) >= data[band].length * FLOAT_SIZEOF)
        len = data[band].length * FLOAT_SIZEOF - buff.position();
      buff.limit(buff.position() + len);
      this.theChannel.read(buff);
      numRows += rowsPerStrip;
      if (numRows >= height) {
        buff.flip();
        FloatBuffer fbuff = buff.asFloatBuffer();
        fbuff.get(data[band]);
        buff.clear();
        ++band;
        numRows = 0;
      }
    }

    return data;
  }
Exemple #28
0
 /**
  * creates a floatbuffer of the given size.
  *
  * @param size
  * @return
  */
 public static FloatBuffer makeFloatBuffer(int size) {
   ByteBuffer bb = ByteBuffer.allocateDirect(size * 4);
   bb.order(ByteOrder.nativeOrder());
   FloatBuffer fb = bb.asFloatBuffer();
   fb.position(0);
   return fb;
 }
Exemple #29
0
  private void init() {
    if (tex != null) {
      tex.dispose();
      tex2.dispose();
    }

    ByteBuffer buffer = ByteBuffer.allocateDirect(BYTES_PER_VERTEX * 3);
    buffer.order(ByteOrder.nativeOrder());
    vertices = buffer.asFloatBuffer();

    float[] verts = {
      1, 0, 0, 1, 0, 1, 0, 1, -0.5f, -0.5f, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0.5f, -0.5f, 0, 0, 0, 1, 1,
      0.5f, 0, 0.5f, 0, 0, 0.5f, 0,
    };
    vertices.put(verts);
    vertices.flip();

    Pixmap pixmap = new Pixmap(256, 256, Format.RGBA8888);
    pixmap.setColor(1, 1, 1, 1);
    pixmap.fill();
    pixmap.setColor(0, 0, 0, 1);
    pixmap.drawLine(0, 0, 256, 256);
    pixmap.drawLine(256, 0, 0, 256);
    tex = new Texture(pixmap, false);
    pixmap.dispose();

    pixmap = new Pixmap(256, 256, Format.RGBA8888);
    pixmap.setColor(1, 1, 1, 1);
    pixmap.fill();
    pixmap.setColor(0, 0, 0, 1);
    pixmap.drawLine(128, 0, 128, 256);
    tex2 = new Texture(pixmap, false);
    pixmap.dispose();
  }
  public Vertices(
      GLGraphics glGraphics,
      int maxVertices,
      int maxIndices,
      boolean hasColor,
      boolean hasTexCoords) {

    this.glGraphics = glGraphics;
    this.hasColor = hasColor;
    this.hasTexCoords = hasTexCoords;
    this.vertexSize =
        (2 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0)) * 4; // golemina na bajtite za tockite

    ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
    buffer.order(ByteOrder.nativeOrder());
    vertices = buffer.asFloatBuffer();

    if (maxIndices > 0) {
      buffer = ByteBuffer.allocateDirect(maxIndices * Short.SIZE / 8);
      buffer.order(ByteOrder.nativeOrder());
      indices = buffer.asShortBuffer();
    } else {
      indices = null;
    }
  }