-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubeRenderer.java
108 lines (91 loc) · 3.89 KB
/
CubeRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cg.bouncycube;
import android.opengl.GLSurfaceView;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
/**
* Created by P17-17 on 3/6/2018.
*/
public class CubeRenderer implements GLSurfaceView.Renderer{
private Cube mCube;
private double mTransY;
private float mAngle;
public final static int SS_SUNLIGHT = GL10.GL_LIGHT0;
public CubeRenderer(){
mCube = new Cube();
}
protected static FloatBuffer makeFloatBuffer(float[] array)
{
ByteBuffer bb = ByteBuffer.allocateDirect(array.length*4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(array);
fb.position(0);
return fb;
}
public void initLighting(GL10 gl){
float[] green = {0.0f, 1.0f, 0.0f, 1.0f};
float[] red = {1.0f, 0.0f, 0.0f, 1.0f};
float[] blue = {0.0f, 0.0f, 1.0f, 1.0f};
float[] yellow = {1.0f, 1.0f, 0.0f, 1.0f};
float[] position = {0.0f, 5.0f, 0.0f, 1.0f};
float[] colorVector={0.2f,0.2f,0.2f, 1.0f};
float direction[]={1.0f,0.0f,0.0f};
//float[] position = {10.0f,0.0f,3.0f,1.0f};
gl.glLightfv(SS_SUNLIGHT, GL10.GL_POSITION, makeFloatBuffer(position));
gl.glLightfv(SS_SUNLIGHT, GL10.GL_DIFFUSE, makeFloatBuffer(red));
gl.glLightfv(SS_SUNLIGHT,GL10.GL_SPECULAR, makeFloatBuffer(red));
//gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, makeFloatBuffer(red));
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, makeFloatBuffer(red));
gl.glLightfv(SS_SUNLIGHT, GL10.GL_AMBIENT, makeFloatBuffer(blue));
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, makeFloatBuffer(blue));
gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, makeFloatBuffer(colorVector));
//gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, makeFloatBuffer(yellow));
gl.glLightf(SS_SUNLIGHT, GL10.GL_LINEAR_ATTENUATION, 0.025f);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPOT_DIRECTION, makeFloatBuffer(direction));
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(SS_SUNLIGHT);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0,0,0,0);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glDepthMask(false);
initLighting(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float fieldOfView = 10.0f/57.3f;
float aspectRatio = (float)width/(float)height;
float zNear = 0.6f;
float zFar = 1000;
float size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));
gl.glFrustumf(-size, size, -size/aspectRatio, size/aspectRatio, zNear, zFar);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, (float) Math.sin(mTransY), -20.0f);
gl.glScalef(1,1,1);
mTransY -= 0.075f;
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f);
gl.glRotatef(mAngle, 1.0f, 0.0f, 0.0f);
mAngle -= 0.4;
mCube.draw(gl);
}
}