Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ f338550a

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.MeshFlat;
29
import org.distorted.library.DistortedTexture;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32

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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

    
48
   private float mF, mX, mY;
49
   private int mWidth, mHeight;
50

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

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

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

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

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

    
71
   int setX(int x)
72
      {
73
      mX = (x-50)*0.02f*mWidth;
74
      mScreen.setProjection(mF,mX,mY);
75
      return (int)mX;
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
   int setY(int y)
81
      {
82
      mY = (y-50)*0.02f*mHeight;
83
      mScreen.setProjection(mF,mX,mY);
84
      return (int)mY;
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
   
89
   public void onDrawFrame(GL10 glUnused) 
90
      {
91
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
92
      mScreen.render( System.currentTimeMillis() );
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
98
      {
99
      mWidth = width;
100
      mHeight= height;
101

    
102
      mScreen.setProjection(mF,mX,mY);
103
      mEffects.abortAllEffects();
104

    
105
      Paint paint = new Paint();
106
      DistortedTexture texture= new DistortedTexture(width,height);
107
      MeshFlat mesh = new MeshFlat(50,50*height/width);
108
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
109
      Canvas bmpCanvas = new Canvas(bmp);
110

    
111
      paint.setColor(0xff008800);
112
      paint.setStyle(Style.FILL);
113
      bmpCanvas.drawRect(0, 0, width, height, paint);
114
      paint.setColor(0xffffffff);
115

    
116
      final int NUMLINES = 10;
117

    
118
      for(int i=0; i<=NUMLINES ; i++ )
119
        {
120
        bmpCanvas.drawRect(width*i/NUMLINES - 1,                    0,  width*i/NUMLINES + 1,  height               , paint);
121
        bmpCanvas.drawRect(                   0, height*i/NUMLINES -1,  width               ,  height*i/NUMLINES + 1, paint);
122
        }
123

    
124
      texture.setTexture(bmp);
125

    
126
      int min = width<height ? width:height;
127

    
128
      Static3D vector = new Static3D(0,0,min/5);
129
      Static4D region = new Static4D(0,0,min/5,min/5);
130

    
131
      mEffects.distort(vector, new Static3D(  width/4,   height/4, 0), region);
132
      mEffects.distort(vector, new Static3D(3*width/4,   height/4, 0), region);
133
      mEffects.distort(vector, new Static3D(  width/4, 3*height/4, 0), region);
134
      mEffects.distort(vector, new Static3D(3*width/4, 3*height/4, 0), region);
135

    
136
      mScreen.detachAll();
137
      mScreen.attach(texture,mEffects,mesh);
138
      mScreen.resize(width, height);
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
    
143
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
144
      {
145
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
146

    
147
      try
148
        {
149
        Distorted.onCreate(mView.getContext());
150
        }
151
      catch(Exception ex)
152
        {
153
        android.util.Log.e("Projection", ex.getMessage() );
154
        }
155
      }
156
}
(2-2/3)