Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

405
LINES

< > BotCompany Repo | #1029752 // Hello Triangle [JOGL sample, OpenGL]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 4423K of libraries. Click here for Pure Java version (3275L/21K).

1  
!7
2  
3  
// seems to run only when cd'ing to program dir first
4  
5  
lib 1400432 // jogl-all
6  
lib 1400430 // jogl-win64
7  
lib 1400433 // jogl gluegen
8  
lib 1400434 // gluegen-rt-natives-windows-amd64
9  
10  
!include once #1029753 // JOGL framework.Semantic
11  
12  
import com.jogamp.newt.event.*;
13  
import com.jogamp.newt.event.KeyEvent;
14  
import com.jogamp.newt.event.KeyListener;
15  
import com.jogamp.newt.event.WindowAdapter;
16  
import com.jogamp.newt.event.WindowEvent;
17  
import com.jogamp.newt.opengl.GLWindow;
18  
import com.jogamp.opengl.*;
19  
import com.jogamp.opengl.math.FloatUtil;
20  
import com.jogamp.opengl.util.Animator;
21  
import com.jogamp.opengl.util.GLBuffers;
22  
import com.jogamp.opengl.util.glsl.ShaderCode;
23  
import com.jogamp.opengl.util.glsl.ShaderProgram;
24  
25  
import java.nio.ByteBuffer;
26  
import java.nio.FloatBuffer;
27  
replace IntBuffer with java.nio.IntBuffer.
28  
import java.nio.ShortBuffer;
29  
30  
import static com.jogamp.opengl.GL.*;
31  
replace GL_FLOAT with com.jogamp.opengl.GL.GL_FLOAT.
32  
replace GL_TRIANGLES with com.jogamp.opengl.GL.GL_TRIANGLES.
33  
replace GL_UNSIGNED_SHORT with com.jogamp.opengl.GL.GL_UNSIGNED_SHORT.
34  
replace GL_DEBUG_SEVERITY_HIGH with com.jogamp.opengl.GL2ES2.GL_DEBUG_SEVERITY_HIGH.
35  
replace GL_DEBUG_SEVERITY_MEDIUM with com.jogamp.opengl.GL2ES2.GL_DEBUG_SEVERITY_MEDIUM.
36  
import static com.jogamp.opengl.GL2ES3.*;
37  
replace GL_UNIFORM_BUFFER with com.jogamp.opengl.GL2ES3.GL_UNIFORM_BUFFER.
38  
replace GL_MAP_COHERENT_BIT with com.jogamp.opengl.GL4.GL_MAP_COHERENT_BIT.
39  
replace GL_MAP_PERSISTENT_BIT with com.jogamp.opengl.GL4.GL_MAP_PERSISTENT_BIT.
40  
41  
/**
42  
 * Created by GBarbieri on 16.03.2017.
43  
 */
44  
 
45  
p/*-exp*/ {
46  
  fixContextClassLoader();
47  
  if (isWindows()) {
48  
    extractResourceWithPath("natives/windows-amd64/gluegen-rt.dll", programDir());
49  
    extractResourceWithPath("natives/windows-amd64/nativewindow_awt.dll", programDir());
50  
    extractResourceWithPath("natives/windows-amd64/nativewindow_win32.dll", programDir());
51  
    //loadNativeLibrary();
52  
    addToNativeLibraryPath(programDir());
53  
  }
54  
  
55  
  new HelloTriangleSimple().setup();
56  
}
57  
58  
sclass HelloTriangleSimple implements GLEventListener, KeyListener {
59  
  private static GLWindow window;
60  
  private static Animator animator;
61  
62  
  private float[] vertexData = {
63  
          -1, -1, 1, 0, 0,
64  
          +0, +2, 0, 0, 1,
65  
          +1, -1, 0, 1, 0};
66  
67  
  private short[] elementData = {0, 2, 1};
68  
69  
  private interface Buffer {
70  
71  
      int VERTEX = 0;
72  
      int ELEMENT = 1;
73  
      int GLOBAL_MATRICES = 2;
74  
      int MODEL_MATRIX = 3;
75  
      int MAX = 4;
76  
  }
77  
78  
  private IntBuffer bufferName = GLBuffers.newDirectIntBuffer(Buffer.MAX);
79  
  private IntBuffer vertexArrayName = GLBuffers.newDirectIntBuffer(1);
80  
81  
  private FloatBuffer clearColor = GLBuffers.newDirectFloatBuffer(4);
82  
  private FloatBuffer clearDepth = GLBuffers.newDirectFloatBuffer(1);
83  
84  
  private FloatBuffer matBuffer = GLBuffers.newDirectFloatBuffer(16);
85  
86  
  private ByteBuffer globalMatricesPointer, modelMatrixPointer;
87  
  // https://jogamp.org/bugzilla/show_bug.cgi?id=1287
88  
  private boolean bug1287 = true;
89  
90  
  private Program program;
91  
92  
  private long start;
93  
94  
95  
  private void setup() {
96  
97  
      GLProfile glProfile = GLProfile.get(GLProfile.GL3);
98  
      GLCapabilities glCapabilities = new GLCapabilities(glProfile);
99  
100  
      window = GLWindow.create(glCapabilities);
101  
102  
      window.setTitle("Hello Triangle (enhanced)");
103  
      window.setSize(1024, 768);
104  
105  
      window.setContextCreationFlags(GLContext.CTX_OPTION_DEBUG);
106  
      window.setVisible(true);
107  
108  
      window.addGLEventListener(this);
109  
      window.addKeyListener(this);
110  
111  
      animator = new Animator(window);
112  
      animator.start();
113  
114  
      window.addWindowListener(new WindowAdapter() {
115  
          @Override
116  
          public void windowDestroyed(WindowEvent e) {
117  
              animator.stop();
118  
              System.exit(1);
119  
          }
120  
      });
121  
  }
122  
123  
124  
  @Override
125  
  public void init(GLAutoDrawable drawable) {
126  
127  
      GL4 gl = drawable.getGL().getGL4();
128  
129  
      initDebug(gl);
130  
131  
      initBuffers(gl);
132  
133  
      initVertexArray(gl);
134  
135  
      program = new Program(gl, "shaders/gl4", "hello-triangle", "hello-triangle");
136  
137  
      gl.glEnable(GL_DEPTH_TEST);
138  
139  
      start = System.currentTimeMillis();
140  
  }
141  
142  
  private void initDebug(GL4 gl) {
143  
144  
      window.getContext().addGLDebugListener(new GLDebugListener() {
145  
          @Override
146  
          public void messageSent(GLDebugMessage event) {
147  
              System.out.println(event);
148  
          }
149  
      });
150  
151  
      gl.glDebugMessageControl(
152  
              GL_DONT_CARE,
153  
              GL_DONT_CARE,
154  
              GL_DONT_CARE,
155  
              0,
156  
              null,
157  
              false);
158  
159  
      gl.glDebugMessageControl(
160  
              GL_DONT_CARE,
161  
              GL_DONT_CARE,
162  
              GL_DEBUG_SEVERITY_HIGH,
163  
              0,
164  
              null,
165  
              true);
166  
167  
      gl.glDebugMessageControl(
168  
              GL_DONT_CARE,
169  
              GL_DONT_CARE,
170  
              GL_DEBUG_SEVERITY_MEDIUM,
171  
              0,
172  
              null,
173  
              true);
174  
  }
175  
176  
  private void initBuffers(GL4 gl) {
177  
178  
      FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);
179  
      ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(elementData);
180  
181  
      gl.glCreateBuffers(Buffer.MAX, bufferName);
182  
183  
      if (!bug1287) {
184  
185  
          gl.glNamedBufferStorage(bufferName.get(Buffer.VERTEX), vertexBuffer.capacity() * Float.BYTES, vertexBuffer,
186  
                  GL_STATIC_DRAW);
187  
          gl.glNamedBufferStorage(bufferName.get(Buffer.ELEMENT), elementBuffer.capacity() * Short.BYTES,
188  
                  elementBuffer, GL_STATIC_DRAW);
189  
190  
          gl.glNamedBufferStorage(bufferName.get(Buffer.GLOBAL_MATRICES), 16 * 4 * 2, null, GL_MAP_WRITE_BIT);
191  
          gl.glNamedBufferStorage(bufferName.get(Buffer.MODEL_MATRIX), 16 * 4, null, GL_MAP_WRITE_BIT);
192  
193  
      } else {
194  
195  
          gl.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
196  
          gl.glBufferStorage(GL_ARRAY_BUFFER, vertexBuffer.capacity() * Float.BYTES, vertexBuffer, 0);
197  
          gl.glBindBuffer(GL_ARRAY_BUFFER, 0);
198  
199  
          gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
200  
          gl.glBufferStorage(GL_ELEMENT_ARRAY_BUFFER, elementBuffer.capacity() * Short.BYTES, elementBuffer, 0);
201  
          gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
202  
203  
204  
          IntBuffer uniformBufferOffset = GLBuffers.newDirectIntBuffer(1);
205  
          gl.glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, uniformBufferOffset);
206  
          int globalBlockSize = Math.max(16 * 4 * 2, uniformBufferOffset.get(0));
207  
          int modelBlockSize = Math.max(16 * 4, uniformBufferOffset.get(0));
208  
209  
          gl.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.GLOBAL_MATRICES));
210  
          gl.glBufferStorage(GL_UNIFORM_BUFFER, globalBlockSize, null, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
211  
          gl.glBindBuffer(GL_UNIFORM_BUFFER, 0);
212  
213  
          gl.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.MODEL_MATRIX));
214  
          gl.glBufferStorage(GL_UNIFORM_BUFFER, modelBlockSize, null, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
215  
          gl.glBindBuffer(GL_UNIFORM_BUFFER, 0);
216  
      }
217  
218  
      // map the transform buffers and keep them mapped
219  
      globalMatricesPointer = gl.glMapNamedBufferRange(
220  
              bufferName.get(Buffer.GLOBAL_MATRICES),
221  
              0,
222  
              16 * 4 * 2,
223  
              GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); // flags
224  
225  
      modelMatrixPointer = gl.glMapNamedBufferRange(
226  
              bufferName.get(Buffer.MODEL_MATRIX),
227  
              0,
228  
              16 * 4,
229  
              GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
230  
  }
231  
232  
  private void initVertexArray(GL4 gl) {
233  
234  
      gl.glCreateVertexArrays(1, vertexArrayName);
235  
236  
      gl.glVertexArrayAttribBinding(vertexArrayName.get(0), Semantic.Attr.POSITION, Semantic.Stream.A);
237  
      gl.glVertexArrayAttribBinding(vertexArrayName.get(0), Semantic.Attr.COLOR, Semantic.Stream.A);
238  
239  
      gl.glVertexArrayAttribFormat(vertexArrayName.get(0), Semantic.Attr.POSITION, 2, GL_FLOAT, false, 0);
240  
      gl.glVertexArrayAttribFormat(vertexArrayName.get(0), Semantic.Attr.COLOR, 3, GL_FLOAT, false, 2 * 4);
241  
242  
      gl.glEnableVertexArrayAttrib(vertexArrayName.get(0), Semantic.Attr.POSITION);
243  
      gl.glEnableVertexArrayAttrib(vertexArrayName.get(0), Semantic.Attr.COLOR);
244  
245  
      gl.glVertexArrayElementBuffer(vertexArrayName.get(0), bufferName.get(Buffer.ELEMENT));
246  
      gl.glVertexArrayVertexBuffer(vertexArrayName.get(0), Semantic.Stream.A, bufferName.get(Buffer.VERTEX), 0, (2 + 3) * 4);
247  
  }
248  
249  
  @Override
250  
  public void display(GLAutoDrawable drawable) {
251  
252  
      GL4 gl = drawable.getGL().getGL4();
253  
254  
255  
      // view matrix
256  
      {
257  
          float[] view = FloatUtil.makeIdentity(new float[16]);
258  
          for (int i = 0; i < 16; i++)
259  
              globalMatricesPointer.putFloat(16 * 4 + i * 4, view[i]);
260  
      }
261  
262  
263  
      gl.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 1f).put(1, .5f).put(2, 0f).put(3, 1f));
264  
      gl.glClearBufferfv(GL_DEPTH, 0, clearDepth.put(0, 1f));
265  
266  
      // model matrix
267  
      {
268  
          long now = System.currentTimeMillis();
269  
          float diff = (float) (now - start) / 1_000;
270  
271  
          float[] scale = FloatUtil.makeScale(new float[16], true, 0.5f, 0.5f, 0.5f);
272  
          float[] rotateZ = FloatUtil.makeRotationAxis(new float[16], 0, diff, 0f, 0f, 1f, new float[3]);
273  
          float[] model = FloatUtil.multMatrix(scale, rotateZ);
274  
          modelMatrixPointer.asFloatBuffer().put(model);
275  
      }
276  
277  
      gl.glUseProgram(program.name);
278  
      gl.glBindVertexArray(vertexArrayName.get(0));
279  
280  
      gl.glBindBufferBase(
281  
              GL_UNIFORM_BUFFER,
282  
              Semantic.Uniform.TRANSFORM0,
283  
              bufferName.get(Buffer.GLOBAL_MATRICES));
284  
285  
      gl.glBindBufferBase(
286  
              GL_UNIFORM_BUFFER,
287  
              Semantic.Uniform.TRANSFORM1,
288  
              bufferName.get(Buffer.MODEL_MATRIX));
289  
290  
      gl.glDrawElements(
291  
              GL_TRIANGLES,
292  
              elementData.length,
293  
              GL_UNSIGNED_SHORT,
294  
              0);
295  
296  
      gl.glUseProgram(0);
297  
      gl.glBindVertexArray(0);
298  
  }
299  
300  
  @Override
301  
  public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
302  
303  
      GL4 gl = drawable.getGL().getGL4();
304  
305  
      // ortho matrix
306  
      float[] ortho = FloatUtil.makeOrtho(new float[16], 0, false, -1f, 1f, -1f, 1f, 1f, -1f);
307  
      globalMatricesPointer.asFloatBuffer().put(ortho);
308  
309  
      gl.glViewport(x, y, width, height);
310  
  }
311  
312  
  @Override
313  
  public void dispose(GLAutoDrawable drawable) {
314  
315  
      GL4 gl = drawable.getGL().getGL4();
316  
317  
      gl.glUnmapNamedBuffer(bufferName.get(Buffer.GLOBAL_MATRICES));
318  
      gl.glUnmapNamedBuffer(bufferName.get(Buffer.MODEL_MATRIX));
319  
320  
      gl.glDeleteProgram(program.name);
321  
      gl.glDeleteVertexArrays(1, vertexArrayName);
322  
      gl.glDeleteBuffers(Buffer.MAX, bufferName);
323  
  }
324  
325  
  @Override
326  
  public void keyPressed(KeyEvent e) {
327  
      if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
328  
          new Thread(() -> {
329  
              window.destroy();
330  
          }).start();
331  
      }
332  
  }
333  
334  
  @Override
335  
  public void keyReleased(KeyEvent e) {
336  
  }
337  
338  
  private class Program {
339  
340  
      public int name = 0;
341  
342  
      public Program(GL4 gl, String root, String vertex, String fragment) {
343  
344  
          ShaderCode vertShader = ShaderCode.create(gl, GL_VERTEX_SHADER, this.getClass(), root, null, vertex,
345  
                  "vert", null, true);
346  
          ShaderCode fragShader = ShaderCode.create(gl, GL_FRAGMENT_SHADER, this.getClass(), root, null, fragment,
347  
                  "frag", null, true);
348  
349  
          ShaderProgram shaderProgram = new ShaderProgram();
350  
351  
          shaderProgram.add(vertShader);
352  
          shaderProgram.add(fragShader);
353  
354  
          shaderProgram.init(gl);
355  
356  
          name = shaderProgram.program();
357  
358  
          shaderProgram.link(gl, System.err);
359  
      }
360  
  }
361  
362  
  private class GlDebugOutput implements GLDebugListener {
363  
364  
      private int source = 0;
365  
      private int type = 0;
366  
      private int id = 0;
367  
      private int severity = 0;
368  
      private int length = 0;
369  
      private String message = null;
370  
      private boolean received = false;
371  
372  
      public GlDebugOutput() {
373  
      }
374  
375  
      public GlDebugOutput(int source, int type, int severity) {
376  
          this.source = source;
377  
          this.type = type;
378  
          this.severity = severity;
379  
          this.message = null;
380  
          this.id = -1;
381  
      }
382  
383  
      public GlDebugOutput(String message, int id) {
384  
          this.source = -1;
385  
          this.type = -1;
386  
          this.severity = -1;
387  
          this.message = message;
388  
          this.id = id;
389  
      }
390  
391  
      @Override
392  
      public void messageSent(GLDebugMessage event) {
393  
394  
          if (event.getDbgSeverity() == GL_DEBUG_SEVERITY_LOW || event.getDbgSeverity() == GL_DEBUG_SEVERITY_NOTIFICATION)
395  
              System.out.println("GlDebugOutput.messageSent(): " + event);
396  
          else
397  
              System.err.println("GlDebugOutput.messageSent(): " + event);
398  
399  
          if (null != message && message == event.getDbgMsg() && id == event.getDbgId())
400  
              received = true;
401  
          else if (0 <= source && source == event.getDbgSource() && type == event.getDbgType() && severity == event.getDbgSeverity())
402  
              received = true;
403  
      }
404  
  }
405  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1029752
Snippet name: Hello Triangle [JOGL sample, OpenGL]
Eternal ID of this version: #1029752/25
Text MD5: e3ca8d6d9afd00a02bbbcf52d26b0feb
Transpilation MD5: b9f1d8528e18bc1cc826d4def6b512c8
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-11 23:00:50
Source code size: 13650 bytes / 405 lines
Pitched / IR pitched: No / No
Views / Downloads: 359 / 1066
Version history: 24 change(s)
Referenced in: [show references]