Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ fe59d375

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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
32 d218d64e leszek
import org.distorted.library.DistortedScreen;
33 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
34 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic;
35
import org.distorted.library.type.DynamicQuat;
36 b01acdaf Leszek Koltunski
import org.distorted.library.MeshCubes;
37 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Static4D;
39
import org.distorted.library.type.Static3D;
40 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
41 427ab7bf Leszek Koltunski
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
45 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
class QuaternionRenderer implements GLSurfaceView.Renderer 
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 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
56 d218d64e leszek
  private DistortedScreen mScreen;
57 7589635e Leszek Koltunski
  private DynamicQuat mRot;
58 427ab7bf Leszek Koltunski
    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 b695a86a Leszek Koltunski
  QuaternionRenderer(GLSurfaceView v)
62 bc0a685b Leszek Koltunski
    {
63 f6d884d5 Leszek Koltunski
    mView    = v;
64 7451c98a Leszek Koltunski
    mTexture = new DistortedTexture(1,1);
65 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
66 f6d884d5 Leszek Koltunski
    mRot     = 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 7589635e Leszek Koltunski
      mRot.add(new Static4D(x/len,y/len,z/len,w/len));
81 427ab7bf Leszek Koltunski
      }
82 bc0a685b Leszek Koltunski
    
83
    mRot.setCount(0);
84
    mRot.setDuration(8000);
85 7589635e Leszek Koltunski
    mRot.setMode(Dynamic.MODE_LOOP);
86 392e16fd Leszek Koltunski
87 d218d64e leszek
    mScreen = new DistortedScreen();
88 fe59d375 Leszek Koltunski
    mScreen.attach(mTexture,mEffects,new MeshCubes(1,1,false));
89 bc0a685b Leszek Koltunski
    }
90 427ab7bf Leszek Koltunski
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
   
93 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
94
    {
95 41a81a14 Leszek Koltunski
    GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
96 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
97 bc0a685b Leszek Koltunski
    }
98 427ab7bf Leszek Koltunski
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
    
101 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
102
    {
103 251e574e Leszek Koltunski
    float scaleFactor = width>height ? height/3:width/3;
104 427ab7bf Leszek Koltunski
105 392e16fd Leszek Koltunski
    mEffects.abortEffects(EffectTypes.MATRIX);
106
    mEffects.move( new Static3D( (width-scaleFactor)/2 , (height-scaleFactor)/2 , 0) );
107
    mEffects.scale(scaleFactor);
108 ff31e865 Leszek Koltunski
    mEffects.quaternion( mRot, new Static3D( (float)mTexture.getWidth()/2, (float)mTexture.getHeight()/2 , 0) );
109 427ab7bf Leszek Koltunski
       
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 41a81a14 Leszek Koltunski
    GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
118 e7a4ef16 Leszek Koltunski
119 bc0a685b Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
120
    Bitmap bitmap;
121 427ab7bf Leszek Koltunski
        
122 bc0a685b Leszek Koltunski
    try 
123
      {
124
      bitmap = BitmapFactory.decodeStream(is);
125
      } 
126
    finally 
127
      {
128 427ab7bf Leszek Koltunski
      try 
129
        {
130 bc0a685b Leszek Koltunski
        is.close();
131 427ab7bf Leszek Koltunski
        } 
132 bc0a685b Leszek Koltunski
      catch(IOException e) { }
133
      }  
134 427ab7bf Leszek Koltunski
      
135 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
136 427ab7bf Leszek Koltunski
      
137 bc0a685b Leszek Koltunski
    try
138
      {
139 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
140 bc0a685b Leszek Koltunski
      }
141
    catch(Exception ex)
142
      {
143
      android.util.Log.e("Quaternion", ex.getMessage() );
144 427ab7bf Leszek Koltunski
      }
145 bc0a685b Leszek Koltunski
    }
146
  }