aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/demos/julia3d/UserSceneController.java
blob: fda54bebc6bdf7c6d35697c96e6218a878ca7ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package com.jogamp.opencl.demos.julia3d;

import com.jogamp.opencl.demos.julia3d.structs.RenderingConfig;
import com.jogamp.opencl.demos.julia3d.structs.Vec;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

import static java.lang.Math.*;
import static com.jogamp.opencl.demos.julia3d.Renderer.*;

/**
 * Utility class for interacting with a scene. Supports rotation and zoom around origin.
 * @author Michael Bien
 */
public class UserSceneController {

    private Point dragstart;
    private RenderingConfig model;
    private Renderer view;

    private enum MOUSE_MODE { DRAG_ROTATE, DRAG_ZOOM }
    private MOUSE_MODE dragmode = MOUSE_MODE.DRAG_ROTATE;


    public void init(Renderer view, Component component, RenderingConfig model) {
        initMouseListeners(component);
        this.view = view;
        this.model = model;
    }

    private void initMouseListeners(Component component) {

        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {

                int x = e.getX();
                int y = e.getY();

                switch (dragmode) {
                    case DRAG_ROTATE:
                        if (dragstart != null) {
                            int height = model.getHeight();
                            int width = model.getWidth();

                            int ry = height - y - 1;
                            int baseMu1 = width - MU_RECT_SIZE - 2;
                            int baseMu2 = 1;
                            int baseMu3 = width - MU_RECT_SIZE - 2;
                            int baseMu4 = MU_RECT_SIZE + 2;

                            if ((x >= baseMu1 && x <= baseMu1 + MU_RECT_SIZE) && (ry >= baseMu2 && ry <= baseMu2 + MU_RECT_SIZE)) {
                                float[] mu = model.getMu();
                                mu[0] = 3.f * ( x - baseMu1) / (float)MU_RECT_SIZE - 1.5f;
                                mu[1] = 3.f * (ry - baseMu2) / (float)MU_RECT_SIZE - 1.5f;
                                model.setMu(mu);
                            } else if ((x >= baseMu3 && x <= baseMu3 + MU_RECT_SIZE) && (ry >= baseMu4 && ry <= baseMu4 + MU_RECT_SIZE)) {
                                float[] mu = model.getMu();
                                mu[2] = 3.f * ( x - baseMu3) / (float)MU_RECT_SIZE - 1.5f;
                                mu[3] = 3.f * (ry - baseMu4) / (float)MU_RECT_SIZE - 1.5f;
                                model.setMu(mu);
                            } else {
                                rotateCameraYbyOrig(0.01f * (x - dragstart.getX()));
                                rotateCameraXbyOrig(0.01f * (y - dragstart.getY()));
                            }
                        }
                        dragstart = e.getPoint();
                        view.update();
                        break;
                    case DRAG_ZOOM:
                        if (dragstart != null) {
                            float zoom = (float) ((y - dragstart.getY()) / 10.0f);
                            zoom(zoom);
                        }
                        dragstart = e.getPoint();
                        view.update();
                        break;
                }

            }

            @Override
            public void mousePressed(MouseEvent e) {
                switch (e.getButton()) {
                    case (MouseEvent.BUTTON1):
                        dragmode = MOUSE_MODE.DRAG_ROTATE;
                        break;
                    case (MouseEvent.BUTTON2):
                        dragmode = MOUSE_MODE.DRAG_ZOOM;
                        break;
                    case (MouseEvent.BUTTON3):
                        dragmode = MOUSE_MODE.DRAG_ZOOM;
                        break;
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                switch (e.getButton()) {
                    case (MouseEvent.BUTTON1):
                        dragmode = MOUSE_MODE.DRAG_ZOOM;
                        break;
                    case (MouseEvent.BUTTON2):
                        dragmode = MOUSE_MODE.DRAG_ROTATE;
                        break;
                    case (MouseEvent.BUTTON3):
                        dragmode = MOUSE_MODE.DRAG_ROTATE;
                        break;
                }

                dragstart = null;
            }

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                float zoom = e.getWheelRotation() * 0.1f;
                zoom(zoom);
                view.update();
            }

        };

        KeyAdapter keyAdapter = new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {

                switch (e.getKeyChar()) {
                    case 'l':
                        model.setEnableShadow(model.getEnableShadow()==0 ? 1 : 0);
                        break;
                    case '1':
                        model.setEpsilon(model.getEpsilon() * 0.75f);
                        break;
                    case '2':
                        model.setEpsilon(model.getEpsilon() * 1.f / 0.75f);
                        break;
                    case '3':
                        model.setMaxIterations(max(1, model.getMaxIterations() -1));
                        break;
                    case '4':
                        model.setMaxIterations(min(12, model.getMaxIterations()+1));
                        break;
                    case '5':
                        model.setSuperSamplingSize(max(1, model.getSuperSamplingSize() -1));
                        break;
                    case '6':
                        model.setSuperSamplingSize(min(5, model.getSuperSamplingSize() +1));
                        break;
                    default:
                        break;
                }
                view.update();

            }

        };

        component.addKeyListener(keyAdapter);

        component.addMouseListener(mouseAdapter);
        component.addMouseMotionListener(mouseAdapter);
        component.addMouseWheelListener(mouseAdapter);

    }
    private void zoom(float zoom) {
        Vec orig = model.getCamera().getOrig();
        orig.setX(orig.getX()+zoom)
            .setY(orig.getY()+zoom)
            .setZ(orig.getZ()+zoom);
    }

    private void rotateLightX(float k) {
        float[] light = model.getLight();
        float y = light[1];
        float z = light[2];
        light[1] = (float) ( y * cos(k) + z * sin(k));
        light[2] = (float) (-y * sin(k) + z * cos(k));
        model.setLight(light);
    }

    private void rotateLightY(float k) {
        float[] light = model.getLight();
        float x = light[0];
        float z = light[2];
        light[0] = (float) (x * cos(k) - z * sin(k));
        light[2] = (float) (x * sin(k) + z * cos(k));
        model.setLight(light);
    }

    private void rotateCameraXbyOrig(double k) {
        Vec orig = model.getCamera().getOrig();
        float y = orig.getY();
        float z = orig.getZ();
        orig.setY((float) ( y * cos(k) + z * sin(k)));
        orig.setZ((float) (-y * sin(k) + z * cos(k)));
    }

    private void rotateCameraYbyOrig(double k) {
        Vec orig = model.getCamera().getOrig();
        float x = orig.getX();
        float z = orig.getZ();
        orig.setX((float) (x * cos(k) - z * sin(k)));
        orig.setZ((float) (x * sin(k) + z * cos(k)));
    }


    public final static void vadd(Vec v, Vec a, Vec b) {
        v.setX(a.getX() + b.getX());
        v.setY(a.getY() + b.getY());
        v.setZ(a.getZ() + b.getZ());
    }

    public final static void vsub(Vec v, Vec a, Vec b) {
        v.setX(a.getX() - b.getX());
        v.setY(a.getY() - b.getY());
        v.setZ(a.getZ() - b.getZ());
    }

    public final static void vmul(Vec v, float s, Vec b) {
        v.setX(s * b.getX());
        v.setY(s * b.getY());
        v.setZ(s * b.getZ());
    }

    public final static float vdot(Vec a, Vec b) {
        return a.getX() * b.getX()
             + a.getY() * b.getY()
             + a.getZ() * b.getZ();
    }

    public final static void vnorm(Vec v) {
        float s = (float) (1.0f / sqrt(vdot(v, v)));
        vmul(v, s, v);
    }

    public final static void vxcross(Vec v, Vec a, Vec b) {
        v.setX(a.getY() * b.getZ() - a.getZ() * b.getY());
        v.setY(a.getZ() * b.getX() - a.getX() * b.getZ());
        v.setZ(a.getX() * b.getY() - a.getY() * b.getX());
    }


}