Package Hexel.rendering

Source Code of Hexel.rendering.GLObj

package Hexel.rendering;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.fixedfunc.GLPointerFunc;

import Hexel.math.HexGeometry;
import Hexel.math.Vector2d;
import Hexel.math.Vector3i;
import Hexel.util.MutableFloat;
import Hexel.util.ResourcePool;

import com.jogamp.common.nio.Buffers;
import com.jogamp.graph.geom.Vertex;
import com.owens.oobjloader.builder.Build;
import com.owens.oobjloader.builder.FaceVertex;
import com.owens.oobjloader.parser.Parse;
import com.owens.oobjloader.builder.Face;

public class GLObj {

  private Build builder = new Build();
  public Color c;

  public GLObj(InputStream stream, Color c){
    this.c = c;

    try {
      File temp = File.createTempFile("globj", ".tmp");
      OutputStream out = new FileOutputStream(temp);
      byte[] buffer = new byte[1024];
      int len;
      while ((len = stream.read(buffer)) != -1){
        out.write(buffer, 0, len);
      }
      out.close();

      new Parse(builder, temp.getAbsolutePath());
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    genArrayList();
  }


  public ArrayList<MutableFloat> vertexData;
  public List<MutableFloat> trimmedVertexData;
  public GLBuffer buffer;

  public void initBuffer(GL2 gl) {
    if (this.trimmedVertexData.size() == 0) {
      this.buffer = null;
      vertexDataPool.recycle(this.vertexData);
      this.vertexData = null;
      this.trimmedVertexData = null;
      return;
    }

    int[] bufferId = new int[] { -1 };

    gl.glGenBuffers(1, bufferId, 0);
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferId[0]);
    gl.glBufferData(GL.GL_ARRAY_BUFFER, this.trimmedVertexData.size()
        * Buffers.SIZEOF_FLOAT, null, GL.GL_DYNAMIC_DRAW);

    ByteBuffer byteBuffer = gl.glMapBuffer(GL.GL_ARRAY_BUFFER,
        GL.GL_WRITE_ONLY);
    FloatBuffer buffer = byteBuffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
    for (MutableFloat aVertexData : this.trimmedVertexData) {
      buffer.put(aVertexData.val);
    }
    gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);

    this.buffer = new GLBuffer(bufferId, this.trimmedVertexData.size() * Buffers.SIZEOF_FLOAT);


    vertexDataPool.recycle(this.vertexData);
    this.vertexData = null;
    this.trimmedVertexData = null;
  }

  private static ResourcePool<ArrayList<MutableFloat>> vertexDataPool =
      new ResourcePool<ArrayList<MutableFloat>>((Class<ArrayList<MutableFloat>>) new ArrayList<MutableFloat>().getClass());

  public void genArrayList(){
    this.vertexData = vertexDataPool.aquire();
    int index = 0;
    for (int i = 0; i < builder.faces.size(); i++){
      Face face = builder.faces.get(i);
      if (face.vertices.size() == 3){
        FaceVertex v0 = face.vertices.get(0);
        FaceVertex v1 = face.vertices.get(1);
        FaceVertex v2 = face.vertices.get(2);
        index = addVertexData(vertexData, index, v0.v.x, v0.v.y, v0.v.z, 0, 0, c);
        index = addVertexData(vertexData, index, v1.v.x, v1.v.y, v1.v.z, 0, 0, c);
        index = addVertexData(vertexData, index, v2.v.x, v2.v.y, v2.v.z, 0, 0, c);
      }
     
    }
    this.trimmedVertexData = this.vertexData.subList(0, index);
  }

  public int addVertexData(ArrayList<MutableFloat> list, int index, double x, double y,
      double z, double tx, double ty, Color c) {
    while (index+9 >= list.size())
      list.add(new MutableFloat());
    list.get(index+0).val = (float)x;
    list.get(index+1).val = (float)y;
    list.get(index+2).val = (float)z;
    list.get(index+3).val = (float)tx;
    list.get(index+4).val = (float)ty;
    list.get(index+5).val = (float)c.r;
    list.get(index+6).val = (float)c.g;
    list.get(index+7).val = (float)c.b;
    list.get(index+8).val = (float)c.alpha;
    return index+9;
  }

  public void render(GL2 gl){

    gl.glPushMatrix();

    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.buffer.id[0]);

    gl.glEnable(GL.GL_BLEND);
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.buffer.id[0]);

    gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GLPointerFunc.GL_COLOR_ARRAY);

    gl.glVertexPointer(3, GL.GL_FLOAT, 9 * 4, 0);
    gl.glColorPointer(4, GL.GL_FLOAT, 9 * 4, 5 * 4);

    gl.glDrawArrays(GL.GL_TRIANGLES, 0, this.buffer.size / (4 * 9));

    gl.glDisableClientState(GLPointerFunc.GL_COLOR_ARRAY);
    gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);

    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.buffer.id[0]);

    gl.glDisable(GL2ES1.GL_ALPHA_TEST);

    gl.glDisable(GL.GL_BLEND);
    gl.glPopMatrix();
  }
}
TOP

Related Classes of Hexel.rendering.GLObj

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.