1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
import com.bioxpr.utils.opengl.Utils4OpenGL;
import com.sun.opengl.util.BufferUtil;
import java.awt.Color;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
public class SimpleGLNode extends AbstractGLNode implements Drawable{
final static private float SIDELENGTH = 0.5f;
private FloatBuffer nodeVertices;
private int bufferVerticesSize;
private FloatBuffer nodeColor;
private int bufferColorSize;
private ByteBuffer nodeIndices;
private byte[]indices = {0, 1, 2, 3};
final static private int BUFFERNAMESSIZE = 2;
private IntBuffer bufferName;
private boolean areBufferGenerated;
/**
*
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
* @param color Color of the node
* @param vector float[3] translation vector to origin
* @param vboCapable vboCapable boolean asserting that OpenGL version can
* support VBO
*/
public SimpleGLNode(
float x,
float y,
float z,
Color color,
float[]vector,
boolean vboCapable){
super(x, y, z, color, vector, vboCapable);
fillBuffers();
}
@Override
protected void setupNodeVerticesBuffer(){
float[]coords = getCoordinates();
float[]vector = getVector();
final float[]array = {
coords[0] - SIDELENGTH + vector[0], coords[1] + SIDELENGTH + vector[1], coords[2],
coords[0] + SIDELENGTH + vector[0], coords[1] + SIDELENGTH + vector[1], coords[2],
coords[0] + SIDELENGTH + vector[0], coords[1] - SIDELENGTH + vector[1], coords[2],
coords[0] - SIDELENGTH + vector[0], coords[1] - SIDELENGTH + vector[1], coords[2]
};
bufferVerticesSize = array.length;
nodeVertices = BufferUtil.newFloatBuffer(bufferVerticesSize);
nodeVertices.put(array);
nodeVertices.rewind();
}
@Override
protected void setupNodeColorBuffer(){
float[]colorComponds = Utils4OpenGL.getColorComponents(this.getColor());
bufferColorSize = colorComponds.length*this.indices.length;
float[]allColorComponds = new float[bufferColorSize];
int j=0;
for(int i=0; i<allColorComponds.length; i++){
allColorComponds[i] = colorComponds[j++];
if(j==colorComponds.length){
j=0;
}
}
nodeColor = BufferUtil.newFloatBuffer(allColorComponds.length);
nodeColor.put(allColorComponds);
nodeColor.rewind();
}
@Override
protected void setupNodeIndicesBuffer(){
nodeIndices = BufferUtil.newByteBuffer(this.indices.length);
nodeIndices.put(this.indices);
nodeIndices.rewind();
}
protected final void preProcessing(final GL gl){
this.bufferName = BufferUtil.newIntBuffer(SimpleGLNode.BUFFERNAMESSIZE);
gl.glGenBuffers(BUFFERNAMESSIZE, bufferName);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(1));
gl.glBufferData(gl.GL_ARRAY_BUFFER, bufferColorSize, nodeColor,
gl.GL_STREAM_DRAW);
gl.glColorPointer(4, gl.GL_FLOAT, 0, 0);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, bufferName.get(0));
gl.glBufferData(gl.GL_ARRAY_BUFFER, bufferVerticesSize, nodeVertices,
gl.GL_DYNAMIC_DRAW);
gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
this.areBufferGenerated = true;
}
protected final void drawWithVertexArray(final GL gl){
gl.glVertexPointer(3, GL.GL_FLOAT, 0, this.nodeVertices);
gl.glColorPointer(4, GL.GL_FLOAT, 0, this.nodeColor);
gl.glDrawElements(gl.GL_QUADS, this.indices.length, gl.GL_UNSIGNED_BYTE,
this.nodeIndices);
}
protected final void drawWithVBO(final GL gl){
gl.glDrawArrays(gl.GL_QUADS, 0, this.indices.length);
}
@Override
public final void draw(GL gl, GLU glu) {
if(this.isVBOCapable()){
if(!areBufferGenerated){
preProcessing(gl);
}
drawWithVBO(gl);
}
else{
drawWithVertexArray(gl);
}
}
@Override
public void draw() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public float getSideLength(){
return SimpleGLNode.SIDELENGTH;
}
} |
Partager