summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.opengl.examples/src/org/eclipse/swt/opengl/examples/BezierTab.java
blob: e66b8414c8532f5b00434331aa7046603db9e7b1 (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
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.opengl.examples;


import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.opengl.*;
import org.eclipse.swt.widgets.*;

class BezierTab extends SelectionTab {
	private boolean showCtrlPoints = true;
	private Point offset;
	private int currentPoint = -1;
	private int lineDivisions = 30;
	private double[][] ctrlPts = {
		{	1.5, 0.5, 0.0, 0.6, 0.9, 0.0, 0.85, 0.12,
			0.0, 1.1, 1.0, 0.0, 0.53, 1.4, 0.0
		},
		{	0.53, 1.4, 0.0, 1.03, 1.87, 0.0, 1.52, 0.26,
			0.0, 1.86, 0.43, 0.0, 1.5, 0.5, 0.0
		}
	};
	private static final int LENGTH = 5;
	private static final int[][] PICK_NAMES = {
		{ 1, 2, 3, 4, 5 },
		{ 6, 7, 8, 9, 10 }
	};

	/**
	 * @see OpenGLTab#createControls(Composite)
	 */
	void createControls(Composite composite) {
		new Label(composite, SWT.NONE).setText("Click and drag points to adjust shape.");
		new Label(composite, SWT.NONE).setText("Line division count:");
		final Slider divisions = new Slider(composite, SWT.NONE);
		divisions.setIncrement(1);
		divisions.setMaximum(52);
		divisions.setMinimum(1);
		divisions.setThumb(2);
		divisions.setPageIncrement(2);
		divisions.setSelection(30);
		divisions.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				lineDivisions = divisions.getSelection();
			}
		});
		
		final Button showPointsButton = new Button(composite, SWT.CHECK);
		showPointsButton.setText("Show Points");
		showPointsButton.setSelection(true);
		showPointsButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				showCtrlPoints = showPointsButton.getSelection();
			}
		});
		
		final Button blendButton = new Button(composite, SWT.CHECK);
		blendButton.setText("Blend");
		blendButton.setSelection(true);
		blendButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				if (blendButton.getSelection()) {
					GL.glEnable(GL.GL_BLEND);
				} else {
					GL.glDisable(GL.GL_BLEND);
				}
			}
		});
		
		final Button smoothLineButton = new Button(composite, SWT.CHECK);
		smoothLineButton.setText("Smooth Line");
		smoothLineButton.setSelection(true);
		smoothLineButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				if (smoothLineButton.getSelection()) {
					GL.glEnable(GL.GL_LINE_SMOOTH);
				} else {
					GL.glDisable(GL.GL_LINE_SMOOTH);
				}
			}
		});
		
		final Canvas glCanvas = getGlCanvas();
		glCanvas.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				offset = null;
			}

		});
		
		glCanvas.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event e) {
				e.y = glCanvas.getClientArea().height - e.y;
				if (e.button == 1) {
					if (processSelection(e.x, e.y, 10) > 0) {
						offset = new Point(e.x, e.y);
					} else {
						currentPoint = 0;
					}
				}
			}
		});
		
		glCanvas.addListener(SWT.MouseMove, new Listener() {
			public void handleEvent(Event e) {
				if (offset == null) return;
				int currentSegment = (currentPoint - 1) / LENGTH;
				int current = (currentPoint - 1) * 3;
				if (currentPoint > LENGTH) {
					current = (currentPoint - 6) * 3;
				}
				Rectangle rect = glCanvas.getClientArea();
				e.y = rect.height - e.y;
				if (0 < e.x && e.x < rect.width && 0 < e.y && e.y < rect.height && currentPoint > 0) {
					ctrlPts[currentSegment][current] = (float) e.x / (float) 200;
					ctrlPts[currentSegment][current + 1] = (float) e.y / (float) 200;
					switch (currentPoint) {
						case 10 :
							ctrlPts[0][0] = (float) e.x / (float) 200;
							ctrlPts[0][1] = (float) e.y / (float) 200;
							break;
						case 6 :
							ctrlPts[0][12] = (float) e.x / (float) 200;
							ctrlPts[0][13] = (float) e.y / (float) 200;
							break;
					}
				}
			}
		});
	}
	
	/**
	 * @see OpenGLTab#getTabText()
	 */
	String getTabText() {
		return "Bezier";
	}
	
	/**
	 * @see OpenGLTab#init()
	 */
	void init() {
		GL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST);
		GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
		GL.glPointSize(7.0f);
		GL.glLineWidth(4.0f);

		GL.glEnable(GL.GL_AUTO_NORMAL);
		GL.glEnable(GL.GL_MAP1_VERTEX_3);
		GL.glEnable(GL.GL_LINE_SMOOTH);
		GL.glEnable(GL.GL_BLEND);
	}
	
	/**
	 * @see SelectionTab.processPick (int[], int)
	 */
	void processPick(int[] pSelectBuff, int hits) {
		int counter = 0;
		currentPoint = 0;
		for (int i = 0; i < hits; i++) {
			int count = pSelectBuff[counter];
			counter += 3;
			for (int j = 0; j < count; j++) {
				currentPoint = pSelectBuff[counter];
				counter++;
			}
		}
	}
	
	/**
	 * @see OpenGLTab#renderScene()
	 */
	void renderScene() {
		GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		GL.glLoadIdentity();
		GL.glTranslatef(-1.0f, -1.0f, -2.45f);
		GL.glColor3f(0.0f, 0.0f, 1.0f);
		for (int i = 0; i < ctrlPts.length; i++) {
			GL.glMapGrid1d(lineDivisions, 0.0f, 1f);
			GL.glMap1d(GL.GL_MAP1_VERTEX_3, 0, 1, 3, LENGTH, ctrlPts[i]);
			GL.glEvalMesh1(GL.GL_LINE, 0, lineDivisions);
		}
		if (showCtrlPoints) {
			GL.glPushName(0);
			for (int j = 0; j < ctrlPts.length; j++) {
				for (int i = 0; i < LENGTH * 3; i += 3) {
					GL.glLoadName(PICK_NAMES[j][i / 3]);
					GL.glBegin(GL.GL_POINTS);
					if (PICK_NAMES[j][i / 3] == currentPoint) {
						GL.glColor3f(1.0f, 0.0f, 0.0f);
					} else {
						GL.glColor3f(0.0f, 0.0f, 0.0f);
					}
					GL.glVertex3d(ctrlPts[j][i], ctrlPts[j][i + 1], ctrlPts[j][i + 2]);
					GL.glEnd();
				}
			}
		}
	}
}