Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package gl3.helloTriangle;
  7.  
  8. import com.jogamp.newt.Display;
  9. import com.jogamp.newt.NewtFactory;
  10. import com.jogamp.newt.Screen;
  11. import com.jogamp.newt.event.KeyEvent;
  12. import com.jogamp.newt.event.KeyListener;
  13. import com.jogamp.newt.opengl.GLWindow;
  14. import com.jogamp.opengl.GL;
  15. import static com.jogamp.opengl.GL.GL_ELEMENT_ARRAY_BUFFER;
  16. import static com.jogamp.opengl.GL.GL_STATIC_DRAW;
  17. import static com.jogamp.opengl.GL.GL_TRIANGLES;
  18. import static com.jogamp.opengl.GL.GL_UNSIGNED_SHORT;
  19. import static com.jogamp.opengl.GL3.*;
  20. import com.jogamp.opengl.GL3;
  21. import com.jogamp.opengl.GLAutoDrawable;
  22. import com.jogamp.opengl.GLCapabilities;
  23. import com.jogamp.opengl.GLEventListener;
  24. import com.jogamp.opengl.GLProfile;
  25. import com.jogamp.opengl.math.FloatUtil;
  26. import com.jogamp.opengl.util.Animator;
  27. import com.jogamp.opengl.util.GLBuffers;
  28. import com.jogamp.opengl.util.glsl.ShaderCode;
  29. import com.jogamp.opengl.util.glsl.ShaderProgram;
  30. import framework.BufferUtils;
  31. import framework.Semantic;
  32. import java.nio.FloatBuffer;
  33. import java.nio.IntBuffer;
  34. import java.nio.ShortBuffer;
  35.  
  36. /**
  37. *
  38. * @author gbarbieri
  39. */
  40. public class HelloTriangle implements GLEventListener, KeyListener {
  41.  
  42. public static GLWindow glWindow;
  43. public static Animator animator;
  44.  
  45. public static void main(String[] args) {
  46.  
  47. Display display = NewtFactory.createDisplay(null);
  48. Screen screen = NewtFactory.createScreen(display, 0);
  49. GLProfile glProfile = GLProfile.get(GLProfile.GL3);
  50. GLCapabilities glCapabilities = new GLCapabilities(glProfile);
  51. glWindow = GLWindow.create(screen, glCapabilities);
  52.  
  53. glWindow.setSize(1024, 768);
  54. glWindow.setPosition(50, 50);
  55. glWindow.setUndecorated(false);
  56. glWindow.setAlwaysOnTop(false);
  57. glWindow.setFullscreen(false);
  58. glWindow.setPointerVisible(true);
  59. glWindow.confinePointer(false);
  60. glWindow.setTitle("Hello Triangle");
  61.  
  62. glWindow.setVisible(true);
  63.  
  64. HelloTriangle helloTriangle = new HelloTriangle();
  65. glWindow.addGLEventListener(helloTriangle);
  66. glWindow.addKeyListener(helloTriangle);
  67.  
  68. animator = new Animator(glWindow);
  69. animator.start();
  70. }
  71.  
  72. private final String SHADERS_ROOT = "src/gl3/helloTriangle/shaders";
  73. private final String SHADERS_NAME = "hello-triangle";
  74.  
  75. private int vertexCount = 3;
  76. private int vertexSize = vertexCount * 5 * Float.BYTES;
  77. private float[] vertexData = new float[]{
  78. -1, -1,/**/ 1, 0, 0,
  79. +0, +2,/**/ 0, 0, 1,
  80. +1, -1,/**/ 0, 1, 0
  81. };
  82.  
  83. private int elementCount = 3;
  84. private int elementSize = elementCount * Short.BYTES;
  85. private short[] elementData = new short[]{
  86. 0, 2, 1
  87. };
  88.  
  89. private static class Buffer {
  90.  
  91. public static final int VERTEX = 0;
  92. public static final int ELEMENT = 1;
  93. public static final int TRANSFORM = 2;
  94. public static final int MAX = 3;
  95. }
  96.  
  97. private IntBuffer bufferName = GLBuffers.newDirectIntBuffer(Buffer.MAX);
  98. private IntBuffer vertexArrayName = GLBuffers.newDirectIntBuffer(1);
  99. private int programName, modelToClipMatrixUL;
  100. /**
  101. * Use pools, you don't want to create and let them cleaned by the garbage
  102. * collector continuously in the display() method.
  103. */
  104. private float[] scale = new float[16], zRotazion = new float[16], modelToClip = new float[16];
  105. private long start, now;
  106.  
  107. public HelloTriangle() {
  108. }
  109.  
  110. @Override
  111. public void init(GLAutoDrawable drawable) {
  112. System.out.println("init");
  113.  
  114. GL3 gl3 = drawable.getGL().getGL3();
  115.  
  116. initBuffers(gl3);
  117.  
  118. initVertexArray(gl3);
  119.  
  120. initProgram(gl3);
  121.  
  122. gl3.glEnable(GL_DEPTH_TEST);
  123.  
  124. start = System.currentTimeMillis();
  125. }
  126.  
  127. private void initBuffers(GL3 gl3) {
  128.  
  129. FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);
  130. ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(elementData);
  131.  
  132. gl3.glGenBuffers(Buffer.MAX, bufferName);
  133. gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
  134. {
  135. gl3.glBufferData(GL_ARRAY_BUFFER, vertexSize, vertexBuffer, GL_STATIC_DRAW);
  136. }
  137. gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);
  138.  
  139. gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
  140. {
  141. gl3.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementSize, elementBuffer, GL_STATIC_DRAW);
  142. }
  143. gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  144.  
  145. BufferUtils.destroyDirectBuffer(vertexBuffer);
  146. BufferUtils.destroyDirectBuffer(elementBuffer);
  147.  
  148. checkError(gl3, "initBuffers");
  149. }
  150.  
  151. private void initVertexArray(GL3 gl3) {
  152. /**
  153. * Let's create the VAO and save in it all the attributes properties.
  154. */
  155. gl3.glGenVertexArrays(1, vertexArrayName);
  156. gl3.glBindVertexArray(vertexArrayName.get(0));
  157. {
  158. /**
  159. * VBO is not part of VAO, we need it to bind it only when we call
  160. * glEnableVertexAttribArray and glVertexAttribPointer, so that VAO
  161. * knows which VBO the attributes refer to, then we can unbind it.
  162. */
  163. gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
  164. {
  165. /**
  166. * This is the vertex attribute layout:
  167. *
  168. * | position x | position y | color R | color G | color B |
  169. */
  170. int stride = (2 + 3) * Float.BYTES;
  171. int offset = 0 * Float.BYTES;
  172. /**
  173. * We draw in 2D on the xy plane, so we need just two
  174. * coordinates for the position, it will be padded to vec4 as
  175. * (x, y, 0, 1) in the vertex shader.
  176. */
  177. gl3.glEnableVertexAttribArray(Semantic.Attr.POSITION);
  178. gl3.glVertexAttribPointer(Semantic.Attr.POSITION, 2, GL_FLOAT, false, stride, offset);
  179. /**
  180. * Color needs three coordinates. Vec3 color will be padded to
  181. * (x, y, z, 1) in the fragment shader.
  182. */
  183. offset = 2 * Float.BYTES;
  184. gl3.glEnableVertexAttribArray(Semantic.Attr.COLOR);
  185. gl3.glVertexAttribPointer(Semantic.Attr.COLOR, 3, GL_FLOAT, false, stride, offset);
  186. }
  187. gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);
  188.  
  189. /**
  190. * Ibo is part of the VAO, so we need to bind it and leave it bound.
  191. */
  192. gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
  193. }
  194. gl3.glBindVertexArray(0);
  195.  
  196. checkError(gl3, "initVao");
  197. }
  198.  
  199. private void initProgram(GL3 gl3) {
  200.  
  201. ShaderCode vertShader = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT,
  202. null, SHADERS_NAME, "vert", null, true);
  203. ShaderCode fragShader = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT,
  204. null, SHADERS_NAME, "frag", null, true);
  205.  
  206. ShaderProgram shaderProgram = new ShaderProgram();
  207. shaderProgram.add(vertShader);
  208. shaderProgram.add(fragShader);
  209.  
  210. shaderProgram.init(gl3);
  211.  
  212. programName = shaderProgram.program();
  213.  
  214. /**
  215. * These links don't go into effect until you link the program. If you
  216. * want to change index, you need to link the program again.
  217. */
  218. gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
  219. gl3.glBindAttribLocation(programName, Semantic.Attr.COLOR, "color");
  220. gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "outputColor");
  221.  
  222. shaderProgram.link(gl3, System.out);
  223. /**
  224. * Take in account that JOGL offers a GLUniformData class, here we don't
  225. * use it, but take a look to it since it may be interesting for you.
  226. */
  227. modelToClipMatrixUL = gl3.glGetUniformLocation(programName, "modelToClipMatrix");
  228.  
  229. checkError(gl3, "initProgram");
  230. }
  231.  
  232. @Override
  233. public void dispose(GLAutoDrawable drawable) {
  234. System.out.println("dispose");
  235.  
  236. GL3 gl3 = drawable.getGL().getGL3();
  237.  
  238. gl3.glDeleteProgram(programName);
  239. gl3.glDeleteVertexArrays(1, vertexArrayName);
  240. gl3.glDeleteBuffers(Buffer.MAX, bufferName);
  241.  
  242. System.exit(0);
  243. }
  244.  
  245. @Override
  246. public void display(GLAutoDrawable drawable) {
  247. // System.out.println("display");
  248.  
  249. GL3 gl3 = drawable.getGL().getGL3();
  250.  
  251. /**
  252. * We set the clear color and depth (although depth is not necessary
  253. * since it is 1 by default).
  254. */
  255. gl3.glClearColor(0f, .33f, 0.66f, 1f);
  256. gl3.glClearDepthf(1f);
  257. gl3.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  258.  
  259. {
  260. // update matrix based on time
  261. now = System.currentTimeMillis();
  262. float diff = (float) (now - start) / 1000;
  263. /**
  264. * Here we build the matrix that will multiply our original vertex
  265. * positions. We scale, halving it, and rotate it.
  266. */
  267. scale = FloatUtil.makeScale(scale, true, 0.5f, 0.5f, 0.5f);
  268. zRotazion = FloatUtil.makeRotationEuler(zRotazion, 0, 0, 0, diff);
  269. modelToClip = FloatUtil.multMatrix(scale, zRotazion);
  270. }
  271. gl3.glUseProgram(programName);
  272. gl3.glBindVertexArray(vertexArrayName.get(0));
  273.  
  274. gl3.glUniformMatrix4fv(modelToClipMatrixUL, 1, false, modelToClip, 0);
  275.  
  276. gl3.glDrawElements(GL_TRIANGLES, elementSize, GL_UNSIGNED_SHORT, 0);
  277. /**
  278. * The following line binds VAO and program to the default values, this
  279. * is not a cheaper binding, it costs always as a binding. Every binding
  280. * means additional validation and overhead, this may affect your
  281. * performances. So you should avoid these calls, but remember that
  282. * OpenGL is a state machine, so what you left bound remains bound!
  283. */
  284. // gl3.glBindVertexArray(0);
  285. // gl3.glUseProgram(0);
  286. /**
  287. * Check always any GL error, but keep in mind this is an implicit
  288. * synchronization between CPU and GPU, so you should use it only for
  289. * debug purposes.
  290. */
  291. checkError(gl3, "display");
  292. }
  293.  
  294. protected void checkError(GL gl, String location) {
  295.  
  296. int error = gl.glGetError();
  297. if (error != GL_NO_ERROR) {
  298. String errorString;
  299. switch (error) {
  300. case GL_INVALID_ENUM:
  301. errorString = "GL_INVALID_ENUM";
  302. break;
  303. case GL_INVALID_VALUE:
  304. errorString = "GL_INVALID_VALUE";
  305. break;
  306. case GL_INVALID_OPERATION:
  307. errorString = "GL_INVALID_OPERATION";
  308. break;
  309. case GL_INVALID_FRAMEBUFFER_OPERATION:
  310. errorString = "GL_INVALID_FRAMEBUFFER_OPERATION";
  311. break;
  312. case GL_OUT_OF_MEMORY:
  313. errorString = "GL_OUT_OF_MEMORY";
  314. break;
  315. default:
  316. errorString = "UNKNOWN";
  317. break;
  318. }
  319. System.out.println("OpenGL Error(" + errorString + "): " + location);
  320. throw new Error();
  321. }
  322. }
  323.  
  324. @Override
  325. public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  326. System.out.println("reshape");
  327. GL3 gl3 = drawable.getGL().getGL3();
  328. /**
  329. * Just the glViewport for this sample, normally here you update your
  330. * projection matrix.
  331. */
  332. gl3.glViewport(x, y, width, height);
  333. }
  334.  
  335. @Override
  336. public void keyPressed(KeyEvent e) {
  337. if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  338. HelloTriangle.animator.stop();
  339. }
  340. }
  341.  
  342. @Override
  343. public void keyReleased(KeyEvent e) {
  344.  
  345. }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement