-
Notifications
You must be signed in to change notification settings - Fork 1
/
SmashoutGLRenderer.java
141 lines (108 loc) · 4.55 KB
/
SmashoutGLRenderer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.supernovamobile.smashout;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import android.content.Context;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
public class SmashoutGLRenderer implements Renderer {
private SMGroup mScene;
private SMModel mCube;
private SMModel mCube2;
private Context mContext;
private boolean light = false;
private float[] mLightAmbient = {0.5f, 0.5f, 0.5f, 1.0f};
private float[] mLightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};
private float[] mLightPosition = {0.0f, 0.0f, 2.0f, 1.0f};
private FloatBuffer mLightAmbientBuffer;
private FloatBuffer mLightDiffuseBuffer;
private FloatBuffer mLightPositionBuffer;
public SmashoutGLRenderer(Context context)
throws IOException, ParserConfigurationException, SAXException
{
mContext = context;
mScene = new SMGroup();
mCube = new SMModel(R.raw.cube, context, "Cube_1");
//mCube2 = new Model(R.raw.sub, context, "Cube_2");
mScene.add(mCube);
//mScene.add(mCube2);
mCube.setRx(45.0f);
ByteBuffer byteBuf = ByteBuffer.allocateDirect(mLightAmbient.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mLightAmbientBuffer = byteBuf.asFloatBuffer();
mLightAmbientBuffer.put(mLightAmbient);
mLightAmbientBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(mLightDiffuse.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mLightDiffuseBuffer = byteBuf.asFloatBuffer();
mLightDiffuseBuffer.put(mLightDiffuse);
mLightDiffuseBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(mLightPosition.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mLightPositionBuffer = byteBuf.asFloatBuffer();
mLightPositionBuffer.put(mLightPosition);
mLightPositionBuffer.position(0);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -7.0f);
//Check if the light flag has been set to enable/disable lighting
light = true;
if(light) {
gl.glEnable(GL10.GL_LIGHTING);
} else {
gl.glDisable(GL10.GL_LIGHTING);
}
//mCube.setRx(mCube.getRx() + 1);
mCube.setRy(mCube.getRy() + 5);
gl.glPushMatrix();
//mCube2.setX(5.0f);
try {
mScene.draw(gl);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gl.glPopMatrix();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, mLightAmbientBuffer); //Setup The Ambient Light
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, mLightDiffuseBuffer); //Setup The Diffuse Light
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, mLightPositionBuffer); //Position The Light
gl.glEnable(GL10.GL_LIGHT0);
//Settings
gl.glDisable(GL10.GL_DITHER); //Disable dithering
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.5f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
try {
mScene.loadTextures(gl, mContext);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}