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
|
public class Bottom {
private Context context;
private int Shader;
private static final int POSITION_COMPONENT_COUNT = 4;
private int u_ColorLocation;
private int a_PositionLocation;
private int u_PositionLocation;
public int width,height;
private FloatBuffer vertexBuffer;
private float[] Plane={
// Triangle 1
-.25f, -.25f, 1f, 1f,
.25f, .25f, 1f, 1f,
-.25f, .25f, 1f, 1f,
// Triangle 2
-.25f, -.25f, 1f, 1f,
.25f, -.25f, 1f, 1f,
.25f, .25f, 1f, 1f
};
private float[] ProjectionsMatrix, ModelviewMatrix, mVPMatrix;
public Bottom(Context context)
{
this.context=context;
// Generate VertexBuffer
ByteBuffer bb= ByteBuffer.allocateDirect(Plane.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(Plane);
vertexBuffer.position(0);
ProjectionsMatrix = new float[16];
ModelviewMatrix = new float[16];
mVPMatrix = new float[16];
// Shader einrichten
String vertexShaderSource=TextResourceReader.readTextFileFromResource(context,R.raw.bottom_vertex_shader);
String fragmentShaderSource=TextResourceReader.readTextFileFromResource(context,R.raw.bottom_fragment_shader);
int vertexShader=ShaderHelper.compileVertexShader(vertexShaderSource);
int fragmentShader=ShaderHelper.compileFragmentShader(fragmentShaderSource);
Shader=ShaderHelper.LinkProgram(vertexShader,fragmentShader);
if(LoggerConfig.ON)
{
ShaderHelper.validateProgram(Shader);
}
}
public void checkGlError(String op) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
Log.e("Renderer", op + ": glError " + gluErrorString(error));
throw new RuntimeException(op + ": glError " + error);
}
}
public void Draw() {
vertexBuffer.position(0);
final float ratio = (float) width / height;
final float left = -ratio;
final float right = ratio;
final float bottom = -1.0f;
final float top = 1.0f;
final float near = .5f;
final float far = 10.0f;
frustumM(ProjectionsMatrix, 0, left, right, bottom, top, near, far);
setIdentityM(ModelviewMatrix, 0);
multiplyMM(mVPMatrix,0,ModelviewMatrix,0,ProjectionsMatrix,0);
u_ColorLocation = glGetUniformLocation(Shader, "u_Color");
a_PositionLocation = glGetAttribLocation(Shader, "a_Position");
u_PositionLocation = glGetUniformLocation(Shader, "u_Position");
glUseProgram(Shader);
glVertexAttribPointer(a_PositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT, false, 0, vertexBuffer);
checkGlError("VertexPointer set :");
glEnableVertexAttribArray(a_PositionLocation);
checkGlError("VertexPointer enable :");
glUniformMatrix4fv(u_PositionLocation, 1, false, mVPMatrix, 0);
checkGlError("Uniform matrix:");
glUniform4f(u_ColorLocation, 1f, 1f, 1f, 1f);
checkGlError("Uniform Color :");
glDrawArrays(GL_TRIANGLES, 0, 6);
checkGlError("glDrawArrays");
}
}
|