Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 061449ed

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 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.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
36 107e4b72 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshCubes;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic;
39
import org.distorted.library.type.DynamicQuat;
40
import org.distorted.library.type.Static4D;
41
import org.distorted.library.type.Static3D;
42 427ab7bf Leszek Koltunski
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLSurfaceView;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 061449ed Leszek Koltunski
class QuaternionRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
50 bc0a685b Leszek Koltunski
  {
51
  private static final int NUM_QUATERNIONS = 5;
52 251e574e Leszek Koltunski
53 bc0a685b Leszek Koltunski
  private GLSurfaceView mView;
54 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
55 d218d64e leszek
  private DistortedScreen mScreen;
56 16b22aab Leszek Koltunski
  private Static3D mScale;
57 c6526577 Leszek Koltunski
58 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 b695a86a Leszek Koltunski
  QuaternionRenderer(GLSurfaceView v)
61 bc0a685b Leszek Koltunski
    {
62 f6d884d5 Leszek Koltunski
    mView    = v;
63 687263cc Leszek Koltunski
    mTexture = new DistortedTexture();
64 c6526577 Leszek Koltunski
65 698ad0a8 Leszek Koltunski
    DistortedEffects effects = new DistortedEffects();
66 c6526577 Leszek Koltunski
    DynamicQuat rot = new DynamicQuat();
67 b695a86a Leszek Koltunski
68
    Random rnd = new Random(System.currentTimeMillis());
69 bc0a685b Leszek Koltunski
    float x,y,z,w, len;
70 427ab7bf Leszek Koltunski
      
71 bc0a685b Leszek Koltunski
    for(int i=0; i<NUM_QUATERNIONS; i++)
72
      {
73 b695a86a Leszek Koltunski
      x = 2*rnd.nextFloat()-1;
74
      y = 2*rnd.nextFloat()-1;
75
      z = 2*rnd.nextFloat()-1;
76
      w = 2*rnd.nextFloat()-1;
77 427ab7bf Leszek Koltunski
    	 
78 bc0a685b Leszek Koltunski
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
79 427ab7bf Leszek Koltunski
    		
80 c6526577 Leszek Koltunski
      rot.add(new Static4D(x/len,y/len,z/len,w/len));
81 427ab7bf Leszek Koltunski
      }
82 bc0a685b Leszek Koltunski
    
83 c6526577 Leszek Koltunski
    rot.setCount(0);
84 2666a48c Leszek Koltunski
    rot.setDuration(8000);
85 c6526577 Leszek Koltunski
    rot.setMode(Dynamic.MODE_LOOP);
86
87
    mScale  = new Static3D(1,1,1);
88
89
    effects.apply(new MatrixEffectScale(mScale));
90 16b22aab Leszek Koltunski
    effects.apply( new MatrixEffectQuaternion(rot,new Static3D(0,0,0)) );
91 392e16fd Leszek Koltunski
92 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
93 687263cc Leszek Koltunski
    mScreen.attach(mTexture,effects,new MeshCubes(1,1,1));
94 bc0a685b Leszek Koltunski
    }
95 427ab7bf Leszek Koltunski
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
   
98 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
99
    {
100 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
101 bc0a685b Leszek Koltunski
    }
102 427ab7bf Leszek Koltunski
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
106
    {
107 061449ed Leszek Koltunski
    float factor = 0.5f*Math.min(width,height);
108 8a99c681 Leszek Koltunski
109 c6526577 Leszek Koltunski
    mScale.set(factor,factor,factor);
110 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
111 bc0a685b Leszek Koltunski
    }
112 427ab7bf Leszek Koltunski
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
116
    {
117
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
118
    Bitmap bitmap;
119 427ab7bf Leszek Koltunski
        
120 bc0a685b Leszek Koltunski
    try 
121
      {
122
      bitmap = BitmapFactory.decodeStream(is);
123
      } 
124
    finally 
125
      {
126 427ab7bf Leszek Koltunski
      try 
127
        {
128 bc0a685b Leszek Koltunski
        is.close();
129 427ab7bf Leszek Koltunski
        } 
130 bc0a685b Leszek Koltunski
      catch(IOException e) { }
131
      }  
132 427ab7bf Leszek Koltunski
      
133 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
134 061449ed Leszek Koltunski
135
    DistortedLibrary.onCreate(mView.getContext(), this);
136
    }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
  public void distortedException(Exception ex)
141
    {
142
    android.util.Log.e("Quaternion", ex.getMessage() );
143 bc0a685b Leszek Koltunski
    }
144
  }