Project

General

Profile

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

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

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