Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 10b7e588

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 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
27 e8b6aa95 Leszek Koltunski
import org.distorted.library.DistortedObject;
28 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
29
import org.distorted.library.type.Static4D;
30 427ab7bf Leszek Koltunski
31
import android.graphics.Bitmap;
32
import android.graphics.Canvas;
33
import android.graphics.Paint;
34
import android.graphics.Paint.Style;
35
import android.opengl.GLES20;
36
import android.opengl.GLSurfaceView;
37
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 10d53839 Leszek Koltunski
class ProjectionRenderer implements GLSurfaceView.Renderer
41 427ab7bf Leszek Koltunski
{
42
   private GLSurfaceView mView;
43 e8b6aa95 Leszek Koltunski
   private DistortedObject mProjection;
44 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
45 10d53839 Leszek Koltunski
46
   private static float mF, mX, mY;
47
   private static int mWidth, mHeight;
48 2f9752c5 Leszek Koltunski
49 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 10d53839 Leszek Koltunski
   ProjectionRenderer(GLSurfaceView view)
52 427ab7bf Leszek Koltunski
      { 
53
      mView = view;
54
      }
55
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 10d53839 Leszek Koltunski
   static int setFOV(int f)
59 427ab7bf Leszek Koltunski
      {
60 10d53839 Leszek Koltunski
      mF = f;
61
      Distorted.setProjection(mF,mX,mY);
62
      return (int)mF;
63 427ab7bf Leszek Koltunski
      }
64 10d53839 Leszek Koltunski
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67
   static int setX(int x)
68
      {
69 8cb9208c Leszek Koltunski
      mX = (x-50)*0.02f*mWidth;
70 10d53839 Leszek Koltunski
      Distorted.setProjection(mF,mX,mY);
71
      return (int)mX;
72
      }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
   static int setY(int y)
77
      {
78 8cb9208c Leszek Koltunski
      mY = (y-50)*0.02f*mHeight;
79 10d53839 Leszek Koltunski
      Distorted.setProjection(mF,mX,mY);
80
      return (int)mY;
81
      }
82
83 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
84
   
85
   public void onDrawFrame(GL10 glUnused) 
86
      {
87
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
88 e8b6aa95 Leszek Koltunski
      mProjection.draw(System.currentTimeMillis(), mGrid);
89 427ab7bf Leszek Koltunski
      }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
94 10d53839 Leszek Koltunski
      {
95
      mWidth = width;
96
      mHeight= height;
97
98 427ab7bf Leszek Koltunski
      Distorted.onSurfaceChanged(width, height);
99
      setupBitmap(width,height);
100
      }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
    
104
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
105 e7a4ef16 Leszek Koltunski
      {
106
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
107
108 427ab7bf Leszek Koltunski
      try
109
        {
110 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
111 427ab7bf Leszek Koltunski
        }
112
      catch(Exception ex)
113
        {
114 10d53839 Leszek Koltunski
        android.util.Log.e("Projection", ex.getMessage() );
115 427ab7bf Leszek Koltunski
        }
116
      }
117
    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
    private void setupBitmap(int w, int h)
121
      {
122 2f9752c5 Leszek Koltunski
      Paint paint = new Paint();
123 e8b6aa95 Leszek Koltunski
      mProjection = new DistortedObject(w,h,1);
124 10b7e588 Leszek Koltunski
      mGrid       = new GridFlat(50,50*h/w);
125 10d53839 Leszek Koltunski
      Bitmap bmp = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
126
      Canvas bmpCanvas = new Canvas(bmp);
127 427ab7bf Leszek Koltunski
128 2f9752c5 Leszek Koltunski
      paint.setColor(0xff008800);
129
      paint.setStyle(Style.FILL);
130 10d53839 Leszek Koltunski
      bmpCanvas.drawRect(0, 0, w, h, paint);
131 2f9752c5 Leszek Koltunski
      paint.setColor(0xffffffff);
132 10d53839 Leszek Koltunski
133
      final int NUMLINES = 10;
134
135 427ab7bf Leszek Koltunski
      for(int i=0; i<=NUMLINES ; i++ )
136
        {
137 10d53839 Leszek Koltunski
        bmpCanvas.drawRect(w*i/NUMLINES - 1,                0,  w*i/NUMLINES + 1,  h               , paint);
138
        bmpCanvas.drawRect(               0, h *i/NUMLINES -1,  w               ,  h*i/NUMLINES + 1, paint);
139 427ab7bf Leszek Koltunski
        }
140
        
141 e8b6aa95 Leszek Koltunski
      mProjection.setTexture(bmp);
142 427ab7bf Leszek Koltunski
        
143
      int min = w<h ? w:h;
144
        
145 7bf107f7 Leszek Koltunski
      Static3D vector = new Static3D(0,0,min/5);
146
      Static4D region = new Static4D(0,0,min/5,min/5);
147
148 10d53839 Leszek Koltunski
      mProjection.distort(vector, new Static3D(  w/4,   h/4, 0), region);
149
      mProjection.distort(vector, new Static3D(3*w/4,   h/4, 0), region);
150
      mProjection.distort(vector, new Static3D(  w/4, 3*h/4, 0), region);
151
      mProjection.distort(vector, new Static3D(3*w/4, 3*h/4, 0), region);
152 427ab7bf Leszek Koltunski
      }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
}