Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObjectNode.java @ b3b79e9b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.main;
11

    
12
import org.distorted.library.effect.MatrixEffectScale;
13
import org.distorted.library.main.DistortedEffects;
14
import org.distorted.library.main.DistortedNode;
15
import org.distorted.library.main.DistortedTexture;
16
import org.distorted.library.mesh.MeshSquare;
17
import org.distorted.library.type.Static3D;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
public class TwistyObjectNode extends DistortedNode
22
  {
23
  private final Static3D mScale;
24
  private int mWidth, mHeight, mMin;
25
  private float mCameraDist;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
  public TwistyObjectNode(int surfaceW, int surfaceH)
30
    {
31
    super(new DistortedTexture(),new DistortedEffects(),new MeshSquare(20,20));
32

    
33
    mScale= new Static3D(1,1,1);
34
    setSize(surfaceW,surfaceH);
35
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
36
    getEffects().apply(scaleEffect);
37
    getMesh().setComponentCenter(0,0,0,-0.1f);
38
    setFOV(60);
39
    }
40

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

    
43
  void setSize(int surfaceW, int surfaceH)
44
    {
45
    mWidth      = surfaceW;
46
    mHeight     = surfaceH;
47
    mMin        = Math.min(mWidth,mHeight);
48

    
49
    resizeFBO(mWidth,mHeight);
50

    
51
    mScale.set(surfaceW,surfaceH,surfaceW);
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  float getCameraDist()
57
    {
58
    return mCameraDist;
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
// PUBLIC API
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
// We assume that the FOV passed is for a square screen. Adjust it for the real dimensions.
65
// if (w,h) are the real dimensions, then realFOV = 2*arctan( (h/w)*tan(FOV/2) )
66
// --> mCameraDist = 0.5/tan(FOV/2)
67

    
68
  public void setFOV(float fov)
69
    {
70
    float  aspect = ((float)mHeight)/mWidth;
71
    double halfFOV = fov * (Math.PI/360);
72
    float tanHalf = (float)Math.tan(halfFOV);
73
    double convertedFOV = 2*Math.atan(aspect*tanHalf);
74
    float convertedFOVinDeg = (float)((180/Math.PI)*convertedFOV);
75

    
76
    mCameraDist = 0.5f/tanHalf;
77
    setProjection( convertedFOVinDeg, 0.1f);
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  public int getMinSize()
83
    {
84
    return mMin;
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  public int getWidth()
90
    {
91
    return mWidth;
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  public int getHeight()
97
    {
98
    return mHeight;
99
    }
100
  }
(13-13/13)