Sr. Guapo
Swordmaster
USA
272 Posts |
Posted - Jun 13 2004 : 1:21:34 PM
|
Okay, I am making a simple terrain heightmap generator for my game. What I need to do is create a mesh file givin the heightmap.
I can create the vertex buffer in a nested loop, but is there a way to convert a vertex buffer to a mesh? I have tried everything, and I looked for two hours on MSDN, but I didn't see anything like what I want to do.
My other option is to create the terrain in Milkshape (it can make a heightmapped terrain) and export it to an X-file. However, I would still need to create a data file containing the height at all the various points.
I would appreciate any suggestions/comments... thanks |
|
game_maker
Knave
Saudi Arabia
83 Posts |
Posted - Jun 13 2004 : 4:04:49 PM
|
Hi
Why do work with vertex buffer ? is it must or optional ?
//////// 1 : creating a mesh file
I made an external program to convert form height map to file named .scp ,,, it contains the following informations :
landsize ( controls the size of the land )
and after having landsize => computing the the height file
NumVertex = (2 * (LandSize ^ 2)) + (2 * LandSize) - 1 NumTraingle = NumVertex - 2 ReDim H(LandSize, LandSize) For I = 0 To LandSize For I2 = 0 To LandSize H(I2, I) = Form1.Picture2.Point(I2 * Screen.TwipsPerPixelX, I * Screen.TwipsPerPixelY) \ 65536 Next I2 Next I
then - saving it
////// 2 : loading the mesh file
I saprate the whole mesh into verticle slices
Public Function Scape_Create(xPath As String, Optional xHeight As Single = 10, Optional ScaleX As Single = 1, Optional ScaleZ As Single = 1) As Long Dim X As Long Dim Y As Long Dim xH As Single Dim DestY As Long Dim VN As D3DVECTOR With Scape_Prop(Scape_Count) Open xPath For Binary As #95 Get #95, , .YCredit Get #95, , .LandSize Get #95, , .NumVertex Get #95, , .NumTraingle ReDim .H(.LandSize, .LandSize) Get #95, , .H Close #95 ReDim .V(.NumVertex / .LandSize, .NumVertex / .LandSize) .ScaleX = ScaleX .ScaleZ = ScaleZ End With For Y = 0 To Scape_Prop(Scape_Count).LandSize - 1 For X = 0 To (Scape_Prop(Scape_Count).LandSize * 2) + 1 With Scape_Prop(Scape_Count).V(X, Y) DestY = IIf(X Mod 2 = 0, 0, 1) xH = xHeight * Scape_Prop(Scape_Count).H(X \ 2, Y + DestY) .X = (X \ 2) * Scape_Prop(Scape_Count).ScaleZ .Y = xH / 256 .Z = (Y + DestY) * Scape_Prop(Scape_Count).ScaleZ .Tu1 = .X / (Scape_Prop(Scape_Count).LandSize * Scape_Prop(Scape_Count).ScaleX) .Tv1 = .Z / (Scape_Prop(Scape_Count).LandSize * Scape_Prop(Scape_Count).ScaleZ) .Tu2 = .Tu1 .Tv2 = .Tv1 End With Next X Next Y For Y = 0 To Scape_Prop(Scape_Count).LandSize - 1 For X = 0 To ((Scape_Prop(Scape_Count).LandSize * 2) + 1) \ 3 - 3 With Scape_Prop(Scape_Count) If X Mod 2 = 0 Then VN = GenerateTriangleNormals(.V(X * 3, Y), .V(X * 3 + 1, Y), .V(X * 3 + 2, Y)) Else VN = GenerateTriangleNormals(.V(X * 3 + 2, Y), .V(X * 3 + 1, Y), .V(X * 3, Y)) End If .V(X * 3, Y).Nx = VN.X: .V(X * 3, Y).Ny = VN.Y: .V(X * 3, Y).Nz = VN.Z .V(X * 3 + 1, Y).Nx = VN.X: .V(X * 3 + 1, Y).Ny = VN.Y: .V(X * 3 + 1, Y).Nz = VN.Z .V(X * 3 + 2, Y).Nx = VN.X: .V(X * 3 + 2, Y).Ny = VN.Y: .V(X * 3 + 2, Y).Nz = VN.Z End With Next X Next Y End Function
///////// 3 : Rendering
For I2 = 0 To .LandSize D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, .LandSize * 2, .V(0, I2), Len(.V(0, I2)) Next I2
Download Attachment: t13.jpg 7.39 KB
|
Edited by - game_maker on Jun 13 2004 4:10:06 PM |
|
|
Sr. Guapo
Swordmaster
USA
272 Posts |
Posted - Jun 13 2004 : 11:37:17 PM
|
Okay, I will try something like that... Right now, I have it so that I create the heightmap myself externally (PSP), then I create an x-file with the terrain in Milkshape using the heightmap. I then load both the mesh and heightmap into the game, and I use both to determine the height at a given location. It works (sorta...) but it is very confusing, so I wanted to make it a little more readable... |
Edited by - Sr. Guapo on Jun 13 2004 11:38:26 PM |
|
|
game_maker
Knave
Saudi Arabia
83 Posts |
Posted - Jun 14 2004 : 12:19:29 AM
|
You need to access to (4) points in your terrain to determine the Y component you standing on,,, so x-files is not a good choise (I think)
you can use 3DSMax to built your tarrain and then save it as 3ds then reading it but even this is a bad choise
you need to write your own mesh-file and reading it
I will try to write an example for this ... and there are some examples in planet-source-code (not that good) |
Edited by - game_maker on Jun 14 2004 12:21:47 AM |
|
|
Sr. Guapo
Swordmaster
USA
272 Posts |
Posted - Jun 14 2004 : 11:58:26 AM
|
I would love to use 3DSMax, except it costs more than $500... I want to write my own heightmap parser, so all I do is create the heightmap, then the game can make the map, however, this is aparently more difficult than I thought... |
|
|
masterbooda
Swordmaster
277 Posts |
Posted - Jun 14 2004 : 2:35:48 PM
|
No kidding, that 3dstudio max is expensive, but it is the only 3d editor I will use... nothing has comapared to it, not even maya.... think god my brother inlaw owns it........whoops......um.... nothing...
oh and about the mesh thing, a lot of this post is over my head, but aren't all files just little ones and zeros... maybe you can find a way to interpret the file yourself and create it from that... And of course you can find file conversions for 3d files of nearly every type on line and free..
I noticed you where using an heightmap file... I know this isn't very accurate, but couldn't you take sort of an average of how far from the x and y the position the man is take the 4 colors and find an average mean... I am probably babbling and making no sense... sorry...
(1)----(2) | | | o | (3)----(4)
Crude I know, but the o is where the dude or character is at, now take the color from the heightmap's four colors (1pixel apart)... and average a mean to the character and that is your height... but I am probably making an easy situation worse...
DaBooda out... |
DaBooda Team is back: http://dabooda.789mb.com/ |
|
|
Sr. Guapo
Swordmaster
USA
272 Posts |
Posted - Jun 14 2004 : 2:59:03 PM
|
That is what I am trying to do. I send the x and z coordinates to a function, GetHeight():
Public Function getHeight(ByVal x As Single, ByVal z As Single) As Single If x < 0 Or x > size - 1 Or z < 0 Or z > size - 1 Then Return (0.0) Else tmpDecX = x - (x \ 1) tmpDecZ = z - (z \ 1) tmpH1 = height(x \ 1, z \ 1) tmpH2 = height(x \ 1 + 1, z \ 1 + 1) tmpDiff = tmpH2 - tmpH1 tmpDist = Math.Sqrt(tmpDecX * tmpDecX + tmpDecZ * tmpDecZ) Return (tmpDiff * tmpDist + tmpH1) End If End Function
Sorry if the code is a little unreadable, I wrote this very late at night... Anyway, I could have made this a one line return statement, but I think it is faster to break things up like this (correct me if I'm wrong). It is still very jerky, like I will be walking along at a certain height, then fly up/down all of a sudden. I thought this function would fix it, but aparently not...
|
Edited by - Sr. Guapo on Jun 14 2004 4:54:13 PM |
|
|
game_maker
Knave
Saudi Arabia
83 Posts |
Posted - Jun 14 2004 : 5:58:04 PM
|
you are working with 2 points right ?
1 - (x,,z) 2 - (x+1,,z+1)
that's the only problem ,,, you should work with 4 points becouse each one of the for point's effect in our Y aren't they ?
1 - (x,y1,z) 2 - (x+1,y2,z) 3 - (x,y3,z+1) 4- (x+1,y4,z+1)
and I use equation of Line (y = mx * b) same as you did
I just design it in 3DSmax
Ok ,,, now we have four points and a point (xd,,zd) and we wan't to find yd
simply we create 2 lines (you can find the line if you have to points or one point and slope)
we have 4 point so we can create (3*2*1) = 6 lines
we only need 2 lines ,,,,,,, so
we pick point 1 and 2 to make a line1 ,,, and pick 3 and 4 to make line2
then we find y1 and y2
y1 ,y2 : the y value of line1 and line2 when x = xd
then we find y3
thats it
notice :
to find y1 : m = ((P2.Y - P1.Y) / (P2.X - P1.X)) xd = (X - P2.X) b = P2.Y y1 = m * xd + b
Public Function getHeight(ByVal x As Single, ByVal z As Single) As Single If x < 0 Or x > size - 1 Or z < 0 Or z > size - 1 Then Return (0.0) Else Y1 = ((P2.Y - P1.Y) / (P2.X - P1.X)) * (X - P2.X) + P2.Y Y2 = ((P4.Y - P3.Y) / (P4.X - P3.X)) * (X - P4.X) + P4.Y Return (((Y2 - Y1) / (P4.Z - P1.Z)) * (Z - P4.Z) + Y2) End If End Function
there is other math method :
you can use equation of plane (you have tocheck what is the plane your character standing on then substitute to find y3 |
Edited by - game_maker on Jun 14 2004 6:02:23 PM |
|
|
Sr. Guapo
Swordmaster
USA
272 Posts |
Posted - Jun 14 2004 : 6:43:08 PM
|
OK, I implemented that function... It just did the same thing as the previous version (though it is more accurate). It is still very jerky like described above...
I want to be able to make my own Vertex Buffer to store the vertices, that is what I need help with. |
|
|
game_maker
Knave
Saudi Arabia
83 Posts |
Posted - Jun 14 2004 : 8:26:53 PM
|
it's smooth really ,,, the problem from choosing your points I guess
OK ,, I just wrote a small example for you
Download Attachment: terrain.zip 109.95 KB
regards
|
|
|
game_maker
Knave
Saudi Arabia
83 Posts |
Posted - Jun 14 2004 : 8:39:09 PM
|
and here how to create terrain files
Download Attachment: terrain-files.zip 31 KB
best regards |
Edited by - game_maker on Jun 14 2004 8:43:12 PM |
|
|
Sr. Guapo
Swordmaster
USA
272 Posts |
Posted - Jun 14 2004 : 11:28:32 PM
|
Thank you very much . I will try and incorporate some of that into my game.
I just have one question. What is the YCredit variable for in the file? It seems you save a huge arbitrary number stored in it (423107387), then do nothing with the number when it is loaded... |
|
|
Sr. Guapo
Swordmaster
USA
272 Posts |
Posted - Jun 15 2004 : 12:22:50 AM
|
Sorry... I get an error when I am trying to load a binary in .NET... It occurs when I try to redim the H() variable in this set of code:
FileOpen(95, path, OpenMode.Binary) FileGet(95, yCredit) FileGet(95, LandSize) FileGet(95, nVertex) FileGet(95, nTriangle) ReDim H(LandSize, LandSize) FileGet(95, H) FileClose(95)
aparently, LandSize comes out as some HUGE number (86,745,454,497,511: 86 trillion). I assume .NET must do something different than VB6, because the code is almost identical. I am very unknowledgable about file I/O in VB, so any help is appreciated... |
Edited by - Sr. Guapo on Jun 15 2004 12:31:37 AM |
|
|
game_maker
Knave
Saudi Arabia
83 Posts |
|
Sr. Guapo
Swordmaster
USA
272 Posts |
Posted - Jun 15 2004 : 10:52:48 AM
|
OK, I figured YCredit was something like that... . I just wanted to make sure it wasn't an important variable. That is a good idea, I should probably do that if I ever plan to make a real game.
The error: Yes, the file loads now. Out of curiousity, why is that? A long should be that, a long integer, so anything an integer can hold, a long can... |
Edited by - Sr. Guapo on Jun 15 2004 10:53:14 AM |
|
|
Eric Coleman
Gladiator
USA
811 Posts |
Posted - Jun 15 2004 : 12:41:30 PM
|
Generally that "LONG" number translates to 4 letters (4 bytes). For example, I use the number 1128811092 ("TJHC") as the first four bytes in an archive file that I created so that I can recognize the file as being something that I created. If you look at wotsit.org, you'll find a lot of file formats do this. Gif, bmp, zip, etc, all have an ID number at the very beginning. |
|
|
|
|