예제 #1
0
파일: Grid.java 프로젝트: peterdocter/zed
  private void setVertex(int i, int j, float x, float y, float z, float u, float v) {
    if (i < 0 || i >= mVertsAcross) {
      throw new IllegalArgumentException("i");
    }
    if (j < 0 || j >= mVertsDown) {
      throw new IllegalArgumentException("j");
    }

    final int index = mVertsAcross * j + i;

    final int posIndex = index * 3;
    final int texIndex = index * 2;

    if (mCoordinateType == GL10.GL_FLOAT) {
      mFloatVertexBuffer.put(posIndex, x);
      mFloatVertexBuffer.put(posIndex + 1, y);
      mFloatVertexBuffer.put(posIndex + 2, z);

      mFloatTexCoordBuffer.put(texIndex, u);
      mFloatTexCoordBuffer.put(texIndex + 1, v);
    } else {
      mFixedVertexBuffer.put(posIndex, (int) (x * (1 << 16)));
      mFixedVertexBuffer.put(posIndex + 1, (int) (y * (1 << 16)));
      mFixedVertexBuffer.put(posIndex + 2, (int) (z * (1 << 16)));

      mFixedTexCoordBuffer.put(texIndex, (int) (u * (1 << 16)));
      mFixedTexCoordBuffer.put(texIndex + 1, (int) (v * (1 << 16)));
    }
  }
예제 #2
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);
  }
 // 初始化顶点坐标与着色数据的方法
 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============================
 }
예제 #4
0
 public static void main(String[] args) {
   ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'});
   bb.rewind();
   printnb("Byte Buffer ");
   while (bb.hasRemaining()) printnb(bb.position() + " -> " + bb.get() + ", ");
   print();
   CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
   printnb("Char Buffer ");
   while (cb.hasRemaining()) printnb(cb.position() + " -> " + cb.get() + ", ");
   print();
   FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
   printnb("Float Buffer ");
   while (fb.hasRemaining()) printnb(fb.position() + " -> " + fb.get() + ", ");
   print();
   IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
   printnb("Int Buffer ");
   while (ib.hasRemaining()) printnb(ib.position() + " -> " + ib.get() + ", ");
   print();
   LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
   printnb("Long Buffer ");
   while (lb.hasRemaining()) printnb(lb.position() + " -> " + lb.get() + ", ");
   print();
   ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
   printnb("Short Buffer ");
   while (sb.hasRemaining()) printnb(sb.position() + " -> " + sb.get() + ", ");
   print();
   DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
   printnb("Double Buffer ");
   while (db.hasRemaining()) printnb(db.position() + " -> " + db.get() + ", ");
 }
예제 #5
0
  private static void convertTexCoords2D(FloatBuffer input, Buffer output) {
    if (output.capacity() < input.capacity())
      throw new RuntimeException("Output must be at least as large as input!");

    input.clear();
    output.clear();
    Vector2f temp = new Vector2f();
    int vertexCount = input.capacity() / 2;

    ShortBuffer sb = null;
    IntBuffer ib = null;

    if (output instanceof ShortBuffer) sb = (ShortBuffer) output;
    else if (output instanceof IntBuffer) ib = (IntBuffer) output;
    else throw new UnsupportedOperationException();

    for (int i = 0; i < vertexCount; i++) {
      BufferUtils.populateFromBuffer(temp, input, i);

      if (sb != null) {
        sb.put((short) (temp.getX() * Short.MAX_VALUE));
        sb.put((short) (temp.getY() * Short.MAX_VALUE));
      } else {
        int v1 = (int) (temp.getX() * ((float) (1 << 16)));
        int v2 = (int) (temp.getY() * ((float) (1 << 16)));
        ib.put(v1).put(v2);
      }
    }
  }
예제 #6
0
  private void initialize() {
    GL11 gl = mGL;

    // First create an nio buffer, then create a VBO from it.
    int size = BOX_COORDINATES.length * Float.SIZE / Byte.SIZE;
    FloatBuffer xyBuffer = allocateDirectNativeOrderBuffer(size).asFloatBuffer();
    xyBuffer.put(BOX_COORDINATES, 0, BOX_COORDINATES.length).position(0);

    int[] name = new int[1];
    GLId.glGenBuffers(1, name, 0);
    mBoxCoords = name[0];

    gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBoxCoords);
    gl.glBufferData(
        GL11.GL_ARRAY_BUFFER,
        xyBuffer.capacity() * (Float.SIZE / Byte.SIZE),
        xyBuffer,
        GL11.GL_STATIC_DRAW);

    gl.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);
    gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);

    // Enable the texture coordinate array for Texture 1
    gl.glClientActiveTexture(GL11.GL_TEXTURE1);
    gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);
    gl.glClientActiveTexture(GL11.GL_TEXTURE0);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // mMatrixValues and mAlpha will be initialized in setSize()
  }
    public Sky(boolean dayTime) {
      super(0, 0, 1, 1);

      texture = new Gradient(dayTime ? day : night);

      float[] vertices = new float[16];
      verticesBuffer = Quad.create();

      vertices[2] = 0.25f;
      vertices[6] = 0.25f;
      vertices[10] = 0.75f;
      vertices[14] = 0.75f;

      vertices[3] = 0;
      vertices[7] = 1;
      vertices[11] = 1;
      vertices[15] = 0;

      vertices[0] = 0;
      vertices[1] = 0;

      vertices[4] = 1;
      vertices[5] = 0;

      vertices[8] = 1;
      vertices[9] = 1;

      vertices[12] = 0;
      vertices[13] = 1;

      verticesBuffer.position(0);
      verticesBuffer.put(vertices);
    }
예제 #8
0
파일: Scene.java 프로젝트: Karm/galgs
  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);
    }
  }
 /** Convenience method */
 public void toFloatBuffer(FloatBuffer $floatBuffer) {
   $floatBuffer.position(0);
   $floatBuffer.put(_x);
   $floatBuffer.put(_y);
   $floatBuffer.put(_z);
   $floatBuffer.position(0);
 }
예제 #10
0
    public circle(float r) {
      transMatrix = new float[16];
      int sides = 24;
      vertexCoords = new float[sides * 3];
      double angle = 2 * Math.PI / sides;

      for (int i = 0; i < sides; i++) {
        vertexCoords[3 * i] = (float) (Math.cos(i * angle) * r);
        vertexCoords[3 * i + 1] = (float) (Math.sin(i * angle) * r);
        vertexCoords[3 * i + 2] = 0f;
      }
      vertexCount = vertexCoords.length / COORDS_PER_VERTEX;
      vertexStride = COORDS_PER_VERTEX * 4;
      ByteBuffer bb = ByteBuffer.allocateDirect(vertexCoords.length * 4);
      bb.order(ByteOrder.nativeOrder());
      vertexBuffer = bb.asFloatBuffer();
      vertexBuffer.put(vertexCoords);
      vertexBuffer.position(0);
      int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
      int fragmentShader = 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
    }
예제 #11
0
  private void generateData() {

    slicesBuffers = new FloatBuffer[slices];
    if (normals) {
      normalsBuffers = new FloatBuffer[slices];
    }
    if (texCoords) {
      texCoordsBuffers = new FloatBuffer[slices];
    }

    for (int i = 0; i < slices; i++) {

      float[] vertexCoords = new float[3 * 2 * (stacks + 1)];
      float[] normalCoords = new float[3 * 2 * (stacks + 1)];
      float[] textureCoords = new float[2 * 2 * (stacks + 1)];

      double alpha0 = (i + 0) * (2 * Math.PI) / slices;
      double alpha1 = (i + 1) * (2 * Math.PI) / slices;

      float cosAlpha0 = (float) Math.cos(alpha0);
      float sinAlpha0 = (float) Math.sin(alpha0);
      float cosAlpha1 = (float) Math.cos(alpha1);
      float sinAlpha1 = (float) Math.sin(alpha1);

      for (int j = 0; j <= stacks; j++) {

        float z = height * (0.5f - ((float) j) / stacks);
        float r = top + (base - top) * j / stacks;

        Utils.setXYZ(vertexCoords, 3 * 2 * j, r * cosAlpha1, r * sinAlpha1, z);

        Utils.setXYZ(vertexCoords, 3 * 2 * j + 3, r * cosAlpha0, r * sinAlpha0, z);

        if (normals) {
          Utils.setXYZn(
              normalCoords, 3 * 2 * j, height * cosAlpha1, height * sinAlpha1, base - top);
          Utils.setXYZn(
              normalCoords, 3 * 2 * j + 3, height * cosAlpha0, height * sinAlpha0, base - top);
        }

        if (texCoords) {
          textureCoords[2 * 2 * j + 0] = ((float) (i + 1)) / slices;
          textureCoords[2 * 2 * j + 1] = ((float) (j + 0)) / stacks;

          textureCoords[2 * 2 * j + 2] = ((float) (i + 0)) / slices;
          textureCoords[2 * 2 * j + 3] = ((float) (j + 0)) / stacks;
        }
      }

      slicesBuffers[i] = FloatBuffer.wrap(vertexCoords);

      if (normals) {
        normalsBuffers[i] = FloatBuffer.wrap(normalCoords);
      }

      if (texCoords) {
        texCoordsBuffers[i] = FloatBuffer.wrap(textureCoords);
      }
    }
  }
예제 #12
0
    public axizLines(float size) {
      triangleCoords =
          new float[] {
            0f, 0f, 0f, size, 0f, 0f, 0f, 0f, 0f, 0f, 0, size, 0f, 0f, 0f, 0f, size, 0f,
            //				0f,0f,0f,
            //				size,size,size
          };
      vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
      vertexStride = COORDS_PER_VERTEX * 4;
      // initialize vertex byte buffer for shape coordinates
      ByteBuffer bb =
          ByteBuffer.allocateDirect(
              // (number of coordinate values * 4 bytes per float)
              triangleCoords.length * 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(triangleCoords);
      // set the buffer to read the first coordinate
      vertexBuffer.position(0);

      // prepare shaders and OpenGL program
      int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
      int fragmentShader = 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
    }
  private void prepareBuffers(GLCanvas canvas) {
    mBufferNames = new int[3];
    GL11 gl = canvas.getGLInstance();
    GLId.glGenBuffers(3, mBufferNames, 0);

    gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBufferNames[0]);
    gl.glBufferData(
        GL11.GL_ARRAY_BUFFER,
        mXyBuffer.capacity() * (Float.SIZE / Byte.SIZE),
        mXyBuffer,
        GL11.GL_STATIC_DRAW);

    gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBufferNames[1]);
    gl.glBufferData(
        GL11.GL_ARRAY_BUFFER,
        mUvBuffer.capacity() * (Float.SIZE / Byte.SIZE),
        mUvBuffer,
        GL11.GL_STATIC_DRAW);

    gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mBufferNames[2]);
    gl.glBufferData(
        GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer.capacity(), mIndexBuffer, GL11.GL_STATIC_DRAW);

    // These buffers are never used again.
    mXyBuffer = null;
    mUvBuffer = null;
    mIndexBuffer = null;
  }
예제 #14
0
  private int createTubeVbo(int tube, Color color) {

    float[] c =
        new float[] {color.getBlue() / 255f, color.getGreen() / 255f, color.getRed() / 255f};

    totalNumVerts[tube] = tubes[tube].indeces.length;

    IntBuffer buf = BufferUtils.createIntBuffer(1);
    GL15.glGenBuffers(buf);
    int vbo = buf.get();

    FloatBuffer data = BufferUtils.createFloatBuffer(tubes[tube].indeces.length * 9);

    for (int i = 0; i < tubes[tube].indeces.length; i++) {
      data.put(c);

      Vector3D vertex = tubes[tube].vertices[tubes[tube].indeces[i]];
      Vector3D normal = tubes[tube].normals[tubes[tube].indeces[i]];

      float[] vertexf = new float[] {vertex.x, vertex.y, vertex.z};
      float[] normalf = new float[] {normal.x, normal.y, normal.z};

      data.put(vertexf);
      data.put(normalf);
    }
    data.rewind();

    int bytesPerFloat = Float.SIZE / Byte.SIZE;
    int numBytes = data.capacity() * bytesPerFloat;
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, data, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    return vbo;
  }
예제 #15
0
 public void setUniformMatrix4(final String name, float[] v) {
   FloatBuffer fb =
       ByteBuffer.allocateDirect(v.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
   fb.put(v);
   fb.position(0);
   setUniformMatrix4(name, fb);
 }
  public void draw() {

    g.clearColor(0);
    g.clear();
    _myShaderTexture.beginDraw();

    g.clearColor(255, 0, 0);
    g.clear();
    g.color(255);
    CCMath.randomSeed(0);
    for (int i = 0; i < 200; i++) {
      g.color(CCMath.random(), CCMath.random(), CCMath.random());
      g.ellipse(CCMath.random(400), CCMath.random(400), 0, 20, 20);
    }
    g.rect(-200, -200, 50, 50);
    _myShaderTexture.endDraw();

    FloatBuffer outputData0 = _myShaderTexture.getData(0);
    System.err.printf("toutput0\toutput1\toutput2\toutput3\n");
    for (int i = 0; i < _myShaderTexture.width() * _myShaderTexture.height() * 3; i++)
      System.err.printf(
          "t%.2f\t%.2f\t%.2f\n", outputData0.get(), outputData0.get(), outputData0.get());

    g.color(255);
    g.image(_myShaderTexture.attachment(0), 0, 0, 200, 200);
    //		g.texture(_myRenderBuffer);
    //		g.beginShape(CCDrawMode.QUADS);
    //		g.vertex(-200, -200, 0, 0f);
    //		g.vertex( 200, -200, 1, 0f);
    //		g.vertex( 200,  200, 1, 1);
    //		g.vertex(-200,  200, 0, 1);
    //		g.endShape();
    //		g.noTexture();

  }
  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);
  }
예제 #18
0
  public void init(float vertices[], float normals[], float texcoords[], char indices[]) {
    int nbVertex = vertices.length / 3;
    mVertices = FloatBuffer.allocate(nbVertex * D3DMesh.nbFloatPerVertex);

    for (int i = 0; i < nbVertex; i++) {
      float px = vertices[i * 3];
      float py = vertices[i * 3 + 1];
      float pz = vertices[i * 3 + 2];
      float nx = 0f;
      float ny = 0f;
      float nz = 0f;
      if (normals != null) {
        nx = normals[i * 3];
        ny = normals[i * 3 + 1];
        nz = normals[i * 3 + 2];
      }
      float tx = 0f;
      float ty = 0f;
      if (texcoords != null) {
        tx = texcoords[i * 2];
        ty = texcoords[i * 2 + 1];
      }

      this.addPoint(px, py, pz, nx, ny, nz, tx, ty);
    }

    mVertices.position(0);

    mIndices = CharBuffer.allocate(indices.length);
    mIndices.put(indices);
    mIndices.position(0);
  }
예제 #19
0
파일: Map.java 프로젝트: bls220/Tribal_Wars
  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);
  }
예제 #20
0
  @Override
  public void init(GLAutoDrawable glad) {
    GL3 gl3 = glad.getGL().getGL3();

    try {
      shaders.add(
          GLSLHelpers.createShader(gl3, getClass(), GL3.GL_VERTEX_SHADER, "glsl/simple_vs"));
      shaders.add(
          GLSLHelpers.createShader(gl3, getClass(), GL3.GL_FRAGMENT_SHADER, "glsl/simple_fs"));
      program = GLSLHelpers.createProgram(gl3, shaders);

      gl3.glGenVertexArrays(1, VAO, 0);
      gl3.glBindVertexArray(VAO[0]);

      gl3.glGenBuffers(1, VBO, 0);
      gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, VBO[0]);
      FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(TRIANGLE);
      gl3.glBufferData(GL3.GL_ARRAY_BUFFER, buffer.capacity() * 4, buffer, GL3.GL_STATIC_DRAW);
      gl3.glEnableVertexAttribArray(0);
      gl3.glVertexAttribPointer(0, 2, GL3.GL_FLOAT, false, 0, 0);

      gl3.glBindVertexArray(0);
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
예제 #21
0
 public static float btLargeDot(java.nio.FloatBuffer a, java.nio.FloatBuffer b, int n) {
   assert a.isDirect() : "Buffer must be allocated direct.";
   assert b.isDirect() : "Buffer must be allocated direct.";
   {
     return LinearMathJNI.btLargeDot(a, b, n);
   }
 }
예제 #22
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);
  }
예제 #23
0
  public Skybox(String texture) {
    vertexBuffer = BufferUtils.newFloatBuffer(72);
    vertexBuffer.put(
        new float[] {
          -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f,
          -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f,
          0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f,
          0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f,
          0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f,
          -0.5f, 0.5f, -0.5f, 0.5f
        });
    vertexBuffer.rewind();

    texCoordBuffer = BufferUtils.newFloatBuffer(48);
    texCoordBuffer.put(
        new float[] {
          0.5f, 0.75f, 0.5f, 1.0f, 0.25f, 0.75f, 0.25f, 1.0f, // bottom
          0.25f, 0.75f, 0f, 0.75f, 0.25f, 0.5f, 0f, 0.5f, // left
          0.25f, 0.5f, 0.25f, 0.25f, 0.5f, 0.5f, 0.5f, 0.25f, // top
          0.5f, 0.5f, 0.75f, 0.5f, 0.5f, 0.75f, 0.75f, 0.75f, // right
          0.75f, 0.75f, 0.75f, 0.5f, 1f, 0.75f, 1.0f, 0.5f, // right right
          0.5f, 0.75f, 0.5f, 0.5f, 0.25f, 0.75f, 0.25f, 0.5f, // center
        });
    texCoordBuffer.rewind();

    tex = new Texture(Gdx.files.internal(texture));
    useTexture = true;
  }
예제 #24
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();
  }
예제 #25
0
  /*
   * 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;
  }
예제 #26
0
  private void buildLeavesVBO(GL gl) {
    // Allocate buffer memory
    FloatBuffer leafVertexBuffer = BufferUtil.newFloatBuffer(81 * 6 * 3);
    FloatBuffer leafTextureBuffer = BufferUtil.newFloatBuffer(81 * 6 * 2);

    nLeafVertices = tree.createLeavesVBO(leafVertexBuffer, leafTextureBuffer);
    leafVertexBuffer.flip();
    leafTextureBuffer.flip();

    // Create VBO Id
    if (branchVBOIds[0] != 0) {
      gl.glDeleteBuffersARB(2, leafVBOIds, 0);
    }
    gl.glGenBuffersARB(2, leafVBOIds, 0); // Get Valid Names

    // Load buffer into graphics card memory
    // Vertex coordinates
    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, leafVBOIds[0]); // Bind The Buffer
    gl.glBufferDataARB(
        GL.GL_ARRAY_BUFFER_ARB,
        nLeafVertices * 3 * BufferUtil.SIZEOF_FLOAT,
        leafVertexBuffer,
        GL.GL_STATIC_DRAW_ARB);

    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, leafVBOIds[1]);
    gl.glBufferDataARB(
        GL.GL_ARRAY_BUFFER_ARB,
        nLeafVertices * 2 * BufferUtil.SIZEOF_FLOAT,
        leafTextureBuffer,
        GL.GL_STATIC_DRAW_ARB);

    leafVertexBuffer = null;
    leafTextureBuffer = null;
  }
예제 #27
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;
        }
    }
예제 #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;
 }
예제 #29
0
  /**
   * Creates vertex buffer objects to render this skybox.
   *
   * @param gl OpenGL interface
   */
  private void createVBO(GL gl) {
    FloatBuffer vertexBuffer =
        BufferUtil.newFloatBuffer(324); // 36 faces, 3 vertices per face, 3 co-ordinates per vertex
    FloatBuffer textureBuffer =
        BufferUtil.newFloatBuffer(216); // 36 faces, 3 vertices per face, 2 co-ordinates per vertex
    nVertices = populateVBOWithVertices(vertexBuffer);
    vertexBuffer.flip();
    populateVBOWithTextureCoords(textureBuffer);
    textureBuffer.flip();

    gl.glGenBuffersARB(2, VBOIds, 0);

    // Load buffer into graphics card memory
    // Vertex coordinates
    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, VBOIds[0]); // Bind The Buffer
    gl.glBufferDataARB(
        GL.GL_ARRAY_BUFFER_ARB,
        nVertices * 3 * BufferUtil.SIZEOF_FLOAT,
        vertexBuffer,
        GL.GL_STATIC_DRAW_ARB);

    gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, VBOIds[1]);
    gl.glBufferDataARB(
        GL.GL_ARRAY_BUFFER_ARB,
        nVertices * 2 * BufferUtil.SIZEOF_FLOAT,
        textureBuffer,
        GL.GL_STATIC_DRAW_ARB);

    vertexBuffer = null;
    textureBuffer = null;
  }
예제 #30
0
  private FloatBuffer storeInFloatBuffer(float[] data) {
    FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
    buffer.put(data);
    buffer.flip();

    return buffer;
  }