Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.quaternion;
21

    
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
import org.distorted.examples.R;
30

    
31
import org.distorted.library.DistortedEffects;
32
import org.distorted.library.DistortedFramebuffer;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic;
35
import org.distorted.library.type.DynamicQuat;
36
import org.distorted.library.GridCubes;
37
import org.distorted.library.DistortedTexture;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.Distorted;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES20;
45
import android.opengl.GLSurfaceView;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class QuaternionRenderer implements GLSurfaceView.Renderer 
50
  {
51
  private static final int NUM_QUATERNIONS = 5;
52

    
53
  private GLSurfaceView mView;
54
  private DistortedTexture mTexture;
55
  private DistortedEffects mEffects;
56
  private DistortedFramebuffer mScreen;
57
  private GridCubes mGrid;
58
  private DynamicQuat mRot;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  QuaternionRenderer(GLSurfaceView v)
63
    {
64
    mView    = v;
65
    mGrid    = new GridCubes(1,1,false);
66
    mTexture = new DistortedTexture(1,1);
67
    mEffects = new DistortedEffects();
68
    mRot     = new DynamicQuat();
69

    
70
    Random rnd = new Random(System.currentTimeMillis());
71
    float x,y,z,w, len;
72
      
73
    for(int i=0; i<NUM_QUATERNIONS; i++)
74
      {
75
      x = 2*rnd.nextFloat()-1;
76
      y = 2*rnd.nextFloat()-1;
77
      z = 2*rnd.nextFloat()-1;
78
      w = 2*rnd.nextFloat()-1;
79
    	 
80
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
81
    		
82
      mRot.add(new Static4D(x/len,y/len,z/len,w/len));
83
      }
84
    
85
    mRot.setCount(0);
86
    mRot.setDuration(8000);
87
    mRot.setMode(Dynamic.MODE_LOOP);
88

    
89
    mScreen = new DistortedFramebuffer(0);
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
   
94
  public void onDrawFrame(GL10 glUnused) 
95
    {
96
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
97
    mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
103
    {
104
    float scaleFactor = width>height ? height/3:width/3;
105

    
106
    mEffects.abortEffects(EffectTypes.MATRIX);
107
    mEffects.move( new Static3D( (width-scaleFactor)/2 , (height-scaleFactor)/2 , 0) );
108
    mEffects.scale(scaleFactor);
109
    mEffects.quaternion( mRot, new Static3D(0.5f,0.5f,0) );
110
       
111
    mScreen.resize(width, height);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
117
    {
118
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
119

    
120
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
121
    Bitmap bitmap;
122
        
123
    try 
124
      {
125
      bitmap = BitmapFactory.decodeStream(is);
126
      } 
127
    finally 
128
      {
129
      try 
130
        {
131
        is.close();
132
        } 
133
      catch(IOException e) { }
134
      }  
135
      
136
    mTexture.setTexture(bitmap);
137
      
138
    try
139
      {
140
      Distorted.onSurfaceCreated(mView.getContext());
141
      }
142
    catch(Exception ex)
143
      {
144
      android.util.Log.e("Quaternion", ex.getMessage() );
145
      }
146
    }
147
  }
(2-2/3)