Project

General

Profile

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

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

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 427ab7bf Leszek Koltunski
20 10d53839 Leszek Koltunski
package org.distorted.examples.projection;
21 427ab7bf Leszek Koltunski
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24
25 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
26 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
27 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
28 d218d64e leszek
import org.distorted.library.DistortedScreen;
29 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
30 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
31 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33 427ab7bf Leszek Koltunski
34
import android.graphics.Bitmap;
35
import android.graphics.Canvas;
36
import android.graphics.Paint;
37
import android.graphics.Paint.Style;
38 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
39 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
40
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 10d53839 Leszek Koltunski
class ProjectionRenderer implements GLSurfaceView.Renderer
44 427ab7bf Leszek Koltunski
{
45
   private GLSurfaceView mView;
46 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
47 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
48 d218d64e leszek
   private DistortedScreen mScreen;
49 b01acdaf Leszek Koltunski
   private MeshFlat mMesh;
50 10d53839 Leszek Koltunski
51 392e16fd Leszek Koltunski
   private float mF, mX, mY;
52
   private int mWidth, mHeight;
53 2f9752c5 Leszek Koltunski
54 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 10d53839 Leszek Koltunski
   ProjectionRenderer(GLSurfaceView view)
57 427ab7bf Leszek Koltunski
      { 
58 392e16fd Leszek Koltunski
      mView   = view;
59 d04a4886 Leszek Koltunski
      mEffects= new DistortedEffects();
60 d218d64e leszek
      mScreen = new DistortedScreen();
61 427ab7bf Leszek Koltunski
      }
62
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 392e16fd Leszek Koltunski
   int setFOV(int f)
66 427ab7bf Leszek Koltunski
      {
67 10d53839 Leszek Koltunski
      mF = f;
68 392e16fd Leszek Koltunski
      mScreen.setProjection(mF,mX,mY);
69 10d53839 Leszek Koltunski
      return (int)mF;
70 427ab7bf Leszek Koltunski
      }
71 10d53839 Leszek Koltunski
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 392e16fd Leszek Koltunski
   int setX(int x)
75 10d53839 Leszek Koltunski
      {
76 8cb9208c Leszek Koltunski
      mX = (x-50)*0.02f*mWidth;
77 392e16fd Leszek Koltunski
      mScreen.setProjection(mF,mX,mY);
78 10d53839 Leszek Koltunski
      return (int)mX;
79
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83 392e16fd Leszek Koltunski
   int setY(int y)
84 10d53839 Leszek Koltunski
      {
85 8cb9208c Leszek Koltunski
      mY = (y-50)*0.02f*mHeight;
86 392e16fd Leszek Koltunski
      mScreen.setProjection(mF,mX,mY);
87 10d53839 Leszek Koltunski
      return (int)mY;
88
      }
89
90 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
91
   
92
   public void onDrawFrame(GL10 glUnused) 
93
      {
94 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
95 b01acdaf Leszek Koltunski
      mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
96 427ab7bf Leszek Koltunski
      }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101 10d53839 Leszek Koltunski
      {
102
      mWidth = width;
103
      mHeight= height;
104
105 392e16fd Leszek Koltunski
      mScreen.setProjection(mF,mX,mY);
106
      mEffects.abortAllEffects();
107 95bc9f69 Leszek Koltunski
108 2f9752c5 Leszek Koltunski
      Paint paint = new Paint();
109 7451c98a Leszek Koltunski
      mTexture    = new DistortedTexture(width,height);
110 b01acdaf Leszek Koltunski
      mMesh = new MeshFlat(50,50*height/width);
111 f6d884d5 Leszek Koltunski
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
112 10d53839 Leszek Koltunski
      Canvas bmpCanvas = new Canvas(bmp);
113 427ab7bf Leszek Koltunski
114 2f9752c5 Leszek Koltunski
      paint.setColor(0xff008800);
115
      paint.setStyle(Style.FILL);
116 f6d884d5 Leszek Koltunski
      bmpCanvas.drawRect(0, 0, width, height, paint);
117 2f9752c5 Leszek Koltunski
      paint.setColor(0xffffffff);
118 10d53839 Leszek Koltunski
119
      final int NUMLINES = 10;
120
121 427ab7bf Leszek Koltunski
      for(int i=0; i<=NUMLINES ; i++ )
122
        {
123 f6d884d5 Leszek Koltunski
        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 427ab7bf Leszek Koltunski
        }
126 f6d884d5 Leszek Koltunski
127
      mTexture.setTexture(bmp);
128
129
      int min = width<height ? width:height;
130
131 7bf107f7 Leszek Koltunski
      Static3D vector = new Static3D(0,0,min/5);
132
      Static4D region = new Static4D(0,0,min/5,min/5);
133
134 392e16fd Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
139 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
140 427ab7bf Leszek Koltunski
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144 f6d884d5 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
145
      {
146 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
147 f6d884d5 Leszek Koltunski
148
      try
149
        {
150 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
151 f6d884d5 Leszek Koltunski
        }
152
      catch(Exception ex)
153
        {
154
        android.util.Log.e("Projection", ex.getMessage() );
155
        }
156
      }
157 427ab7bf Leszek Koltunski
}