Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Writing a Jogl benchmark application  (Read 565 times)
0 Members and 1 Guest are viewing this topic.
Offline jhsveli

JGO n00b
*

Posts: 2



« on: 2008-04-09 10:51:25 »

Hei!

In my project I need to basically draw a large number of rectangles on the screen. Starting out, over a year ago, I accepted advice that Jogl was the way to go as opposed to Java 2D. Now I want to get some numbers, hard evidence if you like, that Jogl performs better, and Java 2D doesn't perform to satisfaction.

I've written a simple bemchmark application that draws an increasing amount of rectangles on the screen, and halts when the time taken to draw a screen goes above 1 s. I'm using GLCanvas as my drawable and I'm listing the code for my GLEventListener below.

I compare the final amount of rectangles that the application could draw within one second. Java2D manages around 800, while Jogl manages in excess of 15000 rectangles.

I have limited experience with graphics programming, and would thus like some comments regarding the "fairness" of this test. Does it unintentionally favour one library over the other, or are the results relatively comparable?

The code for the Java2D implementation can be made available by request.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
package aaa;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;

public class JoglDrawer implements GLEventListener {

   private GL gl;
   private GLU glu = new GLU();
   private JFrame f;
   
   private double objectThickness = Integer.MAX_VALUE;
   private double objectLength =    Integer.MAX_VALUE;
   
   private int width;
   private int height;
   private int n;
   private int oldAmount;
   private int amount;
   private long drawingTime;

   
   public JoglDrawer(JFrame f) {
      this.f = f;
   }
   public void init(GLAutoDrawable drawable) {
      this.gl = ((GLAutoDrawable)drawable).getGL();
      gl.glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );          
     
      objectLength = drawable.getWidth();
      objectThickness = drawable.getHeight();
   }
   public void display(GLAutoDrawable drawable) {
      n++;
      long start = System.currentTimeMillis();
     
      this.gl = drawable.getGL();
      gl.glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );    
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);      
     
      height = drawable.getHeight();
      width = drawable.getWidth();
      oldAmount = amount;
      amount = (int)(width / (objectLength + 1) * (height / (objectThickness + 1)));
     
      int i = 0;      
      int x = 0, y = 0;
     
      while (i++ < amount) {
         
         while (x < width-(objectLength+1)) {
            drawRectangle(x, y, (int)(x+objectLength), (int)(y+objectThickness));
            x += objectLength + 1;            
         }
         x = 0;
         y += objectThickness + 1;
      }
     
      //Drawing complete
     long stop = System.currentTimeMillis();
      drawingTime = stop - start;
      check();
           
      String title = "" + 1 / ((double)drawingTime / 1000) + " FPS";        
      f.setTitle(title);
      drawingTime = 0;
             
   }
   
   private void check() {
      if (drawingTime > 1000) {
         System.out.println("Rendered "+oldAmount+" squares of size "+objectLength+"px by "+objectThickness+"px in less than 1s");
         f.dispose();
         System.exit(0);
      }
      objectLength -= objectLength*.01; //Reduce size by a hundredth      
     objectThickness -= objectThickness*.01;      
   }
   private void drawRectangle(int x1, int y1, int x2, int y2) {
      gl.glBegin(GL.GL_QUADS);
         gl.glColor3f(.25f, .50f, .50f);
         gl.glVertex2i(x1, y2);
         gl.glVertex2i(x2, y2);
         gl.glVertex2i(x2, y1);
         gl.glVertex2i(x1, y1);
      gl.glEnd();      
   }

   public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
   
     
   }  

   public void reshape(GLAutoDrawable drawable, int x, int y, int width,
         int height) {
      this.gl = drawable.getGL();
     
      gl.glMatrixMode( GL.GL_PROJECTION );  
      gl.glLoadIdentity();
      glu.gluOrtho2D(0, width, 0, height);
     
   }

}
Offline turquoise3232

Sr. Member
**

Posts: 332


Java (games) rock!


« Reply #1 on: 2008-04-10 04:10:19 »

Hi,

I have a little remark on your benchmark, you are using immediate mode for opengl drawings whereas VBO or at least vertex arrays are used nowadays to ensure good performance. Maybe you can try these renderings to get more accurate results.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.116 seconds with 21 queries.