1 | /* |
2 | * Copyright (C) 2009 Pierre H�bert <pierrox@pierrox.net> |
3 | * http://www.pierrox.net/mcompass/ |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | * you may not use this file except in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | |
18 | package net.pierrox.mcompass; |
19 | |
20 | import javax.microedition.khronos.egl.EGL10; |
21 | import javax.microedition.khronos.egl.EGLConfig; |
22 | import javax.microedition.khronos.opengles.GL10; |
23 | import android.opengl.GLSurfaceView; |
24 | |
25 | class CompassRenderer implements GLSurfaceView.Renderer { |
26 | private Turntable mTurnTable; |
27 | |
28 | private float mAzimuth; |
29 | private float mPitch=-35; |
30 | private float mRoll=-10; |
31 | |
32 | public CompassRenderer() { |
33 | mTurnTable = new Turntable(); |
34 | } |
35 | |
36 | public int[] getConfigSpec() { |
37 | int[] configSpec = { |
38 | EGL10.EGL_DEPTH_SIZE, 16, |
39 | EGL10.EGL_NONE |
40 | }; |
41 | return configSpec; |
42 | } |
43 | |
44 | public void onSurfaceChanged(GL10 gl, int width, int height) { |
45 | gl.glViewport(0, 0, width, height); |
46 | |
47 | float ratio = (float) width / height; |
48 | gl.glMatrixMode(GL10.GL_PROJECTION); |
49 | gl.glLoadIdentity(); |
50 | gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); |
51 | } |
52 | |
53 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { |
54 | gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); |
55 | |
56 | gl.glClearColorx(1<<16, 1<<16, 1<<16, 1<<16); |
57 | |
58 | gl.glEnable(GL10.GL_CULL_FACE); |
59 | gl.glShadeModel(GL10.GL_SMOOTH); |
60 | gl.glEnable(GL10.GL_DEPTH_TEST); |
61 | gl.glEnable(GL10.GL_NORMALIZE); |
62 | gl.glEnable(GL10.GL_COLOR_MATERIAL); |
63 | |
64 | gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); |
65 | gl.glEnable(GL10.GL_BLEND); |
66 | |
67 | float lightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; |
68 | float lightDiffuse[] = { 0.9f, 0.9f, 0.9f, 1.0f }; |
69 | float lightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
70 | float lightPosition[] = { 0f, 0f, 3f, 0.0f }; |
71 | float lightDirection[] = { 0f, 0f, -1f }; |
72 | |
73 | gl.glEnable(GL10.GL_LIGHTING); |
74 | gl.glEnable(GL10.GL_LIGHT0); |
75 | |
76 | gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0); |
77 | gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0); |
78 | gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightSpecular, 0); |
79 | gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition, 0); |
80 | gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPOT_DIRECTION, lightDirection, 0); |
81 | gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_CUTOFF, 1.2f); |
82 | gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_EXPONENT, 20.0f); |
83 | |
84 | mTurnTable.buildTextures(gl); |
85 | } |
86 | |
87 | public void onDrawFrame(GL10 gl) { |
88 | gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); |
89 | |
90 | gl.glMatrixMode(GL10.GL_MODELVIEW); |
91 | gl.glLoadIdentity(); |
92 | |
93 | gl.glTranslatef(0f, 0f, -3f); |
94 | |
95 | gl.glRotatef(mPitch+90, 1, 0, 0); |
96 | gl.glRotatef(-mRoll, 0, 0, 1); |
97 | gl.glRotatef(mAzimuth+180, 0, 1, 0); |
98 | |
99 | gl.glTranslatef(0f, 0.7f, 0f); |
100 | |
101 | mTurnTable.draw(gl); |
102 | } |
103 | |
104 | public void setOrientation(float azimuth, float pitch, float roll) { |
105 | mAzimuth=azimuth; |
106 | mPitch=pitch; |
107 | mRoll=roll; |
108 | } |
109 | |
110 | public void setParameters(int detailsLevel, boolean reversedRing) { |
111 | mTurnTable.setDetailsLevel(detailsLevel); |
112 | mTurnTable.setReversedRing(reversedRing); |
113 | } |
114 | } |