Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.45 KB | None | 0 0
  1. package stanalone;
  2. import static java.awt.Color.cyan;
  3. import static java.awt.Color.magenta;
  4. import static java.awt.Color.white;
  5. import static java.awt.Color.yellow;
  6. import static java.awt.Color.red;
  7. import static java.lang.Math.PI;
  8. import static java.lang.Math.cos;
  9. import static java.lang.Math.min;
  10. import static java.lang.Math.signum;
  11. import static java.lang.Math.sin;
  12. import static javax.media.opengl.GL.GL_ARRAY_BUFFER;
  13. import static javax.media.opengl.GL.GL_COLOR_BUFFER_BIT;
  14. import static javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT;
  15. import static javax.media.opengl.GL.GL_DEPTH_TEST;
  16. import static javax.media.opengl.GL.GL_FLOAT;
  17. import static javax.media.opengl.GL.GL_FRONT;
  18. import static javax.media.opengl.GL.GL_FRONT_AND_BACK;
  19. import static javax.media.opengl.GL.GL_LEQUAL;
  20. import static javax.media.opengl.GL.GL_MAX_TEXTURE_SIZE;
  21. import static javax.media.opengl.GL.GL_NICEST;
  22. import static javax.media.opengl.GL.GL_POINTS;
  23. import static javax.media.opengl.GL.GL_WRITE_ONLY;
  24. import static javax.media.opengl.GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT;
  25. import static javax.media.opengl.GL2GL3.GL_FILL;
  26. import static javax.media.opengl.fixedfunc.GLLightingFunc.GL_SMOOTH;
  27. import static javax.media.opengl.fixedfunc.GLMatrixFunc.GL_MODELVIEW;
  28. import static javax.media.opengl.fixedfunc.GLMatrixFunc.GL_PROJECTION;
  29.  
  30. import java.awt.Color;
  31. import java.awt.Component;
  32. import java.awt.Dimension;
  33. import java.awt.Point;
  34. import java.awt.event.WindowAdapter;
  35. import java.awt.event.WindowEvent;
  36. import java.awt.geom.Point2D;
  37. import java.nio.ByteBuffer;
  38. import java.nio.ByteOrder;
  39. import java.nio.FloatBuffer;
  40. import java.util.LinkedList;
  41. import java.util.List;
  42. import java.util.Random;
  43. import java.util.concurrent.ScheduledThreadPoolExecutor;
  44. import java.util.concurrent.TimeUnit;
  45.  
  46. import javax.media.opengl.GL;
  47. import javax.media.opengl.GL2;
  48. import javax.media.opengl.GLAutoDrawable;
  49. import javax.media.opengl.GLCapabilities;
  50. import javax.media.opengl.GLEventListener;
  51. import javax.media.opengl.GLProfile;
  52. import javax.media.opengl.awt.GLJPanel;
  53. import javax.media.opengl.fixedfunc.GLLightingFunc;
  54. import javax.media.opengl.fixedfunc.GLPointerFunc;
  55. import javax.media.opengl.glu.GLU;
  56. import javax.swing.JFrame;
  57. import javax.swing.SwingUtilities;
  58.  
  59. import com.jogamp.common.nio.Buffers;
  60. import com.jogamp.opengl.util.FPSAnimator;
  61. import com.jogamp.opengl.util.gl2.GLUT;
  62. class Histogram {
  63. public Histogram() {
  64. this(10,0,1);
  65. }
  66. public Histogram(int bins,double low,double high) {
  67. this.bins=bins;
  68. bin=new int[bins];
  69. this.low=low;
  70. this.high=high;
  71. range=high-low;
  72. }
  73. public void add(double[] x) {
  74. for(int i=0;i<x.length;i++)
  75. add(x[i]);
  76. }
  77. public void add(double x) {
  78. n++;
  79. sum+=x;
  80. final double x2=x*x;
  81. sum2+=x2;
  82. min=Math.min(min,x);
  83. max=Math.max(max,x);
  84. if(x>=high)
  85. overflows++;
  86. else if(x<low)
  87. underflows++;
  88. else {
  89. double val=x-low;
  90. int index=(int)(bins*(val/range));
  91. bin[index]++;
  92. }
  93. }
  94. public void clear() {
  95. for(int i=0;i<bins;i++)
  96. bin[i]=0;
  97. overflows=0;
  98. underflows=0;
  99. min=Double.MAX_VALUE;
  100. max=Double.MIN_VALUE;
  101. }
  102. public int n() {
  103. return n;
  104. }
  105. public double low() {
  106. return low;
  107. }
  108. public double high() {
  109. return high;
  110. }
  111. public double range() {
  112. return high()-low();
  113. }
  114. public int bins() {
  115. return bins;
  116. }
  117. public double min() {
  118. return n==0?Double.NaN:min;
  119. }
  120. public double max() {
  121. return n==0?Double.NaN:max;
  122. }
  123. public double sum() {
  124. return n==0?Double.NaN:sum;
  125. }
  126. public double mean() {
  127. return n==0?Double.NaN:sum/n;
  128. }
  129. public double variance() {
  130. return n==0?Double.NaN:sum2/n-mean()*mean();
  131. }
  132. public int bin(int index) {
  133. if(index<0)
  134. return underflows;
  135. else if(index>=bins)
  136. return overflows;
  137. else return bin[index];
  138. }
  139. public double maxDifference() {
  140. double max=0;
  141. for(int i=0;i<bins();i++)
  142. max=Math.max(max,Math.abs(bin(i)-n()/(double)bins())/(n()/(double)bins()));
  143. return max;
  144. }
  145. public String toString() {
  146. final StringBuffer sb=new StringBuffer();
  147. sb.append((float)min()).append("<=").append((float)mean()).append("<=").append((float)max()).append(" ");
  148. sb.append(bin(-1)).append(",[");
  149. for(int i=0;i<bins;i++)
  150. sb.append(i>0?",":"").append(bin(i));
  151. sb.append("],").append(bin(bins));
  152. return sb.toString();
  153. }
  154. private int[] bin;
  155. private int n,bins,underflows,overflows;
  156. private final double low,high,range;
  157. private double min=Double.MAX_VALUE,max=Double.MIN_VALUE,sum,sum2;
  158. }
  159. class MyDataObject {
  160. MyDataObject(Point2D[] points) {
  161. this(points,white);
  162. }
  163. MyDataObject(Point2D[] points,Color color) {
  164. this.points=points;
  165. this.color=color;
  166. }
  167. Point2D[] points;
  168. Color color;
  169. static Point2D[] randomPoints(Random random,Point2D offset) {
  170. List<Point2D> l=new LinkedList<Point2D>();
  171. int n=nPoints/pieces.length;
  172. for(int j=0;j<n;j++)
  173. l.add(new Point2D.Double(offset.getX()+random.nextFloat(),offset.getY()+random.nextFloat()));
  174. return l.toArray(new Point2D[0]);
  175. }
  176. static MyDataObject[] pieces;
  177. static Color[] colors=new Color[]{cyan,magenta,yellow,white};
  178. static int nPoints=1000000;
  179. }
  180. class StandAlone implements GLEventListener {
  181. StandAlone(GLAutoDrawable drawable) {
  182. this.drawable=drawable;
  183. drawable.addGLEventListener(this);
  184. init();
  185. }
  186. void init() {
  187. nVbos=4;
  188. vertexBufferIndices=new int[nVbos];
  189. for(int i=0;i<vertexBufferIndices.length;i++)
  190. vertexBufferIndices[i]=-1;
  191. numberOFVertices=new int[nVbos];
  192. // drawAxes=true;
  193. MyDataObject.pieces=new MyDataObject[nVbos];
  194. for(int i=0;i<nVbos;i++) {
  195. Point2D offset=new Point.Double(min(0,signum(cos(PI/4+i*PI/2))),min(0,signum(sin(PI/4+i*PI/2))));
  196. MyDataObject.pieces[i]=new MyDataObject(MyDataObject.randomPoints(random,offset),MyDataObject.colors[i%MyDataObject.colors.length]);
  197. }
  198. }
  199. static void setupFrame(Component component) {
  200. setupFrame(component,defaultFps);
  201. }
  202. static void setupFrame(Component component,int fps) {
  203. component.setPreferredSize(new Dimension(displayWidth,displayHeight));
  204. final FPSAnimator animator=new FPSAnimator((GLAutoDrawable)component,fps,true);
  205. final JFrame frame=new JFrame();
  206. frame.getContentPane().add(component);
  207. frame.addWindowListener(new WindowAdapter() {
  208. @Override public void windowClosing(WindowEvent e) {
  209. new Thread() {
  210. @Override public void run() {
  211. if(animator.isStarted())
  212. animator.stop();
  213. System.exit(0);
  214. }
  215. }.start();
  216. }
  217. });
  218. frame.setTitle(TITLE);
  219. frame.pack();
  220. frame.setVisible(true);
  221. animator.start();
  222. }
  223. static void setup() {
  224. GLProfile glprofile=GLProfile.getDefault();
  225. GLCapabilities glcapabilities=new GLCapabilities(glprofile);
  226. GLJPanel panel=new GLJPanel(glcapabilities);
  227. new StandAlone(panel);
  228. setupFrame(panel,200);
  229. }
  230. public static void main(String[] args) {
  231. SwingUtilities.invokeLater(new Runnable() {
  232. @Override public void run() {
  233. setup();
  234. }
  235. });
  236. }
  237. private void createVbo(GL2 gl2,int[] n,int index) {
  238. if(!gl2.isFunctionAvailable("glGenBuffers")||!gl2.isFunctionAvailable("glBindBuffer")||!gl2.isFunctionAvailable("glBufferData")||!gl2.isFunctionAvailable("glDeleteBuffers")) { throw new RuntimeException("Vertex buffer objects not supported."); }
  239. gl2.glGenBuffers(1,vertexBufferIndices,index);
  240. // create vertex buffer data store without initial copy
  241. gl2.glBindBuffer(GL_ARRAY_BUFFER,vertexBufferIndices[index]);
  242. gl2.glBufferData(GL_ARRAY_BUFFER,n[0]*3*Buffers.SIZEOF_FLOAT*2,null,GL.GL_DYNAMIC_DRAW);
  243. }
  244. private static void storeVerticesAndColors(FloatBuffer floatbuffer,MyDataObject w) {
  245. for(Point2D p:w.points) {
  246. floatbuffer.put((float)p.getX()).put((float)p.getY()).put(0);
  247. floatbuffer.put((float)(w.color.getRed()/255.));
  248. floatbuffer.put((float)(w.color.getGreen()/255.));
  249. floatbuffer.put((float)(w.color.getBlue()/255.));
  250. }
  251. floatbuffer.rewind();
  252. }
  253. private void fillVertexBuffer(GL2 gl2,MyDataObject piece,int index) {
  254. // map the buffer and write vertex and color data directly into it
  255. gl2.glBindBuffer(GL_ARRAY_BUFFER,vertexBufferIndices[index]);
  256. ByteBuffer bytebuffer=gl2.glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY);
  257. FloatBuffer floatbuffer=bytebuffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
  258. storeVerticesAndColors(floatbuffer,piece);
  259. gl2.glUnmapBuffer(GL_ARRAY_BUFFER);
  260. }
  261. protected int createAndFillVertexBuffer(GL2 gl2,MyDataObject piece,int index) {
  262. int[] n=new int[]{piece.points.length};
  263. if(vertexBufferIndices[index]==-1)
  264. createVbo(gl2,n,index);
  265. fillVertexBuffer(gl2,piece,index);
  266. return n[0];
  267. }
  268. private void renderPiece(GLAutoDrawable drawable,int index) {
  269. final GL2 gl2=drawable.getGL().getGL2();
  270. gl2.glColorMaterial(GL_FRONT_AND_BACK,GLLightingFunc.GL_AMBIENT_AND_DIFFUSE);
  271. gl2.glEnable(GLLightingFunc.GL_COLOR_MATERIAL);
  272. // draw all objects in vertex buffer
  273. gl2.glBindBuffer(GL_ARRAY_BUFFER,vertexBufferIndices[index]);
  274. gl2.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
  275. gl2.glEnableClientState(GLPointerFunc.GL_COLOR_ARRAY);
  276. gl2.glVertexPointer(3,GL_FLOAT,6*Buffers.SIZEOF_FLOAT,0);
  277. gl2.glColorPointer(3,GL_FLOAT,6*Buffers.SIZEOF_FLOAT,3*Buffers.SIZEOF_FLOAT);
  278. gl2.glPolygonMode(GL_FRONT,GL_FILL);
  279. gl2.glDrawArrays(GL_POINTS,0,1*numberOFVertices[index]);
  280. // disable arrays once we're done
  281. gl2.glBindBuffer(GL_ARRAY_BUFFER,0);
  282. gl2.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
  283. gl2.glDisableClientState(GLPointerFunc.GL_COLOR_ARRAY);
  284. gl2.glDisable(GLLightingFunc.GL_COLOR_MATERIAL);
  285. }
  286. private void startTimeReporting() {
  287. t0=System.nanoTime();
  288. if(frame%reportPeriod==0)
  289. t0Frame=t0;
  290. if(frame>1) {
  291. double dt=(System.nanoTime()-t0Display);
  292. double millis=dt/1000000.;
  293. hDisplay.add(millis);
  294. }
  295. }
  296. void endTimeReporting() {
  297. double dt=(System.nanoTime()-t0);
  298. double millis=dt/1000000.;
  299. hRender.add(millis);
  300. if(++frame%reportPeriod==0) {
  301. System.out.println("average render time: "+hRender.mean()+" ms., max fps="+1000./hRender.mean());
  302. double dtFrames=System.nanoTime()-t0Frame;
  303. System.out.println("average time between calls to display: "+hDisplay.mean()+" ms., actual fps="+reportPeriod/(dtFrames/1000000.)*1000.);
  304. hRender.clear();
  305. }
  306. t0Display=System.nanoTime();
  307. }
  308. void update() {
  309. angle+=1;
  310. }
  311. @Override public void reshape(GLAutoDrawable drawable,int x,int y,int width,int height) {
  312. System.out.println("super.reshape "+drawable);
  313. GL2 gl=drawable.getGL().getGL2();
  314. if(height==0)
  315. height=1;
  316. float aspect=(float)width/height;
  317. gl.glViewport(0,0,width,height);
  318. gl.glMatrixMode(GL_PROJECTION);
  319. gl.glLoadIdentity();
  320. // glu.gluPerspective(45.0,aspect,0.1,100.0);
  321. gl.glMatrixMode(GL_MODELVIEW);
  322. gl.glLoadIdentity();
  323. }
  324. @Override public void init(final GLAutoDrawable drawable) {
  325. GL2 gl=drawable.getGL().getGL2();
  326. glu=new GLU();
  327. glut=new GLUT();
  328. gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
  329. gl.glClearDepth(1.0f);
  330. gl.glEnable(GL_DEPTH_TEST);
  331. gl.glDepthFunc(GL_LEQUAL);
  332. gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
  333. gl.glShadeModel(GL_SMOOTH);
  334. for(int i=0;i<nVbos;i++)
  335. numberOFVertices[i]=createAndFillVertexBuffer(drawable.getGL().getGL2(),MyDataObject.pieces[i],i);
  336. }
  337. @Override public void display(GLAutoDrawable drawable) {
  338. update();
  339. if(doTimeReporting)
  340. startTimeReporting();
  341. GL2 gl=drawable.getGL().getGL2();
  342. gl.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  343. gl.glLoadIdentity();
  344. gl.glRotated(angle,0,1,0);
  345. gl.glColor3d(1,1,1);
  346. for(int i=0;i<nVbos;i++)
  347. renderPiece(drawable,i);
  348. if(doTimeReporting)
  349. endTimeReporting();
  350. }
  351. @Override public void dispose(GLAutoDrawable drawable) {}
  352. final GLAutoDrawable drawable;
  353. double angle;
  354. int nVbos;
  355. int[] vertexBufferIndices;
  356. int[] numberOFVertices;
  357. Random random=new Random();
  358. final int reportPeriod=100;
  359. boolean doTimeReporting=true;
  360. long t0,t0Frame,t0Display;
  361. int frame;
  362. Histogram hRender=new Histogram(10,0,10),hDisplay=new Histogram(10,0,100);
  363. int fps=defaultFps;
  364. protected GLU glu;
  365. protected GLUT glut;
  366. protected static String TITLE="JOGL 2.0 Setup (GLJPanel)";
  367. protected static final int displayWidth=1024;
  368. protected static final int displayHeight=1024;
  369. protected static final int defaultFps=60;
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement