Project

General

Profile

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

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

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.EffectTypes;
32
import org.distorted.library.type.Dynamic;
33
import org.distorted.library.type.DynamicQuat;
34
import org.distorted.library.DistortedCubesGrid;
35
import org.distorted.library.DistortedObject;
36
import org.distorted.library.type.Static4D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.Distorted;
39

    
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLES20;
43
import android.opengl.GLSurfaceView;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class QuaternionRenderer implements GLSurfaceView.Renderer 
48
  {
49
  private static final int NUM_QUATERNIONS = 5;
50
  private static final int SIZE = 100;
51
	
52
  private GLSurfaceView mView;
53
  private DistortedObject mCube;
54
  private DistortedCubesGrid mGrid;
55

    
56
  private DynamicQuat mRot;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  QuaternionRenderer(GLSurfaceView v)
61
    {
62
    mView = v;
63
    mGrid = new DistortedCubesGrid( 3, "000010000", false);
64
    mCube = new DistortedObject(1,1,1);
65

    
66
    mRot = new DynamicQuat();
67

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

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
   
90
  public void onDrawFrame(GL10 glUnused) 
91
    {
92
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
93
    mCube.draw(System.currentTimeMillis(), mGrid);
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
    
98
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
99
    {
100
    mCube.abortEffects(EffectTypes.MATRIX);
101

    
102
    if( width > height )
103
      {
104
      mCube.move( new Static3D((width-height)/2,0,0) );
105
      mCube.scale(height/(3.0f*SIZE));
106
      }  
107
    else
108
      {
109
      mCube.move( new Static3D(0,(height-width)/2,0) );
110
      mCube.scale(width/(3.0f*SIZE));
111
      }
112
     
113
    mCube.quaternion( mRot, new Static3D(3*SIZE/2,3*SIZE/2,0) );
114
       
115
    Distorted.onSurfaceChanged(width, height); 
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
121
    {
122
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
123

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