Jasoco's 3D Engine Thread: Go to Page 6 Please!

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: On the subject of three-dimensions...

Post by kalle2990 »

anjo wrote:For the record, this:

Code: Select all

if ((y3-y1)/(x3-x1) - (y2-y1)/(x2-x1) <= 0) ^ (x1 <= x3 == x1 > x2) {
in Lua would be this:

Code: Select all

if ((y3-y1)/(x3-x1) - (y2-y1)/(x2-x1) <= 0) ~= ((x1 <= x3) == (x1 > x2)) then
You could probably get rid of some of the parentheses, but that should work. (As the article explains, a bitwise xor is just a not-equals operation...)

Though your code looks a bit different from kirupa's. I see a < that became a <=, for example.
With this fix you could make the backface function work correct with this replacing function, looks perfect to me in the drawing ^^

Code: Select all

function backface(x1, y1, z1, x2, y2, z2, x3, y3, z3)
  return not ((y3-y1)/(x3-x1) - (y2-y1)/(x2-x1) <= 0) ~= ((x1 <= x3) == (x1 > x2))
end
EDIT: Uploaded love file
Attachments
3D.love
Fixed drawing order
(3.26 KiB) Downloaded 143 times
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: On the subject of three-dimensions...

Post by Jasoco »

Is it just me or does that seem to have worked?

I need to do some more testing.

Thanks for helping, guys! Let me see what I can make this puppy do...
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Jasoco's 3D Model Extravaganza.. NOW WITH MORE TEAPOT!

Post by Jasoco »

New version. Now with more Utah Newell Teapot! See OP for download!

http://en.wikipedia.org/wiki/Utah_teapot
http://www.sjbaker.org/wiki/index.php?t ... The_Teapot

The Utah Teapot is one of the first 3D models ever created/rendered. It was created in 1974 and has since been a staple of 3D for 36 years. The model was modeled after a teapot belonging to Martin Newell and his wife Sandra. It was created supposedly because Martin didn't have enough interesting models, so he went all out on something with a lot of data. It may not be the first, but it is the most popular. Featured all over from that 3D pipes screensaver in Windows to the library in 3D Studio Max to The Simpsons episode where Homer stumbles into the third dimension. And now it is in Jasoco's Amazing 3D Löve Renderer! Or whatever I call it.

It is buggy only in that since there are openings in the rim and the spout you will see the triangles disappear when viewing from the top. This could be fixed now by creating a rim and filling in the spout, but that's a hell of a lot of work. But it could be done. If I wanted to waste a whole day.

It's all one color right now, I could add more color later.

The part where the base of the spout touches the pot is supposed to be inside the pot, but since I have no way of finding out when two triangles intersect and merging them, you see them flicker back and forth.

There is an extra number in the .j3d file in the POLY section. Not sure what it was for in the model I got, but they're all 0's.


I got the data for this from a JavaScript 3D modeller. It was originally JSON but I converted it. I had to remove three sections. A section called NRM (Not sure what it was but it had more points for some reason), one for Texture location (Since I can't texture it it was unneeded for now) and one to set up the Materials (Which was also not needed right now).
http://www.nihilogic.dk/labs/canvas3dtexture_0.2/

The original data was different in that it used squares and each section was a set of 16 points or something. There was no way to convert it. Sadly. So I used the one on that JavaScript site. It worked out fine. For some reason it starts out upside down so I flip it before hand, and the points are close together so I just set the zoom higher for now.

It's a start. The data is loaded from a .j3d file I created. (The extension I came up with in the 90's for 3D models I loaded in my old 3D wireframe viewers.) So you could modify it, or create your own and have a sort of loading function. I don't know where it will go from here.


ALSO I removed the lines. Since they were there from the start before I realized in order to actually make 3D objects I'd need triangles. I'll probably remove the ability to have them all together eventually. Maybe or maybe not. Who knows. For now just remember nothing will show up in that mode.


NEXT I want to figure out how to shade the triangles based on their angles. Can someone help with that? Right now I have to keep the lines on so you can see the shape. But if I could get the shading working I could remove the lines. (Or make them togglable)


It's no Quake engine, and probably never will be, that's up to the Löve people to put in OpenGL3D stuff. For now, it's just for show, and for fun, and maybe for simple effects in your games.

Suddenly that Star Fox clone seems much more possible.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Jasoco's 3D Model Extravaganza.. NOW WITH MORE TEAPOT!

Post by kikito »

Hi again!

Sweet teapot is sweet! Congrats again!
NEXT I want to figure out how to shade the triangles based on their angles. Can someone help with that? Right now I have to keep the lines on so you can see the shape
I suggest that the light source isn't "fixed" on the camera, but moveable. It will definitely come up handy later.

But sorry, my math-fu is horrendous. I can't help you with the calculations. I hope some of the other enlightened guys can help you out with this one.
When I write def I mean function.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Jasoco's 3D Model Extravaganza.. NOW WITH MORE TEAPOT!

Post by Jasoco »

Yeah, I know. Even that 3D JavaScript method has a light source. I could probably figure out something by looking at that code closer.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Jasoco's 3D Model Extravaganza.. NOW WITH MORE TEAPOT!

Post by Jasoco »

New download below -->

Well, I've kinda sorta got lighting and shading in there. Right now it's one light source and for some reason it doesn't shade gradually, instead it shades at large jumps from one shade to the next. But I'm working on that.

This JavaScript's source has such stupid confusing variable names it's hard to make it all out easily.

The big problem was Normals.

The JavaScript versions models all come with Normals already calculated, but I didn't want to depend on them because it would mean I couldn't just make my own models since I'd once again have to calculate normals. So I tried to calculate them using a mathematical formula from the internet. But it didn't output the same numbers that the original code had. And they didn't work right. So does anyone know how exactly to calculate the normals myself correctly? Because the ones the model comes with work, but the ones I calculate myself don't.

Where p.p1, p2, p3 is the polygon's reference points:

Code: Select all

      nx = (points[p.p1].y * points[p.p2].z) - (points[p.p1].z * points[p.p2].y)
      ny = -((points[p.p2].z * points[p.p1].x) - (points[p.p2].x * points[p.p1].z))
      nz = (points[p.p1].x * points[p.p2].y) - (points[p.p1].y * points[p.p2].x)
      com = (nx * nx) + (ny * ny) + (nz * nz)
      nf = math.sqrt(com)
      nx = nx / nf
      ny = ny / nf
      nz = nz / nf

      poly[i].nx = nx
      poly[i].ny = ny
      poly[i].nz = nz
The de needs to be there for sorting.

For instance, where the Normals that came with the model might be:
0.9133, 0.3653, -0.1803

My calculation comes out to:
-0.60307033799416, 0.78875008509614, 0.11907758266127

What am I doing wrong? All the tutorials I found did the same method.

This is what I get using the already supplied Normals:
Image

This is what I get when I calculate Normals myself:
Image

I mean I hate to say it, but unless I figure out how to calculate Normals right, this won't be very useful at all. Flat unshaded polygons are not fun at all.
Attachments
3D.love
3d Teapot needs more Normal löve
(9.24 KiB) Downloaded 145 times
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Jasoco's 3D Model Extravaganza.. NOTHING IS "NORMAL" ANYMORE

Post by schme16 »

Have you fixed the version available for download? mine looks like the pre-defined normals version:
Image

it's smooth all round, nothing at all like the second image you've got
My Development Diary - http://shanegadsby.info
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Jasoco's 3D Model Extravaganza.. NOTHING IS "NORMAL" ANYMORE

Post by Jasoco »

Open it up and change the "calcNorm" to true.

By default it uses the ones included in the model.

This is where I need help now. Calculating normals so they come out the same as the ones already included because if I can't calculate the normals right when I go to make my own models, what's the point?
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Jasoco's 3D Model Extravaganza.. NOTHING IS "NORMAL" ANYMORE

Post by schme16 »

Ahh, whoops; Such an idiot... ignore what I said then. heh...
My Development Diary - http://shanegadsby.info
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Jasoco's 3D Model Extravaganza.. NOTHING IS "NORMAL" ANYMORE

Post by Jasoco »

I should have mentioned it anyway, but anyway. Stupid Normals. Once I can conquer them, I will have what I need to start working on a real use for it!
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests