normal data from .x

Started by
6 comments, last by VI2 20 years, 12 months ago
How can i obtain the normal data thats stored in the .x file? I read in the mesh and it renders properly so it must already have normal data.... given a selected face or position on a face, how can i determine the normal point???
Advertisement
This is in VB, but it should work if you convert it. Probably a bit messy - haven''t had time to go through it all yet - but it works fine for me.


Function GetFaceNormal(Mesh As D3DXMesh, Face As Long) As D3DVECTOR
Dim Count As Long
Dim Size As Long
Dim I As Long
Dim shrtibPoints As shortFace
Dim ibPoints As lngFace
Dim verts() As D3DVERTEX
Dim ib As Direct3DIndexBuffer8
Dim VB As Direct3DVertexBuffer8
Set VB = Mesh.GetVertexBuffer()
Size = D3DX.GetFVFVertexSize(Mesh.GetFVF())
Count = Mesh.GetNumVertices()
''Mesh.GetIndexBuffer
Dim typ
Dim iBufferDesc As D3DINDEXBUFFER_DESC

Set ib = Mesh.GetIndexBuffer()
ib.GetDesc iBufferDesc
Dim isize As Long
If iBufferDesc.Format = D3DFMT_INDEX16 Then
isize = 2
Call D3DIndexBuffer8GetData(ib, isize * Face, Len(shrtibPoints), D3DLOCK_READONLY, shrtibPoints)
ibPoints.V1 = shrtibPoints.V1
ibPoints.V2 = shrtibPoints.V2
ibPoints.V3 = shrtibPoints.V3
Else
isize = 4
Call D3DIndexBuffer8GetData(ib, isize * Face, Len(ibPoints), D3DLOCK_READONLY, ibPoints)
End If
Dim iCount As Long
''Call ib.Lock(0, Len(ibPoints(0)), ibPoints(0), D3DLOCK_READONLY)
Dim Vects(3) As D3DVECTOR
If Mesh.GetFVF() = D3DFVF_VERTEX Then
ReDim verts(Count)
D3DVertexBuffer8GetData VB, 0, Size * Count, 0, verts(0)
Vects(0).X = verts(ibPoints.V1).X
Vects(0).Y = verts(ibPoints.V1).Y
Vects(0).z = verts(ibPoints.V1).z
Vects(1).X = verts(ibPoints.V2).X
Vects(1).Y = verts(ibPoints.V2).Y
Vects(1).z = verts(ibPoints.V2).z
Vects(2).X = verts(ibPoints.V3).X
Vects(2).Y = verts(ibPoints.V3).Y
Vects(2).z = verts(ibPoints.V3).z
GetFaceNormal = GenerateTriNormals(Vects(0), Vects(1), Vects(2))
Else
Stop
End If


End Function


Public Function GenerateTriNormals(p0 As D3DVECTOR, p1 As D3DVECTOR, p2 As D3DVECTOR) As D3DVECTOR

''//0. Variables required
Dim v01 As D3DVECTOR ''Vector from points 0 to 1
Dim v02 As D3DVECTOR ''Vector from points 0 to 2
Dim VNorm As D3DVECTOR ''The final vector

''//1. Create the vectors from points 0 to 1 and 0 to 2
D3DXVec3Subtract v01, MakeVector(p1.X, p1.Y, p1.z), MakeVector(p0.X, p0.Y, p0.z)
D3DXVec3Subtract v02, MakeVector(p2.X, p2.Y, p2.z), MakeVector(p0.X, p0.Y, p0.z)

''//2. Get the cross product
D3DXVec3Cross VNorm, v01, v02

''//3. Normalize this vector
D3DXVec3Normalize VNorm, VNorm

''//4. Return the value
GenerateTriNormals.X = VNorm.X
GenerateTriNormals.Y = VNorm.Y
GenerateTriNormals.z = VNorm.z
End Function






Learning to fly is easy, but as a tortoise, the landings are really rough.
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...
By the way, the input face is the same as the one returned by the rayintersect function.

Learning to fly is easy, but as a tortoise, the landings are really rough.
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...
Hey thanks... just a quick q cuz i''ve never done VB...

whats dim and redim...
dim is the declaration of the variable...redim is a redeclaration
There must be a better way... I just want to draw a normal of each face on the mesh... like MeshViewer does...

I would think there would be some more efficient functions...

something along the line of GetVertex(mesh, vertex#).normal.x = ...

Anything I might be overlooking? Cuz I just want to draw lines from the faces using the normal data....

MView has to do a bit of work in order to present its views of normals, strips, adjacency, etc.
The non-skinned case is fairly straight forward. Build a new line-list of n lines, where one endpoint of each line is the position of a vertex and the other is the normal added to the vertex. Now you can just draw the line-list whenever you want.

Drawing skinned normals are a bit more difficult. You need to skin the position and the normal before doing the addition. The non-skinned version can work on fixed function, whereas the skinned one requires vertex shaders.

Craig Peeper
Microsoft, Direct3D

This posting is provided "AS IS" with no warranties, and confers no rights.
Craig PeeperMicrosoft, Direct3DThis posting is provided "AS IS" with no warranties, and confers no rights.

MView has to do a bit of work in order to present its views of normals, strips, adjacency, etc.
The non-skinned case is fairly straight forward. Build a new line-list of n lines, where one endpoint of each line is the position of a vertex and the other is the normal added to the vertex. Now you can just draw the line-list whenever you want.

Drawing skinned normals are a bit more difficult. You need to skin the position and the normal before doing the addition. The non-skinned version can work on fixed function, whereas the skinned one requires vertex shaders.

Craig Peeper
Microsoft, Direct3D

This posting is provided "AS IS" with no warranties, and confers no rights.
Craig PeeperMicrosoft, Direct3DThis posting is provided "AS IS" with no warranties, and confers no rights.

This topic is closed to new replies.

Advertisement