Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ 98b6b975

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 mMove, mScale, mCenter;
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
      mMove = new Static3D(0,0,0);
69
      mScale= new Static3D(1,1,1);
70
      mCenter=new Static3D(0,0,0);
71

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

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

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

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

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

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

    
101
      mScreenMin = width<height ? width:height;
102
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
103
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , -factor*mObjDepth/2 );
104
      mScale.set(factor,factor,factor);
105
      mCenter.set( mObjWidth/2, mObjHeight/2, mObjDepth/2 );
106
      mScreen.resize(width, height);
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
112
      {
113
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
114
      Bitmap bitmap;
115

    
116
      try
117
        {
118
        bitmap = BitmapFactory.decodeStream(is);
119
        }
120
      finally
121
        {
122
        try
123
          {
124
          is.close();
125
          }
126
        catch(IOException e) { }
127
        }
128

    
129
      if( mTexture==null ) mTexture = new DistortedTexture();
130
      mTexture.setTexture(bitmap);
131

    
132
      if( mMesh==null ) mMesh = createJoinedMesh();
133

    
134
      mObjWidth = mMesh.getStretchX();
135
      mObjHeight= mMesh.getStretchY();
136
      mObjDepth = mMesh.getStretchZ();
137

    
138
      mScreen.detachAll();
139
      mScreen.attach(mTexture,mEffects,mMesh);
140

    
141
      try
142
        {
143
        DistortedLibrary.onCreate(mView.getContext());
144
        }
145
      catch(Exception ex)
146
        {
147
        android.util.Log.e("MeshJoin", ex.getMessage() );
148
        }
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    private MeshBase createJoinedMesh()
154
      {
155
      MatrixEffect[] effects = new MatrixEffect[1];
156
      effects[0] = new MatrixEffectMove(new Static3D(0,0,0.5f));
157

    
158
      MeshBase mesh = new MeshQuad();
159
      mesh.apply(effects);
160

    
161
      return mesh;
162
      }
163
}
(2-2/3)