1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package com.android.film;
19
20import java.io.Writer;
21import java.lang.Math;
22import android.util.Log;
23
24import android.renderscript.RenderScript;
25import android.renderscript.SimpleMesh;
26
27
28class FilmStripMesh {
29
30    class Vertex {
31        float nx;
32        float ny;
33        float nz;
34        float s;
35        float t;
36        float x;
37        float y;
38        float z;
39
40        Vertex() {
41            nx = 0;
42            ny = 0;
43            nz = 0;
44            s = 0;
45            t = 0;
46            x = 0;
47            y = 0;
48            z = 0;
49        }
50
51        void xyz(float _x, float _y, float _z) {
52            x = _x;
53            y = _y;
54            z = _z;
55        }
56
57        void nxyz(float _x, float _y, float _z) {
58            nx = _x;
59            ny = _y;
60            nz = _z;
61        }
62
63        void st(float _s, float _t) {
64            s = _s;
65            t = _t;
66        }
67
68        void computeNorm(Vertex v1, Vertex v2) {
69            float dx = v1.x - v2.x;
70            float dy = v1.y - v2.y;
71            float dz = v1.z - v2.z;
72            float len = (float)java.lang.Math.sqrt(dx*dx + dy*dy + dz*dz);
73            dx /= len;
74            dy /= len;
75            dz /= len;
76
77            nx = dx * dz;
78            ny = dy * dz;
79            nz = (float)java.lang.Math.sqrt(dx*dx + dy*dy);
80
81            len = (float)java.lang.Math.sqrt(nx*nx + ny*ny + nz*nz);
82            nx /= len;
83            ny /= len;
84            nz /= len;
85        }
86    }
87
88    int[] mTriangleOffsets;
89    float[] mTriangleOffsetsTex;
90    int mTriangleOffsetsCount;
91
92    SimpleMesh init(RenderScript rs)
93    {
94        float vtx[] = new float[] {
95            60.431003f, 124.482050f,
96            60.862074f, 120.872604f,
97            61.705303f, 117.336662f,
98            62.949505f, 113.921127f,
99            64.578177f, 110.671304f,
100            66.569716f, 107.630302f,
101            68.897703f, 104.838457f,
102            71.531259f, 102.332803f,
103            74.435452f, 100.146577f,
104            77.571757f, 98.308777f,
105            80.898574f, 96.843781f,
106            84.371773f, 95.771023f,
107            87.945283f, 95.104731f,
108            98.958994f, 95.267098f,
109            109.489523f, 98.497596f,
110            118.699582f, 104.539366f,
111            125.856872f, 112.912022f,
112            130.392311f, 122.949849f,
113            131.945283f, 133.854731f,
114            130.392311f, 144.759613f,
115            125.856872f, 154.797439f,
116            118.699582f, 163.170096f,
117            109.489523f, 169.211866f,
118            98.958994f, 172.442364f,
119            87.945283f, 172.604731f,
120            72.507313f, 172.672927f,
121            57.678920f, 168.377071f,
122            44.668135f, 160.067134f,
123            34.534908f, 148.420104f,
124            28.104767f, 134.384831f,
125            25.901557f, 119.104731f,
126            28.104767f, 103.824631f,
127            34.534908f, 89.789358f,
128            44.668135f, 78.142327f,
129            57.678920f, 69.832390f,
130            72.507313f, 65.536534f,
131            87.945283f, 65.604731f,
132            106.918117f, 65.688542f,
133            125.141795f, 60.409056f,
134            141.131686f, 50.196376f,
135            153.585137f, 35.882502f,
136            161.487600f, 18.633545f,
137            164.195283f, -0.145269f,
138            161.487600f, -18.924084f,
139            153.585137f, -36.173040f,
140            141.131686f, -50.486914f,
141            125.141795f, -60.699594f,
142            106.918117f, -65.979081f,
143            87.945283f, -65.895269f,
144            80f, -65.895269f,
145            60f, -65.895269f,
146            40f, -65.895269f,
147            20f, -65.895269f,
148            0f, -65.895269f,
149            -20f, -65.895269f,
150            -40f, -65.895269f,
151            -60f, -65.895269f,
152            -80f, -65.895269f,
153            -87.945283f, -65.895269f,
154            -106.918117f, -65.979081f,
155            -125.141795f, -60.699594f,
156            -141.131686f, -50.486914f,
157            -153.585137f, -36.173040f,
158            -161.487600f, -18.924084f,
159            -164.195283f, -0.145269f,
160            -161.487600f, 18.633545f,
161             -153.585137f, 35.882502f,
162             -141.131686f, 50.196376f,
163             -125.141795f, 60.409056f,
164             -106.918117f, 65.688542f,
165             -87.945283f, 65.604731f,
166             -72.507313f, 65.536534f,
167             -57.678920f, 69.832390f,
168             -44.668135f, 78.142327f,
169             -34.534908f, 89.789358f,
170             -28.104767f, 103.824631f,
171             -25.901557f, 119.104731f,
172             -28.104767f, 134.384831f,
173             -34.534908f, 148.420104f,
174             -44.668135f, 160.067134f,
175             -57.678920f, 168.377071f,
176             -72.507313f, 172.672927f,
177             -87.945283f, 172.604731f,
178             -98.958994f, 172.442364f,
179             -109.489523f, 169.211866f,
180             -118.699582f, 163.170096f,
181             -125.856872f, 154.797439f,
182             -130.392311f, 144.759613f,
183             -131.945283f, 133.854731f,
184             -130.392311f, 122.949849f,
185             -125.856872f, 112.912022f,
186             -118.699582f, 104.539366f,
187             -109.489523f, 98.497596f,
188             -98.958994f, 95.267098f,
189             -87.945283f, 95.104731f,
190             -84.371773f, 95.771023f,
191             -80.898574f, 96.843781f,
192             -77.571757f, 98.308777f,
193             -74.435452f, 100.146577f,
194             -71.531259f, 102.332803f,
195             -68.897703f, 104.838457f,
196             -66.569716f, 107.630302f,
197             -64.578177f, 110.671304f,
198             -62.949505f, 113.921127f,
199             -61.705303f, 117.336662f,
200             -60.862074f, 120.872604f,
201             -60.431003f, 124.482050f
202        };
203
204
205        mTriangleOffsets = new int[64];
206        mTriangleOffsetsTex = new float[64];
207
208        mTriangleOffsets[0] = 0;
209        mTriangleOffsetsCount = 1;
210
211        Vertex t = new Vertex();
212        t.nxyz(1, 0, 0);
213        int count = vtx.length / 2;
214
215        SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(
216            rs, 3,
217            SimpleMesh.TriangleMeshBuilder.NORMAL | SimpleMesh.TriangleMeshBuilder.TEXTURE_0);
218
219        float runningS = 0;
220        for (int ct=0; ct < (count-1); ct++) {
221            t.x = -vtx[ct*2] / 100.f;
222            t.z = vtx[ct*2+1] / 100.f;
223            t.s = runningS;
224            t.nx =  (vtx[ct*2+3] - vtx[ct*2 +1]);
225            t.ny =  (vtx[ct*2+2] - vtx[ct*2   ]);
226            float len = (float)java.lang.Math.sqrt(t.nx * t.nx + t.ny * t.ny);
227            runningS += len / 100;
228            t.nx /= len;
229            t.ny /= len;
230            t.y = -0.5f;
231            t.t = 0;
232            tm.setNormal(t.nx, t.ny, t.nz);
233            tm.setTexture(t.s, t.t);
234            tm.addVertex(t.x, t.y, t.z);
235            //android.util.Log.e("rs", "vtx x="+t.x+" y="+t.y+" z="+t.z+" s="+t.s+" t="+t.t);
236            t.y = .5f;
237            t.t = 1;
238            tm.setTexture(t.s, t.t);
239            tm.addVertex(t.x, t.y, t.z);
240            //android.util.Log.e("rs", "vtx x="+t.x+" y="+t.y+" z="+t.z+" s="+t.s+" t="+t.t);
241
242            if((runningS*2) > mTriangleOffsetsCount) {
243                mTriangleOffsets[mTriangleOffsetsCount] = ct*2 * 3;
244                mTriangleOffsetsTex[mTriangleOffsetsCount] = t.s;
245                mTriangleOffsetsCount ++;
246            }
247        }
248
249        count = (count * 2 - 2);
250        for (int ct=0; ct < (count-2); ct+= 2) {
251            tm.addTriangle(ct, ct+1, ct+2);
252            tm.addTriangle(ct+1, ct+3, ct+2);
253        }
254        return tm.create();
255    }
256
257
258}
259
260