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
|
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Log;
public class LineMesh {
public static final int mBytesPerFloat = 4;
private static final String TAG = "Shit";
private float[] vertexData = new float[12];
private float lineWidth = 1.0f;
private FloatBuffer vertexBuffer = null;
private int index = 0;
private boolean hasChanged = true;
public LineMesh(){
vertexBuffer = ByteBuffer.allocateDirect(12 * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
for(int i=0; i<12;++i){
vertexData[i] = 0.0f;
}
}
public void vertex(float x, float y, float r, float g, float b, float a){
vertexData[index*6] = x;
vertexData[index*6+1] = y;
vertexData[index*6+2] = r;
vertexData[index*6+3] = g;
vertexData[index*6+4] = b;
vertexData[index*6+5] = a;
if(index<1)
++index;
hasChanged = true;
}
public void insertVertex(int index, float x, float y, float r, float g, float b, float a){
if(index <2){
vertexData[index] = x;
vertexData[index+1] = y;
vertexData[index+2] = r;
vertexData[index+3] = g;
vertexData[index+4] = b;
vertexData[index+5] = a;
hasChanged = true;
}
}
public void setLineWidth(float width){
lineWidth = width;
}
public void update(){
vertexBuffer.put(vertexData).position(0);
index = 0;
hasChanged = false;
}
public void render(CurveShader shader)
{
if( hasChanged )
update();
GLES20.glUseProgram(shader.programHandle);
vertexBuffer.position(0);
GLES20.glVertexAttribPointer(shader.positionHandle, 2, GLES20.GL_FLOAT, false,
24, vertexBuffer);
GLES20.glEnableVertexAttribArray(shader.positionHandle);
vertexBuffer.position(2);
GLES20.glVertexAttribPointer(shader.colorHandle, 4, GLES20.GL_FLOAT, false,
24, vertexBuffer);
GLES20.glEnableVertexAttribArray(shader.colorHandle);
GLES20.glLineWidth(lineWidth);
GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, 2);
}
}
|