Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ e4330c89

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

    
22
import java.io.File;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27

    
28
import javax.microedition.khronos.egl.EGLConfig;
29
import javax.microedition.khronos.opengles.GL10;
30

    
31
import org.distorted.examples.R;
32

    
33
import org.distorted.library.effect.EffectName;
34
import org.distorted.library.effect.MatrixEffectMove;
35
import org.distorted.library.effect.MatrixEffectScale;
36
import org.distorted.library.effect.VertexEffectSink;
37
import org.distorted.library.main.Distorted;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.main.DistortedTexture;
41
import org.distorted.library.main.MeshFlat;
42
import org.distorted.library.main.DistortedFramebuffer;
43
import org.distorted.library.type.Dynamic1D;
44
import org.distorted.library.type.Static1D;
45
import org.distorted.library.type.Static3D;
46
import org.distorted.library.type.Static4D;
47

    
48
import android.graphics.Bitmap;
49
import android.graphics.BitmapFactory;
50
import android.opengl.GLES30;
51
import android.opengl.GLSurfaceView;
52
import android.os.Environment;
53

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

    
56
class SaveRenderer implements GLSurfaceView.Renderer
57
  {
58
  private GLSurfaceView mView;
59
  private DistortedEffects mEffects;
60
  private DistortedFramebuffer mOffscreen;
61
  private DistortedTexture mTexture;
62
  private MeshFlat mMesh;
63
  private DistortedScreen mScreen;
64
  private Static1D s0;
65
  private Static3D mScaleFactor, mScaleMain, mMove;
66

    
67
  private float mScale;
68
  private int bmpHeight, bmpWidth;
69
  private int scrHeight, scrWidth;
70
  private float boobsSink;
71
  private boolean isSaving = false;
72
  private String mPath;
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  SaveRenderer(GLSurfaceView v)
77
    {
78
    mView = v;
79
      
80
    boobsSink  = 1.0f;
81
      
82
    Static3D pLeft = new Static3D(132, 264, 0);
83
    Static3D pRight= new Static3D(247, 264, 0);
84
      
85
    Static4D sinkRegion = new Static4D(0,0,60,60);
86
      
87
    s0 = new Static1D(boobsSink);
88
      
89
    Dynamic1D diSink = new Dynamic1D();
90
    diSink.add(s0);
91

    
92
    mScale = 1.0f;
93
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
94
    mMove = new Static3D(0,0,0);
95
    mScaleMain = new Static3D(1,1,1);
96

    
97
    mEffects = new DistortedEffects();
98
    mEffects.apply( new VertexEffectSink(diSink, pLeft , sinkRegion) );
99
    mEffects.apply( new VertexEffectSink(diSink, pRight, sinkRegion) );
100
    mEffects.apply( new MatrixEffectMove(mMove));
101
    mEffects.apply( new MatrixEffectScale(mScaleMain));
102
    mEffects.apply( new MatrixEffectScale(mScaleFactor));
103

    
104
    mScreen = new DistortedScreen();
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  void setSize(float s)
110
    {
111
    boobsSink = s;
112
    s0.set(boobsSink);
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  void setScale(float s)
118
    {
119
    mScale = s;
120
    mScaleFactor.set(s,s,1.0f);
121

    
122
    // when we move our scroll bar, this does NOT keep allocating and deallocating
123
    // the whole WxH texture - the allocation happens only once, on next render, i.e. -
124
    // when one presses the 'SAVE' button.
125

    
126
    if( mOffscreen!=null )
127
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  void Save()
133
    {
134
    if( !isSaving )
135
      {
136
      isSaving = true;
137

    
138
      File file;
139
      int lowestNotFound = 1;
140

    
141
      while(true)
142
        {
143
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
144
        file = new File(mPath);
145

    
146
        if( !file.exists() ) break;
147

    
148
        lowestNotFound++;
149
        }
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
   
155
  public void onDrawFrame(GL10 glUnused)
156
    {
157
    long time = System.currentTimeMillis();
158

    
159
    if( isSaving )  // render to an offscreen buffer and read pixels
160
      {
161
      mMove.set(0,0,0);
162
      mScaleMain.set(1,1,1);
163
      mOffscreen.render(time);
164
      applyMatrixEffects(scrWidth,scrHeight);
165

    
166
      int fW =(int)(mScale*bmpWidth);
167
      int fH =(int)(mScale*bmpHeight);
168
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
169
      buf.order(ByteOrder.LITTLE_ENDIAN);
170

    
171
      int textureID = mOffscreen.getTextureID();
172

    
173
      if( textureID>=0 )
174
        {
175
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureID);
176
        GLES30.glReadPixels( 0, 0, fW, fH, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
177
        SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
178
        }
179
      else
180
        {
181
        android.util.Log.e("Save", "Error trying to read from offscreen FBO, textureID="+textureID);
182
        }
183

    
184
      isSaving = false;
185
      }
186

    
187
    mScreen.render(time);
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  private void applyMatrixEffects(int width, int height)
193
    {
194
    if( (float)bmpHeight/bmpWidth > (float)height/width )
195
      {
196
      int w = (height*bmpWidth)/bmpHeight;
197
      float factor = (float)height/bmpHeight;
198

    
199
      mMove.set((width-w)/2,0,0);
200
      mScaleMain.set(factor,factor,factor);
201
      }
202
    else
203
      {
204
      int h = (width*bmpHeight)/bmpWidth;
205
      float factor = (float)width/bmpWidth;
206

    
207
      mMove.set(0,(height-h)/2,0);
208
      mScaleMain.set(factor,factor,factor);
209
      }
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
215
    {
216
    scrWidth = width;
217
    scrHeight= height;
218
    applyMatrixEffects(width, height);
219
    mScreen.resize(width, height);
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
    
224
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
225
    {
226
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
227
    Bitmap bitmap;
228

    
229
    try
230
      {
231
      bitmap = BitmapFactory.decodeStream(is);
232
      }
233
    finally
234
      {
235
      try
236
        {
237
        is.close();
238
        }
239
      catch(IOException e) { }
240
      }
241
      
242
    bmpHeight = bitmap.getHeight();
243
    bmpWidth  = bitmap.getWidth();
244

    
245
    if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
246
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
247
    mTexture.setTexture(bitmap);
248

    
249
    if( mOffscreen==null ) mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight) );
250

    
251
    mOffscreen.detachAll();
252
    mOffscreen.attach(mTexture,mEffects,mMesh);
253
    mScreen.detachAll();
254
    mScreen.attach(mTexture,mEffects,mMesh);
255

    
256
    DistortedEffects.enableEffect(EffectName.SINK);
257

    
258
    try
259
      {
260
      Distorted.onCreate(mView.getContext());
261
      }
262
    catch(Exception ex)
263
      {
264
      android.util.Log.e("Renderer", ex.getMessage() );
265
      }
266
    }
267
  }
(2-2/4)