Project

General

Profile

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

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

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.effect.MatrixEffectScale;
26
import org.distorted.library.effect.VertexEffectDeform;
27
import org.distorted.library.main.DistortedLibrary;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedScreen;
30
import org.distorted.library.mesh.MeshRectangles;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34

    
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
class ProjectionRenderer implements GLSurfaceView.Renderer
44
{
45
   private GLSurfaceView mView;
46
   private DistortedEffects mEffects;
47
   private DistortedScreen mScreen;
48
   private MeshRectangles mMesh;
49
   private DistortedTexture mTexture;
50
   private float mF, mNear;
51
   private Static3D mScale;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
61
      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
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
   float setFOV(int f)
83
      {
84
      mF = f;
85
      mScreen.setProjection(mF,mNear);
86
      return mF;
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
   float setNear(int near)
92
      {
93
      mNear = near/100.0f;
94
      mScreen.setProjection(mF,mNear);
95
      return mNear;
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
   
100
   public void onDrawFrame(GL10 glUnused) 
101
      {
102
      mScreen.render( System.currentTimeMillis() );
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
108
      {
109
      mScreen.setProjection(mF,mNear);
110

    
111
      Paint paint = new Paint();
112
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
113
      Canvas bmpCanvas = new Canvas(bmp);
114

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

    
120
      final int NUMLINES = 10;
121

    
122
      for(int i=0; i<=NUMLINES ; i++ )
123
        {
124
        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
        }
127

    
128
      mScale.set(width,height,width);  // TODO - width in the third scale??
129

    
130
      mTexture.setTexture(bmp);
131

    
132
      if( mMesh!=null )  mMesh.markForDeletion();
133
      mMesh = new MeshRectangles(100,100*height/width);
134

    
135
      mScreen.detachAll();
136
      mScreen.attach(mTexture,mEffects,mMesh);
137
      mScreen.resize(width, height);
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
    
142
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
143
      {
144
      VertexEffectDeform.enable();
145

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