Sign in to follow this  
chrisprij

Question about 3D render in Java

Recommended Posts

chrisprij    157

I've been watching a 3D tutorial on youtube about doing 3D graphics in Java without any exterior libraries -- just standard Java. I've been curious about how 3D graphics work down at the lower levels; I've used Unity and openGL to make applications, and now I'm aiming to go a step deeper into my understanding of 3D graphics (I also am very intrigued into the mathematics behind it all). And here is where my question arises.

 

Below is a function in the Render3D class for the tutorial. Using what's written below, it takes two for loops to go through each pixel in the pixels array, and end up drawing a green and blue checkered pattern shown here:

 

[sharedmedia=core:attachments:13636]

 

public void floor() {

	for (int y = 0; y < height; y++) {

		double ceiling = (y - height / 2.0) / height;
		double z = 8 / ceiling;

		for (int x = 0; x < width; x++) {

			double depth = (x - width / 2.0) / height;
			depth *= z;
			int xx = (int) (depth) & 15;
			int yy = (int) (z) & 15;
			pixels[x + y * width] = (xx << 4)||(yy << 4)<< 8;
		}
	}
}

 

basically, I'm wondering if anyone has a good explanation for how this works or what the name of the algorithm is. I understand how this physically chooses what color to put in each pixel (by following through the code and printing out values), but how does this relate to rendering a 3D environment later on?

 

Thanks in advance! Let me know if more information is needed as well.

--Chris

Share this post


Link to post
Share on other sites
Satharis    2444

I understand how this physically chooses what color to put in each pixel (by following through the code and printing out values), but how does this relate to rendering a 3D environment later on?

That's rather vague and I'm not sure what you mean. Setting the pixels specifically to a color with an algorithm then drawing it still requires some kind of API, what you listed wouldn't draw anything by itself.

 

Not to mention 3d rendering in games is done with rasterization, which is you feeding vertices and other information to an API like openGL and letting it do all the math heavy work to convert it to screen pixels. There are many many, maaaaaaaany books on the subject of 3d graphics. Did you have a more specific question about the thing you've been watching?

Share this post


Link to post
Share on other sites
chrisprij    157

Satharis,

'Rendering' was the wrong word; I meant how does this set the pixels to later render and display a 3D environment. However I now understand vaguely how this algorithm works.

The pixels array is a reference to a Java Swing's screen of pixels, allowing me to set the pixels for the screen (through the buffers, that is). This render method gets called twice, once for the back buffer and once for for the third buffer, and every tick the buffers get pushed up one, and the back buffer gets pushed onto the screen. Anyways, I spent a few hours yesterday playing around with this algorithm and changing variables, and I found out how it works; it uses the y-pixel value to determine the depth on the screen. Then it uses AND's and shifts to determine the color. He later uses this same algorithm, with some minor changes, to print textures onto the 3D environment, using the following change to the last line of this algorithm:

 

pixels[x*y+width] = Textures.floor.pixels[(xx & 7) + (yy & 7) << 3];

 

 

which uses an 64 length array (8 x 8 texture) to color the pixels to the colors the texture uses, transforming the world into this:

 

Episode15.jpg

 

I've used openGL a few times before, and that's what I usually use, but I wanted to understand what openGL might do under the hood through practice, or see other ways of rendering. My question was this: what does this algorithm have to do with any regular algorithm for setting the pixels (done by openGL) given a 3D environment? I know this is still vague but I'm not sure how else to ask this. How does this method, or does this method, relate to anything openGL does (Any better? haha)? Later in the episodes he starts creating blocks and walls and they keep their x-y-z coordinates, but I'm not there yet. Maybe there's some words of wisdom by then? 

I also have a couple 3D graphics books from my university's library now that talk about the underlying information about how openGL works, and that should open up some knowledge about how this might work. I'm starting to think this was a premature question; I'll throw myself into this for a couple weeks to see if I get anything from it.

 

Thanks for your answer though!

--Chris

Edited by chrisprij

Share this post


Link to post
Share on other sites
chrisprij    157

Shadowisadog:

 

Now that you bring that up, would you be able to point out where in the algorithm above it performs a 3D projection? I'm guessing it has something to do with the whole x' = x/z * zoom, y' = y/z * zoom idea....

Share this post


Link to post
Share on other sites
chrisprij    157

The video's are here.

 

 

However he has a really weird time explaining things . . . he'll cover stuff like what an array is and so on (really basic java fundamentals), but he doesn't explain how his code works too well, hence my question!

 

-Chris

Share this post


Link to post
Share on other sites
chrisprij    157

Ah Ha! I found where he (the guy I was watching on youtube) got the algorithm, Notch used it in a Lundum Dare Competition a while back! It starts here if anyone is interested in seeing what's up. biggrin.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this