Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 061449ed

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.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectScale;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.mesh.MeshRectangles;
41
import org.distorted.library.main.DistortedFramebuffer;
42
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  SaveRenderer(GLSurfaceView v)
76
    {
77
    mView = v;
78
      
79
    boobsSink  = 1.0f;
80

    
81
    Static3D pLeft = new Static3D(-68, 36, 0);
82
    Static3D pRight= new Static3D( 47, 36, 0);
83

    
84
    Static4D sinkRegion = new Static4D(0,0,0,60);
85
      
86
    s0 = new Static1D(boobsSink);
87
      
88
    Dynamic1D diSink = new Dynamic1D();
89
    diSink.add(s0);
90

    
91
    mScale = 1.0f;
92
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
93
    mScaleMain   = new Static3D(1,1,1);
94
    mScaleVertex = new Static3D(1,1,1);
95

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

    
103
    mScreen = new DistortedScreen();
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

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

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

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

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

    
137
      File file;
138
      int lowestNotFound = 1;
139

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

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

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
   
154
  public void onDrawFrame(GL10 glUnused)
155
    {
156
    if( isSaving )  // render to an offscreen buffer and read pixels
157
      {
158
      mScaleMain.set(1,1,1);
159
      mOffscreen.render(System.currentTimeMillis());
160
      applyMatrixEffects(scrWidth,scrHeight);
161

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

    
167
      int textureID = mOffscreen.getTextureID();
168

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

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

    
189
    mScreen.render(System.currentTimeMillis());
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  private void applyMatrixEffects(int width, int height)
195
    {
196
    float horiRatio = (float)width / mScaleVertex.get0();
197
    float vertRatio = (float)height/ mScaleVertex.get1();
198
    float factor    = Math.min(horiRatio, vertRatio);
199
    mScaleMain.set(factor,factor,factor);
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
205
    {
206
    scrWidth = width;
207
    scrHeight= height;
208
    applyMatrixEffects(width, height);
209
    mScreen.resize(width, height);
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
    
214
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
215
    {
216
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
217
    Bitmap bitmap;
218

    
219
    try
220
      {
221
      bitmap = BitmapFactory.decodeStream(is);
222
      }
223
    finally
224
      {
225
      try
226
        {
227
        is.close();
228
        }
229
      catch(IOException ignored) { }
230
      }
231
      
232
    bmpHeight = bitmap.getHeight();
233
    bmpWidth  = bitmap.getWidth();
234

    
235
    mScaleVertex.set(bmpWidth,bmpHeight,0);
236

    
237
    if( mMesh   ==null ) mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
238
    if( mTexture==null ) mTexture = new DistortedTexture();
239
    mTexture.setTexture(bitmap);
240

    
241
    if( mOffscreen==null ) mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight),
242
                                                                  1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
243

    
244
    mOffscreen.detachAll();
245
    mOffscreen.attach(mTexture,mEffects,mMesh);
246
    mScreen.detachAll();
247
    mScreen.attach(mTexture,mEffects,mMesh);
248

    
249
    VertexEffectScale.enable();
250
    VertexEffectSink.enable();
251

    
252
    DistortedLibrary.onCreate(mView.getContext(), this);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public void distortedException(Exception ex)
258
    {
259
    android.util.Log.e("Save", ex.getMessage() );
260
    }
261
  }
(2-2/4)