4
4
import android .opengl .GLES20 ;
5
5
import android .opengl .GLSurfaceView ;
6
6
import android .opengl .Matrix ;
7
- import java .nio .ByteBuffer ;
8
- import java .nio .ByteOrder ;
9
- import java .nio .FloatBuffer ;
10
7
import javax .microedition .khronos .egl .EGLConfig ;
11
8
import javax .microedition .khronos .opengles .GL10 ;
12
9
15
12
*/
16
13
class DemoRenderer implements GLSurfaceView .Renderer {
17
14
18
- private static final int BYTES_PER_FLOAT = 4 ;
19
-
20
- private static final int POSITION_COMPONENT_COUNT = 2 ;
21
- private static final int COLOR_COMPONENT_COUNT = 3 ;
22
- private static final int STRIDE = (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT )
23
- * BYTES_PER_FLOAT ;
24
-
25
- private static final String U_MATRIX = "u_Matrix" ;
26
- private static final String A_COLOR = "a_Color" ;
27
- private static final String A_POSITION = "a_Position" ;
28
-
29
15
private final Context mContext ;
30
- private final FloatBuffer mVertexData ;
31
-
32
- private final float [] mModelMatrix = new float [16 ];
33
16
private final float [] mProjectionMatrix = new float [16 ];
17
+ private final float [] mModelMatrix = new float [16 ];
34
18
private final float [] mTmpMatrix = new float [16 ];
35
19
36
- private int mProgram ;
37
- private int mColorLocation ;
38
- private int mPositionLocation ;
39
- private int mMatrixLocation ;
20
+ private Table mTable ;
21
+ private Mallet mMallet ;
22
+ private TextureShaderProgram mTextureShaderProgram ;
23
+ private ColorShaderProgram mColorShaderProgram ;
24
+ private int mTexture ;
40
25
41
26
DemoRenderer (final Context context ) {
42
27
mContext = context ;
43
-
44
- float [] tableVerticesWithTriangles = {
45
- // Order of coordinates: X, Y, R, G, B
46
-
47
- // Triangle Fan
48
- 0f , 0f , 1f , 1f , 1f ,
49
- -0.5f , -0.8f , 0.7f , 0.7f , 0.7f ,
50
- 0.5f , -0.8f , 0.7f , 0.7f , 0.7f ,
51
- 0.5f , 0.8f , 0.7f , 0.7f , 0.7f ,
52
- -0.5f , 0.8f , 0.7f , 0.7f , 0.7f ,
53
- -0.5f , -0.8f , 0.7f , 0.7f , 0.7f ,
54
-
55
- // Line 1
56
- -0.5f , 0f , 1f , 0f , 0f ,
57
- 0.5f , 0f , 1f , 0f , 0f ,
58
-
59
- // Mallets
60
- 0f , -0.4f , 0f , 0f , 1f ,
61
- 0f , 0.4f , 1f , 0f , 0f
62
- };
63
-
64
- mVertexData = ByteBuffer
65
- .allocateDirect (tableVerticesWithTriangles .length * BYTES_PER_FLOAT )
66
- .order (ByteOrder .nativeOrder ())
67
- .asFloatBuffer ();
68
- mVertexData .put (tableVerticesWithTriangles );
69
28
}
70
29
71
30
@ Override
72
31
public void onSurfaceCreated (GL10 unused , EGLConfig config ) {
73
32
GLES20 .glClearColor (0.0f , 0.0f , 0.0f , 0.0f );
74
33
75
- String vertexShaderSrc = Utils .loadShader (mContext , R .raw .vertex );
76
- String fragmentShaderSrc = Utils .loadShader (mContext , R .raw .fragment );
77
-
78
- int vertexShader = ShaderHelper .compileVertexShader (vertexShaderSrc );
79
- int fragmentShader = ShaderHelper .compileFragmentShader (fragmentShaderSrc );
80
-
81
- mProgram = ShaderHelper .linkProgram (vertexShader , fragmentShader );
82
-
83
- if (!ShaderHelper .validateProgram (mProgram )) {
84
- return ;
85
- }
86
-
87
- GLES20 .glUseProgram (mProgram );
88
- mColorLocation = GLES20 .glGetAttribLocation (mProgram , A_COLOR );
89
- mPositionLocation = GLES20 .glGetAttribLocation (mProgram , A_POSITION );
90
- mMatrixLocation = GLES20 .glGetUniformLocation (mProgram , U_MATRIX );
91
-
92
- mVertexData .position (0 );
93
- GLES20 .glVertexAttribPointer (mPositionLocation , POSITION_COMPONENT_COUNT , GLES20 .GL_FLOAT ,
94
- false , STRIDE , mVertexData );
95
- GLES20 .glEnableVertexAttribArray (mPositionLocation );
96
-
97
- mVertexData .position (POSITION_COMPONENT_COUNT );
98
- GLES20 .glVertexAttribPointer (mColorLocation , COLOR_COMPONENT_COUNT , GLES20 .GL_FLOAT ,
99
- false , STRIDE , mVertexData );
100
- GLES20 .glEnableVertexAttribArray (mColorLocation );
34
+ mTable = new Table ();
35
+ mMallet = new Mallet ();
36
+ mTextureShaderProgram = new TextureShaderProgram (mContext );
37
+ mColorShaderProgram = new ColorShaderProgram (mContext );
38
+ mTexture = Utils .loadTexture (mContext , R .drawable .air_hockey_surface );
101
39
}
102
40
103
41
@ Override
@@ -115,18 +53,19 @@ public void onSurfaceChanged(GL10 unused, int width, int height) {
115
53
116
54
@ Override
117
55
public void onDrawFrame (GL10 unused ) {
56
+ // Clear the rendering surface.
118
57
GLES20 .glClear (GLES20 .GL_COLOR_BUFFER_BIT );
119
58
120
- GLES20 . glUniformMatrix4fv ( mMatrixLocation , 1 , false , mProjectionMatrix , 0 );
121
-
122
- // rectangle
123
- GLES20 . glDrawArrays ( GLES20 . GL_TRIANGLE_FAN , 0 , 6 );
124
-
125
- // divide line
126
- GLES20 . glDrawArrays ( GLES20 . GL_LINES , 6 , 2 );
127
-
128
- // points
129
- GLES20 . glDrawArrays ( GLES20 . GL_POINTS , 8 , 1 );
130
- GLES20 . glDrawArrays ( GLES20 . GL_POINTS , 9 , 1 );
59
+ // Draw the table.
60
+ mTextureShaderProgram . useProgram ();
61
+ mTextureShaderProgram . setUniforms ( mProjectionMatrix , mTexture );
62
+ mTable . bindData ( mTextureShaderProgram );
63
+ mTable . draw ();
64
+
65
+ // Draw the mallets.
66
+ mColorShaderProgram . useProgram ();
67
+ mColorShaderProgram . setUniforms ( mProjectionMatrix );
68
+ mMallet . bindData ( mColorShaderProgram );
69
+ mMallet . draw ( );
131
70
}
132
71
}
0 commit comments