Project

General

Profile

« Previous | Next » 

Revision 5055b5d4

Added by Leszek Koltunski over 7 years ago

1. (hopefully) finish the 'Save' app (now we can adjust the size of the resulting file)
2. Fix one long-standing bug in almost every single app.

View differences:

src/main/java/org/distorted/examples/aroundtheworld/AroundTheWorldRenderer.java
78 78
      {
79 79
      mObject.abortEffects(EffectTypes.MATRIX);
80 80

  
81
      if( mObjHeight/mObjWidth > height/width )
81
      if( (float)mObjHeight/mObjWidth > (float)height/width )
82 82
        {
83 83
        int w = (height*mObjWidth)/mObjHeight;
84 84
        float factor = (float)height/mObjHeight;
src/main/java/org/distorted/examples/bean/BeanRenderer.java
97 97
      { 
98 98
      mBean.abortEffects(EffectTypes.MATRIX);
99 99
         
100
      if( bmpHeight/bmpWidth > height/width )
100
      if( (float)bmpHeight/bmpWidth > (float)height/width )
101 101
        {
102 102
        int w = (height*bmpWidth)/bmpHeight;
103 103
        float factor = (float)height/bmpHeight;
src/main/java/org/distorted/examples/check/CheckRenderer.java
84 84
      { 
85 85
      mSuccess.abortEffects(EffectTypes.MATRIX);
86 86
      
87
      if( bmpHeight/bmpWidth > height/width )
87
      if( (float)bmpHeight/bmpWidth > (float)height/width )
88 88
        {
89 89
        int w = (height*bmpWidth)/bmpHeight;
90 90
        float factor = (float)height/bmpHeight;
src/main/java/org/distorted/examples/differentbitmaps/DifferentBitmapsRenderer.java
109 109
        bmp[i].abortEffects(EffectTypes.MATRIX);
110 110
        }
111 111
      
112
      if( bmpHeight/(NUM*bmpWidth) > height/width )
112
      if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
113 113
        {
114 114
        int w = (height*bmpWidth)/bmpHeight;
115 115
        float factor = (float)height/bmpHeight;
src/main/java/org/distorted/examples/differenteffects/DifferentEffectsRenderer.java
77 77

  
78 78
///////////////////////////////////////////////////////////////////////////////////////////////////
79 79
   
80
    public void onDrawFrame(GL10 glUnused) 
81
      {
82
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
80
   public void onDrawFrame(GL10 glUnused)
81
     {
82
     GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
83 83
      
84
      long time = System.currentTimeMillis();
84
     long time = System.currentTimeMillis();
85 85
   
86
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time);
87
      }
86
     for(int i=NUM-1; i>=0; i--) bmp[i].draw(time);
87
     }
88 88

  
89 89
///////////////////////////////////////////////////////////////////////////////////////////////////
90 90
    
91
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
92
      { 
93
      for(int i=NUM-1; i>=0; i--) 
94
        {   
95
        bmp[i].abortEffects(EffectTypes.MATRIX);
96
        }
91
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
92
     {
93
     for(int i=NUM-1; i>=0; i--)
94
       {
95
       bmp[i].abortEffects(EffectTypes.MATRIX);
96
       }
97 97
      
98
      if( bmpHeight/(NUM*bmpWidth) > height/width )
99
        {
100
        int w = (height*bmpWidth)/bmpHeight;
101
        float factor = (float)height/bmpHeight;
102

  
103
        for(int i=NUM-1; i>=0; i--) 
104
          {
105
          bmp[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
106
          bmp[i].scale(factor);
107
          }
108
        }
109
      else
110
        {
111
        int w = width/NUM;  
112
        int h = (width*bmpHeight)/(bmpWidth*NUM);
113
        float factor = (float)width/(bmpWidth*NUM);
114

  
115
        for(int i=NUM-1; i>=0; i--) 
116
          {
117
          bmp[i].move( new Static3D(i*w, (height-h)/2, 0) );
118
          bmp[i].scale(factor);
119
          }
120
        }
98
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
99
       {
100
       int w = (height*bmpWidth)/bmpHeight;
101
       float factor = (float)height/bmpHeight;
102

  
103
       for(int i=NUM-1; i>=0; i--)
104
         {
105
         bmp[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
106
         bmp[i].scale(factor);
107
         }
108
       }
109
     else
110
       {
111
       int w = width/NUM;
112
       int h = (width*bmpHeight)/(bmpWidth*NUM);
113
       float factor = (float)width/(bmpWidth*NUM);
114

  
115
       for(int i=NUM-1; i>=0; i--)
116
         {
117
         bmp[i].move( new Static3D(i*w, (height-h)/2, 0) );
118
         bmp[i].scale(factor);
119
         }
120
       }
121 121
       
122
      Distorted.onSurfaceChanged(width, height); 
123
      }
122
     Distorted.onSurfaceChanged(width, height);
123
     }
124 124

  
125 125
///////////////////////////////////////////////////////////////////////////////////////////////////
126 126
    
127
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
128
      {
129
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
127
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
128
     {
129
     GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
130 130

  
131
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
132
      Bitmap bitmap;
131
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
132
     Bitmap bitmap;
133 133
        
134
      try 
135
        {
136
        bitmap = BitmapFactory.decodeStream(is);
137
        } 
138
      finally 
139
        {
140
        try 
141
          {
142
          is.close();
143
          } 
144
        catch(IOException e) { }
145
        }  
134
     try
135
       {
136
       bitmap = BitmapFactory.decodeStream(is);
137
       }
138
     finally
139
       {
140
       try
141
         {
142
         is.close();
143
         }
144
       catch(IOException e) { }
145
       }
146 146
      
147
      bmpHeight = bitmap.getHeight();
148
      bmpWidth  = bitmap.getWidth();
147
     bmpHeight = bitmap.getHeight();
148
     bmpWidth  = bitmap.getWidth();
149 149
      
150
      bmp = new DistortedBitmap[NUM];
151
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
150
     bmp = new DistortedBitmap[NUM];
151
     bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
152 152
      
153
      for(int i=1; i<NUM; i++) bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_BITMAP);
153
     for(int i=1; i<NUM; i++) bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_BITMAP);
154 154
      
155
      // setting the bitmap once is enough; others are cloned!
156
      bmp[0].setBitmap(bitmap);
155
     // setting the bitmap once is enough; others are cloned!
156
     bmp[0].setBitmap(bitmap);
157 157

  
158
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
159
      sink.add(new Static1D( 1));
160
      sink.add(new Static1D(10));
158
     Dynamic1D sink = new Dynamic1D(2000,0.0f);
159
     sink.add(new Static1D( 1));
160
     sink.add(new Static1D(10));
161 161

  
162
      bmp[0].sink(sink, pLeft, RegionEye);
163
      bmp[0].sink(sink, pRight,RegionEye);
164
      bmp[1].distort(mDI, pNose1);
162
     bmp[0].sink(sink, pLeft, RegionEye);
163
     bmp[0].sink(sink, pRight,RegionEye);
164
     bmp[1].distort(mDI, pNose1);
165 165

  
166
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
167
      chromaDyn.add(new Static1D(0));
168
      chromaDyn.add(new Static1D(1));
166
     Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
167
     chromaDyn.add(new Static1D(0));
168
     chromaDyn.add(new Static1D(1));
169 169

  
170
      bmp[2].chroma(chromaDyn, new Static3D(0,1,0) );
170
     bmp[2].chroma(chromaDyn, new Static3D(0,1,0) );
171 171
      
172
      try
173
        {
174
        Distorted.onSurfaceCreated(mView.getContext());
175
        }
176
      catch(Exception ex)
177
        {
178
        android.util.Log.e("DifferentEffects", ex.getMessage() );
179
        }
180
      }
172
     try
173
       {
174
       Distorted.onSurfaceCreated(mView.getContext());
175
       }
176
     catch(Exception ex)
177
       {
178
       android.util.Log.e("DifferentEffects", ex.getMessage() );
179
       }
180
     }
181 181
}
src/main/java/org/distorted/examples/fbo/FBORenderer.java
72 72
      { 
73 73
      mLisa.abortEffects(EffectTypes.MATRIX);
74 74
         
75
      if( lisaHeight/lisaWidth > height/width )
75
      if( (float)lisaHeight/lisaWidth > (float)height/width )
76 76
        {
77 77
        int w = (height*lisaWidth)/lisaHeight;
78 78
        float factor = (float)height/lisaHeight;
src/main/java/org/distorted/examples/girl/GirlRenderer.java
161 161
      { 
162 162
      mGirl.abortEffects(EffectTypes.MATRIX);
163 163
      
164
      if( bmpHeight/bmpWidth > height/width )
164
      if( (float)bmpHeight/bmpWidth > (float)height/width )
165 165
        {
166 166
        int w = (height*bmpWidth)/bmpHeight;
167 167
        float factor = (float)height/bmpHeight;
src/main/java/org/distorted/examples/listener/ListenerRenderer.java
106 106
      { 
107 107
      water.abortEffects(EffectTypes.MATRIX);
108 108
         
109
      if( bmpHeight/bmpWidth > height/width )
109
      if( (float)bmpHeight/bmpWidth > (float)height/width )
110 110
        {
111 111
        int w = (height*bmpWidth)/bmpHeight;
112 112
        float factor = (float)height/bmpHeight;
src/main/java/org/distorted/examples/monalisa/MonaLisaRenderer.java
84 84
      { 
85 85
      monaLisa.abortEffects(EffectTypes.MATRIX);
86 86

  
87
      if( bmpHeight/bmpWidth > height/width )
87
      if( (float)bmpHeight/bmpWidth > (float)height/width )
88 88
        {
89 89
        int w = (height*bmpWidth)/bmpHeight;
90 90
        float factor = (float)height/bmpHeight;
src/main/java/org/distorted/examples/olimpic/OlimpicRenderer.java
122 122
     
123 123
      bmp.abortEffects(EffectTypes.MATRIX);
124 124
      
125
      if( bmpHeight/bmpWidth > height/width )
125
      if( (float)bmpHeight/bmpWidth > (float)height/width )
126 126
        {
127 127
        int w = (height*bmpWidth)/bmpHeight;
128 128
        float factor = (float)height/bmpHeight;
src/main/java/org/distorted/examples/plainmonalisa/RenderThread.java
214 214

  
215 215
    monaLisa.abortEffects(EffectTypes.MATRIX);
216 216

  
217
    if( bmpHeight/bmpWidth > height/width )
217
    if( (float)bmpHeight/bmpWidth > (float)height/width )
218 218
      {
219 219
      int w = (height*bmpWidth)/bmpHeight;
220 220
      float factor = (float)height/bmpHeight;
src/main/java/org/distorted/examples/save/SaveActivity.java
65 65
     
66 66
    setContentView(R.layout.savelayout);
67 67

  
68
    SeekBar barSize  = (SeekBar)findViewById(R.id.saveSeekBar);
68
    SeekBar barSize  = (SeekBar)findViewById(R.id.saveSeekBarSize);
69 69
    barSize.setOnSeekBarChangeListener(this);
70 70
    barSize.setProgress(50);
71

  
72
    SeekBar barScale = (SeekBar)findViewById(R.id.saveSeekBarScale);
73
    barScale.setOnSeekBarChangeListener(this);
74
    barScale.setProgress(100);
71 75
    }
72 76

  
73 77
///////////////////////////////////////////////////////////////////////////////////////////////////
......
117 121

  
118 122
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
119 123
    {
120
    float s = (progress>50 ? ((progress-50)/16.0f + 1.0f):(0.015f*progress + 0.25f));
121 124
    SaveSurfaceView view = (SaveSurfaceView) this.findViewById(R.id.saveSurfaceView);
122
    view.getRenderer().setSize(s);
125

  
126
    switch (bar.getId())
127
      {
128
      case R.id.saveSeekBarSize : view.getRenderer().setSize(progress>50 ? ((progress-50)/16.0f + 1.0f):(0.015f*progress + 0.25f));
129
                                  break;
130
      case R.id.saveSeekBarScale: view.getRenderer().setScale(0.009f*progress+0.1f);
131
                                  break;
132
      }
123 133
    }
124 134

  
125 135
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/save/SaveRenderer.java
35 35
import org.distorted.library.DistortedFramebuffer;
36 36
import org.distorted.library.EffectTypes;
37 37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Dynamic3D;
38 39
import org.distorted.library.type.Static1D;
39 40
import org.distorted.library.type.Static3D;
40 41
import org.distorted.library.type.Static4D;
......
56 57
  private Static4D sinkRegion;
57 58
  private Dynamic1D diSink;
58 59
  private Static1D s0;
60
  private Dynamic3D mScaleDyn;
61
  private Static3D mScaleFactor;
59 62

  
60
  private int fboHeight, fboWidth;
63
  private float mScale;
61 64
  private int bmpHeight, bmpWidth;
62 65
  private int scrHeight, scrWidth;
63 66
  private float boobsSink;
......
79 82
      
80 83
    s0 = new Static1D(boobsSink);
81 84
      
82
    diSink = new Dynamic1D(0,0.5f);
85
    diSink = new Dynamic1D();
83 86
    diSink.add(s0);
84 87

  
85
    fboHeight = 150;
86
    fboWidth  = 100;
88
    mScale = 1.0f;
89
    mScaleDyn = new Dynamic3D();
90
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
91
    mScaleDyn.add(mScaleFactor);
87 92
    }
88 93

  
89 94
///////////////////////////////////////////////////////////////////////////////////////////////////
......
94 99
    s0.set(boobsSink);
95 100
    }
96 101

  
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

  
104
  void setScale(float s)
105
    {
106
    mScale = s;
107
    mScaleFactor.set(s,s,1.0f);
108

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

  
113
    if( mOffscreen!=null )
114
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
115
    }
116

  
97 117
///////////////////////////////////////////////////////////////////////////////////////////////////
98 118

  
99 119
  void Save()
......
125 145

  
126 146
    if( isSaving )  // render to an offscreen buffer and read pixels
127 147
      {
128
      long scaleID = mGirl.scale(new Static3D( (float)fboWidth/scrWidth, (float)fboHeight/scrHeight, 1.0f));
148
      mGirl.abortEffects(EffectTypes.MATRIX);
149
      mGirl.scale(mScaleFactor);
129 150
      mGirl.draw(System.currentTimeMillis(), mOffscreen);
130
      mGirl.abortEffect(scaleID);
151
      applyMatrixEffects(scrWidth,scrHeight);
131 152

  
132
      ByteBuffer buf = ByteBuffer.allocateDirect( fboWidth*fboHeight*4 );
153
      int fW =(int)(mScale*bmpWidth);
154
      int fH =(int)(mScale*bmpHeight);
155
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
133 156
      buf.order(ByteOrder.LITTLE_ENDIAN);
134 157
      mOffscreen.setAsInput();
135 158

  
136
      GLES20.glReadPixels( 0, 0, fboWidth, fboHeight , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
137
      SaveWorkerThread.newBuffer(buf,fboWidth,fboHeight,mPath);
159
      GLES20.glReadPixels( 0, 0, fW, fH, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
160
      SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
138 161

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

  
165
    mGirl.draw(System.currentTimeMillis());
145 166
    }
146 167

  
168

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

  
171
  private void applyMatrixEffects(int width, int height)
172
    {
154 173
    mGirl.abortEffects(EffectTypes.MATRIX);
155
      
156
    if( bmpHeight/bmpWidth > height/width )
174

  
175
    if( (float)bmpHeight/bmpWidth > (float)height/width )
157 176
      {
158 177
      int w = (height*bmpWidth)/bmpHeight;
159 178
      float factor = (float)height/bmpHeight;
......
169 188
      mGirl.move( new Static3D(0,(height-h)/2,0) );
170 189
      mGirl.scale(factor);
171 190
      }
172
      
191

  
192
    mGirl.scale(mScaleDyn);
193
    }
194

  
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

  
197
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
198
    {
199
    scrWidth = width;
200
    scrHeight= height;
201
    applyMatrixEffects(width, height);
173 202
    Distorted.onSurfaceChanged(width, height);
174 203
    }
175 204

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

  
206
    mOffscreen = new DistortedFramebuffer(fboWidth,fboHeight);
235
    mOffscreen = new DistortedFramebuffer(bmpWidth,bmpHeight);
207 236

  
208 237
    try
209 238
      {
src/main/java/org/distorted/examples/sink/SinkRenderer.java
76 76
    { 
77 77
    sinkBmp.abortEffects(EffectTypes.MATRIX);
78 78
         
79
    if( bmpHeight/bmpWidth > height/width )
79
    if( (float)bmpHeight/bmpWidth > (float)height/width )
80 80
      {
81 81
      int w = (height*bmpWidth)/bmpHeight;
82 82
      float factor = (float)height/bmpHeight;
src/main/res/layout/savelayout.xml
8 8
        android:id="@+id/saveSurfaceView"
9 9
        android:layout_width="fill_parent"
10 10
        android:layout_height="0dp"
11
        android:layout_weight="0.85" />
11
        android:layout_weight="0.9"/>
12 12

  
13 13
    <LinearLayout
14 14
        android:orientation="horizontal"
15 15
        android:layout_width="match_parent"
16 16
        android:layout_height="wrap_content"
17
        android:gravity="bottom"
18
        android:layout_weight="0.03">
17
        android:gravity="bottom">
19 18

  
20 19
        <Button
21 20
            android:id="@+id/saveButton"
22
            android:layout_width="132dp"
23
            android:layout_height="wrap_content"
21
            android:layout_width="100dp"
22
            android:layout_height="fill_parent"
24 23
            android:gravity="center_vertical|center"
25 24
            android:text="@string/save"
26 25
            android:onClick="Save"
27 26
            android:textAppearance="?android:attr/textAppearanceMedium"
28 27
            android:layout_gravity="center_vertical"/>
29 28

  
30
        <SeekBar
31
            android:layout_width="fill_parent"
32
            android:layout_height="wrap_content"
33
            android:id="@+id/saveSeekBar"
34
            android:layout_gravity="center_vertical"
35
            android:layout_margin="5dp"/>
29
        <LinearLayout
30
            android:orientation="vertical"
31
            android:layout_width="match_parent"
32
            android:layout_height="match_parent">
33

  
34
            <SeekBar
35
                android:layout_width="match_parent"
36
                android:layout_height="wrap_content"
37
                android:id="@+id/saveSeekBarSize"
38
                android:layout_gravity="center_vertical"
39
                android:layout_margin="5dp"/>
40
            <SeekBar
41
                android:layout_width="match_parent"
42
                android:layout_height="wrap_content"
43
                android:id="@+id/saveSeekBarScale"
44
                android:layout_gravity="center_vertical"
45
                android:layout_margin="5dp"/>
46
        </LinearLayout>
36 47

  
37 48
    </LinearLayout>
38 49

  

Also available in: Unified diff