Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ 16b22aab

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.rubik;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.library.effect.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.main.Distorted;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshCubes;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37

    
38
import javax.microedition.khronos.egl.EGLConfig;
39
import javax.microedition.khronos.opengles.GL10;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class RubikRenderer implements GLSurfaceView.Renderer
44
{
45
    private static final int CUBE_SIZE = 3;
46
    private static final int VERTICES  = 5;
47
    private static final int SIZE      = 200;
48

    
49
    private GLSurfaceView mView;
50
    private DistortedTexture mTexture;
51
    private DistortedScreen mScreen;
52
    private Static3D mScale;
53
    private MeshCubes[][][] mCubes;
54
    private DistortedEffects[][][] mEffects;
55

    
56
    Static4D mQuat1, mQuat2;
57
    int mScreenMin;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
    RubikRenderer(GLSurfaceView v)
62
      {
63
      mView = v;
64

    
65
      mScreen = new DistortedScreen();
66
   // mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
67

    
68
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
69
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
70

    
71
      mCubes = new MeshCubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
72
      mEffects = new DistortedEffects[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
73
      Static3D[][][] cubeVectors = new Static3D[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
74

    
75
      mScale = new Static3D(1,1,1);
76
      Static3D center= new Static3D(0,0,0);
77

    
78
      MatrixEffectScale      scale = new MatrixEffectScale(mScale);
79
      MatrixEffectQuaternion quat1 = new MatrixEffectQuaternion(mQuat1, center);
80
      MatrixEffectQuaternion quat2 = new MatrixEffectQuaternion(mQuat2, center);
81

    
82
      // 3x2 bitmap = 6 squares:
83
      //
84
      // RED     GREEN   BLUE
85
      // YELLOW  WHITE   ORANGE
86

    
87
      final float ze = 0.0f;
88
      final float ot = 1.0f/3.0f;
89
      final float tt = 2.0f/3.0f;
90
      final float oh = 1.0f/2.0f;
91
      final float of = 1.0f/40.0f;
92

    
93
      final Static4D mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
94
      final Static4D mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
95
      final Static4D mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
96
      final Static4D mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
97
      final Static4D mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
98
      final Static4D mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
99

    
100
      final Static4D mapBlack = new Static4D(ze,ze, ze+of,ze+of);
101

    
102
      Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom;
103

    
104
      for(int x=0; x<CUBE_SIZE; x++)
105
        for(int y=0; y<CUBE_SIZE; y++)
106
          for(int z=0; z<CUBE_SIZE; z++)
107
            {
108
            tmpLeft  = (x==          0 ? mapLeft  :mapBlack);
109
            tmpRight = (x==CUBE_SIZE-1 ? mapRight :mapBlack);
110
            tmpFront = (z==CUBE_SIZE-1 ? mapFront :mapBlack);
111
            tmpBack  = (z==          0 ? mapBack  :mapBlack);
112
            tmpTop   = (y==CUBE_SIZE-1 ? mapTop   :mapBlack);
113
            tmpBottom= (y==          0 ? mapBottom:mapBlack);
114

    
115
            mCubes[x][y][z] = new MeshCubes(VERTICES,VERTICES,VERTICES, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
116

    
117
            cubeVectors[x][y][z] = new Static3D( SIZE*(x-0.5f*(CUBE_SIZE-1)), SIZE*(y-0.5f*(CUBE_SIZE-1)), SIZE*(z-0.5f*(CUBE_SIZE-1)) );
118

    
119
            mEffects[x][y][z] = new DistortedEffects();
120

    
121
            mEffects[x][y][z].apply(scale);
122
            mEffects[x][y][z].apply(quat1);
123
            mEffects[x][y][z].apply(quat2);
124
            mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
125
            }
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
   
130
    public void onDrawFrame(GL10 glUnused) 
131
      {
132
      mScreen.render( System.currentTimeMillis() );
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
    
137
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
138
      {
139
      mScreenMin = width<height ? width:height;
140

    
141
      float w = mTexture.getWidth();
142
      float h = mTexture.getHeight();
143
      float factor = 0.6f*(width>height ? height/h:width/w)/CUBE_SIZE;
144

    
145
      mScale.set(factor,factor,factor);
146
      mScreen.resize(width, height);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
152
      {
153
      Bitmap bitmap;
154

    
155
      final int S = 128;
156
      final int W = 3*S;
157
      final int H = 2*S;
158
      final int R = S/10;
159
      final int M = S/20;
160

    
161
      Paint paint = new Paint();
162
      bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
163
      Canvas canvas = new Canvas(bitmap);
164

    
165
      paint.setAntiAlias(true);
166
      paint.setTextAlign(Paint.Align.CENTER);
167
      paint.setStyle(Paint.Style.FILL);
168

    
169
      // 3x2 bitmap = 6 squares:
170
      //
171
      // RED     GREEN   BLUE
172
      // YELLOW  WHITE   ORANGE
173

    
174
      paint.setColor(0xff000000);                                  // BLACK BACKGROUND
175
      canvas.drawRect(0, 0, W, H, paint);                          //
176

    
177
      paint.setColor(0xffb90000);                                  // RED
178
      canvas.drawRoundRect(  0+M, 0+M,   S-M,   S-M, R, R, paint); //
179
      paint.setColor(0xff009b48);                                  // GREEN
180
      canvas.drawRoundRect(  S+M, 0+M, 2*S-M,   S-M, R, R, paint); //
181
      paint.setColor(0xff0045ad);                                  // BLUE
182
      canvas.drawRoundRect(2*S+M, 0+M, 3*S-M,   S-M, R, R, paint); //
183
      paint.setColor(0xffffd500);                                  // YELLOW
184
      canvas.drawRoundRect(  0+M, S+M,   S-M, 2*S-M, R, R, paint); //
185
      paint.setColor(0xffffffff);                                  // WHITE
186
      canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
187
      paint.setColor(0xffff5900);                                  // ORANGE
188
      canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
189

    
190
      if( mTexture==null ) mTexture = new DistortedTexture(SIZE,SIZE);
191
      mTexture.setTexture(bitmap);
192

    
193
      mScreen.detachAll();
194

    
195
      for(int x=0; x<CUBE_SIZE; x++)
196
        for(int y=0; y<CUBE_SIZE; y++)
197
          for(int z=0; z<CUBE_SIZE; z++)
198
             mScreen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
199

    
200
      try
201
        {
202
        Distorted.onCreate(mView.getContext());
203
        }
204
      catch(Exception ex)
205
        {
206
        android.util.Log.e("Rubik", ex.getMessage() );
207
        }
208
      }
209
}
(2-2/3)