Beispiel #1
0
  /** x, y, z, fov, aspectRatio, zNear, zFar */
  public Camera(long window, Vec3 position, float fov, float aspectRatio, float zNear, float zFar) {

    this.window = window;
    this.position = position;
    this.fov = fov;
    this.zNear = zNear;
    this.zFar = zFar;

    float sine, cotangent, deltaZ;
    float radians = fov / 2 * (float) Math.PI / 180;

    deltaZ = zFar - zNear;
    sine = (float) Math.sin(radians);

    if ((deltaZ == 0) || (sine == 0) || (aspectRatio == 0)) {
      return;
    }

    cotangent = (float) Math.cos(radians) / sine;

    matrix = BufferUtils.createFloatBuffer(16);
    int oldPos = matrix.position();
    matrix.put(IDENTITY_MATRIX);
    matrix.position(oldPos);

    matrix.put(0 * 4 + 0, cotangent / aspectRatio);
    matrix.put(1 * 4 + 1, cotangent);
    matrix.put(2 * 4 + 2, -(zFar + zNear) / deltaZ);
    matrix.put(2 * 4 + 3, -1);
    matrix.put(3 * 4 + 2, -2 * zNear * zFar / deltaZ);
    matrix.put(3 * 4 + 3, 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);
  }
Beispiel #3
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============================
 }
  /**
   * Method for painting any given object. Data must be 7 floats per face – we render one triangle
   * consisting of 3 vertices with each vertice having an rgba color float value.
   *
   * @param data The vertice data containing coordinates and colors to draw.
   */
  private void drawObject(final FloatBuffer data) {

    // Pass in the position information
    data.position(mPositionOffset);
    GLES20.glVertexAttribPointer(
        mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, mStrideBytes, data);

    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Pass in the color information
    data.position(mColorOffset);
    GLES20.glVertexAttribPointer(
        mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, data);

    GLES20.glEnableVertexAttribArray(mColorHandle);

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
    // (which currently contains model * view).
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the
    // MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    // The (1+data.capacity() / 8) tells us how many vertices we need to
    // draw
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, (1 + data.capacity() / 8));
  }
Beispiel #6
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);
  }
  public void onDraw(
      final int textureId, final FloatBuffer cubeBuffer, final FloatBuffer textureBuffer) {
    GLES20.glUseProgram(mGLProgId);
    runPendingOnDrawTasks();
    if (!mIsInitialized) {
      return;
    }

    cubeBuffer.position(0);
    GLES20.glVertexAttribPointer(mGLAttribPosition, 2, GLES20.GL_FLOAT, false, 0, cubeBuffer);
    GLES20.glEnableVertexAttribArray(mGLAttribPosition);
    textureBuffer.position(0);
    GLES20.glVertexAttribPointer(
        mGLAttribTextureCoordinate, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);
    GLES20.glEnableVertexAttribArray(mGLAttribTextureCoordinate);
    if (textureId != OpenGlUtils.NO_TEXTURE) {
      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
      GLES20.glUniform1i(mGLUniformTexture, 0);
    }
    onDrawArraysPre();
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    GLES20.glDisableVertexAttribArray(mGLAttribPosition);
    GLES20.glDisableVertexAttribArray(mGLAttribTextureCoordinate);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
  }
Beispiel #8
0
  public void draw(final int mShader) {

    // Bind Texture VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, getTexVBO());
    // Pass in texture map position
    mTexVertBuffer.position(0);
    final int mTextureCoordinateHandle = GLES20.glGetAttribLocation(mShader, "a_TexCoord");
    GLES20.glVertexAttribPointer(
        mTextureCoordinateHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, 0);
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

    // Bind VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, getPlaneVBO());
    // Pass in the position information
    mPlaneVertBuffer.position(0);
    final int mPositionHandle = GLES20.glGetAttribLocation(mShader, "a_Position");
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, 0);
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    //	Pass in the color information
    final int mColorHandle = GLES20.glGetUniformLocation(mShader, "u_Color");
    GLES20.glUniform4f(mColorHandle, 0, 1, 1, 1);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, indexCount);

    // Unbind buffers
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
  }
Beispiel #9
0
  @Override
  public void render() {
    GL10 gl = Gdx.graphics.getGL10();
    gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    gl.glClearColor(0.7f, 0.7f, 0.7f, 1);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glRotatef(angle, 0, 0, 1);
    angle += angleIncrement;
    gl.glEnable(GL10.GL_TEXTURE_2D);

    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    vertices.position(0);
    gl.glColorPointer(4, GL10.GL_FLOAT, BYTES_PER_VERTEX, vertices);

    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glClientActiveTexture(GL10.GL_TEXTURE0);
    gl.glActiveTexture(GL10.GL_TEXTURE0);
    tex.bind();
    vertices.position(4);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, BYTES_PER_VERTEX, vertices);

    gl.glClientActiveTexture(GL10.GL_TEXTURE1);
    gl.glActiveTexture(GL10.GL_TEXTURE1);
    tex2.bind();
    vertices.position(6);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, BYTES_PER_VERTEX, vertices);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    vertices.position(8);
    gl.glVertexPointer(3, GL10.GL_FLOAT, BYTES_PER_VERTEX, vertices);

    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
  }
  /**
   * Normalizes weights if needed and finds largest amount of weights used for all vertices in the
   * buffer.
   *
   * @param vertCount amount of vertices
   * @param weightsFloatData weights for vertices
   */
  private int endBoneAssigns(int vertCount, FloatBuffer weightsFloatData) {
    int maxWeightsPerVert = 0;
    weightsFloatData.rewind();
    for (int v = 0; v < vertCount; ++v) {
      float w0 = weightsFloatData.get(),
          w1 = weightsFloatData.get(),
          w2 = weightsFloatData.get(),
          w3 = weightsFloatData.get();

      if (w3 != 0) {
        maxWeightsPerVert = Math.max(maxWeightsPerVert, 4);
      } else if (w2 != 0) {
        maxWeightsPerVert = Math.max(maxWeightsPerVert, 3);
      } else if (w1 != 0) {
        maxWeightsPerVert = Math.max(maxWeightsPerVert, 2);
      } else if (w0 != 0) {
        maxWeightsPerVert = Math.max(maxWeightsPerVert, 1);
      }

      float sum = w0 + w1 + w2 + w3;
      if (sum != 1f && sum != 0.0f) {
        weightsFloatData.position(weightsFloatData.position() - 4);
        // compute new vals based on sum
        float sumToB = 1f / sum;
        weightsFloatData.put(w0 * sumToB);
        weightsFloatData.put(w1 * sumToB);
        weightsFloatData.put(w2 * sumToB);
        weightsFloatData.put(w3 * sumToB);
      }
    }
    weightsFloatData.rewind();

    // mesh.setMaxNumWeights(maxWeightsPerVert);
    return maxWeightsPerVert;
  }
Beispiel #11
0
 /**
  * For use with pre-converted data. This is 50x faster than {@link #put(float[])}, and 500x faster
  * than {@link FloatBuffer#put(float[])}, so if you've got float[] data that won't change, {@link
  * #convert(float...)} it to an int[] once and use this method to put it in the buffer
  *
  * @param data floats that have been converted with {@link Float#floatToIntBits(float)}
  */
 public void put(final int[] data) {
   final ByteBuffer byteBuffer = this.mByteBuffer;
   byteBuffer.position(byteBuffer.position() + BYTES_PER_FLOAT * data.length);
   final FloatBuffer floatBuffer = this.mFloatBuffer;
   floatBuffer.position(floatBuffer.position() + data.length);
   this.mIntBuffer.put(data, 0, data.length);
 }
Beispiel #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);
    }
Beispiel #13
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;
  }
 /** Convenience method */
 public void toFloatBuffer(FloatBuffer $floatBuffer) {
   $floatBuffer.position(0);
   $floatBuffer.put(_x);
   $floatBuffer.put(_y);
   $floatBuffer.put(_z);
   $floatBuffer.position(0);
 }
Beispiel #15
0
  private void setTextureRect(
      float x, float y, float w, float h, float sw, float sh, boolean rotated) {
    rect_.set(x, y, w, h);
    rectRotated_ = rotated;

    setContentSize(sw, sh);
    updateTextureCoords(rect_);

    float relativeOffsetX = unflippedOffsetPositionFromCenter_.x;
    float relativeOffsetY = unflippedOffsetPositionFromCenter_.y;

    // issue #732
    if (flipX_) relativeOffsetX = -relativeOffsetX;
    if (flipY_) relativeOffsetY = -relativeOffsetY;

    offsetPosition_.x = relativeOffsetX + (contentSize_.width - rect_.size.width) / 2;
    offsetPosition_.y = relativeOffsetY + (contentSize_.height - rect_.size.height) / 2;

    // rendering using SpriteSheet
    if (usesSpriteSheet_) {
      // update dirty_, don't update recursiveDirty_
      dirty_ = true;
    } else { // self rendering
      // Atlas: Vertex
      float x1 = 0 + offsetPosition_.x;
      float y1 = 0 + offsetPosition_.y;
      float x2 = x1 + w;
      float y2 = y1 + h;

      // Don't update Z.
      vertexes.position(0);
      tmpV[0] = x1;
      tmpV[1] = y2;
      tmpV[2] = 0;
      tmpV[3] = x1;
      tmpV[4] = y1;
      tmpV[5] = 0;
      tmpV[6] = x2;
      tmpV[7] = y2;
      tmpV[8] = 0;
      tmpV[9] = x2;
      tmpV[10] = y1;
      tmpV[11] = 0;
      BufferUtils.copyFloats(tmpV, 0, vertexes, 12);
      //            vertexes.put(x1);
      //            vertexes.put(y2);
      //            vertexes.put(0);
      //            vertexes.put(x1);
      //            vertexes.put(y1);
      //            vertexes.put(0);
      //            vertexes.put(x2);
      //            vertexes.put(y2);
      //            vertexes.put(0);
      //            vertexes.put(x2);
      //            vertexes.put(y1);
      //            vertexes.put(0);
      vertexes.position(0);
    }
  }
Beispiel #16
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);
    }
 @Override
 public void updateVertices(int targetOffset, float[] vertices, int sourceOffset, int count) {
   isDirty = true;
   final int pos = buffer.position();
   buffer.position(targetOffset);
   BufferUtils.copy(vertices, sourceOffset, count, buffer);
   buffer.position(pos);
   bufferChanged();
 }
  /** Draws a cube. */
  private void drawCube() {
    // Pass in the position information
    mCubePositions.position(0);
    GLES20.glVertexAttribPointer(
        mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, mCubePositions);

    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Pass in the color information
    mCubeColors.position(0);
    GLES20.glVertexAttribPointer(
        mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 0, mCubeColors);

    GLES20.glEnableVertexAttribArray(mColorHandle);

    // Pass in the normal information
    mCubeNormals.position(0);
    GLES20.glVertexAttribPointer(
        mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 0, mCubeNormals);

    GLES20.glEnableVertexAttribArray(mNormalHandle);

    // Pass in the texture coordinate information
    mCubeTextureCoordinates.position(0);
    GLES20.glVertexAttribPointer(
        mTextureCoordinateHandle,
        mTextureCoordinateDataSize,
        GLES20.GL_FLOAT,
        false,
        0,
        mCubeTextureCoordinates);

    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
    // (which currently contains model * view).
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

    // Pass in the modelview matrix.
    GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the
    // MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    // Pass in the combined matrix.
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

    // Pass in the light position in eye space.
    GLES20.glUniform3f(
        mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);

    // Draw the cube.
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36);
  }
    public void onDrawFrame(GL10 glUnused) {

      synchronized (this) {
        if (updateSurface) {
          mSurface.updateTexImage();
          mSurface.getTransformMatrix(mSTMatrix);
          updateSurface = false;
        } else {
          return;
        }
      }

      GLES20.glClearColor(255.0f, 255.0f, 255.0f, 1.0f);
      GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

      GLES20.glUseProgram(mProgram);
      checkGlError("glUseProgram");

      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
      GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);

      mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
      GLES20.glVertexAttribPointer(
          maPositionHandle,
          3,
          GLES20.GL_FLOAT,
          false,
          TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
          mTriangleVertices);
      checkGlError("glVertexAttribPointer maPosition");
      GLES20.glEnableVertexAttribArray(maPositionHandle);
      checkGlError("glEnableVertexAttribArray maPositionHandle");

      mTextureVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
      GLES20.glVertexAttribPointer(
          maTextureHandle,
          2,
          GLES20.GL_FLOAT,
          false,
          TEXTURE_VERTICES_DATA_STRIDE_BYTES,
          mTextureVertices);

      checkGlError("glVertexAttribPointer maTextureHandle");
      GLES20.glEnableVertexAttribArray(maTextureHandle);
      checkGlError("glEnableVertexAttribArray maTextureHandle");

      Matrix.setIdentityM(mMVPMatrix, 0);

      GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
      GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);

      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
      checkGlError("glDrawArrays");
      GLES20.glFinish();
    }
Beispiel #20
0
 public void glGetFloatv(int matrixGetName, FloatBuffer params) {
   int pos = params.position();
   if (matrixGetName == GL_MATRIX_MODE) {
     params.put((float) matrixMode);
   } else {
     FloatBuffer matrix = glGetMatrixf(matrixGetName2MatrixModeName(matrixGetName));
     params.put(matrix); // matrix -> params
     matrix.reset();
   }
   params.position(pos);
 }
Beispiel #21
0
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
      m_nViewWidth = width;
      m_nViewHeight = height;

      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);

      // 图形最终显示到屏幕的区域的位置、长和宽
      gl.glViewport(0, 0, width, height);

      // 指定矩阵
      gl.glMatrixMode(GL10.GL_PROJECTION);

      // 将当前的矩阵设置为glMatrixMode指定的矩阵
      gl.glLoadIdentity();

      gl.glOrthof(0, m_nViewWidth, 0, m_nViewHeight, -1, 1);
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

      float coordinates[] = {
        0,
        (float) m_nVideoHeight / (float) m_glTextureSize,
        (float) m_nVideoWidth / (float) m_glTextureSize,
        (float) m_nVideoHeight / (float) m_glTextureSize,
        0,
        0,
        (float) m_nVideoWidth / (float) m_glTextureSize,
        0
      };
      m_coordinatesBuffer.put(coordinates);
      m_coordinatesBuffer.position(0);

      float vertices[] = {
        0,
        0,
        (float) 0.0,
        m_nViewWidth,
        0,
        (float) 0.0,
        0,
        m_nViewHeight,
        (float) 0.0,
        m_nViewWidth,
        m_nViewHeight,
        (float) 0.0
      };
      m_verticesBuffer.put(vertices);
      m_verticesBuffer.position(0);
    }
Beispiel #22
0
 protected float[] getMatrixFloat(FloatBuffer b) {
   if (pmvMatrix.usesBackingArray()) {
     return b.array();
   } else {
     int p = b.position();
     float[] pm = new float[16];
     b.get(pm, p, 16);
     b.position(p);
     return pm;
   }
 }
Beispiel #23
0
 private static void bulkPutBuffer(FloatBuffer b) {
   int n = b.capacity();
   b.clear();
   FloatBuffer c = FloatBuffer.allocate(n + 7);
   c.position(7);
   for (int i = 0; i < n; i++) c.put((float) ic(i));
   c.flip();
   c.position(7);
   b.put(c);
   b.flip();
 }
  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();
  }
Beispiel #25
0
 public Float32Array copy(FloatBuffer buffer) {
   if (GWT.isProdMode()) {
     return ((Float32Array) ((HasArrayBufferView) buffer).getTypedArray())
         .subarray(buffer.position(), buffer.remaining());
   } else {
     ensureCapacity(buffer);
     for (int i = buffer.position(), j = 0; i < buffer.limit(); i++, j++) {
       floatBuffer.set(j, buffer.get(i));
     }
     return floatBuffer.subarray(0, buffer.remaining());
   }
 }
Beispiel #26
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 static void generate(TriMesh mesh) {

    FloatBuffer tangents = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 3);
    FloatBuffer binormals = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 3);

    IntBuffer indexBuffer = mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = mesh.getVertexBuffer();
    FloatBuffer textureBuffer = mesh.getTextureCoords(0).coords;
    indexBuffer.rewind();

    Vector3f tangent = new Vector3f();
    Vector3f binormal = new Vector3f();
    Vector3f normal = new Vector3f();
    Vector3f verts[] = new Vector3f[3];
    Vector2f texcoords[] = new Vector2f[3];

    for (int i = 0; i < 3; i++) {
      verts[i] = new Vector3f();
      texcoords[i] = new Vector2f();
    }

    for (int t = 0; t < indexBuffer.capacity() / 3; t++) {

      int index[] = new int[3];

      for (int v = 0; v < 3; v++) {
        index[v] = indexBuffer.get();
        verts[v].x = vertexBuffer.get(index[v] * 3);
        verts[v].y = vertexBuffer.get(index[v] * 3 + 1);
        verts[v].z = vertexBuffer.get(index[v] * 3 + 2);

        texcoords[v].x = textureBuffer.get(index[v] * 2);
        texcoords[v].y = textureBuffer.get(index[v] * 2 + 1);
      }

      computeTriangleTangentSpace(tangent, binormal, normal, verts, texcoords);

      for (int v = 0; v < 3; v++) {
        tangents.position(index[v] * 3);
        tangents.put(tangent.x);
        tangents.put(tangent.y);
        tangents.put(tangent.z);

        binormals.position(index[v] * 3);
        binormals.put(binormal.x);
        binormals.put(binormal.y);
        binormals.put(binormal.z);
      }
    }

    mesh.setTangentBuffer(tangents);
    mesh.setBinormalBuffer(binormals);
  }
Beispiel #28
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);
  }
  private void doTransforms(
      FloatBuffer bindBufPos,
      FloatBuffer bindBufNorm,
      FloatBuffer bufPos,
      FloatBuffer bufNorm,
      int start,
      int end,
      Matrix4f transform) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;
    Vector3f norm = vars.vect2;

    int length = (end - start) * 3;

    // offset is given in element units
    // convert to be in component units
    int offset = start * 3;
    bindBufPos.rewind();
    bindBufNorm.rewind();
    // bufPos.position(offset);
    // bufNorm.position(offset);
    bindBufPos.get(tmpFloat, 0, length);
    bindBufNorm.get(tmpFloatN, 0, length);
    int index = 0;
    while (index < length) {
      pos.x = tmpFloat[index];
      norm.x = tmpFloatN[index++];
      pos.y = tmpFloat[index];
      norm.y = tmpFloatN[index++];
      pos.z = tmpFloat[index];
      norm.z = tmpFloatN[index];

      transform.mult(pos, pos);
      transform.multNormal(norm, norm);

      index -= 2;
      tmpFloat[index] = pos.x;
      tmpFloatN[index++] = norm.x;
      tmpFloat[index] = pos.y;
      tmpFloatN[index++] = norm.y;
      tmpFloat[index] = pos.z;
      tmpFloatN[index++] = norm.z;
    }
    vars.release();
    bufPos.position(offset);
    // using bulk put as it's faster
    bufPos.put(tmpFloat, 0, length);
    bufNorm.position(offset);
    // using bulk put as it's faster
    bufNorm.put(tmpFloatN, 0, length);
  }
Beispiel #30
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);
  }