Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObjectNode.java @ 2fa1601b

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;
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

    
48
    resizeFBO(mWidth,mHeight);
49

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

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

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

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  public int getWidth()
82
    {
83
    return mWidth;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  public int getHeight()
89
    {
90
    return mHeight;
91
    }
92
  }
(10-10/11)