MaxScript Exporter problems - Vertex Normals/Smooth Groups

Started by
2 comments, last by AndyEsser 13 years, 5 months ago
Hi all, I've tried this at multiple different forums and haven't gotten much help at all. I'm still VERY new to MaxScript and if I can get past this hurdle I'll be able to work through more of this.

I have written an exporter to export from Max to X-Plane. I'm using max 2010 x64.

The format that I'm exporting to requires a list of vertex records, and so I export them using the following:

for f = 1 to num_faces do(    channel = 1    Face = getFace tmesh f    TFace = meshop.getMapFace tmesh channel f    TVx = meshop.getMapVert tmesh channel (TFace.x as integer)    TVy = meshop.getMapVert tmesh channel (TFace.y as integer)    TVz = meshop.getMapVert tmesh channel (TFace.z as integer)                    for v = 1 to 3 do    (        if FaceCCW.state == true then        (            case v of            (                3: (curVert = Face.x; curTVert = TVx)                2: (curVert = Face.y; curTVert = TVy)                1: (curVert = Face.z; curTVert = TVz)            )        ) else (            case v of            (                1: (curVert = Face.x; curTVert = TVx)                2: (curVert = Face.y; curTVert = TVy)                3: (curVert = Face.z; curTVert = TVz)            )        )        Vert = getvert tmesh curVert        VertNorm = getNormal tmesh curVert        VertX = formattedPrint Vert.x format:".6f"        VertY = formattedPrint Vert.y format:".6f"        VertZ = formattedPrint Vert.z format:".6f"        VertNormX = formattedPrint VertNorm.x format:".6f"        VertNormY = formattedPrint VertNorm.y format:".6f"        VertNormZ = formattedPrint VertNorm.z format:".6f"        VertU = formattedPrint curTVert[1] format:".6f"        VertV = formattedPrint curTVert[2] format:".6f"        append AryVerts ("VT " + VertX + " " + VertY + " " + VertZ + " " + VertNormX + " " + VertNormY + " " + VertNormZ + " " + VertU + " " + VertV + "\n")    ))


tmesh is a snapshot of my mesh. I loop through each face in my mesh (CCW or CW) and create a record for each vertex. The vertex requires a triplet for X,Y,Z position, and then an X,Y,Z normal (normalized to 1) and then a U, V coordinate.

Everything is exporting and displaying properly, except my smooth groups are not showing in the engine, and I get an error for each object that says it has normals of the wrong length.

Is there anything I can do in the above code to make sure the normal x,y,z values are normalized to 1 and reflect the smooth groups? The normals also tell X-Plane how to smooth the surfaces, so the normal values should take into account the smooth group choices.

My vertex records are NOT optimized...so they currently reflect num_faces * 3. If anyone has any insight into optimizing my vertex records, that would be awesome!

Here's the specifics from: http://scenery.x-plane.com/library.php?doc=obj8spec.php

Quote:VT <x> <y> <z> <nx> <ny> <nz> <s> <t> This defines a single entry in the triangle vertex table. These are indexed starting from zero based on the record order. The eight numbers represent a triplet coordinate location, a triplet normal, and a pair forming a texture coordinate. This table is only used for triangles.

...

Flat/Smooth shading (default is smooth). In flat shading, the entire triangle is drawn with the same brightness. In smooth shading, the corners of the triangle have the shading levels appropriate for their normals, and the interior is interpolated. Note that since flat shading can be simulating via per-vertex normals, it is usually better to only use smooth shading.

...

Normals are specified as a triplet of fractions forming a 3-space vector whose length is 1. The vector is interpretted in the same coordinate space as the geometry.


Help me get this portion of the exporter working and I'll be set!

I think I need some better way to organize the VT records...a more organized array that I can sort through and compare to as I come across vertices. This way when I loop through my faces I can compare to the array and determine whether to add a new record or not...As long as the vertices are set to make faces in a counter clockwise orientation to determine the facing of a face, and all the vertices are represented based on their texture and vertex information I should be good...but I have NO idea how to go about doing that.

Thanks in advance for the help!
I work with X-Plane
Advertisement
In order to create normal of length 1 you need to perform an operation called normalizing. This is where you divide each component of the normal (x,y,z) by the length of the normal. To calculate the normal you need to do this operation:

normal_length = sqrt(normal.x^2 + normal.y^2 + normal.z^2).

However, I know for a fact that max will give you normalized components already as I do pretty much the same thing in my exporter. So the problem is elsewhere, and may be do to with the way you're writing the normal out to a file, since you are rounding the normal to 6 decimal places. Is this a requirement of your exporter, or can you export it in full?
Heres some sample output from the above code:

VT -5.688458 -0.184925 4.516189 0.394689 -1.995943 -0.241369 0.258005 0.660901
VT -4.491374 -0.011067 5.177628 0.162206 -0.986106 -0.035846 0.428328 0.554014
VT -4.492776 0.002333 4.922896 0.534238 -3.091060 -0.171475 0.395913 0.535761
VT -4.492776 0.002333 4.922896 0.534238 -3.091060 -0.171475 0.395913 0.535761
VT -5.688458 -0.170515 4.403035 0.230695 -1.153887 -0.146945 0.243681 0.652480
VT -5.688458 -0.184925 4.516189 0.394689 -1.995943 -0.241369 0.258005 0.660901
VT -4.491374 -0.011067 5.177628 0.162206 -0.986106 -0.035846 0.428328 0.554014
VT -3.898806 0.074594 5.507529 0.177333 -1.057912 -0.043833 0.512955 0.501291
VT -3.898806 0.088117 5.181144 0.325549 -1.941949 -0.085238 0.471524 0.477767
VT -3.898806 0.088117 5.181144 0.325549 -1.941949 -0.085238 0.471524 0.477767
VT -4.492776 0.002333 4.922896 0.534238 -3.091060 -0.171475 0.395913 0.535761
VT -4.491374 -0.011067 5.177628 0.162206 -0.986106 -0.035846 0.428328 0.554014

I think its required to be rounded to 6, yes...but it looks like they're way off...i think my biggest problem is understanding the way max handles normals and smoothing versus the way x-plane handles it....
I work with X-Plane
Well given the data you've given me the first line has a normal length of 2.0488598927527963 (it should be 1.0). So there's your first problem.

Second problem is that Max exports smoothing group's as a separate parameter. However, how X-Plane respects those is a different question (one I can't answer I'm afraid).

Also, I use getFaceNormal instead of getNormal - not sure how that will impact your output, but might want to take a look.

Why does it need to be 6 decimal places?

This topic is closed to new replies.

Advertisement