Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateRenderer.java @ dc10a48d

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.inflate;
21

    
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

    
25
import org.distorted.library.effect.FragmentEffectAlpha;
26
import org.distorted.library.effect.MatrixEffectQuaternion;
27
import org.distorted.library.effect.MatrixEffectScale;
28
import org.distorted.library.effect.VertexEffectScale;
29
import org.distorted.library.main.DistortedEffects;
30
import org.distorted.library.main.DistortedLibrary;
31
import org.distorted.library.main.DistortedScreen;
32
import org.distorted.library.main.DistortedTexture;
33
import org.distorted.library.mesh.MeshBase;
34
import org.distorted.library.type.DynamicQuat;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static4D;
37
import org.distorted.library.type.Static3D;
38

    
39
import android.app.ActivityManager;
40
import android.content.Context;
41
import android.content.pm.ConfigurationInfo;
42
import android.content.res.Resources;
43
import android.opengl.GLSurfaceView;
44

    
45
import java.io.InputStream;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class InflateRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
50
{
51
    private static final float FOV = 30.0f;
52
    private static final float NEAR = 0.1f;
53

    
54
    private final GLSurfaceView mView;
55
    private final Resources mResources;
56
    private final DistortedTexture mTexture;
57
    private final DistortedEffects mEffects;
58
    private final MeshBase mMesh;
59
    private final DistortedScreen mScreen;
60
    private final float mObjWidth, mObjHeight, mObjDepth;
61
    private final Static3D mScale;
62
    private final Static1D mAlpha;
63

    
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
    InflateRenderer(GLSurfaceView v)
70
      {
71
      mView = v;
72
      mResources = v.getResources();
73

    
74
      mAlpha = new Static1D(1.0f);
75
      mScale= new Static3D(1,1,1);
76

    
77
      Static3D center=new Static3D(0,0,0);
78

    
79
      InflateActivity2 act = (InflateActivity2)v.getContext();
80

    
81
      mTexture = act.getTexture();
82
      mMesh    = act.getMesh();
83

    
84
      mObjWidth = act.getCols();
85
      mObjHeight= act.getRows();
86
      mObjDepth = act.getSlic();
87

    
88
      mQuat1 = new Static4D(0,0,0,1);  // unity
89
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
90
      
91
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
92
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
93

    
94
      quatInt1.add(mQuat1);
95
      quatInt2.add(mQuat2);
96

    
97
      mEffects = new DistortedEffects();
98
      mEffects.apply( new VertexEffectScale(new Static3D(mObjWidth,mObjHeight,mObjDepth) ) );
99
      mEffects.apply( new MatrixEffectScale(mScale));
100
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
101
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
102
      mEffects.apply( new FragmentEffectAlpha(mAlpha));
103

    
104
      mScreen = new DistortedScreen();
105
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
106
      mScreen.setProjection(FOV, NEAR);
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
   
111
    public void onDrawFrame(GL10 glUnused) 
112
      {
113
      mScreen.render( System.currentTimeMillis() );
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
119
      {
120
      final float SCALE = 0.75f;
121

    
122
      mScreenMin = Math.min(width, height);
123
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
124
      mScale.set(factor,factor,factor);
125
      mScreen.resize(width, height);
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
    void setRenderModeToOIT(boolean oit)
131
      {
132
      mScreen.setOrderIndependentTransparency(oit);
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
    void setTransparency(int level)
138
      {
139
      mAlpha.set((float)level/100.0f);
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
    float setLevel(int level)
145
      {
146
      float inflateLevel = (level-50)/50.0f;
147
      mMesh.setInflate(inflateLevel);
148

    
149
      return inflateLevel;
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
    
154
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
155
      {
156
      InflateActivity2 act = (InflateActivity2)mView.getContext();
157

    
158
      mTexture.setTexture( act.getBitmap() );
159
      mScreen.detachAll();
160
      mScreen.attach(mTexture,mEffects,mMesh);
161

    
162
      VertexEffectScale.enable();
163
      FragmentEffectAlpha.enable();
164

    
165
      DistortedLibrary.onSurfaceCreated(this);
166
      }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
    public void distortedException(Exception ex)
171
      {
172
      android.util.Log.e("Inflate", ex.getMessage() );
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
    public int openGlVersion()
178
      {
179
      Context context = mView.getContext();
180
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
181
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
182
      int glESversion = configurationInfo.reqGlEsVersion;
183
      int major = glESversion >> 16;
184
      int minor = glESversion & 0xff;
185

    
186
      return 100*major + 10*minor;
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    public InputStream localFile(int fileID)
192
      {
193
      return mResources.openRawResource(fileID);
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    public void logMessage(String message)
199
      {
200
      android.util.Log.e("Inflate", message );
201
      }
202
}
(4-4/5)