Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 a2cb31e9 Leszek Koltunski
20
package org.distorted.examples.save;
21
22 7cfeeb63 Leszek Koltunski
import java.io.File;
23 a2cb31e9 Leszek Koltunski
import java.io.IOException;
24
import java.io.InputStream;
25 e4237c21 Leszek Koltunski
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27 a2cb31e9 Leszek Koltunski
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 3c8433ae Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
36 a2cb31e9 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 7589635e Leszek Koltunski
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 a2cb31e9 Leszek Koltunski
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES20;
45
import android.opengl.GLSurfaceView;
46 7cfeeb63 Leszek Koltunski
import android.os.Environment;
47 a2cb31e9 Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
51 b9615af9 Leszek Koltunski
  {
52
  private GLSurfaceView mView;
53 13a5c5cd Leszek Koltunski
  private DistortedBitmap mGirl;
54 3c8433ae Leszek Koltunski
  private DistortedFramebuffer mOffscreen;
55 334c13fa Leszek Koltunski
  private Static3D pLeft, pRight;
56 7589635e Leszek Koltunski
  private Static4D sinkRegion;
57 13a5c5cd Leszek Koltunski
  private Dynamic1D diSink;
58
  private Static1D s0;
59 b9615af9 Leszek Koltunski
60 3c8433ae Leszek Koltunski
  private int fboHeight, fboWidth;
61 b9615af9 Leszek Koltunski
  private int bmpHeight, bmpWidth;
62 13a5c5cd Leszek Koltunski
  private int scrHeight, scrWidth;
63
  private float boobsSink;
64
  private boolean isSaving = false;
65
  private String mPath;
66 b9615af9 Leszek Koltunski
67 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 13a5c5cd Leszek Koltunski
  SaveRenderer(GLSurfaceView v)
70 b9615af9 Leszek Koltunski
    {
71
    mView = v;
72 a2cb31e9 Leszek Koltunski
      
73 b9615af9 Leszek Koltunski
    boobsSink  = 1.0f;
74 a2cb31e9 Leszek Koltunski
      
75 334c13fa Leszek Koltunski
    pLeft = new Static3D(132, 264, 0);
76
    pRight= new Static3D(247, 264, 0);
77 a2cb31e9 Leszek Koltunski
      
78 7589635e Leszek Koltunski
    sinkRegion = new Static4D(0,0,60,60);
79 a2cb31e9 Leszek Koltunski
      
80 7589635e Leszek Koltunski
    s0 = new Static1D(boobsSink);
81 a2cb31e9 Leszek Koltunski
      
82 f988589e Leszek Koltunski
    diSink = new Dynamic1D(0,0.5f);
83 b9615af9 Leszek Koltunski
    diSink.add(s0);
84 3c8433ae Leszek Koltunski
85
    fboHeight = 150;
86
    fboWidth  = 100;
87 b9615af9 Leszek Koltunski
    }
88 a2cb31e9 Leszek Koltunski
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91 13a5c5cd Leszek Koltunski
  void setSize(float s)
92 e4237c21 Leszek Koltunski
    {
93
    boobsSink = s;
94
    s0.set(boobsSink);
95 b9615af9 Leszek Koltunski
    }
96 a2cb31e9 Leszek Koltunski
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98 b9615af9 Leszek Koltunski
99 13a5c5cd Leszek Koltunski
  void Save()
100 b9615af9 Leszek Koltunski
    {
101 92040610 Leszek Koltunski
    if( !isSaving )
102 b9615af9 Leszek Koltunski
      {
103 e4237c21 Leszek Koltunski
      isSaving = true;
104 b9615af9 Leszek Koltunski
105 e4237c21 Leszek Koltunski
      File file;
106
      int lowestNotFound = 1;
107
108
      while(true)
109 a2cb31e9 Leszek Koltunski
        {
110 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
111
        file = new File(mPath);
112
113
        if( !file.exists() ) break;
114
115
        lowestNotFound++;
116 a2cb31e9 Leszek Koltunski
        }
117 b9615af9 Leszek Koltunski
      }
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 e4237c21 Leszek Koltunski
126 3c8433ae Leszek Koltunski
    if( isSaving )  // render to an offscreen buffer and read pixels
127 e4237c21 Leszek Koltunski
      {
128 3c8433ae Leszek Koltunski
      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 e4237c21 Leszek Koltunski
      buf.order(ByteOrder.LITTLE_ENDIAN);
134 3c8433ae Leszek Koltunski
      mOffscreen.setAsInput();
135 e4237c21 Leszek Koltunski
136 3c8433ae Leszek Koltunski
      GLES20.glReadPixels( 0, 0, fboWidth, fboHeight , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
137
      SaveWorkerThread.newBuffer(buf,fboWidth,fboHeight,mPath);
138 e4237c21 Leszek Koltunski
139
      isSaving = false;
140
      }
141 3c8433ae Leszek Koltunski
    else
142
      {
143
      mGirl.draw(System.currentTimeMillis());
144
      }
145 b9615af9 Leszek Koltunski
    }
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 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
160
161
      mGirl.move( new Static3D((width-w)/2,0,0) );
162 f988589e Leszek Koltunski
      mGirl.scale(factor);
163 a2cb31e9 Leszek Koltunski
      }
164 b9615af9 Leszek Koltunski
    else
165
      {
166
      int h = (width*bmpHeight)/bmpWidth;
167 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
168
169
      mGirl.move( new Static3D(0,(height-h)/2,0) );
170 f988589e Leszek Koltunski
      mGirl.scale(factor);
171 b9615af9 Leszek Koltunski
      }
172
      
173
    Distorted.onSurfaceChanged(width, height);
174
    }
175 a2cb31e9 Leszek Koltunski
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
    
178 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
179
    {
180 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
181
182 b9615af9 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
183
    Bitmap bitmap;
184 a2cb31e9 Leszek Koltunski
        
185 b9615af9 Leszek Koltunski
    try
186
      {
187
      bitmap = BitmapFactory.decodeStream(is);
188
      }
189
    finally
190
      {
191
      try
192 a2cb31e9 Leszek Koltunski
        {
193 b9615af9 Leszek Koltunski
        is.close();
194
        }
195
      catch(IOException e) { }
196
      }
197 a2cb31e9 Leszek Koltunski
      
198 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
199
    bmpWidth  = bitmap.getWidth();
200 a2cb31e9 Leszek Koltunski
      
201 b9615af9 Leszek Koltunski
    mGirl = new DistortedBitmap(bitmap, 30);
202 a2cb31e9 Leszek Koltunski
203 7bf107f7 Leszek Koltunski
    mGirl.sink( diSink, pLeft , sinkRegion);
204
    mGirl.sink( diSink, pRight, sinkRegion);
205 a2cb31e9 Leszek Koltunski
206 3c8433ae Leszek Koltunski
    mOffscreen = new DistortedFramebuffer(fboWidth,fboHeight);
207
208 b9615af9 Leszek Koltunski
    try
209
      {
210
      Distorted.onSurfaceCreated(mView.getContext());
211
      }
212
    catch(Exception ex)
213
      {
214
      android.util.Log.e("Renderer", ex.getMessage() );
215 a2cb31e9 Leszek Koltunski
      }
216 b9615af9 Leszek Koltunski
    }
217
  }