Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 56cbe1cf

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.vertex3d;
21
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLES20;
25
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 794e6c4f Leszek Koltunski
import org.distorted.library.DistortedBitmap;
30 261fe5bd Leszek Koltunski
import org.distorted.library.DistortedObject;
31 08f92d82 Leszek Koltunski
import org.distorted.library.EffectTypes;
32 833685d0 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
33 08f92d82 Leszek Koltunski
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35
36
import java.io.IOException;
37
import java.io.InputStream;
38
39
import javax.microedition.khronos.egl.EGLConfig;
40
import javax.microedition.khronos.opengles.GL10;
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
class Vertex3DRenderer implements GLSurfaceView.Renderer
45
{
46
    private GLSurfaceView mView;
47 d2337a3a Leszek Koltunski
    private DistortedObject mObject;
48 56cbe1cf Leszek Koltunski
    private DistortedBitmap mBackground;
49 261fe5bd Leszek Koltunski
    private int mObjWidth, mObjHeight;
50 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
51
    Static4D mQuat1, mQuat2;
52 d2337a3a Leszek Koltunski
    int mScreenMin;
53 833685d0 Leszek Koltunski
54 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56
    public Vertex3DRenderer(GLSurfaceView v)
57
      {
58
      mView = v;
59 d40cfeb2 Leszek Koltunski
60 56cbe1cf Leszek Koltunski
      mObject     = ((Vertex3DActivity)v.getContext()).getObject();
61
      mBackground = new DistortedBitmap(100, 100, 1);
62 d40cfeb2 Leszek Koltunski
63 261fe5bd Leszek Koltunski
      mObjWidth = mObject.getWidth();
64
      mObjHeight= mObject.getHeight();
65 d40cfeb2 Leszek Koltunski
66 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
67
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
68
69
      mQuatInt1 = new DynamicQuat(0,0.5f);
70
      mQuatInt2 = new DynamicQuat(0,0.5f);
71
72
      mQuatInt1.add(mQuat1);
73
      mQuatInt2.add(mQuat2);
74 08f92d82 Leszek Koltunski
      }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77 56cbe1cf Leszek Koltunski
78
    public void onDrawFrame(GL10 glUnused)
79 08f92d82 Leszek Koltunski
      {
80
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
81
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
82 71d8ad03 Leszek Koltunski
83
      long time = System.currentTimeMillis();
84
85 56cbe1cf Leszek Koltunski
      mBackground.draw(time);
86 71d8ad03 Leszek Koltunski
      mObject.draw(time);
87 08f92d82 Leszek Koltunski
      }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90 56cbe1cf Leszek Koltunski
91
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
92 08f92d82 Leszek Koltunski
      {
93 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
94
95 261fe5bd Leszek Koltunski
      mObject.abortEffects(EffectTypes.MATRIX);
96 56cbe1cf Leszek Koltunski
      mBackground.abortEffects(EffectTypes.MATRIX);
97
      float factorObj;
98 d40cfeb2 Leszek Koltunski
99 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
100 d40cfeb2 Leszek Koltunski
        {
101 56cbe1cf Leszek Koltunski
        factorObj = (0.80f*height)/mObjHeight;
102 261fe5bd Leszek Koltunski
        }
103 d40cfeb2 Leszek Koltunski
      else
104
        {
105 56cbe1cf Leszek Koltunski
        factorObj = (0.80f*width)/mObjWidth;
106 d40cfeb2 Leszek Koltunski
        }
107
108 2bf60c29 Leszek Koltunski
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
109
110 56cbe1cf Leszek Koltunski
      mObject.move( new Static3D( (width-factorObj*mObjWidth)/2 , (height-factorObj*mObjHeight)/2 , 0) );
111
      mObject.scale(factorObj);
112 2bf60c29 Leszek Koltunski
      mObject.quaternion(mQuatInt1, rotateObj);
113
      mObject.quaternion(mQuatInt2, rotateObj);
114 833685d0 Leszek Koltunski
115 56cbe1cf Leszek Koltunski
      int backgroundSize = mBackground.getWidth();
116
      float factorBackX = ((float)width)/backgroundSize;
117
      float factorBackY = ((float)height)/backgroundSize;
118 833685d0 Leszek Koltunski
119 56cbe1cf Leszek Koltunski
      mBackground.move(new Static3D( -width/2, -height/2,-factorObj*(mObjWidth+mObjHeight)/2) );
120
      mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
121 71d8ad03 Leszek Koltunski
122 56cbe1cf Leszek Koltunski
      Distorted.onSurfaceChanged(width, height);
123 08f92d82 Leszek Koltunski
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126 56cbe1cf Leszek Koltunski
127
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
128 08f92d82 Leszek Koltunski
      {
129 bcc8e016 Leszek Koltunski
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
130
131
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
132 56cbe1cf Leszek Koltunski
      InputStream is2 = act.getResources().openRawResource(R.raw.water);
133 71d8ad03 Leszek Koltunski
134
      Bitmap bitmap1,bitmap2;
135 08f92d82 Leszek Koltunski
        
136
      try 
137
        {
138 71d8ad03 Leszek Koltunski
        bitmap1 = BitmapFactory.decodeStream(is1);
139
        bitmap2 = BitmapFactory.decodeStream(is2);
140
        }
141 08f92d82 Leszek Koltunski
      finally 
142
        {
143
        try 
144
          {
145 71d8ad03 Leszek Koltunski
          is1.close();
146
          is2.close();
147 08f92d82 Leszek Koltunski
          } 
148
        catch(IOException e) { }
149
        }  
150
      
151 71d8ad03 Leszek Koltunski
      mObject.setBitmap(bitmap1);
152 56cbe1cf Leszek Koltunski
      mBackground.setBitmap(bitmap2)
153
      ;
154 08f92d82 Leszek Koltunski
      try
155
        {
156
        Distorted.onSurfaceCreated(mView.getContext());
157
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("Vertex3D", ex.getMessage() );
161
        }
162
      }
163 56cbe1cf Leszek Koltunski
}