Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateRenderer.java @ 16b22aab

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.main.DistortedEffects;
29
import org.distorted.library.main.DistortedScreen;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.mesh.MeshBase;
32
import org.distorted.library.type.DynamicQuat;
33
import org.distorted.library.type.Static1D;
34
import org.distorted.library.type.Static4D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.main.Distorted;
37

    
38
import android.opengl.GLSurfaceView;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
class InflateRenderer implements GLSurfaceView.Renderer
43
{
44
    private static final float FOV = 30.0f;
45
    private static final float NEAR = 0.1f;
46

    
47
    private GLSurfaceView mView;
48
    private DistortedTexture mTexture;
49
    private DistortedEffects mEffects;
50
    private MeshBase mMesh;
51
    private DistortedScreen mScreen;
52
    private int mObjWidth, mObjHeight;
53
    private Static3D mScale;
54
    private Static1D mAlpha;
55

    
56
    Static4D mQuat1, mQuat2;
57
    int mScreenMin;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
    InflateRenderer(GLSurfaceView v)
62
      {
63
      mView = v;
64

    
65
      mAlpha = new Static1D(1.0f);
66

    
67
      mScale= new Static3D(1,1,1);
68

    
69
      Static3D center=new Static3D(0,0,0);
70

    
71
      InflateActivity2 act = (InflateActivity2)v.getContext();
72

    
73
      mTexture = act.getTexture();
74
      mMesh = act.getMesh();
75

    
76
      mObjWidth = mTexture.getWidth();
77
      mObjHeight= mTexture.getHeight();
78

    
79
      mQuat1 = new Static4D(0,0,0,1);  // unity
80
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
81
      
82
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
83
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
84

    
85
      quatInt1.add(mQuat1);
86
      quatInt2.add(mQuat2);
87

    
88
      mEffects = new DistortedEffects();
89
      mEffects.apply( new MatrixEffectScale(mScale));
90
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
91
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
92
      mEffects.apply( new FragmentEffectAlpha(mAlpha));
93

    
94
      mScreen = new DistortedScreen();
95
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
96
      mScreen.setProjection(FOV, NEAR);
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
   
101
    public void onDrawFrame(GL10 glUnused) 
102
      {
103
      mScreen.render( System.currentTimeMillis() );
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
109
      {
110
      final float SCALE = 0.75f;
111

    
112
      mScreenMin = width<height ? width:height;
113
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
114
      mScale.set(factor,factor,factor);
115
      mScreen.resize(width, height);
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    void setRenderModeToOIT(boolean oit)
121
      {
122
      mScreen.setOrderIndependentTransparency(oit);
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    void setTransparency(int level)
128
      {
129
      mAlpha.set((float)level/100.0f);
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
    float setLevel(int level)
135
      {
136
      float inflateLevel = (level-50)/50.0f;
137
      mMesh.setInflate(inflateLevel);
138

    
139
      return inflateLevel;
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
145
      {
146
      InflateActivity2 act = (InflateActivity2)mView.getContext();
147

    
148
      mTexture.setTexture( act.getBitmap() );
149
      mScreen.detachAll();
150
      mScreen.attach(mTexture,mEffects,mMesh);
151

    
152
      FragmentEffectAlpha.enable();
153

    
154
      try
155
        {
156
        Distorted.onCreate(act);
157
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("Inflate", ex.getMessage() );
161
        }
162
      }
163
}
(3-3/4)