I'm working on a new project that will require some 3d rendering. I am planning on writing all of the calculations for each individual pixel (bypassing a graphics engine), and I'd like to do it with Javascript, but I don't know if there's a way. Posting HTML to display thousands of pixels is way too slow. I need something quick and responsive, so I'm considering using JAVA.
My question is, do either of these have the ability to perform calculations on individual pixels, quickly? And how would I access the display in such a way?
Thanks
If you have the time to get familiar with OpenGL, you could use OpenGL in java:
http://en.wikipedia.org/wiki/Java_OpenGL
This would allow you to directly manipulate a specified OpenGL-frame. Kindof like what runescape does.
Related
I am having a problem with manipulating images in java 7. I have researched the problem I am having for three weeks and have failed to find a solution,
I am trying to set an image over an area, basically setting the corner posistions.
I am using a BufferedImage.
This is for a 3D game where I am writing the 3D conversion code. I have managed to create a 3d world, and populate it with cubes, filling in the sides with graphics.fillPolygon():
What I would like to do is draw an image that fills a polygon's shape.
Any help I will be grateful for (even if it is formating this post better).
You can use transformations on your graphics. Here is a little tutorial on it. With that you can transform already drawn stuff. If you want to transform multiple images in different ways, you can draw them on different canvases and transform then separately. Then you can merge the images together...
But I would recommend using a 3D engine (JOGL, jMonkeyEngine, or others) for that (unless you want to learn about the geometric calculations with this task). It is also much faster to use OpenGL than drawing the stuff by yourself and doing the calculations in code (most probably meaning: on the CPU and not the GPU).
Have you tried using the drawImage? instead of .fillPolygon?
I have a system in place for a game yet I don't know what I should use to display it. I am making a vertical shooter game and I have written the methods for all the classes controlling enemies and players, but I have not idea how to efficiently display the game. I was thinking a Canvas, that would repaint every frame, but is that really the most efficient method?
Important details:
ideal framerate: 25fps
It is a 2d game
There are anywhere between 25-100 objects on the screen at any one time, all of which are moving
All objects being displayed are images, all in PNG format
The window is 640px by 480px
Right now all the images are loaded as BufferedImage, although I could easily change this
7. I need a coordinate plane. This is the only fundamental part that cannot be changed without completely restructuring my code.
Most importantly the way I have everything set up, every frame all of the objects move and interact in a coordinate plane I devised (deals with collision detection and movement, no graphical component), then everything should get painted to the screen, simply by going through the ArrayLists which keep track of all moving objects and painting them one by one.
If Swing is acceptable, JPanel is double-buffered by default, and a javax.swing.Timer with a period of 40 ms will give you ~25 Hz updates. This example shows the basic approach, while this example shows a number of images in motion.
I need a coordinate plane.
It's not unusual to have the the model and view use different coordinates; all that's needed are functions to map one system to the other. In this game, the view relies on four methods that map tiles to pixels and vice-versa. The same approach is outlined here for map tiles.
You have a number of options available to you:
Firstly, you could use one of the existing Java game frameworks:
JMonkeyEngine (http://jmonkeyengine.com/)
Slick (http://slick.cokeandcode.com/index.php)
(Slick is aimed at 2D graphics, while JMonkey is aimed at 3D and uses OpenGL - While I have looked into their use, I've not actually used them myself)
Alternatively you can code everything yourself. From the sounds of things this is your first (graphical) game, so you may want to read up on a technique known as double buffering, whereby you write each frame off-screen and the just paint the whole thing to screen, as this can lead to smoother animation.
To get you into games development a bit more, I would highly recommend reading this site, Killer Game Programming in Java by Dr Andrew Davison, as he gives some good pointers, and also provides a good progressive learning path for new game developers, and moving them into 2D and then 3D development.
HTH
The answer really depends on whether this game is 2D or 3D.
If your game is 2D, an easy way to do what you want is to use Java's own 2D Graphics API. A good tutorial to start you off (at least in my opinion) can be found at The Java Tutorials. In my experience I have found that Java's graphics API is easy to learn, and is more efficient than one might expect. The basic technique is to have your game code keep track of the positions of all your objects, then translate those coordinates into screen coordinates and display appropriate images at those locations. I made a (very, very simple) game in Java once, and this is the method I used.
If your game is in 3D, OpenGL is definetly the way to go. I have only limited experience with OpenGL, so I'm not sure how easy the Java bindings are to work with. In general, 3D programming is a massive topic, so if this is a first-time project or you aren't prepared for a major time investment, I would attempt to find a good game framework to use or make the game 2D. If you are interested in OpenGL, or 3D programming in general, a quick Google turned up the JOGL project. I would recommend investigating JOGL as a way to access the OpenGL API from within Java code, but for actually learning OpenGL I recommend The OpenGL SuperBible(5th edition), commonly known as "The Blue Book".
The code examples are all in C++, but for the OpenGL functions it could possibly be just a matter of using a wrapper library. For example:
glDrawElements(...);
May become:
JavaGLWrapperObject.glDrawElements(...);
Unfortunately I can't give concrete examples because I haven't used OpenGL with a Java program, but the above example very coarsely approximates how OpenGL ES is used on the Android platform.
As far as performance... Java's API comes with a non-trivial ammount of overhead, but I could see it doing alright for your purposes. You may have to put a little more effort into making your algorithms efficient, and your game may not run as well on less-capable hardware. If you do decide to go the OpenGL route, it will almost certainly be much faster (again, depending on how efficient your algorithms are), but is correspondingly much harder to learn and get started with. Certainly doable, but it will be a challenge.
Canvas will quickly be too slow for 25+ fps targeted.
the number of object is irrelevant, what is is how complex they are.
if it is 100 images will take nothing compared to 1 3D model of Avatar for example.
in Java you can use either Opengl or Java3D
I would tend to go with Opengl as a personel choise
I'm making an applet that lets users crop out a piece of an image and save it. For cropping, I'm going to implement a "magic wand"-esque tool. I can do all this in Matlab but i'm having some trouble figuring out the Java libraries. Here are a few tasks I need to perform:
Randomly access pixels in an image by (x,y) and return a single object (java.awt.Color, ARGB int, short[], whatever -- as long as I'm not dealing with channels individually)
Create an alpha channel from a boolean[ ][ ]
Create a N by M image that's initialized to green
Any pros out there who can help me? Just some code snippets off the top of your head would be fine.
Many thanks,
Neal
You want to use the Java2D libraries. Specifically, you want to use the BufferedImage class from the library to deal with your images. You can access individual pixels and do all of the things you have specified above. Sun/Oracle has a good tutorial to get you started in the right direction. The second part in that tutorial goes over creating an alpha channel. Oh, and to access individual pixels, you want to use the WritableRaster class. So you can do something like this. Hope this gets you started.
WritableRaster imageRaster = Bufferedimg.getRaster();
//use java random generation to get a random x and y coordinate, then call this to access the pixel
imageRaster.getPixel(x, y,(int[])null);
ImageJ is a mature, open-source image processing framework that supports macros, plugins and a host of other features.
Marvin is a Java image processing framework that can help you. It provides algorithms for filtering, feature extraction, morphological analysis, tranformations, segmentation and so forth. Moreover, its architecture supports real-time video processing with the same algorithms.
I need to draw some execution sequences for some real time systems scheduled using rate monotonic algorithm. The drawing has to be done using Java and it has to be very basic.
Do you a very simple way to do this drawing? Simpler than Graphics2D?
Thanks a lot!
The diagram I want to be as simple or even simpler as this one:
I would suggest you to try with Processing, which is able to do such things with Java in zero time.. home page here.
You can easily embed a Processing frame inside your app, but remember that they are based on AWT so they are heavy weight and don't like being overlayed (for example in a JInternalFrame)
By the way what you seem to want to draw is a Gantt chart right?
You can try with JFreeChart too (see demo here), it's quite customizable and works fine.. homepage here
I'm writing a game in Java, LJGWL (OpenGL). I'm using a library that handles a lot of messy details for me, but need to find a lot faster way to do this.
Basically I want to set every pixel on the screen to say a random color as fast a possible. The "random colors" is just an Array [][] that gets updated every 2-3 seconds. I've tried drawing rects and using images, both are pretty slow for what I want to do.
I think I want to learn how to write a GPU shader? That is the fastest way to do this? LJGWL exposes OpenGL api to java. Any basic tutorials on how to get started with OpenGL shaders? Or should I dynamically create a texture of some sort and then just throw up the entire texture, would that be faster?
If it were the case that you were statically displaying the same image, than using a texture or display list would suffice. But as you want to frequently update it, shaders really are the best option. Shader code executes on the GPU and modifies data in GRAM, so you have no bottle neck transferring from CPU to GPU. The next best thing would probably be a Pixel or Frame Buffer Object. Buffer Objects let you read/write to GRAM via DMA (without having to go through the CPU) so they can be pretty fast.
I haven't written any shaders yet, so I can't recommend any good resources. But SongHo's OpenGL pages are a good place to learn about Buffer Objects. (His examples are in C++ though)
Textures are the fastest way to draw something on screen, draw a texture mapped quad into the screen, it should be fast enough. When you need to reupload the texture data, use glTexSubimage2D to update it.
No need to use shaders.
I've yet to do any work with shaders in OpenGL, but given the same scenario in multiple occasions, I handled it with a texture I threw up across the screen on top, and it worked quite effectively.
I don't know how you are drawing your pixels exactly, but this limit you hit could be because of the amount of data you transfer (inefficiently?). Updating a screen full of pixels every 2-3 seconds shouldn't be hard at all. Although shaders bring you closer to the graphics card, they will never make inefficient methods fast, so...
Why is your code so slow?
What code? What code exactly did you try? What texture did you use, render to, ...?
Is it slow? How slow? How fast do you expect it to be?
How quickly can one get 1920x1080(?) pixels in video ram, what's your hardware, drivers, OS?
I think you need to edit/repost before we can help you solve your problem. Just because it is slow, is no guarantee at all that shaders will even be one bit faster.