Example #1
0
  /** Constructor this should be called in GL context */
  public GLDrawer2D() {
    pVertex =
        ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer();
    pVertex.put(VERTICES);
    pVertex.flip();
    pTexCoord =
        ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer();
    pTexCoord.put(TEXCOORD);
    pTexCoord.flip();

    hProgram = loadShader(vss, fss);
    GLES20.glUseProgram(hProgram);
    maPositionLoc = GLES20.glGetAttribLocation(hProgram, "aPosition");
    maTextureCoordLoc = GLES20.glGetAttribLocation(hProgram, "aTextureCoord");
    muMVPMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uMVPMatrix");
    muTexMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uTexMatrix");

    Matrix.setIdentityM(mMvpMatrix, 0);
    GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mMvpMatrix, 0);
    GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, mMvpMatrix, 0);
    GLES20.glVertexAttribPointer(maPositionLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pVertex);
    GLES20.glVertexAttribPointer(
        maTextureCoordLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pTexCoord);
    GLES20.glEnableVertexAttribArray(maPositionLoc);
    GLES20.glEnableVertexAttribArray(maTextureCoordLoc);
  }
Example #2
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);
    }
Example #3
0
  private void init(BobView view, int layers) {
    this.view = view;

    obs = new ArrayList<GameObject>(OBJECTS);

    instances = 0;

    // Set up vertex buffer
    ByteBuffer vertexByteBuffer =
        ByteBuffer.allocateDirect(
            VERTEX_BYTES); // a float has 4 bytes so we allocate for each coordinate 4 bytes
    vertexByteBuffer.order(ByteOrder.nativeOrder());
    vertexBuffer =
        vertexByteBuffer
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer(); // allocates the memory from the bytebuffer
    vertexBuffer.position(0); // puts the curser position at the beginning of the buffer

    // Set up texture buffer
    vertexByteBuffer = ByteBuffer.allocateDirect(TEX_BYTES);
    vertexByteBuffer.order(ByteOrder.nativeOrder());
    textureBuffer = vertexByteBuffer.asFloatBuffer();
    textureBuffer.position(0);

    // Set up index buffer
    vertexByteBuffer = ByteBuffer.allocateDirect(INDEX_BYTES);
    vertexByteBuffer.order(ByteOrder.nativeOrder());
    indexBuffer = new ShortBuffer[layers];
    for (int i = 0; i < layers; i++) {
      indexBuffer[i] = vertexByteBuffer.asShortBuffer();
      indexBuffer[i].position(0);
    }

    this.layers = layers;
    lastIndex = new int[layers];

    red = new float[layers];
    green = new float[layers];
    blue = new float[layers];
    alpha = new float[layers];

    for (int i = 0; i < layers; i++) {
      red[i] = green[i] = blue[i] = alpha[i] = 1f;
    }

    for (int i = 0; i < buttonNewpress.length; i++) {
      buttonNewpress[i] = -1;
    }

    for (int i = 0; i < buttonReleased.length; i++) {
      buttonReleased[i] = -1;
    }

    // Camera initialization
    camX = 0;
    camY = 0;
    camZoom = 1;
    cAnchorX = 0;
    cAnchorY = 0;
  }
  public GLObject(GL10 gl, Bitmap bitmap) {
    this.gl = gl;
    this.bitmap = bitmap;

    width = bitmap.getWidth();
    height = bitmap.getHeight();
    float[] position =
        new float[] {
          -width / 2, -height / 2, // LB
          width / 2, -height / 2, // RB
          width / 2, height / 2, // RT
          -width / 2, height / 2, // LT
        };
    short[] textureVer =
        new short[] {
          0, 1,
          1, 1,
          1, 0,
          0, 0,
        };
    vertex =
        ByteBuffer.allocateDirect(position.length * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
            .put(position)
            .flip();
    texture =
        ByteBuffer.allocateDirect(textureVer.length * 2)
            .order(ByteOrder.nativeOrder())
            .asShortBuffer()
            .put(textureVer)
            .flip();
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, this.bitmap, 0); // 3--Öƶ¨ÎÆÀí
  }
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    mCubeVetexBuffer = util.createFloatBuffer(box);
    mCubeTexBuffer = util.createFloatBuffer(texCoords);

    mAmbientBuffer = util.createFloatBuffer(LightAmbient);
    mDiffuseBuffer = util.createFloatBuffer(LightDiffuse);
    mPositionBuffer = util.createFloatBuffer(LightPosition);

    // font
    mVetexBuffer = ByteBuffer.allocateDirect(12 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    mTexBuffer = ByteBuffer.allocateDirect(8 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();

    loadBitmapTex(gl);

    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, mAmbientBuffer);
    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, mDiffuseBuffer);
    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, mPositionBuffer);
    gl.glEnable(GL10.GL_LIGHT1);

    gl.glEnable(GL10.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
    gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
    gl.glClearDepthf(1.0f); // Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
    gl.glDepthFunc(GL10.GL_LEQUAL); // The Type Of Depth Testing To Do
    gl.glHint(
        GL10.GL_PERSPECTIVE_CORRECTION_HINT,
        GL10.GL_NICEST); // Really Nice Perspective Calculations
  }
Example #6
0
  /**
   * 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);
  }
Example #7
0
  /**
   * Creates a new composite buffer which wraps the slices of the specified NIO buffers without
   * copying them. A modification on the content of the specified buffers will be visible to the
   * returned buffer.
   *
   * @throws IllegalArgumentException if the specified buffers' endianness are different from each
   *     other
   */
  public static ChannelBuffer wrappedBuffer(ByteBuffer... buffers) {
    switch (buffers.length) {
      case 0:
        break;
      case 1:
        if (buffers[0].hasRemaining()) {
          return wrappedBuffer(buffers[0]);
        }
        break;
      default:
        ByteOrder order = null;
        final List<ChannelBuffer> components = new ArrayList<ChannelBuffer>(buffers.length);
        for (ByteBuffer b : buffers) {
          if (b == null) {
            break;
          }
          if (b.hasRemaining()) {
            if (order != null) {
              if (!order.equals(b.order())) {
                throw new IllegalArgumentException("inconsistent byte order");
              }
            } else {
              order = b.order();
            }
            components.add(wrappedBuffer(b));
          }
        }
        return compositeBuffer(order, components);
    }

    return EMPTY_BUFFER;
  }
  public Square() {
    // 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);

    // 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
  }
 // 初始化顶点坐标与着色数据的方法
 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============================
 }
Example #10
0
  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;
    }
  }
Example #11
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);
  }
Example #12
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);
    }
Example #13
0
  @Override
  protected void initModel() {
    final short[] _indicesArray = {0, 1, 2, 0, 2, 3};

    // float has 4 bytes
    ByteBuffer vbb = ByteBuffer.allocateDirect(_indicesArray.length * 3 * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();

    // short has 2 bytes
    ByteBuffer ibb = ByteBuffer.allocateDirect(_indicesArray.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();

    final float[] coords = {
      0.0f, -0.1f, 0.0f, // 0
      0.1f, 0.0f, 0.0f, // 1
      0.0f, 0.6f, 0.0f, // 2
      -0.1f, 0.0f, 0.0f, // 3
    };

    vertexBuffer.put(coords);
    indexBuffer.put(_indicesArray);

    vertexBuffer.position(0);
    indexBuffer.position(0);
  }
Example #14
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 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);
  }
Example #16
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);
  }
Example #17
0
  protected void setBinaries(Map<CLDevice, byte[]> binaries) {
    if (this.devices == null) {
      this.devices = new CLDevice[binaries.size()];
      int iDevice = 0;
      for (CLDevice device : binaries.keySet()) this.devices[iDevice++] = device;
    }
    int nDevices = this.devices.length;
    NativeSize[] lengths = new NativeSize[nDevices];
    cl_device_id[] deviceIds = new cl_device_id[nDevices];
    Memory binariesArray = new Memory(Pointer.SIZE * nDevices);
    Memory[] binariesMems = new Memory[nDevices];

    for (int iDevice = 0; iDevice < nDevices; iDevice++) {
      CLDevice device = devices[iDevice];
      byte[] binary = binaries.get(device);

      Memory binaryMem = binariesMems[iDevice] = new Memory(binary.length);
      binaryMem.write(0, binary, 0, binary.length);
      binariesArray.setPointer(iDevice * Pointer.SIZE, binaryMem);

      lengths[iDevice] = toNS(binary.length);
      deviceIds[iDevice] = device.getEntity();
    }
    PointerByReference binariesPtr = new PointerByReference();
    binariesPtr.setPointer(binariesArray);

    IntBuffer errBuff = NIOUtils.directInts(1, ByteOrder.nativeOrder());
    int previousAttempts = 0;
    IntBuffer statuses = NIOUtils.directInts(nDevices, ByteOrder.nativeOrder());
    do {
      entity =
          CL.clCreateProgramWithBinary(
              context.getEntity(), nDevices, deviceIds, lengths, binariesPtr, statuses, errBuff);
    } while (failedForLackOfMemory(errBuff.get(0), previousAttempts++));
  }
    public NgnProxyVideoConsumerGLPreview(
        Context context, ByteBuffer buffer, int bufferWidth, int bufferHeight, int fps) {
      super(context);
      setEGLContextClientVersion(2);
      setEGLConfigChooser(8, 8, 8, 8, 16, 0);
      setRenderer(this);
      getHolder().setFormat(PixelFormat.TRANSLUCENT);
      getHolder().setType(SurfaceHolder.SURFACE_TYPE_GPU);
      setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

      setBuffer(buffer, bufferWidth, bufferHeight);
      mContext = context;

      mTriangleVertices =
          ByteBuffer.allocateDirect(TRIANFLE_VERTICES_DATA.length * FLOAT_SIZE_BYTES)
              .order(ByteOrder.nativeOrder())
              .asFloatBuffer();
      mTriangleVertices.put(TRIANFLE_VERTICES_DATA).position(0);

      mIndices =
          ByteBuffer.allocateDirect(INDICES_DATA.length * SHORT_SIZE_BYTES)
              .order(ByteOrder.nativeOrder())
              .asShortBuffer();
      mIndices.put(INDICES_DATA).position(0);
    }
Example #19
0
    public void memoryCopyParition() throws IOException {
      if (DEBUG_MODE) {
        mBuf.flip();
        CommonUtils.printByteBuffer(LOG, mBuf);
      }
      mBuf.flip();
      long sum = 0;
      String str = "th " + mMsg + " @ Worker ";

      if (mOneToMany) {
        ByteBuffer dst = null;
        RandomAccessFile file = null;
        if (mMemoryOnly) {
          dst = ByteBuffer.allocateDirect(FILE_BYTES);
        }
        for (int times = mLeft; times < mRight; times++) {
          long startTimeMs = System.currentTimeMillis();
          if (!mMemoryOnly) {
            file = new RandomAccessFile(FOLDER + (mWorkerId + BASE_FILE_NUMBER), "rw");
            dst = file.getChannel().map(MapMode.READ_WRITE, 0, FILE_BYTES);
          }
          dst.order(ByteOrder.nativeOrder());
          for (int k = 0; k < BLOCKS_PER_FILE; k++) {
            mBuf.array()[0] = (byte) (k + mWorkerId);
            dst.put(mBuf.array());
          }
          dst.clear();
          sum += dst.get(times);
          dst.clear();
          if (!mMemoryOnly) {
            file.close();
          }
          logPerIteration(startTimeMs, times, str, mWorkerId);
        }
      } else {
        ByteBuffer dst = null;
        RandomAccessFile file = null;
        if (mMemoryOnly) {
          dst = ByteBuffer.allocateDirect(FILE_BYTES);
        }
        for (int times = mLeft; times < mRight; times++) {
          long startTimeMs = System.currentTimeMillis();
          if (!mMemoryOnly) {
            file = new RandomAccessFile(FOLDER + (mWorkerId + BASE_FILE_NUMBER), "rw");
            dst = file.getChannel().map(MapMode.READ_WRITE, 0, FILE_BYTES);
          }
          dst.order(ByteOrder.nativeOrder());
          for (int k = 0; k < BLOCKS_PER_FILE; k++) {
            dst.get(mBuf.array());
          }
          sum += mBuf.get(times % 16);
          dst.clear();
          if (!mMemoryOnly) {
            file.close();
          }
          logPerIteration(startTimeMs, times, str, mWorkerId);
        }
      }
      Results[mWorkerId] = sum;
    }
Example #20
0
  protected void read(DataInputStream s) {
    try {
      ref = new WeakReference<DataBuffer>(this, Nd4j.bufferRefQueue());
      referencing = Collections.synchronizedSet(new HashSet<String>());
      dirty = new AtomicBoolean(false);
      allocationMode = AllocationMode.valueOf(s.readUTF());
      length = s.readInt();
      Type t = Type.valueOf(s.readUTF());
      if (t == Type.DOUBLE) {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // double -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // double -> double
            doubleData = new double[length()];
          } else {
            // double -> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        }
      } else {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // float -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // float -> double
            doubleData = new double[length()];
          } else {
            // float-> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Example #21
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);
  }
Example #23
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;
  }
  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();
  }
Example #25
0
  public String glGetShaderInfoLog(int shader) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 10);
    buffer.order(ByteOrder.nativeOrder());
    ByteBuffer tmp = ByteBuffer.allocateDirect(4);
    tmp.order(ByteOrder.nativeOrder());
    IntBuffer intBuffer = tmp.asIntBuffer();

    GL20.glGetShaderInfoLog(shader, intBuffer, buffer);
    int numBytes = intBuffer.get(0);
    byte[] bytes = new byte[numBytes];
    buffer.get(bytes);
    return new String(bytes);
  }
Example #26
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);
  }
  @Setup
  public void init() {
    this.byteBuffer = ByteBuffer.allocateDirect(expectedEncoderSize);
    this.byteBuffer.order(ByteOrder.nativeOrder());
    this.addJournalRecordEncoder = new AddJournalRecordEncoder();

    this.record = new JournalAddRecord(true, 1, (byte) 1, ZeroEncodingSupport.Instance);
    this.record.setFileID(1);
    this.record.setCompactCount((short) 1);
    this.outBuffer =
        new ChannelBufferWrapper(
            Unpooled.directBuffer(this.record.getEncodeSize(), this.record.getEncodeSize())
                .order(ByteOrder.nativeOrder()));
  }
Example #28
0
  public Cube() {
    int one = 0x10000;
    int vertices[] = {
      -one, -one, -one, one, -one, -one, one, one, -one, -one, one, -one, -one, -one, one, one,
      -one, one, one, one, one, -one, one, one,
    };

    float[] colors = {
      0f, 0f, 0f, 0.0f,
      1f, 0f, 0f, 0.0f,
      1f, 1f, 0f, 0.0f,
      0f, 1f, 0f, 0.0f,
      0f, 0f, 1f, 0.0f,
      1f, 0f, 1f, 0.0f,
      1f, 1f, 1f, 0.0f,
      0f, 1f, 1f, 0.0f,
    };

    //	0f,    0f,  0f,  0.5f,
    //	1f,    0f,  0f,  0.1f,
    //	1f,	   1f,  0f,  0.5f,
    //	0f,    1f,  0f,  0.1f,
    //	0f,    0f,  1f,  0.1f,
    //	1f,    0f,  1f,  0.2f,
    //	1f,    1f,  1f,  0.1f,
    //	0f,    1f,  1f,  0.1f,        };

    byte indices[] = {
      0, 4, 5, 0, 5, 1,
      1, 5, 6, 1, 6, 2,
      2, 6, 7, 2, 7, 3,
      3, 7, 4, 3, 4, 0,
      4, 7, 6, 4, 6, 5,
      3, 0, 1, 3, 1, 2
    };

    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    mVertexBuffer = vbb.asIntBuffer();
    mVertexBuffer.put(vertices);
    mVertexBuffer.position(0);
    ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
    cbb.order(ByteOrder.nativeOrder());
    mColorBuffer = cbb.asFloatBuffer();
    mColorBuffer.put(colors);
    mColorBuffer.position(0);
    mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
    mIndexBuffer.put(indices);
    mIndexBuffer.position(0);
  }
Example #29
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;
 }