Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ ba9ae2c8

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

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.MatrixEffect;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedLibrary;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.mesh.MeshBase;
36
import org.distorted.library.mesh.MeshQuad;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
import java.io.IOException;
42
import java.io.InputStream;
43

    
44
import javax.microedition.khronos.egl.EGLConfig;
45
import javax.microedition.khronos.opengles.GL10;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class MeshJoinRenderer implements GLSurfaceView.Renderer
50
{
51
    private GLSurfaceView mView;
52
    private DistortedTexture mTexture;
53
    private DistortedScreen mScreen;
54
    private DistortedEffects mEffects;
55
    private Static3D mScale;
56
    private MeshBase mMesh;
57
    private float mObjHeight, mObjWidth, mObjDepth;
58

    
59
    Static4D mQuat1, mQuat2;
60
    int mScreenMin;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
    MeshJoinRenderer(GLSurfaceView v)
65
      {
66
      mView = v;
67
      mScreen = new DistortedScreen();
68
      mScale= new Static3D(1,1,1);
69
      Static3D center=new Static3D(0,0,0);
70

    
71
      mQuat1 = new Static4D(0,0,0,1);  // unity
72
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
73

    
74
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
75
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
76

    
77
      quatInt1.add(mQuat1);
78
      quatInt2.add(mQuat2);
79

    
80
      mEffects = new DistortedEffects();
81
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
82
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
83
      mEffects.apply( new MatrixEffectScale(mScale));
84
      }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
   
88
    public void onDrawFrame(GL10 glUnused) 
89
      {
90
      mScreen.render( System.currentTimeMillis() );
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
    
95
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
96
      {
97
      final float SCALE = 0.75f;
98

    
99
      mScreenMin = width<height ? width:height;
100
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
101
      mScale.set(factor,factor,factor);
102
      mScreen.resize(width, height);
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
108
      {
109
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
110
      Bitmap bitmap;
111

    
112
      try
113
        {
114
        bitmap = BitmapFactory.decodeStream(is);
115
        }
116
      finally
117
        {
118
        try
119
          {
120
          is.close();
121
          }
122
        catch(IOException e) { }
123
        }
124

    
125
      if( mTexture==null ) mTexture = new DistortedTexture();
126
      mTexture.setTexture(bitmap);
127

    
128
      if( mMesh==null ) mMesh = createJoinedMesh();
129

    
130
      mObjWidth = mMesh.getStretchX();
131
      mObjHeight= mMesh.getStretchY();
132
      mObjDepth = mMesh.getStretchZ();
133

    
134
      mScreen.detachAll();
135
      mScreen.attach(mTexture,mEffects,mMesh);
136

    
137
      try
138
        {
139
        DistortedLibrary.onCreate(mView.getContext());
140
        }
141
      catch(Exception ex)
142
        {
143
        android.util.Log.e("MeshJoin", ex.getMessage() );
144
        }
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
    private MeshBase createJoinedMesh()
150
      {
151
      MatrixEffect[] effects = new MatrixEffect[1];
152
      effects[0] = new MatrixEffectMove(new Static3D(0,0,0.5f));
153

    
154
      MeshBase mesh = new MeshQuad();
155
      mesh.apply(effects);
156

    
157
      return mesh;
158
      }
159
}
(2-2/3)