|
3 | 3 | import android.content.Context;
|
4 | 4 | import android.opengl.GLES11Ext;
|
5 | 5 | import android.opengl.GLES20;
|
| 6 | + |
6 | 7 | import com.google.ar.core.Frame;
|
7 | 8 |
|
8 | 9 | import java.net.URL;
|
|
12 | 13 |
|
13 | 14 | public class ARBackground {
|
14 | 15 |
|
15 |
| - private static final int COORDS_PER_VERTEX = 3; |
16 |
| - private static final int TEXCOORDS_PER_VERTEX = 2; |
17 |
| - private static final int FLOAT_SIZE = 4; |
| 16 | + private static final int COORDS_PER_VERTEX = 3; |
| 17 | + private static final int TEXCOORDS_PER_VERTEX = 2; |
| 18 | + private static final int FLOAT_SIZE = 4; |
| 19 | + |
| 20 | + private FloatBuffer quadVertices; |
| 21 | + private FloatBuffer quadTexCoord; |
| 22 | + private FloatBuffer quadTexCoordTransformed; |
| 23 | + |
| 24 | + private int quadProgram; |
| 25 | + |
| 26 | + private int quadPositionParam; |
| 27 | + private int quadTexCoordParam; |
| 28 | + private int textureId = -1; |
| 29 | + |
| 30 | + static private URL screenquad_vertex = |
| 31 | + ARBackground.class.getResource("/assets/shaders/screenquad_vertex.glsl"); |
| 32 | + static private URL screenquad_fragment = |
| 33 | + ARBackground.class.getResource("/assets/shaders/screenquad_fragment.glsl"); |
| 34 | + |
| 35 | + private String VERTICES_ERROR = "Unexpected number of vertices in BackgroundRenderer"; |
| 36 | + private String ERROR_TAG = "Error"; |
| 37 | + private String CREATION_ERROR = "Program creation"; |
| 38 | + private String PARAMETERS_ERROR = "Program parameters"; |
| 39 | + private String DRAW_ERROR = "Draw"; |
| 40 | + |
| 41 | + public ARBackground() { |
| 42 | + } |
| 43 | + |
| 44 | + public int getTextureId() { |
| 45 | + return textureId; |
| 46 | + } |
| 47 | + |
| 48 | + public void createOnGlThread(Context context) { |
| 49 | + int[] textures = new int[1]; |
| 50 | + GLES20.glGenTextures(1, textures, 0); |
| 51 | + textureId = textures[0]; |
| 52 | + int textureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES; |
| 53 | + GLES20.glBindTexture(textureTarget, textureId); |
| 54 | + GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); |
| 55 | + GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); |
| 56 | + GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); |
| 57 | + GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); |
| 58 | + |
| 59 | + int numVertices = 4; |
| 60 | + if (numVertices != QUAD_COORDS.length / COORDS_PER_VERTEX) { |
| 61 | + throw new RuntimeException(VERTICES_ERROR); |
| 62 | + } |
18 | 63 |
|
19 |
| - private FloatBuffer quadVertices; |
20 |
| - private FloatBuffer quadTexCoord; |
21 |
| - private FloatBuffer quadTexCoordTransformed; |
| 64 | + ByteBuffer bbVertices = ByteBuffer.allocateDirect(QUAD_COORDS.length * FLOAT_SIZE); |
| 65 | + bbVertices.order(ByteOrder.nativeOrder()); |
| 66 | + quadVertices = bbVertices.asFloatBuffer(); |
| 67 | + quadVertices.put(QUAD_COORDS); |
| 68 | + quadVertices.position(0); |
22 | 69 |
|
23 |
| - private int quadProgram; |
| 70 | + ByteBuffer bbTexCoords = |
| 71 | + ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); |
| 72 | + bbTexCoords.order(ByteOrder.nativeOrder()); |
| 73 | + quadTexCoord = bbTexCoords.asFloatBuffer(); |
| 74 | + quadTexCoord.put(QUAD_TEXCOORDS); |
| 75 | + quadTexCoord.position(0); |
24 | 76 |
|
25 |
| - private int quadPositionParam; |
26 |
| - private int quadTexCoordParam; |
27 |
| - private int textureId = -1; |
| 77 | + ByteBuffer bbTexCoordsTransformed = |
| 78 | + ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); |
| 79 | + bbTexCoordsTransformed.order(ByteOrder.nativeOrder()); |
| 80 | + quadTexCoordTransformed = bbTexCoordsTransformed.asFloatBuffer(); |
28 | 81 |
|
29 |
| - static private URL screenquad_vertex = |
30 |
| - ARBackground.class.getResource("/assets/shaders/screenquad_vertex.glsl"); |
31 |
| - static private URL screenquad_fragment = |
32 |
| - ARBackground.class.getResource("/assets/shaders/screenquad_fragment.glsl"); |
| 82 | + int vertexShader = |
| 83 | + Utils.loadGLShader(ERROR_TAG, context, GLES20.GL_VERTEX_SHADER, screenquad_vertex); |
| 84 | + int fragmentShader = |
| 85 | + Utils.loadGLShader( |
| 86 | + ERROR_TAG, context, GLES20.GL_FRAGMENT_SHADER, screenquad_fragment); |
33 | 87 |
|
34 |
| - private String VERTICES_ERROR = "Unexpected number of vertices in BackgroundRenderer"; |
35 |
| - private String ERROR_TAG = "Error"; |
36 |
| - private String CREATION_ERROR = "Program creation"; |
37 |
| - private String PARAMETERS_ERROR = "Program parameters"; |
38 |
| - private String DRAW_ERROR = "Draw"; |
| 88 | + quadProgram = GLES20.glCreateProgram(); |
| 89 | + GLES20.glAttachShader(quadProgram, vertexShader); |
| 90 | + GLES20.glAttachShader(quadProgram, fragmentShader); |
| 91 | + GLES20.glLinkProgram(quadProgram); |
| 92 | + GLES20.glUseProgram(quadProgram); |
39 | 93 |
|
40 |
| - public ARBackground() {} |
| 94 | + Utils.checkGLError(ERROR_TAG, CREATION_ERROR); |
41 | 95 |
|
42 |
| - public int getTextureId() { |
43 |
| - return textureId; |
44 |
| - } |
| 96 | + quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position"); |
| 97 | + quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord"); |
45 | 98 |
|
46 |
| - public void createOnGlThread(Context context) { |
47 |
| - int[] textures = new int[1]; |
48 |
| - GLES20.glGenTextures(1, textures, 0); |
49 |
| - textureId = textures[0]; |
50 |
| - int textureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES; |
51 |
| - GLES20.glBindTexture(textureTarget, textureId); |
52 |
| - GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); |
53 |
| - GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); |
54 |
| - GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); |
55 |
| - GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); |
56 |
| - |
57 |
| - int numVertices = 4; |
58 |
| - if (numVertices != QUAD_COORDS.length / COORDS_PER_VERTEX) { |
59 |
| - throw new RuntimeException(VERTICES_ERROR); |
60 |
| - } |
61 |
| - |
62 |
| - ByteBuffer bbVertices = ByteBuffer.allocateDirect(QUAD_COORDS.length * FLOAT_SIZE); |
63 |
| - bbVertices.order(ByteOrder.nativeOrder()); |
64 |
| - quadVertices = bbVertices.asFloatBuffer(); |
65 |
| - quadVertices.put(QUAD_COORDS); |
66 |
| - quadVertices.position(0); |
67 |
| - |
68 |
| - ByteBuffer bbTexCoords = |
69 |
| - ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); |
70 |
| - bbTexCoords.order(ByteOrder.nativeOrder()); |
71 |
| - quadTexCoord = bbTexCoords.asFloatBuffer(); |
72 |
| - quadTexCoord.put(QUAD_TEXCOORDS); |
73 |
| - quadTexCoord.position(0); |
74 |
| - |
75 |
| - ByteBuffer bbTexCoordsTransformed = |
76 |
| - ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); |
77 |
| - bbTexCoordsTransformed.order(ByteOrder.nativeOrder()); |
78 |
| - quadTexCoordTransformed = bbTexCoordsTransformed.asFloatBuffer(); |
79 |
| - |
80 |
| - int vertexShader = |
81 |
| - Utils.loadGLShader(ERROR_TAG, context, GLES20.GL_VERTEX_SHADER, screenquad_vertex); |
82 |
| - int fragmentShader = |
83 |
| - Utils.loadGLShader( |
84 |
| - ERROR_TAG, context, GLES20.GL_FRAGMENT_SHADER, screenquad_fragment); |
85 |
| - |
86 |
| - quadProgram = GLES20.glCreateProgram(); |
87 |
| - GLES20.glAttachShader(quadProgram, vertexShader); |
88 |
| - GLES20.glAttachShader(quadProgram, fragmentShader); |
89 |
| - GLES20.glLinkProgram(quadProgram); |
90 |
| - GLES20.glUseProgram(quadProgram); |
91 |
| - |
92 |
| - Utils.checkGLError(ERROR_TAG, CREATION_ERROR); |
93 |
| - |
94 |
| - quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position"); |
95 |
| - quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord"); |
96 |
| - |
97 |
| - Utils.checkGLError(ERROR_TAG, PARAMETERS_ERROR); |
98 |
| - } |
| 99 | + Utils.checkGLError(ERROR_TAG, PARAMETERS_ERROR); |
| 100 | + } |
99 | 101 |
|
100 |
| - public void draw(Frame frame) { |
| 102 | + public void draw(Frame frame) { |
101 | 103 |
|
102 |
| - if (frame.hasDisplayGeometryChanged()) { |
103 |
| - frame.transformDisplayUvCoords(quadTexCoord, quadTexCoordTransformed); |
104 |
| - } |
| 104 | + if (frame.hasDisplayGeometryChanged()) { |
| 105 | + frame.transformDisplayUvCoords(quadTexCoord, quadTexCoordTransformed); |
| 106 | + } |
105 | 107 |
|
106 |
| - GLES20.glDisable(GLES20.GL_DEPTH_TEST); |
107 |
| - GLES20.glDepthMask(false); |
| 108 | + GLES20.glDisable(GLES20.GL_DEPTH_TEST); |
| 109 | + GLES20.glDepthMask(false); |
108 | 110 |
|
109 |
| - GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); |
| 111 | + GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); |
110 | 112 |
|
111 |
| - GLES20.glUseProgram(quadProgram); |
| 113 | + GLES20.glUseProgram(quadProgram); |
112 | 114 |
|
113 |
| - GLES20.glVertexAttribPointer( |
114 |
| - quadPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, quadVertices); |
| 115 | + GLES20.glVertexAttribPointer( |
| 116 | + quadPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, quadVertices); |
115 | 117 |
|
116 |
| - GLES20.glVertexAttribPointer( |
117 |
| - quadTexCoordParam, |
118 |
| - TEXCOORDS_PER_VERTEX, |
119 |
| - GLES20.GL_FLOAT, |
120 |
| - false, |
121 |
| - 0, |
122 |
| - quadTexCoordTransformed); |
| 118 | + GLES20.glVertexAttribPointer( |
| 119 | + quadTexCoordParam, |
| 120 | + TEXCOORDS_PER_VERTEX, |
| 121 | + GLES20.GL_FLOAT, |
| 122 | + false, |
| 123 | + 0, |
| 124 | + quadTexCoordTransformed); |
123 | 125 |
|
124 |
| - GLES20.glEnableVertexAttribArray(quadPositionParam); |
125 |
| - GLES20.glEnableVertexAttribArray(quadTexCoordParam); |
| 126 | + GLES20.glEnableVertexAttribArray(quadPositionParam); |
| 127 | + GLES20.glEnableVertexAttribArray(quadTexCoordParam); |
126 | 128 |
|
127 |
| - GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); |
| 129 | + GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); |
128 | 130 |
|
129 |
| - GLES20.glDisableVertexAttribArray(quadPositionParam); |
130 |
| - GLES20.glDisableVertexAttribArray(quadTexCoordParam); |
| 131 | + GLES20.glDisableVertexAttribArray(quadPositionParam); |
| 132 | + GLES20.glDisableVertexAttribArray(quadTexCoordParam); |
131 | 133 |
|
132 |
| - GLES20.glDepthMask(true); |
133 |
| - GLES20.glEnable(GLES20.GL_DEPTH_TEST); |
| 134 | + GLES20.glDepthMask(true); |
| 135 | + GLES20.glEnable(GLES20.GL_DEPTH_TEST); |
134 | 136 |
|
135 |
| - Utils.checkGLError(ERROR_TAG, DRAW_ERROR); |
136 |
| - } |
| 137 | + Utils.checkGLError(ERROR_TAG, DRAW_ERROR); |
| 138 | + } |
| 139 | + |
| 140 | + private static final float[] QUAD_COORDS = |
| 141 | + new float[]{ |
| 142 | + -1.0f, -1.0f, 0.0f, |
| 143 | + -1.0f, +1.0f, 0.0f, |
| 144 | + +1.0f, -1.0f, 0.0f, |
| 145 | + +1.0f, +1.0f, 0.0f, |
| 146 | + }; |
137 | 147 |
|
138 |
| - private static final float[] QUAD_COORDS = |
139 |
| - new float[] { |
140 |
| - -1.0f, -1.0f, 0.0f, |
141 |
| - -1.0f, +1.0f, 0.0f, |
142 |
| - +1.0f, -1.0f, 0.0f, |
143 |
| - +1.0f, +1.0f, 0.0f, |
144 |
| - }; |
145 |
| - |
146 |
| - private static final float[] QUAD_TEXCOORDS = |
147 |
| - new float[] { |
148 |
| - 0.0f, 1.0f, |
149 |
| - 0.0f, 0.0f, |
150 |
| - 1.0f, 1.0f, |
151 |
| - 1.0f, 0.0f, |
152 |
| - }; |
| 148 | + private static final float[] QUAD_TEXCOORDS = |
| 149 | + new float[]{ |
| 150 | + 0.0f, 1.0f, |
| 151 | + 0.0f, 0.0f, |
| 152 | + 1.0f, 1.0f, |
| 153 | + 1.0f, 0.0f, |
| 154 | + }; |
153 | 155 | }
|
0 commit comments