• Advertisement
Sign in to follow this  

how to create a 3d box manually?

This topic is 3895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just started to learn direct3d in vb .net. What I want to do is: create a 3d box, and paste different texture on different faces. Since the size of the faces are different, I think maybe need manually create it in program. Below it is the code I have. For some reasons, it creates a weird look image. I draw it on a panel, whenever I reset the device, it pops an error. However, if I use mesh.box to create the mesh, then it is ok, although I can't use different texture on different faces. Anyone know what the problem is? Or have any code examples? thanks and appreciate the help. reader. ============== Function createMesh() As Mesh Dim me1 As Mesh = New Mesh(12, 8, 0, VertexFormats.Position, device) '// Set up the 8 corners of the cube Dim front As Single = -1 Dim back As Single = 1 Dim left As Single = -1 Dim right As Single = 1 Dim top As Single = 1 Dim bottom As Single = -1 Dim vertices(0 To 7) As CustomVertex.PositionOnly vertices(0) = New CustomVertex.PositionOnly(left, bottom, front) ', // 0 vertices(1) = New CustomVertex.PositionOnly(right, bottom, front) ', // 1 vertices(2) = New CustomVertex.PositionOnly(left, top, front) ', // 2 vertices(3) = New CustomVertex.PositionOnly(right, top, front) ', // 3 vertices(4) = New CustomVertex.PositionOnly(left, bottom, back) ', // 4 vertices(5) = New CustomVertex.PositionOnly(right, bottom, back) ', // 5 vertices(6) = New CustomVertex.PositionOnly(left, top, back) ', // 6 vertices(7) = New CustomVertex.PositionOnly(right, top, back) ' // 7 me1.SetVertexBufferData(vertices, LockFlags.None) Dim leftbottomfront As Integer = 0 Dim rightbottomfront As Integer = 1 Dim lefttopfront As Integer = 2 Dim righttopfront As Integer = 3 Dim leftbottomback As Integer = 4 Dim rightbottomback As Integer = 5 Dim lefttopback As Integer = 6 Dim righttopback As Integer = 7 Dim indices(0 To 11, 0 To 2) As Integer ' left faces indices(0, 0) = lefttopfront indices(0, 1) = lefttopback indices(0, 2) = leftbottomback ', // 0 indices(1, 0) = leftbottomback indices(1, 1) = leftbottomfront indices(1, 2) = lefttopfront ', // 1 ' // Front faces indices(2, 0) = lefttopfront indices(2, 1) = leftbottomfront indices(2, 2) = rightbottomfront ', // 2 indices(3, 0) = rightbottomfront indices(3, 1) = righttopfront indices(3, 2) = lefttopfront ', // 3 ' // Right faces indices(4, 0) = righttopback indices(4, 1) = righttopfront indices(4, 2) = rightbottomfront ', // 4 indices(5, 0) = rightbottomfront indices(5, 1) = rightbottomback indices(5, 2) = righttopback ', // 5 ' // Back faces indices(6, 0) = leftbottomback indices(6, 1) = lefttopback indices(6, 2) = righttopback ', // 6 indices(7, 0) = righttopback indices(7, 1) = rightbottomback indices(7, 2) = leftbottomback ', // 7 ' // Top faces indices(8, 0) = righttopfront indices(8, 1) = righttopback indices(8, 2) = lefttopback ', // 8 indices(9, 0) = lefttopback indices(9, 1) = lefttopfront indices(9, 2) = righttopfront ', // 9 ' // Bottom faces indices(10, 0) = leftbottomfront indices(10, 1) = leftbottomback indices(10, 2) = rightbottomback ', // 10 indices(11, 0) = rightbottomback indices(11, 1) = rightbottomfront indices(11, 2) = leftbottomfront ' // 11 me1.SetIndexBufferData(indices, LockFlags.None) Dim subset As Integer() subset = me1.LockAttributeBufferArray(LockFlags.Discard) '), Integer) For i As Integer = 0 To 5 subset(2 * i) = i subset(2 * i + 1) = i Next me1.UnlockAttributeBuffer(subset) 'Dim me1 As Mesh ' ''me1 = Mesh.Box(device, 10.0F, 10.0F, 10.0F) Dim TexturedBox As Mesh = me1.Clone(me1.Options.Value, _ VertexFormats.Position Or VertexFormats.Normal Or _ VertexFormats.Texture0 Or VertexFormats.Texture1, _ me1.Device) 'Private Function SetTextureCoordinates(ByVal TexturedBox As Mesh) As Mesh 'The following code makes the assumption that the vertices of the box ' are generated the same way as they are in the April 2005 SDK Using vb As VertexBuffer = TexturedBox.VertexBuffer Dim verts As CustomVertex.PositionNormalTextured() = _ DirectCast(vb.Lock(0, GetType(CustomVertex.PositionNormalTextured), _ LockFlags.None, TexturedBox.NumberVertices), _ CustomVertex.PositionNormalTextured()) Try Dim i As Integer = 0 Do While (i < verts.Length) verts(i).Tu = 0.0! verts(i).Tv = 0.0! verts((i + 1)).Tu = 1.0! 'Me.NumXTiles verts((i + 1)).Tv = 0.0! verts((i + 2)).Tu = 1.0! 'Me.NumXTiles verts((i + 2)).Tv = 1.0! 'Me.NumYTiles verts((i + 3)).Tu = 0.0! verts((i + 3)).Tv = 1.0! 'Me.NumYTiles i = (i + 4) Loop Finally vb.Unlock() End Try End Using Dim adjacency(0 To TexturedBox.NumberFaces * 3 - 1) As Integer ' = new int[mesh.NumberFaces * 3] TexturedBox.GenerateAdjacency(0.000001F, adjacency) TexturedBox.OptimizeInPlace(MeshFlags.OptimizeVertexCache, adjacency) Return TexturedBox End Function =========================================

Share this post


Link to post
Share on other sites
Advertisement
I haven't read your code, so can't comment about it, but here are some tips:

You can scale a cube to any size box, with different size faces, so you don't need to create a different box for every size.

Enable debug more for D3D in the DirectX control panel (which used to be a real control panel, and in recent SDK's is under the DX utilities). Look what it prints when you get the error, and it should give you an idea what the problem is.

Posting a picture or a link to it is helpful when you have "a weird look image". It can help people understand better what the problem may be.

Share this post


Link to post
Share on other sites
also, in your posts and replies, I encourage you to use the Source tag for any source code. This will make your post alot more easy to read.

if you don't know how to use tags it's -> [ Source ] [ /Source ] without spaces


//Here is some code

private int SeeColors = 5;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement