Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 107e4b72

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 5068fa06 Leszek Koltunski
package org.distorted.examples.quaternion;
21 427ab7bf Leszek Koltunski
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.util.Random;
25
26
import javax.microedition.khronos.egl.EGLConfig;
27
import javax.microedition.khronos.opengles.GL10;
28
29 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
30 427ab7bf Leszek Koltunski
31 c6526577 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedScreen;
36 107e4b72 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshObject;
38
import org.distorted.library.mesh.MeshCubes;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic;
40
import org.distorted.library.type.DynamicQuat;
41
import org.distorted.library.type.Static4D;
42
import org.distorted.library.type.Static3D;
43 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
44 427ab7bf Leszek Koltunski
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
class QuaternionRenderer implements GLSurfaceView.Renderer 
52 bc0a685b Leszek Koltunski
  {
53
  private static final int NUM_QUATERNIONS = 5;
54 251e574e Leszek Koltunski
55 bc0a685b Leszek Koltunski
  private GLSurfaceView mView;
56 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
57 8a99c681 Leszek Koltunski
  private MeshObject mMesh;
58 d218d64e leszek
  private DistortedScreen mScreen;
59 c6526577 Leszek Koltunski
  private Static3D mMove, mScale, mCenter;
60
61 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 b695a86a Leszek Koltunski
  QuaternionRenderer(GLSurfaceView v)
64 bc0a685b Leszek Koltunski
    {
65 f6d884d5 Leszek Koltunski
    mView    = v;
66 7451c98a Leszek Koltunski
    mTexture = new DistortedTexture(1,1);
67 58e9e190 leszek
    mMesh    = new MeshCubes(1,1,1);
68 c6526577 Leszek Koltunski
69
    DistortedEffects effects = new DistortedEffects();
70
    DynamicQuat rot = new DynamicQuat();
71 b695a86a Leszek Koltunski
72
    Random rnd = new Random(System.currentTimeMillis());
73 bc0a685b Leszek Koltunski
    float x,y,z,w, len;
74 427ab7bf Leszek Koltunski
      
75 bc0a685b Leszek Koltunski
    for(int i=0; i<NUM_QUATERNIONS; i++)
76
      {
77 b695a86a Leszek Koltunski
      x = 2*rnd.nextFloat()-1;
78
      y = 2*rnd.nextFloat()-1;
79
      z = 2*rnd.nextFloat()-1;
80
      w = 2*rnd.nextFloat()-1;
81 427ab7bf Leszek Koltunski
    	 
82 bc0a685b Leszek Koltunski
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
83 427ab7bf Leszek Koltunski
    		
84 c6526577 Leszek Koltunski
      rot.add(new Static4D(x/len,y/len,z/len,w/len));
85 427ab7bf Leszek Koltunski
      }
86 bc0a685b Leszek Koltunski
    
87 c6526577 Leszek Koltunski
    rot.setCount(0);
88
    rot.setDuration(8000);
89
    rot.setMode(Dynamic.MODE_LOOP);
90
91
    mMove   = new Static3D(0,0,0);
92
    mScale  = new Static3D(1,1,1);
93
    mCenter = new Static3D(0,0,0);
94
95
    effects.apply( new MatrixEffectMove(mMove));
96
    effects.apply(new MatrixEffectScale(mScale));
97
    effects.apply( new MatrixEffectQuaternion(rot,mCenter) );
98 392e16fd Leszek Koltunski
99 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
100 c6526577 Leszek Koltunski
    mScreen.attach(mTexture,effects,mMesh);
101 bc0a685b Leszek Koltunski
    }
102 427ab7bf Leszek Koltunski
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
   
105 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
106
    {
107 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
108 bc0a685b Leszek Koltunski
    }
109 427ab7bf Leszek Koltunski
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
113
    {
114 8a99c681 Leszek Koltunski
    float w = mTexture.getWidth();
115
    float h = mTexture.getHeight();
116
    float d = mTexture.getDepth(mMesh);
117
118 c6526577 Leszek Koltunski
    float factor = 0.5f*(width>height ? height/h:width/w);
119
120
    mCenter.set(w/2,h/2,-d/2);
121
    mMove.set( (width-factor*w)/2 , (height-factor*h)/2 , 0 );
122
    mScale.set(factor,factor,factor);
123 427ab7bf Leszek Koltunski
124 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
125 bc0a685b Leszek Koltunski
    }
126 427ab7bf Leszek Koltunski
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
130
    {
131
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
132
    Bitmap bitmap;
133 427ab7bf Leszek Koltunski
        
134 bc0a685b Leszek Koltunski
    try 
135
      {
136
      bitmap = BitmapFactory.decodeStream(is);
137
      } 
138
    finally 
139
      {
140 427ab7bf Leszek Koltunski
      try 
141
        {
142 bc0a685b Leszek Koltunski
        is.close();
143 427ab7bf Leszek Koltunski
        } 
144 bc0a685b Leszek Koltunski
      catch(IOException e) { }
145
      }  
146 427ab7bf Leszek Koltunski
      
147 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
148 427ab7bf Leszek Koltunski
      
149 bc0a685b Leszek Koltunski
    try
150
      {
151 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
152 bc0a685b Leszek Koltunski
      }
153
    catch(Exception ex)
154
      {
155
      android.util.Log.e("Quaternion", ex.getMessage() );
156 427ab7bf Leszek Koltunski
      }
157 bc0a685b Leszek Koltunski
    }
158
  }