Dx7 Surface Normals - RESOLVED

Started by
4 comments, last by Waxy 19 years, 3 months ago
I know that VB is hated with a passion by the C'ers so plz don't give me an ear full. :D I'm posting in the beginners section because, frankly, i've never been to college for programing or anything of the such so i'm guessing this is the level of my question. I'm working with DX7 on VB6.0 . I am sucessfuly converting Quake 1 maps to a format where i can read them and import texture cordinates, planes, and entities. I can correctly load all aspects of map files, however when i try to enable lighting i am lacking normals. I belive i understand what they are and i know i'll also need down the line for proper collision detection( right now i am just drawing a complete 3D rectangle around a plane for collision - needless it causes weird behavior with slopes.) my trouble is in actualy calculating the normals. 1 - Sience a vector is only a direction, then for a polygon of 5 points won't all normal vectors be the same for all points of that polygon? if you slice the polygon into triangles then essentialy all the triangles - despite being in diffrent locations - will be facing the same direction so thus the same normal vector, yes? if i'm correct in the above, then how come this doesn't give me proper normals? ( point(1) is actualy point(0) ) ---------------------------------------------------------------

                If Points >= 3 Then 'if 3 or more points exsist then find normals for surface
                    ' subtract point3 - point1 to find a vector
                    DataVector2.X = PointX(3) - PointX(1)
                    DataVector2.Y = PointY(3) - PointY(1)
                    DataVector2.Z = PointZ(3) - PointZ(1)
                    'subtract point2 - point1 to find another vector
                    DataVector3.X = PointX(2) - PointX(1)
                    DataVector3.Y = PointY(2) - PointY(1)
                    DataVector3.Z = PointZ(2) - PointZ(1)
                    
                    ' find vector cross product and store in DV1
                                        
                    DataVector1.X = DataVector3.X - DataVector2.X
                    DataVector1.Y = DataVector3.Y - DataVector2.Y
                    DataVector1.Z = DataVector3.Z - DataVector2.Z
                                      
                    ' normalize vector for light
                    Dx.VectorNormalize DataVector1
                    Debug.Print "Normals:  X"; DataVector1.X; "  Y"; DataVector1.Y; "  Z"; DataVector1.Z
                End If
                
                For Counter = 1 To Points
                    ' create edge vertexes of face, re-scale the textures
                     Dx.CreateD3DVertex PointX(Counter), PointY(Counter), PointZ(Counter), DataVector1.X, DataVector1.Y, DataVector1.Z, TexCordX(Counter) / 130, TexCordY(Counter) / 130, Faces(NumPolys).vFace(Counter)
                                    
                Next

---------
Render Code:
------------
            For i = 1 To NumPolys '- 1  'Go through each part...
              Device.SetTexture 0, AllTextures(Faces(i).Texture).tTextures  
              If Faces(i).IsVisible = True Then
                    Device.DrawPrimitive D3DPT_TRIANGLEFAN, D3DFVF_VERTEX, Faces(i).vFace(1), UBound(Faces(i).vFace()), D3DDP_DEFAULT
              End If
            Next i


---------------------------------------------- Lighting is both made and enabled. Any help in advance that you may provide. Ty. [Edited by - Waxy on January 10, 2005 6:42:44 AM]
Advertisement
Quote:Original post by Waxy
' find vector cross product and store in DV1                                        DataVector1.X = DataVector3.X - DataVector2.XDataVector1.Y = DataVector3.Y - DataVector2.YDataVector1.Z = DataVector3.Z - DataVector2.Z



That doesn't look like the right way to calculate the cross product. I would expect:

DataVector1.X = DataVector3.Y * DataVector2.Z - DataVector3.Z * DataVector2.YDataVector1.Y = DataVector3.Z * DataVector2.X - DataVector3.X * DataVector2.ZDataVector1.Z = DataVector3.X * DataVector2.Y - DataVector3.Y * DataVector2.X


I'm not sure on the winding, you may want to invert it.
Harry.
Thank you for trying harry. but it's still not working.

New current code:
-----------------------------

             'enable when doing lights             If Points >= 3 Then 'if 3 or more points exsist then find normals for surface                    ' subtract point2 - point1 to find a vector                    ' no light, trying new method of point3 - point2                    DataVector2.X = PointX(3) - PointX(2)                    DataVector2.Y = PointY(3) - PointY(2)                    DataVector2.Z = PointZ(3) - PointZ(2)                    'subtract point3 - point1 to find another vector                    DataVector3.X = PointX(2) - PointX(1)                    DataVector3.Y = PointY(2) - PointY(1)                    DataVector3.Z = PointZ(2) - PointZ(1)                                                            DataVector1.X = (DataVector2.Y * DataVector3.Z) - (DataVector2.Z * DataVector1.Y)                    DataVector1.Y = (DataVector2.Z * DataVector3.X) - (DataVector2.X * DataVector1.Z)                    DataVector1.Z = (DataVector2.X * DataVector3.Y) - (DataVector2.Y * DataVector1.X)                                        ' find vector cross product and store in DV1                    ' tried reversing the subtraction, also.                    DataVector1.X = DataVector2.X - DataVector3.X                    DataVector1.Y = DataVector2.Y - DataVector3.Y                    DataVector1.Z = DataVector2.Z - DataVector3.Z                                                                                                Dist = Sqr(DataVector1.X ^ 2 + DataVector1.Y ^ 2 + DataVector1.Z ^ 2)                    ' normalize vector for light                    DataVector4.X = DataVector1.X / Dist                    DataVector4.Y = DataVector1.Y / Dist                    DataVector4.Z = DataVector1.Z / Dist                                                          DataVector1 = DataVector4                                        Debug.Print "Normals:  X"; DataVector1.X; "  Y"; DataVector1.Y; "  Z"; DataVector1.Z                End If                                For Counter = 1 To Points                    ' create edge vertexes of face, re-scale the textures later                     Dx.CreateD3DVertex PointX(Counter), PointY(Counter), PointZ(Counter), DataVector1.X, DataVector1.Y, DataVector1.Z, TexCordX(Counter) / 130, TexCordY(Counter) / 130, Faces(NumPolys).vFace(Counter)                Next


-----------------
Useful Link:
(i just can't get it to work.)
http://www.tjhsst.edu/~dhyatt/supercomp/n310.html


Thank you again to whoever gives it a try.
Er... it looks to me like you're recalculating DataVector1 in the same way as before immediately after making it the cross product of DataVector2 and DataVector3. Was that intentional? I would expect your modified code to do exactly the same as before.

I was expecting something more like this:

'enable when doing lights             If Points >= 3 Then 'if 3 or more points exsist then find normals for surface                    ' subtract point2 - point1 to find a vector                    ' no light, trying new method of point3 - point2                    DataVector2.X = PointX(3) - PointX(2)                    DataVector2.Y = PointY(3) - PointY(2)                    DataVector2.Z = PointZ(3) - PointZ(2)                    'subtract point3 - point1 to find another vector                    DataVector3.X = PointX(2) - PointX(1)                    DataVector3.Y = PointY(2) - PointY(1)                    DataVector3.Z = PointZ(2) - PointZ(1)                                                            ' find vector cross product and store in DV1                    DataVector1.X = (DataVector2.Y * DataVector3.Z) - (DataVector2.Z * DataVector1.Y)                    DataVector1.Y = (DataVector2.Z * DataVector3.X) - (DataVector2.X * DataVector1.Z)                    DataVector1.Z = (DataVector2.X * DataVector3.Y) - (DataVector2.Y * DataVector1.X)                                                                              Dist = Sqr(DataVector1.X ^ 2 + DataVector1.Y ^ 2 + DataVector1.Z ^ 2)                    ' normalize vector for light                    DataVector4.X = DataVector1.X / Dist                    DataVector4.Y = DataVector1.Y / Dist                    DataVector4.Z = DataVector1.Z / Dist                                                          DataVector1 = DataVector4                                        Debug.Print "Normals:  X"; DataVector1.X; "  Y"; DataVector1.Y; "  Z"; DataVector1.Z                End If                                For Counter = 1 To Points                    ' create edge vertexes of face, re-scale the textures later                     Dx.CreateD3DVertex PointX(Counter), PointY(Counter), PointZ(Counter), DataVector1.X, DataVector1.Y, DataVector1.Z, _                                        TexCordX(Counter) / 130, TexCordY(Counter) / 130, Faces(NumPolys).vFace(Counter)                Next
Harry.
THANK YOU VERY MUCH!

EURIKA! (or how ever you spell it)

Thank you very very much.

now it's time for me to surf the web for tutorals on atenuation and shadows.

You rock, man!
Resolved. moved to http://www.gamedev.net/community/forums/topic.asp?topic_id=293427 for shadow casting. (DX programming forum)

This topic is closed to new replies.

Advertisement