Project

General

Profile

Download (7.64 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 1af8e143

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.meshfile;
21

    
22
import android.content.Context;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28

    
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedLibrary;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.mesh.MeshBase;
36
import org.distorted.library.mesh.MeshFile;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
import java.io.DataInputStream;
42
import java.io.IOException;
43
import java.io.InputStream;
44

    
45
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class MeshFileRenderer implements GLSurfaceView.Renderer
51
{
52
    private GLSurfaceView mView;
53
    private DistortedTexture mTexture;
54
    private DistortedScreen mScreen;
55
    private DistortedEffects mEffects;
56
    private Static3D mScale;
57
    private MeshBase mMesh;
58

    
59
    Static4D mQuat1, mQuat2;
60
    int mScreenMin;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
    MeshFileRenderer(GLSurfaceView v)
65
      {
66
      mView = v;
67
      mScreen = new DistortedScreen();
68
      mScale= new Static3D(1,1,1);
69
      Static3D center=new Static3D(0,0,0);
70

    
71
      mQuat1 = new Static4D(0,0,0,1);
72
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
73

    
74
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
75
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
76

    
77
      quatInt1.add(mQuat1);
78
      quatInt2.add(mQuat2);
79

    
80
      mEffects = new DistortedEffects();
81
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
82
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
83
      mEffects.apply( new MatrixEffectScale(mScale));
84

    
85
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
   
90
    public void onDrawFrame(GL10 glUnused) 
91
      {
92
      mScreen.render( System.currentTimeMillis() );
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
98
      {
99
      final float SCALE = 0.3f;
100
      mScreenMin = Math.min(width, height);
101
      float factor = SCALE*mScreenMin;
102
      mScale.set(factor,factor,factor);
103
      mScreen.resize(width, height);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
109
      {
110
      if( mTexture==null ) mTexture = new DistortedTexture();
111
      mTexture.setTexture( createTexture(0) );
112

    
113
      try
114
        {
115
        DistortedLibrary.onCreate(mView.getContext());
116
        }
117
      catch(Exception ex)
118
        {
119
        android.util.Log.e("MeshFile", ex.getMessage() );
120
        }
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
    void open(int resourceID)
126
      {
127
      mMesh = createMesh(resourceID);
128

    
129
      mScreen.detachAll();
130
      mScreen.attach(mTexture,mEffects,mMesh);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    private Bitmap createTexture(int resourceID)
136
      {
137
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
138
      final int FACES=FACE_COLORS.length;
139
      int SIZE = 200;
140
      float STROKE = 0.05f*SIZE;
141
      float OFF = STROKE/2 -1;
142
      float OFF2 = 0.5f*SIZE + OFF;
143
      float HEIGHT = SIZE - OFF;
144
      float RADIUS = SIZE/12;
145
      float ARC1_H = 0.2f*SIZE;
146
      float ARC1_W = SIZE*0.5f;
147
      float ARC2_W = 0.153f*SIZE;
148
      float ARC2_H = 0.905f*SIZE;
149
      float ARC3_W = SIZE-ARC2_W;
150

    
151
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
152
      Canvas canvas = new Canvas(result);
153
      Paint paint = new Paint();
154
      paint.setAntiAlias(true);
155
      paint.setStrokeWidth(STROKE);
156

    
157
      for(int i=0; i<FACES; i++)
158
        {
159
        paint.setColor(FACE_COLORS[i]);
160
        paint.setStyle(Paint.Style.FILL);
161

    
162
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
163

    
164
        paint.setColor(0xff000000);
165
        paint.setStyle(Paint.Style.STROKE);
166

    
167
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
168
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
169
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
170

    
171
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
172
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
173
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
174
        }
175

    
176
      return result;
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    private MeshBase createMesh(int resourceID)
182
      {
183
      Context con = mView.getContext();
184
      Resources res = con.getResources();
185
      InputStream is = res.openRawResource(resourceID);
186
      DataInputStream dos = new DataInputStream(is);
187
      MeshBase mesh = new MeshFile(dos);
188

    
189
      try
190
        {
191
        is.close();
192
        }
193
      catch(IOException e)
194
        {
195
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
196
        }
197

    
198
      return mesh;
199
      }
200
}
(2-2/3)