-
Notifications
You must be signed in to change notification settings - Fork 89
/
SwtDemo.java
163 lines (149 loc) · 5.73 KB
/
SwtDemo.java
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
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
package org.lwjgl.demo.opengl.swt;
import static org.lwjgl.opengl.GL20.*;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
public class SwtDemo {
public static void main(String[] args) {
int minClientWidth = 640;
int minClientHeight = 480;
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.stateMask == SWT.ALT && (e.keyCode == SWT.KEYPAD_CR || e.keyCode == SWT.CR)) {
shell.setFullScreen(!shell.getFullScreen());
}
}
});
int dw = shell.getSize().x - shell.getClientArea().width;
int dh = shell.getSize().y - shell.getClientArea().height;
shell.setMinimumSize(minClientWidth + dw, minClientHeight + dh);
GLData data = new GLData();
data.doubleBuffer = true;
final GLCanvas canvas = new GLCanvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE, data);
canvas.setCurrent();
GL.createCapabilities();
final Rectangle rect = new Rectangle(0, 0, 0, 0);
canvas.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
Rectangle bounds = canvas.getBounds();
rect.width = bounds.width;
rect.height = bounds.height;
}
});
shell.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event event) {
switch (event.detail) {
case SWT.TRAVERSE_ESCAPE:
shell.close();
event.detail = SWT.TRAVERSE_NONE;
event.doit = false;
break;
default:
break;
}
}
});
glClearColor(0.3f, 0.5f, 0.8f, 1.0f);
// Create a simple shader program
int program = glCreateProgram();
int vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs,
"uniform float rot;" +
"uniform float aspect;" +
"void main(void) {" +
" vec4 v = gl_Vertex * 0.5;" +
" vec4 v_ = vec4(0.0, 0.0, 0.0, 1.0);" +
" v_.x = v.x * cos(rot) - v.y * sin(rot);" +
" v_.y = v.y * cos(rot) + v.x * sin(rot);" +
" v_.x /= aspect;" +
" gl_Position = v_;" +
"}");
glCompileShader(vs);
glAttachShader(program, vs);
int fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs,
"void main(void) {" +
" gl_FragColor = vec4(0.1, 0.3, 0.5, 1.0);" +
"}");
glCompileShader(fs);
glAttachShader(program, fs);
glLinkProgram(program);
glUseProgram(program);
final int rotLocation = glGetUniformLocation(program, "rot");
final int aspectLocation = glGetUniformLocation(program, "aspect");
// Create a simple quad
int vbo = glGenBuffers();
int ibo = glGenBuffers();
float[] vertices = {
-1, -1, 0,
1, -1, 0,
1, 1, 0,
-1, 1, 0
};
int[] indices = {
0, 1, 2,
2, 3, 0
};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, (FloatBuffer) BufferUtils
.createFloatBuffer(vertices.length).put(vertices).flip(),
GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (IntBuffer) BufferUtils
.createIntBuffer(indices.length).put(indices).flip(),
GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0L);
shell.setSize(800, 600);
shell.open();
display.asyncExec(new Runnable() {
float rot;
long lastTime = System.nanoTime();
public void run() {
if (!canvas.isDisposed()) {
canvas.setCurrent();
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, rect.width, rect.height);
float aspect = (float) rect.width / rect.height;
glUniform1f(aspectLocation, aspect);
glUniform1f(rotLocation, rot);
glDrawElements(GL11.GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
canvas.swapBuffers();
display.asyncExec(this);
long thisTime = System.nanoTime();
float delta = (thisTime - lastTime) / 1E9f;
rot += delta;
if (rot > 2.0 * Math.PI) {
rot -= 2.0f * (float) Math.PI;
}
lastTime = thisTime;
}
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}