Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1.  
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.text.DecimalFormat;
  6. import javax.swing.*;
  7. import com.jogamp.opengl.GL2;
  8. import com.jogamp.opengl.GLAutoDrawable;
  9. import com.jogamp.opengl.GLEventListener;
  10. import com.jogamp.opengl.awt.GLCanvas;
  11. import com.jogamp.opengl.glu.GLU;
  12. import com.jogamp.opengl.util.FPSAnimator;
  13. import com.jogamp.opengl.util.awt.TextRenderer;
  14. import static com.jogamp.opengl.GL.*; // GL constants
  15. import static com.jogamp.opengl.GL2.*; // GL2 constants
  16. import static com.jogamp.opengl.fixedfunc.GLLightingFunc.GL_COLOR_MATERIAL;
  17. import static com.jogamp.opengl.fixedfunc.GLLightingFunc.GL_LIGHT0;
  18. import static com.jogamp.opengl.fixedfunc.GLLightingFunc.GL_LIGHTING;
  19.  
  20.  
  21. public class MainFrame implements GLEventListener { // Renderer
  22.  
  23. private static final String TITLE = "3D TEXT"; // window's title
  24. private static final int CANVAS_WIDTH = 640; // width of the drawable
  25. private static final int CANVAS_HEIGHT = 480; // height of the drawable
  26. private static final int FPS = 10; // animator's target frames per second
  27.  
  28. private GLU glu; // for the GL Utility
  29.  
  30. private TextRenderer textRenderer;
  31. private String msg = "Hello World 3d ";
  32. private DecimalFormat formatter = new DecimalFormat("###0.00");
  33.  
  34. private float textPosX; // x-position of the 3D text
  35. private float textPosY; // y-position of the 3D text
  36. private float textScaling; // scaling factor for 3D text
  37.  
  38. private static float rotateAngle = 0.0f;
  39.  
  40.  
  41. public static void main(String[] args) {
  42. // Create the OpenGL rendering canvas
  43. GLCanvas canvas = new GLCanvas(); // heavy-weight GLCanvas
  44. canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
  45. canvas.addGLEventListener(new MainFrame());
  46.  
  47. // Create a animator that drives canvas' display() at the specified FPS.
  48. final FPSAnimator animator = new FPSAnimator(canvas, FPS, true);
  49.  
  50. // Create the top-level container frame
  51. final JFrame frame = new JFrame(); // Swing's JFrame or AWT's Frame
  52. frame.getContentPane().add(canvas);
  53. frame.addWindowListener(new WindowAdapter() {
  54. @Override
  55. public void windowClosing(WindowEvent e) {
  56. // Use a dedicate thread to run the stop() to ensure that the
  57. // animator stops before program exits.
  58. new Thread() {
  59. @Override
  60. public void run() {
  61. animator.stop(); // stop the animator loop
  62. System.exit(0);
  63. }
  64. }.start();
  65. }
  66. });
  67. frame.setTitle(TITLE);
  68. frame.pack();
  69. frame.setVisible(true);
  70. animator.start(); // start the animation loop
  71. }
  72.  
  73. // ------ Implement methods declared in GLEventListener ------
  74.  
  75. /*
  76. * Called back immediately after the OpenGL context is initialized. Can be used
  77. * to perform one-time initialization. Run only once.
  78. */
  79. @Override
  80. public void init(GLAutoDrawable drawable) {
  81. GL2 gl = drawable.getGL().getGL2(); // get the OpenGL graphics context
  82. glu = new GLU(); // get GL Utilities
  83. gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color
  84. gl.glClearDepth(1.0f); // set clear depth value to farthest
  85. gl.glEnable(GL_DEPTH_TEST); // enables depth testing
  86. gl.glDepthFunc(GL_LEQUAL); // the type of depth test to do
  87. gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // best perspective correction
  88. gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting
  89.  
  90. gl.glEnable(GL_LIGHT0); // Enable default light (quick and dirty)
  91. gl.glEnable(GL_LIGHTING); // Enable lighting
  92. gl.glEnable(GL_COLOR_MATERIAL); // Enable coloring of material
  93.  
  94. // Allocate textRenderer with the chosen font
  95. textRenderer = new TextRenderer(new Font("TrueTypeFont", Font.BOLD, 12));
  96.  
  97. // Calculate the position and scaling factor
  98. // Rectangle2D bounds = textRenderer.getBounds(msg + "00.00");
  99. // int textWidth = (int)bounds.getWidth();
  100. // int textHeight = (int)bounds.getHeight();
  101. // System.out.println("w = " + textWidth);
  102. // System.out.println("h = " + textHeight);
  103. // 104 x 14
  104. }
  105.  
  106. /*
  107. * Call-back handler for window re-size event. Also called when the drawable is
  108. * first set to visible.
  109. */
  110. @Override
  111. public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  112. GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context
  113.  
  114. if (height == 0) height = 1; // prevent divide by zero
  115. float aspect = (float)width / height;
  116.  
  117. // Set the view port (display area) to cover the entire window
  118. gl.glViewport(0, 0, width, height);
  119.  
  120. // Setup perspective projection, with aspect ratio matches viewport
  121. gl.glMatrixMode(GL_PROJECTION); // choose projection matrix
  122. gl.glLoadIdentity(); // reset projection matrix
  123. glu.gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zNear, zFar
  124.  
  125. // Enable the model-view transform
  126. gl.glMatrixMode(GL_MODELVIEW);
  127. gl.glLoadIdentity(); // reset
  128. }
  129.  
  130. /*
  131. * Called back by the animator to perform rendering.
  132. */
  133. @Override
  134. public void display(GLAutoDrawable drawable) {
  135. GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context
  136. gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
  137. gl.glLoadIdentity(); // reset the model-view matrix
  138.  
  139. // ----- Rendering 3D text using TextRenderer class -----
  140. textRenderer.begin3DRendering();
  141.  
  142. gl.glTranslatef(0.0f, 0.0f, -50.0f);
  143. gl.glRotatef(rotateAngle, 1.0f, 0.0f, 0.0f);
  144. gl.glRotatef(rotateAngle * 1.5f, 0.0f, 1.0f, 0.0f);
  145. gl.glRotatef(rotateAngle * 1.4f, 0.0f, 0.0f, 1.0f);
  146.  
  147. // Pulsing Colors Based On Text Position
  148. textRenderer.setColor((float)(Math.cos(rotateAngle / 20.0f)), // R
  149. (float)(Math.sin(rotateAngle / 25.0f)), // G
  150. 1.0f - 0.5f * (float)(Math.cos(rotateAngle / 17.0f)), 0.5f); // B
  151.  
  152. // String, x, y, z, and scaling - need to scale down
  153. // Not too sure how to compute the x, y and scaling - trial and error!
  154. textRenderer.draw3D(msg + formatter.format(rotateAngle / 50), -20.0f,
  155. 0.0f, 0.0f, 0.4f);
  156.  
  157. // Clean up rendering
  158. textRenderer.end3DRendering();
  159.  
  160. // Update the rotate angle
  161. rotateAngle += 0.1f;
  162. }
  163.  
  164. /*
  165. * Called back before the OpenGL context is destroyed. Release resource such as buffers.
  166. */
  167. @Override
  168. public void dispose(GLAutoDrawable drawable) { }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement