Jogamp
79e1d26105aad2d8b41dba32d343b85ac0944612
[jocl-demos.git] / src / com / mbien / opencl / demos / joglinterop / GLCLInteroperabilityDemo.java
1 package com.mbien.opencl.demos.joglinterop;
2
3 import com.mbien.opencl.CLCommandQueue;
4 import com.mbien.opencl.CLContext;
5 import com.mbien.opencl.CLDevice;
6 import com.mbien.opencl.CLException;
7 import com.mbien.opencl.CLKernel;
8 import com.mbien.opencl.CLPlatform;
9 import com.mbien.opencl.CLProgram;
10 import com.sun.opengl.util.Animator;
11 import com.sun.opengl.util.BufferUtil;
12 import java.awt.event.WindowAdapter;
13 import java.awt.event.WindowEvent;
14 import java.io.IOException;
15 import java.nio.FloatBuffer;
16 import java.nio.IntBuffer;
17 import javax.media.opengl.DebugGL2;
18 import javax.media.opengl.GL2;
19 import javax.media.opengl.GLAutoDrawable;
20 import javax.media.opengl.GLCapabilities;
21 import javax.media.opengl.GLEventListener;
22 import javax.media.opengl.GLProfile;
23 import javax.media.opengl.awt.GLCanvas;
24 import javax.media.opengl.glu.gl2.GLUgl2;
25 import javax.swing.JFrame;
26 import javax.swing.SwingUtilities;
27
28 import static com.sun.opengl.util.BufferUtil.*;
29
30 /**
31  * Sample for interoperability between JOCL and JOGL.
32  * @author Michael Bien
33  */
34 public class GLCLInteroperabilityDemo implements GLEventListener {
35
36     private final GLUgl2 glu = new GLUgl2();
37
38     private final int GRID_SIZE = 100;
39     
40     private int width;
41     private int height;
42
43     private final FloatBuffer vb;
44     private final IntBuffer ib;
45
46     private final int[] buffer = new int[2];
47     private final int INDICES  = 0;
48     private final int VERTICES = 1;
49
50     private final UserSceneInteraction usi;
51
52     private CLContext clContext;
53     private CLKernel kernel;
54     private CLCommandQueue commandQueue;
55     private final CLProgram program;
56
57     public GLCLInteroperabilityDemo() throws IOException {
58
59         this.usi = new UserSceneInteraction();
60
61         vb = newFloatBuffer(GRID_SIZE * GRID_SIZE * 4);
62         ib = newIntBuffer((GRID_SIZE - 1) * (GRID_SIZE - 1) * 2 * 3);
63
64         // build indices
65         //    0---3
66         //    | \ |
67         //    1---2
68         for (int h = 0; h < GRID_SIZE - 1; h++) {
69             for (int w = 0; w < GRID_SIZE - 1; w++) {
70
71                 // 0 - 3 - 2
72                 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6,      w + (h) * (GRID_SIZE)        );
73                 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 1,  w + (h) * (GRID_SIZE) + 1    );
74                 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 2,  w + (h + 1) * (GRID_SIZE) + 1);
75
76                 // 0 - 2 - 1
77                 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 3,  w + (h) * (GRID_SIZE)        );
78                 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 4,  w + (h + 1) * (GRID_SIZE) + 1);
79                 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 5,  w + (h + 1) * (GRID_SIZE)    );
80
81             }
82         }
83         ib.rewind();
84
85         // build grid
86         for (int w = 0; w < GRID_SIZE; w++) {
87             for (int h = GRID_SIZE; h > 0; h--) {
88                 vb.put(w - GRID_SIZE / 2).put(h - GRID_SIZE / 2).put(0).put(1);
89             }
90         }
91         vb.rewind();
92
93         try {
94             clContext = CLContext.create();
95             program = clContext.createProgram(getClass().getResourceAsStream("JoglInterop.cl"));
96 //            System.out.println(program.getSource());
97 //            program.build();
98         } catch (IOException ex) {
99             throw new CLException("can not handle exception", ex);
100         }
101
102         SwingUtilities.invokeLater(new Runnable() {
103             public void run() {
104                 initUI();
105             }
106         });
107
108     }
109
110     private void initUI() {
111
112         this.width  = 600;
113         this.height = 400;
114
115         GLCapabilities config = new GLCapabilities(GLProfile.get(GLProfile.GL2));
116         config.setSampleBuffers(true);
117         config.setNumSamples(4);
118
119         GLCanvas canvas = new GLCanvas(config);
120         canvas.addGLEventListener(this);
121         usi.init(canvas);
122
123         JFrame frame = new JFrame("JOGL-JOCL Interoperability Example");
124         frame.addWindowListener(new WindowAdapter() {
125             @Override
126             public void windowClosed(WindowEvent e) {
127                 deinit();
128             }
129
130         });
131         frame.add(canvas);
132         frame.setSize(width, height);
133
134         frame.setVisible(true);
135
136     }
137
138
139     public void init(GLAutoDrawable drawable) {
140
141         // enable GL error checking using the composable pipeline
142         drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
143
144         GL2 gl = drawable.getGL().getGL2();
145
146         gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
147         
148         gl.glGenBuffers(buffer.length, buffer, 0);
149         
150         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffer[INDICES]);
151         gl.glBufferData(GL2.GL_ARRAY_BUFFER, ib.capacity() * SIZEOF_FLOAT, ib, GL2.GL_STATIC_DRAW);
152         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
153
154         gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, buffer[VERTICES]);
155         gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, vb.capacity() * SIZEOF_FLOAT, vb, GL2.GL_DYNAMIC_DRAW);
156         gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
157
158         // OpenCL
159 //        commandQueue = clContext.getMaxFlopsDevice().createCommandQueue();
160
161 //            kernel = program.getCLKernel("sineWave");
162 //        CLBuffer<FloatBuffer> clBuffer = clContext.createFromGLBuffer(vb, buffer[VERTICES], CLBuffer.Mem.WRITE_ONLY);
163 //        kernel.setArg(0, clBuffer);
164 //        kernel.setArg(1, GRID_SIZE);
165 //        kernel.setArg(2, GRID_SIZE);
166         
167
168         pushPerspectiveView(gl);
169
170         // start rendering thread
171         Animator animator = new Animator(drawable);
172         animator.start();
173     }
174
175     public void display(GLAutoDrawable drawable) {
176
177         GL2 gl = drawable.getGL().getGL2();
178         gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
179         gl.glLoadIdentity();
180
181         usi.interact(gl);
182
183         gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
184
185             gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffer[VERTICES]);
186             gl.glVertexPointer(4, GL2.GL_FLOAT, 0, 0);
187
188             gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, buffer[INDICES]);
189             gl.glDrawElements(GL2.GL_TRIANGLES, ib.capacity(), GL2.GL_UNSIGNED_INT, 0);
190
191         gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
192
193         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
194
195     }
196
197     private void pushPerspectiveView(GL2 gl) {
198
199         gl.glMatrixMode(GL2.GL_PROJECTION);
200         gl.glPushMatrix();
201
202             gl.glLoadIdentity();
203
204             glu.gluPerspective(60, width / (float)height, 1, 1000);
205             gl.glMatrixMode(GL2.GL_MODELVIEW);
206
207             gl.glPushMatrix();
208                 gl.glLoadIdentity();
209
210     }
211
212     private void popView(GL2 gl) {
213
214                 gl.glMatrixMode(GL2.GL_PROJECTION);
215             gl.glPopMatrix();
216
217             gl.glMatrixMode(GL2.GL_MODELVIEW);
218         gl.glPopMatrix();
219
220     }
221
222
223     public void reshape(GLAutoDrawable drawable, int arg1, int arg2, int width, int height) {
224         this.width = width;
225         this.height = height;
226         GL2 gl = drawable.getGL().getGL2();
227         popView(gl);
228         pushPerspectiveView(gl);
229     }
230
231     public void dispose(GLAutoDrawable drawable) {  }
232     
233     private void deinit() {
234         clContext.release();
235         System.exit(0);
236     }
237
238     public static void main(String[] args) throws IOException {
239         new GLCLInteroperabilityDemo();
240     }
241
242 }
http://JogAmp.org git info: FAQ, tutorial and man pages.