3D square city

Hi everybody, I programmed for the first time a little 3d application.

I just draw the stuff on a bufferedImage and show it on a JPanel…
I use polygons of 4 coordninates.
My result for drawing time is 1ms per 1000 polygons… 58 ms per 50 000 polygons.

What I implemented so far:
-drawing 3d-objects
-sorting the objects so that the objects are going to be drawn in the correct order
-calculating shadow
-hiding polygons which are not showing their front

If you are interested, download the jar file:
http://www.java-gaming.org/user-generated-content/members/243187/citydream.jar
(you can directly execute the jar file when downloaded)

code:
http://www.java-gaming.org/user-generated-content/members/243187/src.jar
(replace jar with zip, unzip, open)

image:

Yes I know that there is very little drawing bug, but it’s barely visible…
What’s your drawing time??

I had a download, I think you need to increase the accuracy of the timing. I had 1ms for 5 polygons and 1ms for many 1000’s so if you are after timings then everyone will probably have the sametime for 1000

Your formula for calculating drawing time looks a little off.

this.meanTime = ((9L * this.meanTime + (System.currentTimeMillis() - time)) / 10L);

Edit: Yeah I changed your mean time function to

this.meanTime = System.currentTimeMillis() - time;

and I’m getting 3ms for 1245 polygons and 32ms for 17025 polygons.

@ziozio

I changed the timing a bit, I hope it is better now.

@CopyableCougar4

You are right. I now changed the code to:

meanTime = (9 * meanTime + 1000 * (System.currentTimeMillis() - time)) / 10;
g2.drawString("Drawing time: " + (meanTime / 1000) + "ms", fH - xImage, 3 * fH / 2 - yImage);

Why?
I want have the average time of the last drawings. I prevent the rounding inacurracy by multilying and in the following dividing by 1000…
I hope it is now better and shows more plausible numbers?

I get something like 58 ms per 50 000 polygons…
But remember: Some of the 50 000 polygons will not be drawn because the don’t show their frontside.

Edit: By the way, thanks for the medal! (@VaTTeRGeR)

For your FPS averaging, couldn’t you simply do something along the lines of this?

private float totalTime = 0, frames = 0;

totalTime += System.currentTimeMillis() - time;
frames++;

meanTime = totalTime / frames;

Yes you are simply calculating the average.
But if I would do it like that, I would have to reset the variables totalTime and frames every time when I change the polygon size. That is not a lot of effort, I know, but I always try to keep the things as simple as possible.

Look at this case:


        int averageTime = 0;
        int currentTime = 10;
        for (int i = 80; i >= 0; i--) {
            averageTime = (9 * averageTime + 1000 * currentTime) / 10;
            System.out.println((80 - i) + ": " + (averageTime / 1000) + "." + (averageTime - averageTime / 1000 * 1000));
        }

Here the averageTime goes closer and closer to currentTime.
No need to change anything when increasing the polygon size (thus the currentTime).

Edit: The decimal place of println is wrong in some cases, but that’s irrelevant…

Why would you need to reset thise variables when the polygon count changes?

totalTime = 1000
frames = 10
totalTime/frames = 100

now I increase the polygon size, after another 10 frames the values are (now each frame costing 200ms):
totalTime = 3000
frames = 20
totalTime/frames = 150

But actually the additional 10 frames would each have a time of 200, not 150

4000 => 5ms
20000 => 20ms
52000 => 50ms
100000 => 85ms

yeah my goal is actually to programm a little 3d game. One that with good performance, one that doesn’t make my laptop warm while running. Is this even possible? Somehow I think I should use the gpu that takes the polygon drawing for me…
Is it right that in this program (3d city) only the cpu is working when drawing the polygons on the bufferedImage? I have no clue…

The underlying implementation of Graphics2D might use hardware acceleration…
(https://docs.oracle.com/javase/8/docs/technotes/guides/2d/flags.html#opengl it won’t be enabled by default!)
…but it cannot be as effective as OpenGL because of the way the drawing works with many single calls instead of sending big buffers to the GPU.
It might get close with some optimizations done under the hood, but that is dependent on the Java implementation you use to run your program.

You could try running it with -Dsun.java2d.opengl=true it might speed up the rendering or not.

If you are using Intel integrated graphics then Java2D doesn’t seem to use hardware accelleration by default. This environment variable improved performance 4x in my Java2D application on at least one system using Intel graphics:

J2D_D3D_NO_HWCHECK=true