Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateRenderer.java @ 698ad0a8

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