Project

General

Profile

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

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

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.DistortedFramebuffer;
28
import org.distorted.library.DistortedScreen;
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 DistortedTexture mTexture;
47
   private DistortedEffects mEffects;
48
   private DistortedScreen mScreen;
49
   private MeshFlat mMesh;
50

    
51
   private float mF, mX, mY;
52
   private int mWidth, mHeight;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
   ProjectionRenderer(GLSurfaceView view)
57
      { 
58
      mView   = view;
59
      mEffects= new DistortedEffects();
60
      mScreen = new DistortedScreen();
61
      }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   int setFOV(int f)
66
      {
67
      mF = f;
68
      mScreen.setProjection(mF,mX,mY);
69
      return (int)mF;
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
   
92
   public void onDrawFrame(GL10 glUnused) 
93
      {
94
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
95
      mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101
      {
102
      mWidth = width;
103
      mHeight= height;
104

    
105
      mScreen.setProjection(mF,mX,mY);
106
      mEffects.abortAllEffects();
107

    
108
      Paint paint = new Paint();
109
      mTexture    = new DistortedTexture(width,height);
110
      mMesh = new MeshFlat(50,50*height/width);
111
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
112
      Canvas bmpCanvas = new Canvas(bmp);
113

    
114
      paint.setColor(0xff008800);
115
      paint.setStyle(Style.FILL);
116
      bmpCanvas.drawRect(0, 0, width, height, paint);
117
      paint.setColor(0xffffffff);
118

    
119
      final int NUMLINES = 10;
120

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

    
127
      mTexture.setTexture(bmp);
128

    
129
      int min = width<height ? width:height;
130

    
131
      Static3D vector = new Static3D(0,0,min/5);
132
      Static4D region = new Static4D(0,0,min/5,min/5);
133

    
134
      mEffects.distort(vector, new Static3D(  width/4,   height/4, 0), region);
135
      mEffects.distort(vector, new Static3D(3*width/4,   height/4, 0), region);
136
      mEffects.distort(vector, new Static3D(  width/4, 3*height/4, 0), region);
137
      mEffects.distort(vector, new Static3D(3*width/4, 3*height/4, 0), region);
138

    
139
      mScreen.resize(width, height);
140
      }
141

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

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