Author: theagentd (posted 2012-11-02 17:52:45, viewed 161 times)
| 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
| package dam.test;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import net.mokyu.threading.GameExecutor;
import net.mokyu.threading.MultithreadedExecutor;
import net.mokyu.threading.SplitTask;
import net.mokyu.threading.TaskTree;
import net.mokyu.threading.TaskTreeBuilder;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.Timer;
import dam.gl.shader.Shader;
import dam.gl.shader.ShaderProgram;
import dam.gl.tex.Sampler;
import dam.gl.tex.Texture2D;
import dam.gl.tex.TextureUtils;
import dam.test.ParticleTest10.Particle;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL31.*;
import static org.lwjgl.opengl.GL33.*;
public class OGL3ParticleTest {
private static final int WIDTH = 1600, HEIGHT = 900;
private static final int MAX_PARTICLES = 50;
private static final int THREADS = 8;
private static final int PARTICLE_SIZE = 24;
private static final int POSITION_OFFSET = 0;
private static final int ROTATION_OFFSET = 8;
private static final int SIZE_OFFSET = 12;
private static final int COLOR_OFFSET = 20;
private ShaderProgram geometryShader;
private int positionLocation;
private int rotationLocation;
private int sizeLocation;
private int colorLocation;
private Texture2D texture;
private Sampler sampler;
private ByteBuffer data;
private int dataVBO;
private ByteBuffer mappedVBO;
private GameExecutor executor;
private TaskTree taskTree;
private ArrayList<Particle> particles;
private Random r;
public OGL3ParticleTest(){
initLWJGL();
initShaders();
initBuffers();
initParticles();
initTexture();
initThreading();
}
private void initLWJGL() {
try{
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
}catch(LWJGLException ex){
ex.printStackTrace();
}
}
private void initShaders() {
geometryShader = new ShaderProgram();
geometryShader.attachShader(new Shader(Shader.VERTEX_SHADER, new File("shaders/particles/geometry shader.vert")));
geometryShader.attachShader(new Shader(Shader.GEOMETRY_SHADER, new File("shaders/particles/geometry shader.geom")));
geometryShader.attachShader(new Shader(Shader.FRAGMENT_SHADER, new File("shaders/particles/geometry shader.frag")));
geometryShader.link();
positionLocation = geometryShader.getAttribLocation("position");
rotationLocation = geometryShader.getAttribLocation("rotation");
sizeLocation = geometryShader.getAttribLocation("size");
colorLocation = geometryShader.getAttribLocation("color");
geometryShader.bind();
glUniform2f(geometryShader.getUniformLocation("screenSize"), Display.getWidth(), Display.getHeight());
}
private void initBuffers() {
data = BufferUtils.createByteBuffer(PARTICLE_SIZE * MAX_PARTICLES);
dataVBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, dataVBO);
glBufferData(GL_ARRAY_BUFFER, PARTICLE_SIZE * MAX_PARTICLES, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
private void initParticles() {
particles = new ArrayList<>();
r = new Random();
}
private void initTexture(){
texture = new Texture2D(GL_TEXTURE_2D, GL_RGBA8, GL_UNSIGNED_BYTE, GL_RGBA);
texture.bind();
try{
texture.load(0, 16, 16, TextureUtils.loadImage(ImageIO.read(new File("files/particle/particle.png")), TextureUtils.PIXEL_HANDLER_RGBA_PREMULTIPLIED));
}catch(IOException ex){
ex.printStackTrace();
}
texture.generateMipmaps();
sampler = new Sampler();
sampler.setWrapModes(GL_CLAMP_TO_EDGE);
sampler.setMinMagFilter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
sampler.setAnisotropy(16f);
}
private void initThreading() {
executor = new MultithreadedExecutor(THREADS);
TaskTreeBuilder builder = new TaskTreeBuilder();
builder.addTask(new MyTask(THREADS));
taskTree = builder.build();
}
private void gameloop() {
while(!Display.isCloseRequested()){
handleInput();
updateParticles();
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
texture.bind();
sampler.bind(0);
renderGeometry();
Display.update();
}
}
private void handleInput(){
if(Keyboard.isKeyDown(Keyboard.KEY_G)){
for(int i = 0; i < 1000 && particles.size() != MAX_PARTICLES; i++){
particles.add(new Particle());
}
Display.setTitle("Particles: " + particles.size());
}
}
private boolean pull;
private int x, y;
private void updateParticles() {
pull = Mouse.isButtonDown(0);
x = Mouse.getX();
y = HEIGHT - 1 - Mouse.getY();
executor.run(taskTree);
data.limit(particles.size() * PARTICLE_SIZE);
glBindBuffer(GL_ARRAY_BUFFER, dataVBO);
mappedVBO = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, particles.size() * PARTICLE_SIZE, mappedVBO);
mappedVBO.put(data);
glUnmapBuffer(GL_ARRAY_BUFFER);
data.clear();
mappedVBO.clear();
}
private void renderGeometry() {
geometryShader.bind();
glBindBuffer(GL_ARRAY_BUFFER, dataVBO);
glVertexAttribPointer(positionLocation, 2, GL_FLOAT, false, PARTICLE_SIZE, POSITION_OFFSET);
glVertexAttribPointer(rotationLocation, 1, GL_FLOAT, false, PARTICLE_SIZE, ROTATION_OFFSET);
glVertexAttribPointer(sizeLocation, 2, GL_FLOAT, false, PARTICLE_SIZE, SIZE_OFFSET);
glVertexAttribPointer(colorLocation, 4, GL_UNSIGNED_BYTE, true, PARTICLE_SIZE, COLOR_OFFSET);
glEnableVertexAttribArray(positionLocation);
glEnableVertexAttribArray(rotationLocation);
glEnableVertexAttribArray(sizeLocation);
glEnableVertexAttribArray(colorLocation);
glDrawArrays(GL_POINTS, 0, particles.size());
glDisableVertexAttribArray(positionLocation);
glDisableVertexAttribArray(rotationLocation);
glDisableVertexAttribArray(sizeLocation);
glDisableVertexAttribArray(colorLocation);
}
private class Particle{
private float x, y;
private float vx, vy;
private float width, height;
private float rotation;
private int color;
public Particle(){
x = r.nextFloat() * WIDTH;
y = r.nextFloat() * HEIGHT;
rotation = r.nextFloat() * 360;
width = 10;
height = 10;
color = 0x0640FF40;
}
public void pull(int mouseX, int mouseY){
float dx = mouseX - x, dy = mouseY - y;
float distSqrd = dx*dx + dy*dy;
float force = 2f / distSqrd;
vx += dx * force;
vy += dy * force;
}
public void update() {
vx *= 0.999;
vy *= 0.999;
x += vx;
y += vy;
if((x < 0 && vx < 0) || (x > WIDTH && vx > 0)){
vx = -vx;
}
if((y < 0 && vy < 0) || (y > HEIGHT && vy > 0)){
vy = -vy;
}
}
public void put(ByteBuffer data) {
data.
putFloat(x).putFloat(y).
putFloat(rotation).
putFloat(width).putFloat(height).
putInt(color);
}
}
private class MyTask extends SplitTask{
private int totalSubtasks;
public MyTask(int subtasks) {
super(0, 0, subtasks);
totalSubtasks = subtasks;
}
@Override
protected void runSubtask(int subtask) {
int start = subtask * particles.size() / totalSubtasks;
int end = (subtask + 1) * particles.size() / totalSubtasks;
ByteBuffer view = data.slice();
view.order(ByteOrder.nativeOrder());
view.position(start * PARTICLE_SIZE);
view.limit(end * PARTICLE_SIZE);
for(int i = start; i < end; i++){
Particle p = particles.get(i);
if(pull){
p.pull(x, y);
}
p.update();
p.put(view);
}
}
@Override
public void finish() {
}
}
public static void main(String[] args){
new OGL3ParticleTest().gameloop();
}
} |
Special syntax:
- To highlight a line (yellow background), prefix it with '@@'
- To indicate that a line should be removed (red background), prefix it with '-'
- To indicate that a line should be added (green background), prefix it with '+'
- To post multiple snippets, seperate them by '~~~~'
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|