Skip to content

Commit af3d56c

Browse files
committed
draw textured table!
1 parent 5b51f42 commit af3d56c

13 files changed

+351
-85
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.piasy.openglestutorial_android;
2+
3+
import android.content.Context;
4+
import android.opengl.GLES20;
5+
6+
/**
7+
* Created by Piasy{github.com/Piasy} on 15/07/2017.
8+
*/
9+
10+
public class ColorShaderProgram extends ShaderProgram {
11+
// Uniform locations
12+
private final int mUMatrixLocation;
13+
14+
// Attribute locations
15+
private final int mAPositionLocation;
16+
private final int mAColorLocation;
17+
18+
public ColorShaderProgram(final Context context) {
19+
super(context, R.raw.vertex, R.raw.fragment);
20+
21+
// Retrieve uniform locations for the shader program.
22+
mUMatrixLocation = GLES20.glGetUniformLocation(mProgram, U_MATRIX);
23+
// Retrieve attribute locations for the shader program.
24+
mAPositionLocation = GLES20.glGetAttribLocation(mProgram, A_POSITION);
25+
mAColorLocation = GLES20.glGetAttribLocation(mProgram, A_COLOR);
26+
}
27+
28+
public void setUniforms(float[] matrix) {
29+
// Pass the matrix into the shader program.
30+
GLES20.glUniformMatrix4fv(mUMatrixLocation, 1, false, matrix, 0);
31+
}
32+
33+
public int getPositionAttributeLocation() {
34+
return mAPositionLocation;
35+
}
36+
37+
public int getColorAttributeLocation() {
38+
return mAColorLocation;
39+
}
40+
}

app/src/main/java/com/github/piasy/openglestutorial_android/DemoRenderer.java

+23-84
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import android.opengl.GLES20;
55
import android.opengl.GLSurfaceView;
66
import android.opengl.Matrix;
7-
import java.nio.ByteBuffer;
8-
import java.nio.ByteOrder;
9-
import java.nio.FloatBuffer;
107
import javax.microedition.khronos.egl.EGLConfig;
118
import javax.microedition.khronos.opengles.GL10;
129

@@ -15,89 +12,30 @@
1512
*/
1613
class DemoRenderer implements GLSurfaceView.Renderer {
1714

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-
2915
private final Context mContext;
30-
private final FloatBuffer mVertexData;
31-
32-
private final float[] mModelMatrix = new float[16];
3316
private final float[] mProjectionMatrix = new float[16];
17+
private final float[] mModelMatrix = new float[16];
3418
private final float[] mTmpMatrix = new float[16];
3519

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;
4025

4126
DemoRenderer(final Context context) {
4227
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);
6928
}
7029

7130
@Override
7231
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
7332
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
7433

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);
10139
}
10240

10341
@Override
@@ -115,18 +53,19 @@ public void onSurfaceChanged(GL10 unused, int width, int height) {
11553

11654
@Override
11755
public void onDrawFrame(GL10 unused) {
56+
// Clear the rendering surface.
11857
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
11958

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();
13170
}
13271
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.github.piasy.openglestutorial_android;
2+
3+
import android.opengl.GLES20;
4+
5+
/**
6+
* Created by Piasy{github.com/Piasy} on 15/07/2017.
7+
*/
8+
9+
public class Mallet {
10+
private static final int POSITION_COMPONENT_COUNT = 2;
11+
private static final int COLOR_COMPONENT_COUNT = 3;
12+
private static final int STRIDE = (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT)
13+
* Utils.BYTES_PER_FLOAT;
14+
15+
private static final float[] VERTEX_DATA = {
16+
// Order of coordinates: X, Y, R, G, B
17+
0f, -0.4f, 0f, 0f, 1f,
18+
0f, 0.4f, 1f, 0f, 0f
19+
};
20+
21+
private final VertexArray mVertexArray;
22+
23+
public Mallet() {
24+
mVertexArray = new VertexArray(VERTEX_DATA);
25+
}
26+
27+
public void bindData(ColorShaderProgram colorProgram) {
28+
mVertexArray.setVertexAttribPointer(
29+
0,
30+
colorProgram.getPositionAttributeLocation(),
31+
POSITION_COMPONENT_COUNT,
32+
STRIDE);
33+
mVertexArray.setVertexAttribPointer(
34+
POSITION_COMPONENT_COUNT,
35+
colorProgram.getColorAttributeLocation(),
36+
COLOR_COMPONENT_COUNT,
37+
STRIDE);
38+
}
39+
40+
public void draw() {
41+
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 2);
42+
}
43+
}

app/src/main/java/com/github/piasy/openglestutorial_android/ShaderHelper.java

+15
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ private static int compileShader(int type, String shader) {
8585

8686
return shaderObj;
8787
}
88+
89+
public static int buildProgram(String vertexShaderSource, String fragmentShaderSource) {
90+
int program;
91+
92+
// Compile the shaders.
93+
int vertexShader = compileVertexShader(vertexShaderSource);
94+
int fragmentShader = compileFragmentShader(fragmentShaderSource);
95+
96+
// Link them into a shader program.
97+
program = linkProgram(vertexShader, fragmentShader);
98+
99+
validateProgram(program);
100+
101+
return program;
102+
}
88103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.piasy.openglestutorial_android;
2+
3+
import android.content.Context;
4+
import android.opengl.GLES20;
5+
import android.support.annotation.RawRes;
6+
7+
/**
8+
* Created by Piasy{github.com/Piasy} on 15/07/2017.
9+
*/
10+
11+
public abstract class ShaderProgram {
12+
// Uniform constants
13+
protected static final String U_MATRIX = "u_Matrix";
14+
protected static final String U_TEXTURE_UNIT = "u_TextureUnit";
15+
16+
// Attribute constants
17+
protected static final String A_POSITION = "a_Position";
18+
protected static final String A_COLOR = "a_Color";
19+
protected static final String A_TEXTURE_COORDINATES = "a_TextureCoordinates";
20+
21+
// Shader program
22+
protected final int mProgram;
23+
24+
protected ShaderProgram(Context context, @RawRes int vertexShaderResourceId,
25+
@RawRes int fragmentShaderResourceId) {
26+
// Compile the shaders and link the program.
27+
mProgram = ShaderHelper.buildProgram(
28+
Utils.loadShader(context, vertexShaderResourceId),
29+
Utils.loadShader(context, fragmentShaderResourceId));
30+
}
31+
32+
public void useProgram() {
33+
// Set the current OpenGL shader program to this program.
34+
GLES20.glUseProgram(mProgram);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.piasy.openglestutorial_android;
2+
3+
import android.opengl.GLES20;
4+
5+
/**
6+
* Created by Piasy{github.com/Piasy} on 15/07/2017.
7+
*/
8+
9+
public class Table {
10+
private static final int POSITION_COMPONENT_COUNT = 2;
11+
private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
12+
private static final int STRIDE = (POSITION_COMPONENT_COUNT
13+
+ TEXTURE_COORDINATES_COMPONENT_COUNT)
14+
* Utils.BYTES_PER_FLOAT;
15+
16+
private static final float[] VERTEX_DATA = {
17+
// Order of coordinates: X, Y, S, T
18+
19+
// Triangle Fan
20+
0f, 0f, 0.5f, 0.5f,
21+
-0.5f, -0.8f, 0f, 0.9f,
22+
0.5f, -0.8f, 1f, 0.9f,
23+
0.5f, 0.8f, 1f, 0.1f,
24+
-0.5f, 0.8f, 0f, 0.1f,
25+
-0.5f, -0.8f, 0f, 0.9f
26+
};
27+
28+
private final VertexArray mVertexArray;
29+
30+
public Table() {
31+
mVertexArray = new VertexArray(VERTEX_DATA);
32+
}
33+
34+
public void bindData(TextureShaderProgram textureProgram) {
35+
mVertexArray.setVertexAttribPointer(
36+
0,
37+
textureProgram.getPositionAttributeLocation(),
38+
POSITION_COMPONENT_COUNT,
39+
STRIDE);
40+
41+
mVertexArray.setVertexAttribPointer(
42+
POSITION_COMPONENT_COUNT,
43+
textureProgram.getTextureCoordinatesAttributeLocation(),
44+
TEXTURE_COORDINATES_COMPONENT_COUNT,
45+
STRIDE);
46+
}
47+
48+
public void draw() {
49+
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.github.piasy.openglestutorial_android;
2+
3+
import android.content.Context;
4+
import android.opengl.GLES20;
5+
6+
/**
7+
* Created by Piasy{github.com/Piasy} on 15/07/2017.
8+
*/
9+
10+
public class TextureShaderProgram extends ShaderProgram {
11+
// Uniform locations
12+
private final int mUMatrixLocation;
13+
private final int mUTextureUnitLocation;
14+
15+
// Attribute locations
16+
private final int mAPositionLocation;
17+
private final int mATextureCoordinatesLocation;
18+
19+
public TextureShaderProgram(final Context context) {
20+
super(context, R.raw.texture_vertex, R.raw.texture_fragment);
21+
22+
// Retrieve uniform locations for the shader program.
23+
mUMatrixLocation = GLES20.glGetUniformLocation(mProgram, U_MATRIX);
24+
mUTextureUnitLocation = GLES20.glGetUniformLocation(mProgram, U_TEXTURE_UNIT);
25+
// Retrieve attribute locations for the shader program.
26+
mAPositionLocation = GLES20.glGetAttribLocation(mProgram, A_POSITION);
27+
mATextureCoordinatesLocation = GLES20.glGetAttribLocation(mProgram, A_TEXTURE_COORDINATES);
28+
}
29+
30+
public void setUniforms(float[] matrix, int textureId) {
31+
// Pass the matrix into the shader program.
32+
GLES20.glUniformMatrix4fv(mUMatrixLocation, 1, false, matrix, 0);
33+
34+
// Set the active texture unit to texture unit 0.
35+
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
36+
37+
// Bind the texture to this unit.
38+
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
39+
40+
// Tell the texture uniform sampler to use this texture in the shader by
41+
// telling it to read from texture unit 0.
42+
GLES20.glUniform1i(mUTextureUnitLocation, 0);
43+
}
44+
45+
public int getPositionAttributeLocation() {
46+
return mAPositionLocation;
47+
}
48+
49+
public int getTextureCoordinatesAttributeLocation() {
50+
return mATextureCoordinatesLocation;
51+
}
52+
}

0 commit comments

Comments
 (0)