Project

General

Profile

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

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

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
    if( isSaving )  // render to an offscreen buffer and read pixels
158
      {
159
      mMove.set(0,0,0);
160
      mScaleMain.set(1,1,1);
161
      mOffscreen.render(System.currentTimeMillis());
162
      applyMatrixEffects(scrWidth,scrHeight);
163

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

    
169
      int textureID = mOffscreen.getTextureID();
170

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

    
184
    // Quite subtle issue here. Since we share the DistortedEffects object between the mOffscreen
185
    // and mScreen surfaces, we cannot render both of them in one go using the same time. That
186
    // would make the second render, in this case mScreen's render, re-use the values of the
187
    // Dynamics calculated the first time around and we don't want that (mOffscreen's scale and move
188
    // are different! What would happen is the mScreen would be - one render when we are 'saving' -
189
    // get drawn with different dimensions and on-screen position, i.e. we would see a flash.
190

    
191
    mScreen.render(System.currentTimeMillis());
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  private void applyMatrixEffects(int width, int height)
197
    {
198
    if( (float)bmpHeight/bmpWidth > (float)height/width )
199
      {
200
      int w = (height*bmpWidth)/bmpHeight;
201
      float factor = (float)height/bmpHeight;
202

    
203
      mMove.set((width-w)/2,0,0);
204
      mScaleMain.set(factor,factor,factor);
205
      }
206
    else
207
      {
208
      int h = (width*bmpHeight)/bmpWidth;
209
      float factor = (float)width/bmpWidth;
210

    
211
      mMove.set(0,(height-h)/2,0);
212
      mScaleMain.set(factor,factor,factor);
213
      }
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
219
    {
220
    scrWidth = width;
221
    scrHeight= height;
222
    applyMatrixEffects(width, height);
223
    mScreen.resize(width, height);
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
    
228
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
229
    {
230
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
231
    Bitmap bitmap;
232

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

    
249
    if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
250
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
251
    mTexture.setTexture(bitmap);
252

    
253
    if( mOffscreen==null ) mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight),
254
                                                                  1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
255

    
256
    mOffscreen.detachAll();
257
    mOffscreen.attach(mTexture,mEffects,mMesh);
258
    mScreen.detachAll();
259
    mScreen.attach(mTexture,mEffects,mMesh);
260

    
261
    VertexEffectSink.enable();
262

    
263
    try
264
      {
265
      Distorted.onCreate(mView.getContext());
266
      }
267
    catch(Exception ex)
268
      {
269
      android.util.Log.e("Renderer", ex.getMessage() );
270
      }
271
    }
272
  }
(2-2/4)