Project

General

Profile

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

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

1 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 77a500b3 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 77a500b3 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 77a500b3 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.inflate;
21
22 64558e4e Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24 77a500b3 Leszek Koltunski
25 64558e4e Leszek Koltunski
import org.distorted.library.effect.FragmentEffectAlpha;
26 77a500b3 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
27
import org.distorted.library.effect.MatrixEffectScale;
28 e3c87517 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
29 77a500b3 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
30 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
31 77a500b3 Leszek Koltunski
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 64558e4e Leszek Koltunski
import org.distorted.library.type.Static1D;
36 77a500b3 Leszek Koltunski
import org.distorted.library.type.Static4D;
37 64558e4e Leszek Koltunski
import org.distorted.library.type.Static3D;
38 77a500b3 Leszek Koltunski
39 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
40
import android.content.Context;
41
import android.content.pm.ConfigurationInfo;
42
import android.content.res.Resources;
43 64558e4e Leszek Koltunski
import android.opengl.GLSurfaceView;
44 77a500b3 Leszek Koltunski
45 dc10a48d Leszek Koltunski
import java.io.InputStream;
46
47 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 dc10a48d Leszek Koltunski
class InflateRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
50 77a500b3 Leszek Koltunski
{
51 0579fe3a Leszek Koltunski
    private static final float FOV = 30.0f;
52
    private static final float NEAR = 0.1f;
53
54 dc10a48d Leszek Koltunski
    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 77a500b3 Leszek Koltunski
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
    InflateRenderer(GLSurfaceView v)
70
      {
71
      mView = v;
72 dc10a48d Leszek Koltunski
      mResources = v.getResources();
73 77a500b3 Leszek Koltunski
74 64558e4e Leszek Koltunski
      mAlpha = new Static1D(1.0f);
75 77a500b3 Leszek Koltunski
      mScale= new Static3D(1,1,1);
76 16b22aab Leszek Koltunski
77
      Static3D center=new Static3D(0,0,0);
78 77a500b3 Leszek Koltunski
79
      InflateActivity2 act = (InflateActivity2)v.getContext();
80
81
      mTexture = act.getTexture();
82 687263cc Leszek Koltunski
      mMesh    = act.getMesh();
83 77a500b3 Leszek Koltunski
84 e3c87517 Leszek Koltunski
      mObjWidth = act.getCols();
85
      mObjHeight= act.getRows();
86
      mObjDepth = act.getSlic();
87 77a500b3 Leszek Koltunski
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 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
98 e3c87517 Leszek Koltunski
      mEffects.apply( new VertexEffectScale(new Static3D(mObjWidth,mObjHeight,mObjDepth) ) );
99 dae661e9 Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(mScale));
100 16b22aab Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
101 ea9b68db Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
102 64558e4e Leszek Koltunski
      mEffects.apply( new FragmentEffectAlpha(mAlpha));
103 77a500b3 Leszek Koltunski
104
      mScreen = new DistortedScreen();
105
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
106 0579fe3a Leszek Koltunski
      mScreen.setProjection(FOV, NEAR);
107 77a500b3 Leszek Koltunski
      }
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 a4d59c0b Leszek Koltunski
      final float SCALE = 0.75f;
121
122 e3c87517 Leszek Koltunski
      mScreenMin = Math.min(width, height);
123 a4d59c0b Leszek Koltunski
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
124 77a500b3 Leszek Koltunski
      mScale.set(factor,factor,factor);
125
      mScreen.resize(width, height);
126
      }
127
128 64558e4e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e3c87517 Leszek Koltunski
      VertexEffectScale.enable();
163 64558e4e Leszek Koltunski
      FragmentEffectAlpha.enable();
164
165 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
166 061449ed Leszek Koltunski
      }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170
    public void distortedException(Exception ex)
171
      {
172
      android.util.Log.e("Inflate", ex.getMessage() );
173 77a500b3 Leszek Koltunski
      }
174 dc10a48d Leszek Koltunski
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
    public InputStream localFile(int fileID)
178
      {
179
      return mResources.openRawResource(fileID);
180
      }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
    public void logMessage(String message)
185
      {
186
      android.util.Log.e("Inflate", message );
187
      }
188 77a500b3 Leszek Koltunski
}