Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 18231014 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
26 01cef452 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
27 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
28 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedScreen;
30 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
31 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
32 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34 427ab7bf Leszek Koltunski
35
import android.graphics.Bitmap;
36
import android.graphics.Canvas;
37
import android.graphics.Paint;
38
import android.graphics.Paint.Style;
39
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 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
47 d218d64e leszek
   private DistortedScreen mScreen;
48 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
49 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
50 af662543 leszek
   private float mF, mNear;
51 18231014 Leszek Koltunski
   private Static3D mScale;
52 1f218177 Leszek Koltunski
53 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 10d53839 Leszek Koltunski
   ProjectionRenderer(GLSurfaceView view)
56 427ab7bf Leszek Koltunski
      { 
57 392e16fd Leszek Koltunski
      mView   = view;
58 698ad0a8 Leszek Koltunski
      mEffects= new DistortedEffects();
59 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
60 1f218177 Leszek Koltunski
61 18231014 Leszek Koltunski
      Static3D point1 = new Static3D(  0.25f,   0.25f, 0);
62
      Static3D point2 = new Static3D( -0.25f,   0.25f, 0);
63
      Static3D point3 = new Static3D(  0.25f,  -0.25f, 0);
64
      Static3D point4 = new Static3D( -0.25f,  -0.25f, 0);
65
      Static4D region = new Static4D( 0, 0, 0, 0.25f);
66
      Static3D vector = new Static3D( 0, 0, 0.25f);
67
68
      mScale = new Static3D(1,1,1);
69
70
      mEffects.apply( new MatrixEffectScale(mScale) );
71
72
      mEffects.apply( new VertexEffectDeform(vector, point1, region) );
73
      mEffects.apply( new VertexEffectDeform(vector, point2, region) );
74
      mEffects.apply( new VertexEffectDeform(vector, point3, region) );
75
      mEffects.apply( new VertexEffectDeform(vector, point4, region) );
76
77
      mTexture= new DistortedTexture();
78 427ab7bf Leszek Koltunski
      }
79
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82 af662543 leszek
   float setFOV(int f)
83 427ab7bf Leszek Koltunski
      {
84 10d53839 Leszek Koltunski
      mF = f;
85 af662543 leszek
      mScreen.setProjection(mF,mNear);
86
      return mF;
87 427ab7bf Leszek Koltunski
      }
88 10d53839 Leszek Koltunski
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91 af662543 leszek
   float setNear(int near)
92 10d53839 Leszek Koltunski
      {
93 af662543 leszek
      mNear = near/100.0f;
94
      mScreen.setProjection(mF,mNear);
95
      return mNear;
96 10d53839 Leszek Koltunski
      }
97
98 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
   
100
   public void onDrawFrame(GL10 glUnused) 
101
      {
102 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
103 427ab7bf Leszek Koltunski
      }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
108 10d53839 Leszek Koltunski
      {
109 af662543 leszek
      mScreen.setProjection(mF,mNear);
110 95bc9f69 Leszek Koltunski
111 2f9752c5 Leszek Koltunski
      Paint paint = new Paint();
112 f6d884d5 Leszek Koltunski
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
113 10d53839 Leszek Koltunski
      Canvas bmpCanvas = new Canvas(bmp);
114 427ab7bf Leszek Koltunski
115 2f9752c5 Leszek Koltunski
      paint.setColor(0xff008800);
116
      paint.setStyle(Style.FILL);
117 f6d884d5 Leszek Koltunski
      bmpCanvas.drawRect(0, 0, width, height, paint);
118 2f9752c5 Leszek Koltunski
      paint.setColor(0xffffffff);
119 10d53839 Leszek Koltunski
120
      final int NUMLINES = 10;
121
122 427ab7bf Leszek Koltunski
      for(int i=0; i<=NUMLINES ; i++ )
123
        {
124 a4d59c0b Leszek Koltunski
        bmpCanvas.drawRect(width*i/NUMLINES-1,                   0, width*i/NUMLINES+1, height             , paint);
125
        bmpCanvas.drawRect(                 0, height*i/NUMLINES-1, width             , height*i/NUMLINES+1, paint);
126 427ab7bf Leszek Koltunski
        }
127 f6d884d5 Leszek Koltunski
128 18231014 Leszek Koltunski
      mScale.set(width,height,width);  // TODO - width in the third scale??
129 f6d884d5 Leszek Koltunski
130 b0ebdf5e Leszek Koltunski
      mTexture.setTexture(bmp);
131
132 698ad0a8 Leszek Koltunski
      if( mMesh!=null )  mMesh.markForDeletion();
133 f3ca895f Leszek Koltunski
      mMesh = new MeshRectangles(100,100*height/width);
134 5f2a53b6 leszek
135 fe59d375 Leszek Koltunski
      mScreen.detachAll();
136 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
137 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
138 427ab7bf Leszek Koltunski
      }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
    
142 f6d884d5 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
143
      {
144 01cef452 Leszek Koltunski
      VertexEffectDeform.enable();
145 6637d0f2 Leszek Koltunski
146 f6d884d5 Leszek Koltunski
      try
147
        {
148 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
149 f6d884d5 Leszek Koltunski
        }
150
      catch(Exception ex)
151
        {
152
        android.util.Log.e("Projection", ex.getMessage() );
153
        }
154
      }
155 427ab7bf Leszek Koltunski
}