Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 855ba24e

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.projection;
21

    
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

    
25
import org.distorted.library.Distorted;
26
import org.distorted.library.DistortedEffects;
27
import org.distorted.library.DistortedScreen;
28
import org.distorted.library.EffectNames;
29
import org.distorted.library.MeshFlat;
30
import org.distorted.library.DistortedTexture;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33

    
34
import android.graphics.Bitmap;
35
import android.graphics.Canvas;
36
import android.graphics.Paint;
37
import android.graphics.Paint.Style;
38
import android.opengl.GLES30;
39
import android.opengl.GLSurfaceView;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class ProjectionRenderer implements GLSurfaceView.Renderer
44
{
45
   private GLSurfaceView mView;
46
   private DistortedEffects mEffects;
47
   private DistortedScreen mScreen;
48

    
49
   private float mF, mNear;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
   ProjectionRenderer(GLSurfaceView view)
54
      { 
55
      mView   = view;
56
      mEffects= new DistortedEffects();
57
      mScreen = new DistortedScreen();
58
      }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
   float setFOV(int f)
63
      {
64
      mF = f;
65
      mScreen.setProjection(mF,mNear);
66
      return mF;
67
      }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
   float setNear(int near)
72
      {
73
      mNear = near/100.0f;
74
      mScreen.setProjection(mF,mNear);
75
      return mNear;
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
   
80
   public void onDrawFrame(GL10 glUnused) 
81
      {
82
      mScreen.render( System.currentTimeMillis() );
83
      }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
    
87
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
88
      {
89
      mScreen.setProjection(mF,mNear);
90
      mEffects.abortAllEffects();
91

    
92
      Paint paint = new Paint();
93
      DistortedTexture texture= new DistortedTexture(width,height);
94
      MeshFlat mesh = new MeshFlat(50,50*height/width);
95
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
96
      Canvas bmpCanvas = new Canvas(bmp);
97

    
98
      paint.setColor(0xff008800);
99
      paint.setStyle(Style.FILL);
100
      bmpCanvas.drawRect(0, 0, width, height, paint);
101
      paint.setColor(0xffffffff);
102

    
103
      final int NUMLINES = 10;
104

    
105
      for(int i=0; i<=NUMLINES ; i++ )
106
        {
107
        bmpCanvas.drawRect(width*i/NUMLINES - 1,                    0,  width*i/NUMLINES + 1,  height               , paint);
108
        bmpCanvas.drawRect(                   0, height*i/NUMLINES -1,  width               ,  height*i/NUMLINES + 1, paint);
109
        }
110

    
111
      texture.setTexture(bmp);
112

    
113
      int min = width<height ? width:height;
114

    
115
      Static3D vector = new Static3D(0,0,min/5);
116
      Static4D region = new Static4D(0,0,min/5,min/5);
117

    
118
      mEffects.distort(vector, new Static3D(  width/4,   height/4, 0), region);
119
      mEffects.distort(vector, new Static3D(3*width/4,   height/4, 0), region);
120
      mEffects.distort(vector, new Static3D(  width/4, 3*height/4, 0), region);
121
      mEffects.distort(vector, new Static3D(3*width/4, 3*height/4, 0), region);
122

    
123
      mScreen.detachAll();
124
      mScreen.attach(texture,mEffects,mesh);
125
      mScreen.resize(width, height);
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
    
130
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
131
      {
132
      DistortedEffects.enableEffect(EffectNames.DISTORT);
133

    
134
      try
135
        {
136
        Distorted.onCreate(mView.getContext());
137
        }
138
      catch(Exception ex)
139
        {
140
        android.util.Log.e("Projection", ex.getMessage() );
141
        }
142
      }
143
}
(2-2/3)