Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 3c8433ae

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.Distorted;
34
import org.distorted.library.DistortedBitmap;
35
import org.distorted.library.DistortedFramebuffer;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES20;
45
import android.opengl.GLSurfaceView;
46
import android.os.Environment;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class SaveRenderer implements GLSurfaceView.Renderer
51
  {
52
  private GLSurfaceView mView;
53
  private DistortedBitmap mGirl;
54
  private DistortedFramebuffer mOffscreen;
55
  private Static3D pLeft, pRight;
56
  private Static4D sinkRegion;
57
  private Dynamic1D diSink;
58
  private Static1D s0;
59

    
60
  private int fboHeight, fboWidth;
61
  private int bmpHeight, bmpWidth;
62
  private int scrHeight, scrWidth;
63
  private float boobsSink;
64
  private boolean isSaving = false;
65
  private String mPath;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  SaveRenderer(GLSurfaceView v)
70
    {
71
    mView = v;
72
      
73
    boobsSink  = 1.0f;
74
      
75
    pLeft = new Static3D(132, 264, 0);
76
    pRight= new Static3D(247, 264, 0);
77
      
78
    sinkRegion = new Static4D(0,0,60,60);
79
      
80
    s0 = new Static1D(boobsSink);
81
      
82
    diSink = new Dynamic1D(0,0.5f);
83
    diSink.add(s0);
84

    
85
    fboHeight = 150;
86
    fboWidth  = 100;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  void setSize(float s)
92
    {
93
    boobsSink = s;
94
    s0.set(boobsSink);
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  void Save()
100
    {
101
    if( !isSaving )
102
      {
103
      isSaving = true;
104

    
105
      File file;
106
      int lowestNotFound = 1;
107

    
108
      while(true)
109
        {
110
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
111
        file = new File(mPath);
112

    
113
        if( !file.exists() ) break;
114

    
115
        lowestNotFound++;
116
        }
117
      }
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
   
122
  public void onDrawFrame(GL10 glUnused)
123
    {
124
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
125

    
126
    if( isSaving )  // render to an offscreen buffer and read pixels
127
      {
128
      long scaleID = mGirl.scale(new Static3D( (float)fboWidth/scrWidth, (float)fboHeight/scrHeight, 1.0f));
129
      mGirl.draw(System.currentTimeMillis(), mOffscreen);
130
      mGirl.abortEffect(scaleID);
131

    
132
      ByteBuffer buf = ByteBuffer.allocateDirect( fboWidth*fboHeight*4 );
133
      buf.order(ByteOrder.LITTLE_ENDIAN);
134
      mOffscreen.setAsInput();
135

    
136
      GLES20.glReadPixels( 0, 0, fboWidth, fboHeight , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
137
      SaveWorkerThread.newBuffer(buf,fboWidth,fboHeight,mPath);
138

    
139
      isSaving = false;
140
      }
141
    else
142
      {
143
      mGirl.draw(System.currentTimeMillis());
144
      }
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
    
149
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
150
    {
151
    scrWidth = width;
152
    scrHeight= height;
153

    
154
    mGirl.abortEffects(EffectTypes.MATRIX);
155
      
156
    if( bmpHeight/bmpWidth > height/width )
157
      {
158
      int w = (height*bmpWidth)/bmpHeight;
159
      float factor = (float)height/bmpHeight;
160

    
161
      mGirl.move( new Static3D((width-w)/2,0,0) );
162
      mGirl.scale(factor);
163
      }
164
    else
165
      {
166
      int h = (width*bmpHeight)/bmpWidth;
167
      float factor = (float)width/bmpWidth;
168

    
169
      mGirl.move( new Static3D(0,(height-h)/2,0) );
170
      mGirl.scale(factor);
171
      }
172
      
173
    Distorted.onSurfaceChanged(width, height);
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
    
178
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
179
    {
180
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
181

    
182
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
183
    Bitmap bitmap;
184
        
185
    try
186
      {
187
      bitmap = BitmapFactory.decodeStream(is);
188
      }
189
    finally
190
      {
191
      try
192
        {
193
        is.close();
194
        }
195
      catch(IOException e) { }
196
      }
197
      
198
    bmpHeight = bitmap.getHeight();
199
    bmpWidth  = bitmap.getWidth();
200
      
201
    mGirl = new DistortedBitmap(bitmap, 30);
202

    
203
    mGirl.sink( diSink, pLeft , sinkRegion);
204
    mGirl.sink( diSink, pRight, sinkRegion);
205

    
206
    mOffscreen = new DistortedFramebuffer(fboWidth,fboHeight);
207

    
208
    try
209
      {
210
      Distorted.onSurfaceCreated(mView.getContext());
211
      }
212
    catch(Exception ex)
213
      {
214
      android.util.Log.e("Renderer", ex.getMessage() );
215
      }
216
    }
217
  }
(2-2/4)