Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ 508ee98f

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.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedLibrary;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshBase;
35
import org.distorted.library.mesh.MeshQuad;
36
import org.distorted.library.type.DynamicQuat;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

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

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
58
    Static4D mQuat1, mQuat2;
59
    int mScreenMin;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
    MeshJoinRenderer(GLSurfaceView v)
64
      {
65
      mView = v;
66
      mScreen = new DistortedScreen();
67
      mMove = new Static3D(0,0,0);
68
      mScale= new Static3D(1,1,1);
69
      mCenter=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, mCenter) );
82
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
83
      mEffects.apply( new MatrixEffectScale(mScale));
84
      mEffects.apply( new MatrixEffectMove(mMove) );
85
      }
86

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

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

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

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

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

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

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

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

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    private MeshBase createJoinedMesh()
153
      {
154
      return new MeshQuad();
155
      }
156
}
(2-2/3)