Project

General

Profile

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

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

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
import org.distorted.library.EffectTypes;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 a2cb31e9 Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES20;
44
import android.opengl.GLSurfaceView;
45 7cfeeb63 Leszek Koltunski
import android.os.Environment;
46 a2cb31e9 Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
50 b9615af9 Leszek Koltunski
  {
51
  private GLSurfaceView mView;
52 13a5c5cd Leszek Koltunski
  private DistortedBitmap mGirl;
53 334c13fa Leszek Koltunski
  private Static3D pLeft, pRight;
54 7589635e Leszek Koltunski
  private Static4D sinkRegion;
55 13a5c5cd Leszek Koltunski
  private Dynamic1D diSink;
56
  private Static1D s0;
57 b9615af9 Leszek Koltunski
58
  private int bmpHeight, bmpWidth;
59 13a5c5cd Leszek Koltunski
  private int scrHeight, scrWidth;
60
  private float boobsSink;
61
  private boolean isSaving = false;
62
  private String mPath;
63 b9615af9 Leszek Koltunski
64 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 13a5c5cd Leszek Koltunski
  SaveRenderer(GLSurfaceView v)
67 b9615af9 Leszek Koltunski
    {
68
    mView = v;
69 a2cb31e9 Leszek Koltunski
      
70 b9615af9 Leszek Koltunski
    boobsSink  = 1.0f;
71 a2cb31e9 Leszek Koltunski
      
72 334c13fa Leszek Koltunski
    pLeft = new Static3D(132, 264, 0);
73
    pRight= new Static3D(247, 264, 0);
74 a2cb31e9 Leszek Koltunski
      
75 7589635e Leszek Koltunski
    sinkRegion = new Static4D(0,0,60,60);
76 a2cb31e9 Leszek Koltunski
      
77 7589635e Leszek Koltunski
    s0 = new Static1D(boobsSink);
78 a2cb31e9 Leszek Koltunski
      
79 f988589e Leszek Koltunski
    diSink = new Dynamic1D(0,0.5f);
80 b9615af9 Leszek Koltunski
    diSink.add(s0);
81
    }
82 a2cb31e9 Leszek Koltunski
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85 13a5c5cd Leszek Koltunski
  void setSize(float s)
86 e4237c21 Leszek Koltunski
    {
87
    boobsSink = s;
88
    s0.set(boobsSink);
89 b9615af9 Leszek Koltunski
    }
90 a2cb31e9 Leszek Koltunski
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92 b9615af9 Leszek Koltunski
93 13a5c5cd Leszek Koltunski
  void Save()
94 b9615af9 Leszek Koltunski
    {
95 92040610 Leszek Koltunski
    if( !isSaving )
96 b9615af9 Leszek Koltunski
      {
97 e4237c21 Leszek Koltunski
      isSaving = true;
98 b9615af9 Leszek Koltunski
99 e4237c21 Leszek Koltunski
      File file;
100
      int lowestNotFound = 1;
101
102
      while(true)
103 a2cb31e9 Leszek Koltunski
        {
104 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
105
        file = new File(mPath);
106
107
        if( !file.exists() ) break;
108
109
        lowestNotFound++;
110 a2cb31e9 Leszek Koltunski
        }
111 b9615af9 Leszek Koltunski
      }
112
    }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
   
116
  public void onDrawFrame(GL10 glUnused)
117
    {
118
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
119 a2cb31e9 Leszek Koltunski
      
120 b9615af9 Leszek Koltunski
    mGirl.draw(System.currentTimeMillis());
121 e4237c21 Leszek Koltunski
122
    if( isSaving )  // we should save the buffer to location pointed at by mPath
123
      {
124
      ByteBuffer buf = ByteBuffer.allocateDirect( scrWidth*scrHeight*4 );
125
      buf.order(ByteOrder.LITTLE_ENDIAN);
126
      GLES20.glReadPixels( 0, 0, scrWidth, scrHeight , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
127
128 13a5c5cd Leszek Koltunski
      SaveWorkerThread.newBuffer(buf,scrWidth,scrHeight,mPath);
129 e4237c21 Leszek Koltunski
130
      isSaving = false;
131
      }
132 b9615af9 Leszek Koltunski
    }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
    
136
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
137
    {
138
    scrWidth = width;
139
    scrHeight= height;
140
141
    mGirl.abortEffects(EffectTypes.MATRIX);
142
      
143
    if( bmpHeight/bmpWidth > height/width )
144
      {
145
      int w = (height*bmpWidth)/bmpHeight;
146 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
147
148
      mGirl.move( new Static3D((width-w)/2,0,0) );
149 f988589e Leszek Koltunski
      mGirl.scale(factor);
150 a2cb31e9 Leszek Koltunski
      }
151 b9615af9 Leszek Koltunski
    else
152
      {
153
      int h = (width*bmpHeight)/bmpWidth;
154 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
155
156
      mGirl.move( new Static3D(0,(height-h)/2,0) );
157 f988589e Leszek Koltunski
      mGirl.scale(factor);
158 b9615af9 Leszek Koltunski
      }
159
      
160
    Distorted.onSurfaceChanged(width, height);
161
    }
162 a2cb31e9 Leszek Koltunski
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
166
    {
167 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
168
169 b9615af9 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
170
    Bitmap bitmap;
171 a2cb31e9 Leszek Koltunski
        
172 b9615af9 Leszek Koltunski
    try
173
      {
174
      bitmap = BitmapFactory.decodeStream(is);
175
      }
176
    finally
177
      {
178
      try
179 a2cb31e9 Leszek Koltunski
        {
180 b9615af9 Leszek Koltunski
        is.close();
181
        }
182
      catch(IOException e) { }
183
      }
184 a2cb31e9 Leszek Koltunski
      
185 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
186
    bmpWidth  = bitmap.getWidth();
187 a2cb31e9 Leszek Koltunski
      
188 b9615af9 Leszek Koltunski
    mGirl = new DistortedBitmap(bitmap, 30);
189 a2cb31e9 Leszek Koltunski
190 7bf107f7 Leszek Koltunski
    mGirl.sink( diSink, pLeft , sinkRegion);
191
    mGirl.sink( diSink, pRight, sinkRegion);
192 a2cb31e9 Leszek Koltunski
193 b9615af9 Leszek Koltunski
    try
194
      {
195
      Distorted.onSurfaceCreated(mView.getContext());
196
      }
197
    catch(Exception ex)
198
      {
199
      android.util.Log.e("Renderer", ex.getMessage() );
200 a2cb31e9 Leszek Koltunski
      }
201 b9615af9 Leszek Koltunski
    }
202
  }