Skip to content

Commit f6939fd

Browse files
committed
texture test
1 parent b47d092 commit f6939fd

File tree

5 files changed

+214
-269
lines changed

5 files changed

+214
-269
lines changed

app/src/main/java/com/asha/md360player4android/MDRenderer.java

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.opengl.GLES20;
55
import android.opengl.GLES30;
6-
import android.opengl.Matrix;
76
import android.util.Log;
87

98
import com.asha.md360player4android.common.TextureHelper;
@@ -13,6 +12,13 @@
1312
import java.nio.FloatBuffer;
1413
import java.nio.IntBuffer;
1514

15+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMathDegreesToRadians;
16+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4Identity;
17+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4MakePerspective;
18+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4Multiply;
19+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4Rotate;
20+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4Scale;
21+
1622
/**
1723
* Created by hzqiujiadi on 16/1/7.
1824
* hzqiujiadi ashqalcn@gmail.com
@@ -116,74 +122,6 @@ public void draw(Context context){
116122
private static final float MIN_OVERTURE = 25.0f;
117123
private static final float DEFAULT_OVERTURE = 85.0f;
118124

119-
private float GLKMathDegreesToRadians(float degrees) {
120-
return (float) (degrees * (Math.PI / 180));
121-
}
122-
123-
private float GLKMathRadiansToDegrees(float angle) {
124-
return (float) (angle * 180 / Math.PI );
125-
}
126-
127-
private void GLKMatrix4MakePerspective(float fovyRadians, float aspect, float nearZ, float farZ, float[] out) {
128-
float cotan = (float) (1.0f / Math.tan(fovyRadians / 2.0f));
129-
out[0] = cotan / aspect;
130-
out[1] = 0.0f;
131-
out[2] = 0.0f;
132-
out[3] = 0.0f;
133-
134-
out[4] = 0.0f;
135-
out[5] = cotan;
136-
out[6] = 0.0f;
137-
out[7] = 0.0f;
138-
139-
out[8] = 0.0f;
140-
out[9] = 0.0f;
141-
out[10] = (farZ + nearZ) / (nearZ - farZ);
142-
out[11] = -1.0f;
143-
144-
out[12] = 0.0f;
145-
out[13] = 0.0f;
146-
out[14] = (2.0f * farZ * nearZ) / (nearZ - farZ);
147-
out[15] = 0.0f;
148-
}
149-
150-
private void GLKMatrix4Identity(float[] out){
151-
out[0] = 1.0f;
152-
out[1] = 0.0f;
153-
out[2] = 0.0f;
154-
out[3] = 0.0f;
155-
156-
out[4] = 0.0f;
157-
out[5] = 1.0f;
158-
out[6] = 0.0f;
159-
out[7] = 0.0f;
160-
161-
out[8] = 0.0f;
162-
out[9] = 0.0f;
163-
out[10] = 1.0f;
164-
out[11] = 0.0f;
165-
166-
out[12] = 0.0f;
167-
out[13] = 0.0f;
168-
out[14] = 0.0f;
169-
out[15] = 1.0f;
170-
}
171-
172-
private float[] GLKMatrix4Scale(float[] m, float sx, float sy, float sz){
173-
float[] a = {m[0] * sx,m[1] * sx, m[2] * sx, m[3] * sx,
174-
m[4] * sy, m[5] * sy, m[6] * sy, m[7] * sy,
175-
m[8] * sz, m[9] * sz, m[10] * sz, m[11] * sz,
176-
m[12], m[13], m[14], m[15]};
177-
return a;
178-
}
179-
180-
private void GLKMatrix4Multiply(float[] matrixLeft, float[] matrixRight, float[] out){
181-
Matrix.multiplyMM(out,0,matrixLeft,0,matrixRight,0);
182-
}
183-
184-
private void GLKMatrix4Rotate(float[] matrix, float radians, float x, float y, float z){
185-
Matrix.rotateM(matrix,0,GLKMathRadiansToDegrees(radians),x,y,z);
186-
}
187125

188126
private float[] projectionMatrix = new float[16];
189127
private float[] modelViewMatrix = new float[16];

app/src/main/java/com/asha/md360player4android/Sphere.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
*/
77
public class Sphere{
88
public float[] vertices;
9+
public float[] colors;
910
public float[] texCoords;
11+
public float[] normals;
1012
public int[] indices;
1113
public int numIndices;
1214
public int numVertices;
@@ -22,6 +24,8 @@ private int createSphere(int numSlices, float radius){
2224
numIndices = numParallels * numSlices * 6;
2325
vertices = new float[3 * numVertices];
2426
texCoords = new float[2 * numVertices];
27+
colors = new float[4 * numVertices];
28+
normals = new float[3 * numVertices];
2529
indices = new int[numIndices];
2630

2731
for ( i = 0; i < numParallels + 1; i++ ){
@@ -34,6 +38,18 @@ private int createSphere(int numSlices, float radius){
3438
int texIndex = ( i * (numSlices + 1) + j ) * 2;
3539
texCoords[texIndex] = (float) j / (float) numSlices;
3640
texCoords[texIndex + 1] = 1.0f - ((float) i / (float) (numParallels));
41+
42+
// RGBA
43+
int colorIndex = ( i * (numSlices + 1) + j ) * 4;
44+
colors[colorIndex] = 1.0f;
45+
colors[colorIndex+1] = 1.0f;
46+
colors[colorIndex+2] = 1.0f;
47+
colors[colorIndex+3] = 1.0f;
48+
49+
normals[vertex] = 0;
50+
normals[vertex+1] = 0;
51+
normals[vertex+2] = 1;
52+
3753
}
3854
}
3955

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.asha.md360player4android.common;
2+
3+
import android.opengl.Matrix;
4+
5+
/**
6+
* Created by hzqiujiadi on 16/1/10.
7+
* hzqiujiadi ashqalcn@gmail.com
8+
*/
9+
public class GLKMatrixUtil {
10+
public static float GLKMathDegreesToRadians(float degrees) {
11+
return (float) (degrees * (Math.PI / 180));
12+
}
13+
14+
public static float GLKMathRadiansToDegrees(float angle) {
15+
return (float) (angle * 180 / Math.PI );
16+
}
17+
18+
public static void GLKMatrix4MakePerspective(float fovyRadians, float aspect, float nearZ, float farZ, float[] out) {
19+
float cotan = (float) (1.0f / Math.tan(fovyRadians / 2.0f));
20+
out[0] = cotan / aspect;
21+
out[1] = 0.0f;
22+
out[2] = 0.0f;
23+
out[3] = 0.0f;
24+
25+
out[4] = 0.0f;
26+
out[5] = cotan;
27+
out[6] = 0.0f;
28+
out[7] = 0.0f;
29+
30+
out[8] = 0.0f;
31+
out[9] = 0.0f;
32+
out[10] = (farZ + nearZ) / (nearZ - farZ);
33+
out[11] = -1.0f;
34+
35+
out[12] = 0.0f;
36+
out[13] = 0.0f;
37+
out[14] = (2.0f * farZ * nearZ) / (nearZ - farZ);
38+
out[15] = 0.0f;
39+
}
40+
41+
public static void GLKMatrix4Identity(float[] out){
42+
out[0] = 1.0f;
43+
out[1] = 0.0f;
44+
out[2] = 0.0f;
45+
out[3] = 0.0f;
46+
47+
out[4] = 0.0f;
48+
out[5] = 1.0f;
49+
out[6] = 0.0f;
50+
out[7] = 0.0f;
51+
52+
out[8] = 0.0f;
53+
out[9] = 0.0f;
54+
out[10] = 1.0f;
55+
out[11] = 0.0f;
56+
57+
out[12] = 0.0f;
58+
out[13] = 0.0f;
59+
out[14] = 0.0f;
60+
out[15] = 1.0f;
61+
}
62+
63+
public static float[] GLKMatrix4Scale(float[] m, float sx, float sy, float sz){
64+
float[] a = {m[0] * sx,m[1] * sx, m[2] * sx, m[3] * sx,
65+
m[4] * sy, m[5] * sy, m[6] * sy, m[7] * sy,
66+
m[8] * sz, m[9] * sz, m[10] * sz, m[11] * sz,
67+
m[12], m[13], m[14], m[15]};
68+
return a;
69+
}
70+
71+
public static void GLKMatrix4Multiply(float[] matrixLeft, float[] matrixRight, float[] out){
72+
Matrix.multiplyMM(out,0,matrixLeft,0,matrixRight,0);
73+
}
74+
75+
public static void GLKMatrix4Rotate(float[] matrix, float radians, float x, float y, float z){
76+
Matrix.rotateM(matrix,0,GLKMathRadiansToDegrees(radians),x,y,z);
77+
}
78+
}

app/src/main/java/com/asha/md360player4android/lesson1/LessonOneRenderer.java

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
import android.opengl.GLSurfaceView;
55
import android.opengl.Matrix;
66

7+
import com.asha.md360player4android.Sphere;
8+
79
import java.nio.FloatBuffer;
810
import java.nio.IntBuffer;
911

1012
import javax.microedition.khronos.egl.EGLConfig;
1113
import javax.microedition.khronos.opengles.GL10;
1214

15+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMathDegreesToRadians;
16+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4Identity;
17+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4MakePerspective;
18+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4Multiply;
19+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4Rotate;
20+
import static com.asha.md360player4android.common.GLKMatrixUtil.GLKMatrix4Scale;
21+
1322
/**
1423
* This class implements our custom renderer. Note that the GL10 parameter passed in is unused for OpenGL ES 2.0
1524
* renderers -- the static class GLES20 is used instead.
@@ -44,57 +53,51 @@ public class LessonOneRenderer implements GLSurfaceView.Renderer
4453
/** This will be used to pass in model color information. */
4554
private int mColorHandle;
4655

47-
/** How many bytes per float. */
48-
private final int mBytesPerFloat = 4;
49-
50-
/** How many elements per vertex. */
51-
private final int mStrideBytes = 7 * mBytesPerFloat;
52-
53-
/** Offset of the position data. */
54-
private final int mPositionOffset = 0;
55-
5656
/** Size of the position data in elements. */
5757
private final int mPositionDataSize = 3;
5858

59-
/** Offset of the color data. */
60-
private final int mColorOffset = 3;
6159

6260
/** Size of the color data in elements. */
6361
private final int mColorDataSize = 4;
6462

63+
private Sphere sphere = new Sphere(50,1.0f);
64+
6565
/**
6666
* Initialize the model data.
6767
*/
6868
public LessonOneRenderer()
6969
{
7070
// Define points for equilateral triangles.
7171
}
72-
72+
73+
74+
private static final float DEFAULT_OVERTURE = 85.0f;
75+
76+
77+
private float[] projectionMatrix = new float[16];
78+
private float[] modelViewMatrix = new float[16];
79+
//private float[] mMVPMatrix = new float[16];
80+
81+
public void update(){
82+
float aspect = 1.5f;
83+
GLKMatrix4MakePerspective(GLKMathDegreesToRadians(DEFAULT_OVERTURE),aspect, 0.1f, 400.0f,projectionMatrix);
84+
GLKMatrix4Rotate(projectionMatrix, (float) Math.PI, 1.0f, 0.0f, 0.0f);
85+
86+
GLKMatrix4Identity(modelViewMatrix);
87+
modelViewMatrix = GLKMatrix4Scale(modelViewMatrix, 300.0f, 300.0f, 300.0f);
88+
89+
GLKMatrix4Multiply(projectionMatrix,modelViewMatrix,mMVPMatrix);
90+
}
91+
92+
93+
7394
@Override
7495
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
7596
{
7697
// Set the background clear color to gray.
7798
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
78-
79-
// Position the eye behind the origin.
80-
final float eyeX = 0.0f;
81-
final float eyeY = 0.0f;
82-
final float eyeZ = 1.5f;
83-
84-
// We are looking toward the distance
85-
final float lookX = 0.0f;
86-
final float lookY = 0.0f;
87-
final float lookZ = -5.0f;
88-
89-
// Set our up vector. This is where our head would be pointing were we holding the camera.
90-
final float upX = 0.0f;
91-
final float upY = 1.0f;
92-
final float upZ = 0.0f;
93-
94-
// Set the view matrix. This matrix can be said to represent the camera position.
95-
// NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
96-
// view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
97-
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
99+
100+
update();
98101

99102
final String vertexShader =
100103
"uniform mat4 u_MVPMatrix; \n" // A constant representing the combined model/view/projection matrix.
@@ -119,7 +122,7 @@ public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
119122
// triangle per fragment.
120123
+ "void main() \n" // The entry point for our fragment shader.
121124
+ "{ \n"
122-
+ " gl_FragColor = v_Color; \n" // Pass the color directly through the pipeline.
125+
+ " gl_FragColor = v_Color; \n" // Pass the color directly through the pipeline.
123126
+ "} \n";
124127

125128
// Load in the vertex shader.
@@ -222,33 +225,19 @@ public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
222225
GLES20.glUseProgram(programHandle);
223226

224227

225-
initTriangle();
228+
initSphere();
226229
}
227230

228-
private void initTriangle() {
229-
// This triangle is red, green, and blue.
230-
final float[] triangle1VerticesData = {
231-
// X, Y, Z,
232-
-0.5f, -0.25f, 0.0f,
231+
private void initSphere() {
233232

234-
0.5f, -0.25f, 0.0f,
235-
236-
0.0f, 0.559016994f, 0.0f,
237-
};
238-
239-
final float[] colorVerticesData = {
240-
// R, G, B, A
241-
1.0f, 0.0f, 0.0f, 1.0f,
242-
243-
0.0f, 0.0f, 1.0f, 1.0f,
244-
245-
0.0f, 1.0f, 0.0f, 1.0f
246-
};
247233
IntBuffer vertexBufferId = IntBuffer.allocate(1);
248234
IntBuffer colorBufferId = IntBuffer.allocate(1);
249-
FloatBuffer vertexBuffer = FloatBuffer.wrap(triangle1VerticesData);
250-
FloatBuffer colorBuffer = FloatBuffer.wrap(colorVerticesData);
251-
int numVertices = 3;
235+
IntBuffer vertexIndicesBufferId = IntBuffer.allocate(1);
236+
FloatBuffer vertexBuffer = FloatBuffer.wrap(sphere.vertices);
237+
FloatBuffer colorBuffer = FloatBuffer.wrap(sphere.colors);
238+
IntBuffer indicesBuffer = IntBuffer.wrap(sphere.indices);
239+
int numVertices = sphere.numVertices;
240+
int numIndices = sphere.numIndices;
252241

253242
GLES20.glGenBuffers(1,vertexBufferId);
254243
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBufferId.get(0));
@@ -261,6 +250,10 @@ private void initTriangle() {
261250
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, numVertices*4*4, colorBuffer, GLES20.GL_DYNAMIC_DRAW);
262251
GLES20.glVertexAttribPointer(mColorHandle,mColorDataSize,GLES20.GL_FLOAT, false, 0, 0);
263252
GLES20.glEnableVertexAttribArray(mColorHandle);
253+
254+
GLES20.glGenBuffers(1,vertexIndicesBufferId);
255+
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,vertexIndicesBufferId.get(0));
256+
GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER,numIndices*4,indicesBuffer,GLES20.GL_STATIC_DRAW);
264257
}
265258

266259
@Override
@@ -300,11 +293,11 @@ public void onDrawFrame(GL10 glUnused)
300293
*/
301294
private void draw()
302295
{
303-
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
304-
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
296+
//Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
297+
//Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
305298
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
306299

307-
308-
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
300+
//GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,sphere.numVertices);
301+
GLES20.glDrawElements(GLES20.GL_TRIANGLES, sphere.numIndices,GLES20.GL_UNSIGNED_SHORT,0);
309302
}
310303
}

0 commit comments

Comments
 (0)