@@ -25,19 +25,12 @@ public class SurfaceTextureRenderer {
2525 private static final float [] TRIANGLE_VERTICES_DATA = {
2626 // X, Y, Z, U, V
2727 -1.0f , -1.0f , 0.f , 0.f , 0.f ,
28- 1.0f , -1.0f , 0.f , 1.f , 0.f ,
28+ 1.0f , -1.0f , 0.f , 1.f , 0.f ,
2929 -1.0f , 1.0f , 0.f , 0.f , 1.f ,
30- 1.0f , 1.0f , 0.f , 1.f , 1.f ,
31- };
32-
33- private static final float [] TRIANGLE_VERTICES_DATA_2 = {
34- // X, Y, Z, U, V
35- -1.0f , -1.0f , 0.f , 0.f , 1.f ,
36- 1.0f , -1.0f , 0.f , 1.f , 1.f ,
37- -1.0f , 1.0f , 0.f , 0.f , 0.f ,
38- 1.0f , 1.0f , 0.f , 1.f , 0.f ,
30+ 1.0f , 1.0f , 0.f , 1.f , 1.f ,
3931 };
4032
33+ private final FloatBuffer mDefaultTriangleVertices ;
4134 private final FloatBuffer mTriangleVertices ;
4235 private final boolean mInverse ;
4336
@@ -78,20 +71,54 @@ public class SurfaceTextureRenderer {
7871 * @param inverse テクスチャの反転フラグ
7972 */
8073 public SurfaceTextureRenderer (boolean inverse ) {
74+ this (inverse , TRIANGLE_VERTICES_DATA );
75+ }
76+
77+ /**
78+ * コンストラクタ.
79+ * @param inverse テクスチャの反転フラグ
80+ * @param vertices テクスチャの頂点バッファ(初期値)
81+ */
82+ public SurfaceTextureRenderer (boolean inverse , float [] vertices ) {
8183 mInverse = inverse ;
84+ if (inverse ) {
85+ vertices = inverseVertices (vertices );
86+ }
87+
88+ mDefaultTriangleVertices = ByteBuffer .allocateDirect (
89+ TRIANGLE_VERTICES_DATA .length * FLOAT_SIZE_BYTES )
90+ .order (ByteOrder .nativeOrder ()).asFloatBuffer ();
91+ mDefaultTriangleVertices .put (vertices ).position (0 );
92+
8293 mTriangleVertices = ByteBuffer .allocateDirect (
8394 TRIANGLE_VERTICES_DATA .length * FLOAT_SIZE_BYTES )
8495 .order (ByteOrder .nativeOrder ()).asFloatBuffer ();
85- if (inverse ) {
86- mTriangleVertices .put (TRIANGLE_VERTICES_DATA_2 ).position (0 );
87- } else {
88- mTriangleVertices .put (TRIANGLE_VERTICES_DATA ).position (0 );
89- }
96+ mTriangleVertices .put (vertices ).position (0 );
9097
9198 Matrix .setIdentityM (mSTMatrix , 0 );
9299 Matrix .setIdentityM (mMVPMatrix , 0 );
93100 }
94101
102+ private static float [] inverseVertices (final float [] vertices ) {
103+ int len = vertices .length ;
104+ float [] inverse = new float [len ];
105+ System .arraycopy (vertices , 0 , inverse , 0 , len );
106+ inverse [3 ] = vertices [13 ];
107+ inverse [4 ] = vertices [14 ];
108+ inverse [13 ] = vertices [3 ];
109+ inverse [14 ] = vertices [4 ];
110+ inverse [8 ] = vertices [18 ];
111+ inverse [9 ] = vertices [19 ];
112+ inverse [18 ] = vertices [8 ];
113+ inverse [19 ] = vertices [9 ];
114+ return inverse ;
115+ }
116+
117+ public void setTextureVertices (final float [] vertices ) {
118+ mTriangleVertices .clear ();
119+ mTriangleVertices .put (vertices ).position (0 );
120+ }
121+
95122 /**
96123 * テクスチャの ID を取得します.
97124 *
@@ -227,11 +254,8 @@ public void drawFrame(SurfaceTexture st, int displayRotation) {
227254 */
228255 public void clearCropRect () {
229256 mTriangleVertices .clear ();
230- if (mInverse ) {
231- mTriangleVertices .put (TRIANGLE_VERTICES_DATA_2 ).position (0 );
232- } else {
233- mTriangleVertices .put (TRIANGLE_VERTICES_DATA ).position (0 );
234- }
257+ mDefaultTriangleVertices .position (0 );
258+ mTriangleVertices .put (mDefaultTriangleVertices ).position (0 );
235259 }
236260
237261 /**
@@ -293,8 +317,7 @@ private void setCropRect(float l, float t, float r, float b) {
293317 ex , ey , 0.f , r , (1 - b ),
294318 };
295319
296- mTriangleVertices .clear ();
297- mTriangleVertices .put (triangleVerticesData ).position (0 );
320+ setTextureVertices (triangleVerticesData );
298321 }
299322
300323 private int loadShader (int shaderType , String source ) {
@@ -366,4 +389,4 @@ private static void checkLocation(int location, String label) {
366389 throw new RuntimeException ("Unable to locate '" + label + "' in program" );
367390 }
368391 }
369- }
392+ }
0 commit comments