Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 8cb9208c

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