From http://www.jwz.org/xscreensaver/xscreensaver-5.34.tar.gz
[xscreensaver] / android / project / xscreensaver / src / org / jwz / xscreensaver / ARenderer.java
1 package org.jwz.xscreensaver;
2
3 import javax.microedition.khronos.egl.EGLConfig;
4 import javax.microedition.khronos.opengles.GL10;
5
6 import net.rbgrn.android.glwallpaperservice.*;
7 import android.opengl.GLU;
8
9 import android.content.Context;
10 import android.content.SharedPreferences;
11
12
13 // Original code provided by Robert Green
14 // http://www.rbgrn.net/content/354-glsurfaceview-adapted-3d-live-wallpapers
15 public class ARenderer implements GLWallpaperService.Renderer {
16
17     CallNative cn;
18     boolean secondRun = false;
19
20     // Used for Lighting
21     //private static float[] ambientComponent0 = {0.3f, 0.3f, 1.0f, 1.0f};
22     //private static float[] ambientComponent0 = {0.3f, 0.3f, 0.3f, 1.0f};
23     private static float[] ambientComponent0 = {0.5f, 0.5f, 0.5f, 1.0f};
24     private static float[] diffuseComponent0 = {1.0f, 1.0f, 1.0f, 1.0f};
25     private static float[] lightPosition0 =    {1f, 1f, -1f, 0f};
26
27     public void onDrawFrame(GL10 gl) {
28         if (!secondRun) {
29             secondRun = true;
30             return;
31         }
32         //gl.glClearColor(0.2f, 0.6f, 0.2f, 1f);
33         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
34     }
35
36     public void onSurfaceChanged(GL10 gl, int width, int height) {
37
38         gl.glMatrixMode(GL10.GL_PROJECTION);
39         gl.glLoadIdentity();
40         GLU.gluPerspective(gl, 60f, (float)width/(float)height, 1f, 100f);
41
42         gl.glMatrixMode(GL10.GL_MODELVIEW);
43         gl.glLoadIdentity();
44
45         gl.glMatrixMode(GL10.GL_MODELVIEW);
46         gl.glTranslatef(0, 0, -5);
47         gl.glRotatef(30f, 1, 0, 0);
48
49         gl.glEnable(GL10.GL_LIGHTING);
50         gl.glEnable(GL10.GL_RESCALE_NORMAL);
51         gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
52 //Set the color of light bouncing off of surfaces to respect the surface color
53         gl.glEnable(GL10.GL_COLOR_MATERIAL);
54
55         setupLightSources(gl);
56
57 // Turn on a global ambient light. The "Cosmic Background Radiation", if you will.
58         //float[] ambientLightRGB = {0.3f, 0.3f, 0.3f, 1.0f};
59         float[] ambientLightRGB = {0.5f, 0.5f, 0.5f, 1.0f};
60         gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, ambientLightRGB, 0);
61         NonSurfaceChanged(width, height);
62     }
63
64     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
65
66         if (!secondRun) {
67             return;
68         }
69         cn = new CallNative();
70
71         gl.glClearDepthf(1f);
72         gl.glEnable(GL10.GL_DEPTH_TEST);
73         gl.glDepthFunc(GL10.GL_LEQUAL);
74
75 //Turn on culling, so OpenGL only draws one side of the primitives
76         gl.glEnable(GL10.GL_CULL_FACE);
77 //Define the front of a primitive to be the side where the listed vertexes are counterclockwise
78         gl.glFrontFace(GL10.GL_CCW);
79 //Do not draw the backs of primitives
80         gl.glCullFace(GL10.GL_BACK);
81
82     }
83
84     private void setupLightSources(GL10 gl) {
85         if (!secondRun) {
86             return;
87         }
88         //Enable Light source 0
89         gl.glEnable(GL10.GL_LIGHT0);
90
91         //Useful part of the Arrays start a 0
92         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientComponent0, 0);
93         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, diffuseComponent0, 0);
94
95         //Position the light in the scene
96         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition0, 0);
97     }
98
99     /**
100      * Called when the engine is destroyed. Do any necessary clean up because
101      * at this point your renderer instance is now done for.
102      */
103     public void release() {
104         NonDone();
105
106     }
107
108     public void NonSurfaceCreated() {
109         cn.nativeInit();
110     }
111
112     void NonSurfaceChanged(int w, int h) {
113         if (!secondRun) {
114             return;
115         }
116         cn.nativeResize(w, h);
117     }
118
119     void NonDrawFrame() {
120         cn.nativeRender();
121     }
122
123     void NonDone() {
124         cn.nativeDone();
125     }
126
127     static
128     {
129         System.loadLibrary ("xscreensaver");
130     }
131
132 }