Project

General

Profile

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

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

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.DistortedScreen;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Dynamic;
36
import org.distorted.library.type.DynamicQuat;
37
import org.distorted.library.MeshCubes;
38
import org.distorted.library.DistortedTexture;
39
import org.distorted.library.type.Static4D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.Distorted;
42

    
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLES30;
46
import android.opengl.GLSurfaceView;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
54
  private GLSurfaceView mView;
55
  private DistortedTexture mTexture;
56
  private DistortedEffects mEffects;
57
  private DistortedScreen mScreen;
58
  private MeshCubes mMesh;
59
  private DynamicQuat mRot;
60
    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

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

    
90
    mScreen = new DistortedScreen();
91
    }
92

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

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

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

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

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