Project

General

Profile

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

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

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
import org.distorted.library.main.DistortedEffects;
29 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
30 77a500b3 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.mesh.MeshBase;
33
import org.distorted.library.type.DynamicQuat;
34 64558e4e Leszek Koltunski
import org.distorted.library.type.Static1D;
35 77a500b3 Leszek Koltunski
import org.distorted.library.type.Static4D;
36 64558e4e Leszek Koltunski
import org.distorted.library.type.Static3D;
37 77a500b3 Leszek Koltunski
38 64558e4e Leszek Koltunski
import android.opengl.GLSurfaceView;
39 77a500b3 Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42
class InflateRenderer implements GLSurfaceView.Renderer
43
{
44 0579fe3a Leszek Koltunski
    private static final float FOV = 30.0f;
45
    private static final float NEAR = 0.1f;
46
47 77a500b3 Leszek Koltunski
    private GLSurfaceView mView;
48
    private DistortedTexture mTexture;
49
    private DistortedEffects mEffects;
50
    private MeshBase mMesh;
51
    private DistortedScreen mScreen;
52 698ad0a8 Leszek Koltunski
    private float mObjWidth, mObjHeight, mObjDepth;
53 16b22aab Leszek Koltunski
    private Static3D mScale;
54 64558e4e Leszek Koltunski
    private Static1D mAlpha;
55 77a500b3 Leszek Koltunski
56
    Static4D mQuat1, mQuat2;
57
    int mScreenMin;
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
    InflateRenderer(GLSurfaceView v)
62
      {
63
      mView = v;
64
65 64558e4e Leszek Koltunski
      mAlpha = new Static1D(1.0f);
66
67 77a500b3 Leszek Koltunski
      mScale= new Static3D(1,1,1);
68 16b22aab Leszek Koltunski
69
      Static3D center=new Static3D(0,0,0);
70 77a500b3 Leszek Koltunski
71
      InflateActivity2 act = (InflateActivity2)v.getContext();
72
73
      mTexture = act.getTexture();
74 687263cc Leszek Koltunski
      mMesh    = act.getMesh();
75 77a500b3 Leszek Koltunski
76 698ad0a8 Leszek Koltunski
      mObjWidth = mMesh.getStretchX();
77
      mObjHeight= mMesh.getStretchY();
78
      mObjDepth = mMesh.getStretchZ();
79 77a500b3 Leszek Koltunski
80
      mQuat1 = new Static4D(0,0,0,1);  // unity
81
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
82
      
83
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
84
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
85
86
      quatInt1.add(mQuat1);
87
      quatInt2.add(mQuat2);
88
89 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
90 dae661e9 Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(mScale));
91 16b22aab Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
92 ea9b68db Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
93 64558e4e Leszek Koltunski
      mEffects.apply( new FragmentEffectAlpha(mAlpha));
94 77a500b3 Leszek Koltunski
95
      mScreen = new DistortedScreen();
96
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
97 0579fe3a Leszek Koltunski
      mScreen.setProjection(FOV, NEAR);
98 77a500b3 Leszek Koltunski
      }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
   
102
    public void onDrawFrame(GL10 glUnused) 
103
      {
104
      mScreen.render( System.currentTimeMillis() );
105
      }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
110
      {
111 a4d59c0b Leszek Koltunski
      final float SCALE = 0.75f;
112
113 77a500b3 Leszek Koltunski
      mScreenMin = width<height ? width:height;
114 a4d59c0b Leszek Koltunski
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
115 77a500b3 Leszek Koltunski
      mScale.set(factor,factor,factor);
116
      mScreen.resize(width, height);
117
      }
118
119 64558e4e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121
    void setRenderModeToOIT(boolean oit)
122
      {
123
      mScreen.setOrderIndependentTransparency(oit);
124
      }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
128
    void setTransparency(int level)
129
      {
130
      mAlpha.set((float)level/100.0f);
131
      }
132
133 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135
    float setLevel(int level)
136
      {
137
      float inflateLevel = (level-50)/50.0f;
138
      mMesh.setInflate(inflateLevel);
139
140
      return inflateLevel;
141
      }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
    
145
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
146
      {
147
      InflateActivity2 act = (InflateActivity2)mView.getContext();
148
149
      mTexture.setTexture( act.getBitmap() );
150
      mScreen.detachAll();
151
      mScreen.attach(mTexture,mEffects,mMesh);
152
153 64558e4e Leszek Koltunski
      FragmentEffectAlpha.enable();
154
155 77a500b3 Leszek Koltunski
      try
156
        {
157 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(act);
158 77a500b3 Leszek Koltunski
        }
159
      catch(Exception ex)
160
        {
161
        android.util.Log.e("Inflate", ex.getMessage() );
162
        }
163
      }
164
}