Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformRenderer.java @ c1f047b9

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.predeform;
21

    
22
import android.opengl.GLSurfaceView;
23

    
24
import org.distorted.library.effect.FragmentEffectAlpha;
25
import org.distorted.library.effect.MatrixEffectQuaternion;
26
import org.distorted.library.effect.MatrixEffectScale;
27
import org.distorted.library.effect.VertexEffectScale;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedLibrary;
30
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
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36

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

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

    
42
class PredeformRenderer 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 float mObjWidth, mObjHeight, mObjDepth;
53
    private Static3D mScale;
54

    
55
    Static4D mQuat1, mQuat2;
56
    int mScreenMin;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
    PredeformRenderer(GLSurfaceView v)
61
      {
62
      mView = v;
63

    
64
      mScale= new Static3D(1,1,1);
65

    
66
      Static3D center=new Static3D(0,0,0);
67

    
68
      PredeformActivity2 act = (PredeformActivity2)v.getContext();
69

    
70
      mTexture = act.getTexture();
71
      mMesh    = act.getMesh();
72

    
73
      mObjWidth = act.getCols();
74
      mObjHeight= act.getRows();
75
      mObjDepth = act.getSlic();
76

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

    
83
      quatInt1.add(mQuat1);
84
      quatInt2.add(mQuat2);
85

    
86
      mEffects = new DistortedEffects();
87
      mEffects.apply( new VertexEffectScale(new Static3D(mObjWidth,mObjHeight,mObjDepth) ) );
88
      mEffects.apply( new MatrixEffectScale(mScale));
89
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
90
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
91

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

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

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

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

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    float setLevel(int level)
119
      {
120
      float inflateLevel = (level-50)/50.0f;
121
      mMesh.setInflate(inflateLevel);
122

    
123
      return inflateLevel;
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
129
      {
130
      PredeformActivity2 act = (PredeformActivity2)mView.getContext();
131

    
132
      mTexture.setTexture( act.getBitmap() );
133
      mScreen.detachAll();
134
      mScreen.attach(mTexture,mEffects,mMesh);
135

    
136
      VertexEffectScale.enable();
137
      FragmentEffectAlpha.enable();
138

    
139
      try
140
        {
141
        DistortedLibrary.onCreate(act);
142
        }
143
      catch(Exception ex)
144
        {
145
        android.util.Log.e("Inflate", ex.getMessage() );
146
        }
147
      }
148
}
(6-6/7)