Vertex Buffer -> Mesh
Sr. GuapoOkay, 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_makerHi 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 [code] 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 [/code] then - saving it ////// 2 : loading the mesh file I saprate the whole mesh into verticle slices [code] 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 [/code] ///////// 3 : Rendering [code] For I2 = 0 To .LandSize D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, .LandSize * 2, .V(0, I2), Len(.V(0, I2)) Next I2 [/code] Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/game_maker/200461316937_t13.jpg"][img]icon_paperclip.gif[/img]t13.jpg[/url]
7.39 KB
Sr. GuapoOkay, 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...
game_makerYou 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) [8)]
Sr. GuapoI 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...
masterboodaNo 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...
Sr. GuapoThat is what I am trying to do. I send the x and z coordinates to a function, GetHeight(): [code] 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 [/code] 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...
game_makeryou 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 [img]http://www.vbgamer.com/msgboard/uploaded/game_maker/200461417571_1.jpg[/img] 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 [code] 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 [/code] 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 [:)]
Sr. GuapoOK, 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_makerit's smooth really ,,, the problem from choosing your points I guess [8)] OK ,, I just wrote a small example for you Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/game_maker/200461420269_terrain.zip"][img]icon_paperclip.gif[/img]terrain.zip[/url]
109.95 KB regards [:)]
game_makerand here how to create terrain files Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/game_maker/2004614204258_terrain-files.zip"][img]icon_paperclip.gif[/img]terrain-files.zip[/url]
31 KB best regards [:)]
Sr. GuapoThank you very much [:D]. 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. GuapoSorry... 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: [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) [/code] 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... [:)]
game_makerHi 423107387 this is just YazeedCredit (My ID number) ,, I saw Quake did it in md2 and I made the same [code] if YCredit <> 423107387 then exit sub [/code] that's the idea behind Credits (protect your files) but I didn't wrote this condition in all of my files (i.e. I make No sence) [|)] about the error : in VB.Net change all Long Variable Types to Integer [:)]
Sr. GuapoOK, 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...
Eric ColemanGenerally 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.
VBBRIn VB6: - Integer: 2 bytes - Long: 4 bytes In VB.NET: - Short: 2 bytes - Integer: 4 bytes So that's just a name exchange basically. [:D] It screws up file reading because you could be reading 4 bytes in a row instead of 2 or something like that, so the numbers end up mixed up or half their original bytes.
game_makerand Long in VB.Net = 8 bytes that's why if you compute your file from vb.6 and read it from VB.Net here is the problem but if you compute from VB.Net and read it from VB.Net you won't face any problem with Variable Types[:)]
Sr. GuapoOkay, I always learned that 32 bit machines (like all of the currect PC's) can process a 32 bit number (long/single in VB6) faster than another type (integer, byte, double in VB6). Does this mean I should use integers in VB .NET since they are 32 bit?
Sr. GuapoOK, new problem. I have been attempting to make this work in VB .NET and DX9. I am trying to load the data into a 2D array of vertices. Should I just use the PositionTextured vertices? Also, should I use a vertex buffer, or just load the straight vertex data when rendering? Sorry, I am really confused... DX9 (and VB .NET for that matter) is different from what I am used to, so it is giving me headaches. PS - these are very DX9 specific questions, if anyone is familiar with it...