Java Graphics2D: How to Apply Perspective Transform to Graphics2D object? - java

Something I've been banging my head against for a while in Java is Perspective Transform.
I want to make an object in my game animate trapezoidaly. This means applying a different Perspective Transformation to it each frame.
The problem is that Graphics2D.transform() only accepts AffineTransformations for some reason. A PerspectiveTransform class does exist, as well as the ease of making a Double[3][3] matrix, but neither of those work.
Like...it would be easy if I could just set the projection matrix manually, but an AffineTransformation doesn't actually have variables for locations M20 and M21.
I've googled and searched on this site for DAYS and while a handful of people have brought it up, nobody has given an answer.

Related

Anti Aliasing based on colors (not textures)

I was searching for an anti-aliasing algorithm for my OpenGL program (so I searched for a good shader). The thing is, all shaders want to do something with the textures, but I dont use textures, only colors. I looked at FXAA most of the time, so is there a anti-aliasing algorithm that just works with colors? My game, what this is for looks blocky like minecraft, but only works with colors and cubes of different size.
I hope someone can help me.
Greetings
Anti-aliasing has nothing specifically to do with either textures or colors.
Proper anti-aliasing is about sample rate, which while highly technical can be thought of as doing extra work to make a better educated guess at some value that cannot be directly looked up (e.g. a pixel that is only partially covered by a triangle).
Multisample Anti-Aliasing (MSAA) will work nicely for you, it will only anti-alias polygon edges and does nothing for texture aliasing on the interior of a polygon. Since you are not using textures you do not need to worry about aliasing inside a polygon.
Incidentally, FXAA is not proper anti-aliasing. FXAA is basically a shader-based edge detection and blur image processing filter. FXAA will blur any part of the scene with sharp edges, whether it is a polygon edge or an edge due to a mapped texture. It indiscriminately blurs anything it thinks is an aliased edge and gets this wrong often, resulting in blurry textures.
To use MSAA, you need:
A framebuffer with at least 2 samples
Enable multisample rasterization
Satisfying (1) is going to depend on what you used to create your window (in this case LWJGL). Most frameworks let you select the sample count as one of the parameters at the time of creation.
Framebuffer Objects can also be used to do this without messing with your window's parameters, but they are more complicated than need be for this discussion.
(2) is as simple as calling glEnable (GL_MULTISAMPLE).

Square BoundingBox with OpenGL JOGL Java

I'm trying to make a project in OpenGL using JOGL.
If you see my image http://imgur.com/DDHoXEz, I have 4 viewports with different projections but all Teapots are out of "scale", and I want to make something like a bounding box, a square with side 1, that contains all objects on the viewports, to make a scale out of the square.
Any tips?
Unless you're going to use the base teapot model for programs (which you shouldn't), I don't think this is something to spend your time on. When you get into actually using your own models, you will have direct control over the scale.
I would recommend at this point learning about different drawing methods in OpenGL (e.g., GL_TRIANGLE_FAN, GL_LINE_LOOP). Then move on to learning about vertex arrays and maybe write an OBJ importer. I can point you in the right direction if you'd like.
Here is a good place to get started on different drawing techniques.
Happy coding!

3D Shadow implementation idea

Lets assume your eye is in the surface point P1 on an object A and there is a target object B and there is a point-light source behind object B.
Question: am i right if i look to the light source and say "i am in a shadow" if i cannot see the light because of object B ?. Then i flag that point of object A as "one of the shadow points of B on A" .
If this is true, then can we build a "shadow geometry"(black-colored) object on the surface of A then change it constantly because of motion of light,B,A, etc... in realtime ? Lets say a sphere(A) has 1000 vertices and other sphere (B)has 1000 vertices too, so does this mean 1 milion comparations? (is shadowing, O(N^2) (time) complexity?). I am not sure about the complexity becuse the changing the P1(eye) also changes the seen point of B (between P1 and light source point). What about the second-order shadows and higher (such as lights being reflecting between two objects many times) ?
I am using java-3D now but it doesnt have shadow capabilities so i think of moving to other java-compatible libraries.
Thanks.
Edit: i need to disable the "camera" when moving the camera to build that shadow. How can i do this? Does this decrease the performance badly?
New idea: java3D has built-in collision detection. I will create lines(invisible) from light to target polygon-vertex then check for a collision from another object. If collision occurs, add that vertex corrd. to the shadow list but this would work only for point-lights :( .
Anyone who supplys with a real shade library for java3d, will be much helpful.
Very small sample Geomlib shadow/raytracing in java3D would be the best
Ray-tracing example maybe?
I know this is a little hard but could have been tried by at least a hundred people.
Thanks.
Shadows is probably the most complex topic in 3D graphics programming, and there are many approaches, but the best option should be identified according to the task requirements. The algorithm you are talking about is the simplest way to implement shadows from a spot light source onto the plane. It should not be done on the CPU, as you already use GPU for 3D rendering.
Basically the approach is to render the same object twice: once from the camera view point, and once from the light source point. You will need to prepare model view matrices to convert between these two views. Once you render the object from the light point, you get the depth map, in which each point lies closest to the light source. Then, for each pixel of the normal rendering, you should convert its 3D coordinates into the previous view, and check against the corresponding depth value. This essentially gives you a way to tell which pixels are covered by shadow.
The performance impact comes from rendering the same object twice. If your task doesn't assume high scalability of shadow casting solution, then it might be a way to go.
A number of relevant questions:
How Do I Create Cheap Shadows In OpenGL?
Is there an easy way to get shadows in OpenGL?
What is the simplest method for rendering shadows on a scene in OpenGL?
Your approach can be summarised like this:
foreach (point p to be shaded) {
foreach (light) {
if (light is visible from p)
// p is lit by that light
else
// p is in shadow
}
}
The funny fact is that's how real-time shadows are done today on the GPU.
However it's not trivial for this to work efficiently. Rendering the scene is a streamlined process, triangle-by-triangle. It would be very cumbersome if for every single point (pixel, fragment) in every single triangle you'd need to consider all other triangles in other to check for ray intersection.
So how to do that efficiently? Answer: Reverse the process.
There's a lot fewer lights than pixels on the scene, usually. Let's take advantage of this fact and do some preprocessing:
// preprocess
foreach (light) {
// find all pixels p on the scene reachable from the light
}
// then render the whole scene...
foreach (point p to be shaded) {
foreach (light) {
// simply look up into what was calculated before...
if (p is visible by the light)
// p is lit
else
// p is in shadow
}
}
That seems a lot faster... But two problems remain:
how to find all pixels visible by the light?
how to make them accessible quickly for lookup during rendering?
There's the tricky part:
In order to find all points visible by a light, place a camera there and render the whole scene! Depth test will reject the invisible points.
To make this result accessible later, save it as a texture and use that texture for lookup during the actual rendering stage.
This technique is called Shadow Mapping, and the texture with pixels visible from a light is called a Shadow Map. For a more detailed explanation, see for example the Wikipedia article.
Basically yes, your approach will produce shadows. But doing it point by point is not feasible performance wise (for realtime), unless its done at the GPU. I'm not familiar with what the API's offer today, but I'm sure any recent engine will offer some shadow out of the box.
Your 'New idea' is how shadows were implemented back in the days when rendering was still done with the CPU. If the number of polygons isn't too big (or you can efficently reject entire bunches by having grouping volumes etc.) it can be done with fairly little CPU power.
3D shadow rendering on vanilla Java is never going to be efficient. You best use graphical libraries written to utilize the full capabilities range of the graphical card, such as OpenGL or DirectX. As you are using Canvas (from the screenshot you provided), you can even paint that Canvas from native code using JNI. So you could use all the technology from graphial libraries, do just a little fiddling and paint your Canvas directly from the native code. There would be very little work involved to make it work, compared to writing your own 3D engine.
Wiki link about AWT native access: http://en.wikipedia.org/wiki/Java_AWT_Native_Interface
Documentation: http://docs.oracle.com/javase/7/docs/technotes/guides/awt/AWT_Native_Interface.html

converting a 2d window point to a 3d point

i am writing a 3d modeler similar to Blender for a game i am making. Since programs like blender export very complicated file types with alot of unneeded data i wanted to write a simple editor for my game. what i cannot figure out is how to map a point from a 2d projection on the window to where i have clicked in the 3d world with the world being rotated.
If anyone knows any good tutorials on how to do this or the method any help would be appreciated. I know i could use ray tracing but that would be to complicated i think.
The two main methods of mouse picking are:
Intersection Testing
Color Picking
Intersection tests are the more popular of the two, and at the simplest level involves 'shooting' out a ray and checking if it has intersected any points. The ray can also be replaced by a polytope if one wants to achieve more sensitive picking (useful for choosing points on vertices).
Color picking involves disabling AA, blending, shadows, etc. and re-drawing the scene using solid colors for the objects. glReadPixels is then used to find the color at the point of the mouse and this color is used to determine if it clicked on an applicable object.
Ray Picking:
Mouse Ray Picking Explained
Picking, Alpha Blending, Alpha Testing, Sorting
Color Picking:
OpenGL Selection Using Unique Color IDs
Picking Tutorial
The term you are looking for is mouse picking.
The method you need is gluUnProject. You'll need window x,y and the depth.
I think, in your case, it might be a lot easier to write a simple exporter for Blender.

Creating a composite Shape in Java 2D

Using Java 2D I've patched several Bezier curves (CubicCurve2D) together to create a "blob". The problem I now face is how to:
Efficiently fill the blob with a given colour.
Efficiently determine whether a given point lies inside the blob.
I noticed thst CubicCurve2D implements Shape which provides numerous contains methods for determining "insideness" and that Graphics2D is able to fill a Shape via the fill(Shape) (which I believe uses Shape's getPathIterator methods to do this).
Given this I was hoping I could create a composite Shape, whereby my getPathIterator(AffineTransform) method would simply link the underlying PathIterators together. However, this is producing a NoSuchElementException once my shape contains more than one CubicCurve2D. Even if I do manage to achieve this I'm not convinced it will work as expected because a CubicCurve2D is always filled on the convex side, and my "blob" is composed of concave and convex curves. The "contains" problem is even harder as a point can legitimately lie within the blob but not within any of the individual curves.
Am I approaching this problem in the correct way (trying to implement Shape?) or is there an idiomatic way to do this that I'm unaware of? I would have thought that the problem of compositing geometric shapes would be fairly common.
Does anyone have any suggestions regarding how to solve this problem?
Thanks in advance.
I'm not sure I understand your question but composite shapes can be created with the class java/awt/geom/Area.
Looking to Shape for a solution is the right way to go about this. If you have a collection of curves that you are trying to assemble into a shape, I would suggest that you use a GeneralPath. Simply add your curves, or straight line segments, as required. Look to the interface to see the various append methods. Also note that you can 'complete' the shape by joining the last point to the starting point.
Once the path is closed, there are a number of different versions of contains() that can be used, please take the time to read each of their descriptions, as there are trade-offs in terms of speed and accuracy, depends on your application.
Also it is easy to get a shape from the path, and fill it, transform it, etc.

Categories