Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ 687263cc

1 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 8647eae2 Leszek Koltunski
// Copyright 2019 Leszek Koltunski                                                               //
3 5b4a2d76 Leszek Koltunski
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 5b4a2d76 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 5b4a2d76 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 5b4a2d76 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.rubik;
21
22
import android.opengl.GLSurfaceView;
23
24 94cc96ff Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
25 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
26 5b4a2d76 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
27 b62eb334 Leszek Koltunski
import org.distorted.library.message.EffectListener;
28 5b4a2d76 Leszek Koltunski
import org.distorted.library.type.Static3D;
29
30
import javax.microedition.khronos.egl.EGLConfig;
31
import javax.microedition.khronos.opengles.GL10;
32
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
35 b62eb334 Leszek Koltunski
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
36 5b4a2d76 Leszek Koltunski
{
37 e4c0057e Leszek Koltunski
    private static final float CUBE_SCREEN_RATIO = 0.5f;
38 8a40abf4 Leszek Koltunski
    private static final float CAMERA_DISTANCE   = 0.6f;  // 0.6 of the length of max(scrHeight,scrWidth)
39 6e18bd32 Leszek Koltunski
    private static final int MIN_CUBE_SIZE = 1;
40
    private static final int MAX_CUBE_SIZE = 6;
41 e4c0057e Leszek Koltunski
42 18709ae0 Leszek Koltunski
    private RubikSurfaceView mView;
43 5b4a2d76 Leszek Koltunski
    private DistortedScreen mScreen;
44 8647eae2 Leszek Koltunski
    private Static3D mMove, mScale;
45 e3a72781 Leszek Koltunski
    private int mNextCubeSize;
46 6e18bd32 Leszek Koltunski
    private boolean mChangeCubeSizeNow;
47
    private int mNumCube;
48 8647eae2 Leszek Koltunski
    private RubikCube mCube;
49 5b4a2d76 Leszek Koltunski
50 af8b42cc Leszek Koltunski
    private int mScreenWidth, mScreenHeight;
51
52 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 18709ae0 Leszek Koltunski
    RubikRenderer(RubikSurfaceView v)
55 5b4a2d76 Leszek Koltunski
      {
56
      mView = v;
57
58
      mScreen = new DistortedScreen();
59
60 af8b42cc Leszek Koltunski
      mScreenWidth = mScreenHeight = 0;
61
62 5b4a2d76 Leszek Koltunski
      mMove  = new Static3D(0,0,0);
63
      mScale = new Static3D(1,1,1);
64 8647eae2 Leszek Koltunski
65 6e18bd32 Leszek Koltunski
      mNextCubeSize= MIN_CUBE_SIZE;
66
      mChangeCubeSizeNow = false;
67 e3a72781 Leszek Koltunski
68 6e18bd32 Leszek Koltunski
      mNumCube = 0;
69 5b4a2d76 Leszek Koltunski
      }
70
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72 6e18bd32 Leszek Koltunski
// change the cube size here, 'after the next render' as not to cause artifacts.
73 b62eb334 Leszek Koltunski
74 5b4a2d76 Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
75
      {
76
      mScreen.render( System.currentTimeMillis() );
77 94cc96ff Leszek Koltunski
78 6e18bd32 Leszek Koltunski
      if( mChangeCubeSizeNow )
79 8647eae2 Leszek Koltunski
        {
80 6e18bd32 Leszek Koltunski
        mChangeCubeSizeNow = false;
81
        createNextCube();
82 e3a72781 Leszek Koltunski
        }
83 5b4a2d76 Leszek Koltunski
      }
84
85 b62eb334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
88 b62eb334 Leszek Koltunski
     {
89 8d5a8e06 Leszek Koltunski
     mNextCubeSize++;
90
     if( mNextCubeSize> MAX_CUBE_SIZE ) mNextCubeSize = MIN_CUBE_SIZE;
91
     mChangeCubeSizeNow = true;
92 b62eb334 Leszek Koltunski
     }
93
94 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96 da231553 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
97
     {
98
     float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
99
     float fovInDegrees   = computeFOV(cameraDistance,height);
100 7f986357 Leszek Koltunski
101 da231553 Leszek Koltunski
     mScreen.setProjection( fovInDegrees, 0.1f);
102
     mScreen.resize(width, height);
103 5b4a2d76 Leszek Koltunski
104 da231553 Leszek Koltunski
     recomputeScaleFactor(width,height);
105 5b4a2d76 Leszek Koltunski
106 da231553 Leszek Koltunski
     mScreenHeight = height;
107
     mScreenWidth  = width;
108
     }
109 5b4a2d76 Leszek Koltunski
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112 da231553 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
113
     {
114
     createNextCube();
115
116
     VertexEffectSink.enable();
117
118
     try
119
       {
120 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
121 da231553 Leszek Koltunski
       }
122
     catch(Exception ex)
123
       {
124
       android.util.Log.e("Rubik", ex.getMessage() );
125
       }
126
     }
127 e4c0057e Leszek Koltunski
128 483ae94e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 6e18bd32 Leszek Koltunski
   private void createNextCube()
131 af8b42cc Leszek Koltunski
     {
132 6e18bd32 Leszek Koltunski
     if( mCube!=null ) mCube.releaseResources();
133
     mCube = new RubikCube(mNextCubeSize, mMove, mScale);
134
     mCube.createTexture();
135 8647eae2 Leszek Koltunski
136 6e18bd32 Leszek Koltunski
     if( mScreenWidth!=0 ) recomputeScaleFactor(mScreenWidth,mScreenHeight);
137 e4c0057e Leszek Koltunski
138 6e18bd32 Leszek Koltunski
     mScreen.detachAll();
139
     mCube.attachToScreen(mScreen);
140
     mCube.addRotation(this);
141
     RubikActivity act = mView.getRubikActivity();
142
     act.setText(++mNumCube);
143 e3a72781 Leszek Koltunski
     }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147 6e18bd32 Leszek Koltunski
   private float computeFOV(float cameraDistance, int screenHeight)
148 af8b42cc Leszek Koltunski
     {
149 6e18bd32 Leszek Koltunski
     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
150
     return (float)(2*halfFOVInRadians*(180/Math.PI));
151 af8b42cc Leszek Koltunski
     }
152 e4c0057e Leszek Koltunski
153 d4374cd3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155 e3a72781 Leszek Koltunski
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
156 af8b42cc Leszek Koltunski
     {
157 6e18bd32 Leszek Koltunski
     float cubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
158 687263cc Leszek Koltunski
     float texSize = mCube.getStretchSize();
159 6e18bd32 Leszek Koltunski
     float scaleFactor = cubeSizeInScreenSpace/(texSize*mCube.getSize());
160 af8b42cc Leszek Koltunski
161
     mMove.set( (screenWidth-scaleFactor*texSize)/2 , (screenHeight-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
162
     mScale.set(scaleFactor,scaleFactor,scaleFactor);
163
     }
164 5b4a2d76 Leszek Koltunski
}