Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  What is the fastest way of setting screen pixels?  (Read 4144 times)
0 Members and 1 Guest are viewing this topic.
Offline SiestaGuru

Senior Newbie


Medals: 2



« Posted 2017-12-30 11:11:01 »

Hiya, sorry for the newbie question, but I couldn't find a good answer for this anywhere.

Basically I want to try to experiment a bit with building my own tiny (realtime) 3D rendering engine in java. I'd like to have a screen, then want to calculate a color for every pixel, and drop them on the screen.

It's the last part, just putting the pixels on screen, that's giving me some trouble. Methods like .drawRect() or .drawLine()  on graphics objects for one pixel just feel way to slow. Even just iterating over all pixels and setting them to one color is way too slow.  I'm looking for the fastest way of doing it, with as low of a level as necessary. Manipulating a large byte array representing colors for example would be great if that's the fastest way of doing it.  The only requirement besides speed is that it must allow me to calculate the pixels myself (so no libraries that have you send models/polygons over)

I don't particularly care what the pixels are drawn on, as long as whoever is using the program can actually see what I'm drawing in real time. Small bonus points are given if it also works on Android (with minor modifications), but it's really speed that's most important to me here.

Of course the speed would be much higher if I can offload calculations to the GPU, so ideally this method allows me to use one.

What are my options here?
Offline KaiHH

JGO Kernel


Medals: 622



« Reply #1 - Posted 2017-12-30 11:31:01 »

Since you are explicitly striving for performance and seemingly not for the "joy of building a rasterizer" you've basically presented the only viable solution/answer yourself:
OpenGL (on Android: "OpenGL for Embedded Systems").

Quote
I'm looking for the fastest way of doing it, with as low of a level as necessary

=> OpenGL or Vulkan (but for simplicity I suggest OpenGL)

Quote
The only requirement besides speed is that it must allow me to calculate the pixels myself (so no libraries that have you send models/polygons over)

=> Shaders in OpenGL

Quote
Small bonus points are given if it also works on Android (with minor modifications)

=> OpenGL ES

Quote
but it's really speed that's most important to me here

=> OpenGL

Quote
Of course the speed would be much higher if I can offload calculations to the GPU, so ideally this method allows me to use one.

=> OpenGL!
Offline SiestaGuru

Senior Newbie


Medals: 2



« Reply #2 - Posted 2017-12-30 12:17:49 »

Actually, the joy of building a rasterizer is an essential component which I meant with the ability to set pixels myself / building a rendering engine. Unless I'm mistaken about OpenGL, it's basically doing exactly the same thing as what I'd like to build myself here, which kind of defeats the point.

The speed is important to me because I'd like to see how fast of a rendering engine I can build in Java. While obviously there's no way to compete with OpenGL because we're sitting in the JVM, it feels kind of dumb to have an instant dramatic performance hit just for putting the pixels on screen.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Archive
« Reply #3 - Posted 2017-12-30 18:02:04 »

Check out mine, lol. I did exactly what you're going for. Avoid using the awt Graphics and only use it to blit a BufferedImage to your frame. Your renderer should literally just set pixels in an array (the buffer in the BufferedImage).

Offline buddyBro
« Reply #4 - Posted 2017-12-30 18:05:24 »

Quote
OpenGL, it's basically doing exactly the same thing as what I'd like to build myself

OpenGL can do what you're trying to do; e.g., given some world representation, convert them to screen pixels. But it's really up to u how u decide to use openGl. You could totally give openGl a 1d or 2d array of pixel colors, and have openGl draw them on screen.

I've built some projects that did the conversion from 3D world representation to 2D screen uniform-color quads/triangles, and then let openGl take care of those (because it's faster than Java2D). So it's really up to u how much of you're logic you want to do "reinvent" and how much you want to use openGl to do.

Side note, openGl doesn't really "do things for you", it's a library/framework that provides you with the tools that make it easier and simpler to do yourself. You're still multiplying model matrices with your vertexes and such. I wouldn't call it an "engine."
Offline gouessej
« Reply #5 - Posted 2017-12-30 18:36:56 »

Hi

You can use JOGL as it supports both desktop and embedded environments, it exposes both OpenGL and OpenGL-ES APIs. You can make your own rasterizer and simply call glDrawPixels to draw the result:
https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glDrawPixels.xml

However, glDrawPixels is only in OpenGL (backward compatible context), not in OpenGL ES. You could use a FBO instead.

Julien Gouesse | Personal blog | Website | Jogamp
Offline SiestaGuru

Senior Newbie


Medals: 2



« Reply #6 - Posted 2017-12-31 11:41:23 »

Check out mine, lol. I did exactly what you're going for. Avoid using the awt Graphics and only use it to blit a BufferedImage to your frame. Your renderer should literally just set pixels in an array (the buffer in the BufferedImage).

Yeah that sounds like exactly the thing I'm looking for. I couldn't find the relevant part of the source on your page though



And thanks buddyBro and gouessej, that does make OpenGL sound like it could do what I'm looking for. I'll try exploring it and see if just sending pixels there works better than the alternatives







Edit:
Alright, I'm satisfied, thanks for your help! I'm now using the LWJGL  library to bind to OpenGL, and it has a    glDrawPixels  method that just takes a pixel array, this allows ~800 fps on my machine on a baseline test where I don't change any pixels. This is more than plenty to experiment with
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1599 views)
2018-06-10 19:43:48

EgonOlsen (1700 views)
2018-06-10 19:43:44

EgonOlsen (1154 views)
2018-06-10 19:43:20

DesertCoockie (1582 views)
2018-05-13 18:23:11

nelsongames (1182 views)
2018-04-24 18:15:36

nelsongames (1705 views)
2018-04-24 18:14:32

ivj94 (2473 views)
2018-03-24 14:47:39

ivj94 (1690 views)
2018-03-24 14:46:31

ivj94 (2771 views)
2018-03-24 14:43:53

Solater (911 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!