Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObjectNode.java @ 97a6aa87

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objectlib.main;
21

    
22
import org.distorted.library.effect.MatrixEffectScale;
23
import org.distorted.library.main.DistortedEffects;
24
import org.distorted.library.main.DistortedNode;
25
import org.distorted.library.main.DistortedTexture;
26
import org.distorted.library.mesh.MeshSquare;
27
import org.distorted.library.type.Static3D;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class TwistyObjectNode extends DistortedNode
32
  {
33
  private final Static3D mScale;
34
  private int mWidth, mHeight, mMin;
35
  private float mCameraDist;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  public TwistyObjectNode(int surfaceW, int surfaceH)
40
    {
41
    super(new DistortedTexture(),new DistortedEffects(),new MeshSquare(20,20));
42

    
43
    mScale= new Static3D(1,1,1);
44
    setSize(surfaceW,surfaceH);
45
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
46
    getEffects().apply(scaleEffect);
47
    getMesh().setComponentCenter(0,0,0,-0.1f);
48
    setFOV(60);
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  void setSize(int surfaceW, int surfaceH)
54
    {
55
    mWidth      = surfaceW;
56
    mHeight     = surfaceH;
57
    mMin        = Math.min(mWidth,mHeight);
58

    
59
    resizeFBO(mWidth,mHeight);
60

    
61
    mScale.set(surfaceW,surfaceH,surfaceW);
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  float getCameraDist()
67
    {
68
    return mCameraDist;
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
// PUBLIC API
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
// We assume that the FOV passed is for a square screen. Adjust it for the real dimensions.
75
// if (w,h) are the real dimensions, then realFOV = 2*arctan( (h/w)*tan(FOV/2) )
76
// --> mCameraDist = 0.5/tan(FOV/2)
77

    
78
  public void setFOV(float fov)
79
    {
80
    float  aspect = ((float)mHeight)/mWidth;
81
    double halfFOV = fov * (Math.PI/360);
82
    float tanHalf = (float)Math.tan(halfFOV);
83
    double convertedFOV = 2*Math.atan(aspect*tanHalf);
84
    float convertedFOVinDeg = (float)((180/Math.PI)*convertedFOV);
85

    
86
    mCameraDist = 0.5f/tanHalf;
87
    setProjection( convertedFOVinDeg, 0.1f);
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  public int getMinSize()
93
    {
94
    return mMin;
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  public int getWidth()
100
    {
101
    return mWidth;
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  public int getHeight()
107
    {
108
    return mHeight;
109
    }
110
  }
(12-12/12)