|
Post by davy on May 30, 2006 22:46:54 GMT -5
Ok, everything is explained in the README and the test.gb file is an example program, it is well-commented. You simply include these programs with your program, use the instructions givin and you will never have to write out your own model loading routines again! Here: p13.wikispaces.com/space/showimage/EasyModel.zipI am considering adding support for various other model files, as well as including a per-vertex normal calulator in the loader, so you can use lighting on the models. I will also add some multi-model loading capabilities... Maybe animation, but I will have a hard time keeping that function user friendly... Anyway, ENJOY!
|
|
stu
Posts a bit
Posts: 150
|
Post by stu on May 30, 2006 23:08:48 GMT -5
Awesome. Model loading made easier. Although your original OBJ loading code was very simple, brief, and easy to follow, this is REALLY easy. Unfortunately, I've not been using OBJ lately.
|
|
|
Post by davy on May 31, 2006 18:51:35 GMT -5
Thanks. You can always convert your models to .obj... I am going to try to add support to a whole library of formats if I get enough resources. I have also though alot about the automatic normal calc, that WILL be in the next release, so you can have per-vertex lighting on your models with just a few commands. I am even considering a self loading model format... Really just a program that fills all of the arrays with the right info, but also a program that generates these "self loading models" by saving all of the code and data required. I could even save some of the textures to some data tables and have them self install when the model loads... That would be cool.
But, for easymodel... Automatic vertex lighting = For sure Multi Model support = For sure More model formats also = for sure
|
|
stu
Posts a bit
Posts: 150
|
Post by stu on May 31, 2006 20:33:48 GMT -5
Great! The one thing I still don't fully understand about model loading on my own is the lighting. Based on the example code Tom posted a little while ago, I did some simple lighting, but it's not that good. So, I would definitely use a lighting program.
|
|
|
Post by davy on May 31, 2006 20:39:21 GMT -5
Well.. its probably the normals that you have a problem with. There are two ways really... Per-face, where each face is uniformly lit, and per-vertex, where each vertex has its own lighting info. The biggest problem people have with this is calculating the normals... I can show you the routines for calculating them both (as you need the face normals to find the vertex normals) if you'd like, I just have to punch out a quick demo. Per-vertex is the best... Very nice looking, makes everything looks smoother and cleaner, thats what the TMF model format that I made up for atropine uses. You just have to do a paragraph's worth of code when you load the model to calculate the normals and store them in an array for when you draw it.
|
|
stu
Posts a bit
Posts: 150
|
Post by stu on May 31, 2006 20:44:50 GMT -5
Yes, it's the normals. But if you're going to add lighting into your model loading code for download, there shouldn't be any need for an additional example, I think. I can wait and just dissect the code from that.
|
|
|
Post by davy on May 31, 2006 20:56:48 GMT -5
To late... lol, if you want to wait, you can... But I just typed this out really quick. I'm sure someone has been wondering about this.
Now, I make alot of assumptions in this post... so bare with me...
Ok, this only works with triangulated faces. I am assuming you have all of your vertices stored in an array called: vert#()()
The first dimension of the array is for 1-3 which represents the x,y,z of this vertex, the next dimension is for the vertex number.
Then I am assuming you have an array called face#()() Where the first dimension is for the face number 1-3 and the second is the face number.
MaxFaces is the number of faces in the model...
This is how you would calculate the per-face normals
for i=1 to MaxFaces f1=Face#(1)(i) f2=Face#(2)(i) f3=Face#(3)(i) d1#=vec3(Vert#(1)(f2)-Vert#(1)(f1), Vert#(2)(f2)-Vert#(2)(f1), Vert#(3)(f2)-Vert#(3)(f1)) d2#=vec3(Vert#(1)(f3)-Vert#(1)(f2), Vert#(2)(f3)-Vert#(2)(f2), Vert#(3)(f3)-Vert#(3)(f2)) cross1# = d1#(1) * d2#(2) - d1#(2) * d2#(1) cross2# = d1#(2) * d2#(0) - d1#(0) * d2#(2) cross3# = d1#(0) * d2#(1) - d1#(1) * d2#(0) Dist#=sqr(Cross1#*Cross1#+Cross2#*Cross2#+Cross3#*Cross3#) if Dist#<>0 then Norm#(1)(i)=Cross1#/Dist# Norm#(2)(i)=Cross2#/Dist# Norm#(3)(i)=Cross3#/Dist# endif next
That would store the normals in the array call norm#()() The first dimension is 1-3 for x,y,z and the second is for the face number that the normals go to.
So, then with this... Whenever you draw the model, just put a:
glNormal3f(Norm#(1)(i), Norm#(2)(i), Norm#(3)(i))
where i represents the current face in the for/next loop of your draw routine.
Ok, now with per-face norms out of the way... Lets calculate some vertex normals. Basically, this is done by taking the average of each face normal that connects to the pending vertex.
for i=1 to MaxVerts for i2=1 to MaxFaces for i3=1 to 3 if Face#(i3)(i2)=i then VNorm#(1)(i)=VNorm#(1)(i)+Norm#(1)(i2) VNorm#(2)(i)=VNorm#(2)(i)+Norm#(2)(i2) VNorm#(3)(i)=VNorm#(3)(i)+Norm#(3)(i2) endif next next Dist#=sqr(VNorm#(1)(i)*VNorm#(1)(i)+VNorm#(2)(i)*VNorm#(2)(i)+VNorm#(3)(i)*VNorm#(3)(i)) if Dist#<>0 then VNorm#(1)(i)=VNorm#(1)(i)/Dist# VNorm#(2)(i)=VNorm#(2)(i)/Dist# VNorm#(3)(i)=VNorm#(3)(i)/Dist# Endif next
Naturally, MaxVerts is the number of vertices in the model. From that routine, you will get the array VNorm#()() where... the first element is 1-3 for the x,y,z of the normal... And the second is the vertex number of the normal. So, when you draw your model, for every glvertex3f command, add a glnormal3f before it, using the vertex number in the array.
Hopefully this helps some, if not, PM me or reply to this post and I can further explain... This is absolutely essential for good 3d model-using programs with lighting... ANd it makes the models look 10x better.
|
|
stu
Posts a bit
Posts: 150
|
Post by stu on May 31, 2006 22:12:45 GMT -5
Thanks. My models are looking much better now! I'll have to look over the code later to understand it, but for now, it's working fine.
|
|
|
Post by davy on Jun 1, 2006 0:20:10 GMT -5
Ok, I did add normals and a small demo showing off the lighting. So, now all you have to do is add a light and your models will have per-vertex lighting, without any math at all. p13.wikispaces.com/space/showimage/EasyModel2.zipSo, thats one down... I have multimodel support, and support for more model formats still left to do written. Anyone who knows enough about any other model formats should really help out and PM me or reply to this post.
|
|
|
Post by Tom Mulgrew on Jun 1, 2006 5:23:35 GMT -5
Just a suggestion about storing vectors in arrays. Rather than using the first array index for the X,Y,Z and the second for the vector index, I suggest swapping them around. E.g.
dim vectors#(100)(2) To dim 100 vectors, instead of
dim vectors#(3)(100) (Where index 0 = X coord, 1 = Y coord, 2 = Z coord.) The advantage is that vectors#(i) now refers to a single 3D vector. So you can simply write
vectors#(i) instead of
vec3(vectors#(1)(i), vectors#(2)(i), vectors#(3)(i)) You can also take advantage of built-in vector arithmetic, and the vector functions.
For example, your normal finding code would become:
Norm#(i) = Normalize(CrossProduct(Vert#(f2) - Vert#(f1), Vert#(f3) - Vert#(f1)) And you can pass vectors to certain OpenGL functions (typically those that end with the v suffix). So you could write:
glNormal3fv(Norm#(i)) Or multiply them by matrices:
Rotated#(i) = MatrixRotateY(45) * Norm#(i)
It's just a lot easier than multiplying out all the coordinates long-hand.
-Tom
|
|
|
Post by davy on Jun 1, 2006 9:16:11 GMT -5
Thank you Tom... For some reason, that is what I do with most of my particles... But thats not what I did with my models, I guess I was just overlooking a very big area for improvement. I changed the easyload system... It compressed it alot. And with the crossproduct command, and the normalize command... I dont even feel like I'm even programming, Basic4GL just does it all for me. lol.
Unfortunately... Now I get to change Atropine... *Look of horror* but I'm sure its 100 times worse when you have to go back and change something in Basic4GL, so I probably shouldnt complain. But once again, thank you, that is such a logical thing to do... Dont know why I didnt, lol.
|
|
|
Post by James :) (aka Madcow) on Jun 14, 2006 17:00:32 GMT -5
you could for your next easy model loader have bgm support
|
|
|
Post by davy on Jun 14, 2006 18:09:33 GMT -5
I suppose I could learn it, but its not a very common model format... In fact, I think Tom just made it up... But, if you want, I can, sure.
|
|
stu
Posts a bit
Posts: 150
|
Post by stu on Jun 14, 2006 21:08:45 GMT -5
Well, I think md2 might have been a better format to support (if you know it), but whatever is good.
|
|
|
Post by James :) (aka Madcow) on Jun 15, 2006 0:20:45 GMT -5
not really the smallest loader is the md2 lite which is contained with basic 4gl but even thats very complex
|
|