Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateRenderer.java @ 77a500b3

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 android.opengl.GLSurfaceView;
23

    
24
import org.distorted.library.effect.MatrixEffectMove;
25
import org.distorted.library.effect.MatrixEffectQuaternion;
26
import org.distorted.library.effect.MatrixEffectScale;
27
import org.distorted.library.main.Distorted;
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.Static3D;
34
import org.distorted.library.type.Static4D;
35

    
36
import javax.microedition.khronos.egl.EGLConfig;
37
import javax.microedition.khronos.opengles.GL10;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
class InflateRenderer implements GLSurfaceView.Renderer
42
{
43
    private GLSurfaceView mView;
44
    private DistortedTexture mTexture;
45
    private DistortedEffects mEffects;
46
    private MeshBase mMesh;
47
    private DistortedScreen mScreen;
48
    private int mObjWidth, mObjHeight, mObjDepth;
49
    private Static3D mMove, mScale, mCenter;
50

    
51
    Static4D mQuat1, mQuat2;
52
    int mScreenMin;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
    InflateRenderer(GLSurfaceView v)
57
      {
58
      mView = v;
59

    
60
      mMove = new Static3D(0,0,0);
61
      mScale= new Static3D(1,1,1);
62
      mCenter=new Static3D(0,0,0);
63

    
64
      InflateActivity2 act = (InflateActivity2)v.getContext();
65

    
66
      mTexture = act.getTexture();
67
      mMesh    = act.getMesh();
68

    
69
      mObjWidth = mTexture.getWidth();
70
      mObjHeight= mTexture.getHeight();
71
      mObjDepth = mTexture.getDepth(mMesh);
72

    
73
      mQuat1 = new Static4D(0,0,0,1);  // unity
74
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
75
      
76
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
77
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
78

    
79
      quatInt1.add(mQuat1);
80
      quatInt2.add(mQuat2);
81

    
82
      mEffects = new DistortedEffects();
83
      mEffects.apply( new MatrixEffectMove(mMove) );
84
      mEffects.apply( new MatrixEffectScale(mScale));
85
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
86
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
87

    
88
      mScreen = new DistortedScreen();
89
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
90
      }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
   
94
    public void onDrawFrame(GL10 glUnused) 
95
      {
96
      mScreen.render( System.currentTimeMillis() );
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
    
101
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
102
      {
103
      mScreenMin = width<height ? width:height;
104
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.75f*height)/mObjHeight :  (0.75f*width)/mObjWidth;
105
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
106
      mScale.set(factor,factor,factor);
107
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, -(float)mObjDepth/2 );
108
      mScreen.resize(width, height);
109
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
    float setLevel(int level)
114
      {
115
      float inflateLevel = (level-50)/50.0f;
116
      mMesh.setInflate(inflateLevel);
117

    
118
      return inflateLevel;
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
124
      {
125
      InflateActivity2 act = (InflateActivity2)mView.getContext();
126

    
127
      mTexture.setTexture( act.getBitmap() );
128

    
129
      mScreen.detachAll();
130
      mScreen.attach(mTexture,mEffects,mMesh);
131

    
132
      try
133
        {
134
        Distorted.onCreate(mView.getContext());
135
        }
136
      catch(Exception ex)
137
        {
138
        android.util.Log.e("Inflate", ex.getMessage() );
139
        }
140
      }
141
}
(3-3/4)