Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 251e574e

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

    
51
  private GLSurfaceView mView;
52
  private DistortedObject mCube;
53
  private DistortedCubesGrid mGrid;
54

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

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

    
65
    mRot = new DynamicQuat();
66

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

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

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
98
    {
99
    float scaleFactor = width>height ? height/3:width/3;
100

    
101
    mCube.abortEffects(EffectTypes.MATRIX);
102
    mCube.move( new Static3D( (width-scaleFactor)/2 , (height-scaleFactor)/2 , 0) );
103
    mCube.scale(scaleFactor);
104
    mCube.quaternion( mRot, new Static3D(0.5f,0.5f,0) );
105
       
106
    Distorted.onSurfaceChanged(width, height); 
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
112
    {
113
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
114

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