Go Back   AC3D Forums > Technical > AC3D Developers
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 20th November 2004, 02:57 PM   #1
fractile
Junior Member
Member
 
Join Date: Sep 2004
Location: Tampere, Finland
Posts: 14
Default surface types and smoothing

I wrote a very simple export plugin. If I filter out surfaces only of type POLYGON some (visually random) surfaces don't get exported. So, what surface types exactly are there?

If I smooth the surfaces before exporting, nothing gets exported. The surface type will then be 8 (undocumented) if I remember correctly..

I got things working for now when I process all polygons regardless of their type and just try to triangulate them.. Is this the right way to go?

- Mikko
fractile is offline   Reply With Quote
Old 20th November 2004, 03:17 PM   #2
fractile
Junior Member
Member
 
Join Date: Sep 2004
Location: Tampere, Finland
Posts: 14
Default smooth surface vertex normals

I ofcourse forgot the most important problem I have:

Vertex normals seem to be all messed up. I read vertex normals from the vertices of triangulated (if not triangle already) surfaces (svertex->v->normal). They aren't even unit vectors, length is close to zero. The normals from the surface itself seems fine, but I need the vertex normals to allow smooth surfaces when rendering the resulting exported model..

Any Ideas what might be wrong? I tried recalculating normals using object_calc_normals_force(), but I didn't help.
fractile is offline   Reply With Quote
Old 21st November 2004, 05:40 AM   #3
Andy
Administrator
Professional user
 
Andy's Avatar
 
Join Date: Jun 2003
Posts: 4,379
Default

Can you post the main part or send the plugin code?

Andy
Andy is offline   Reply With Quote
Old 21st November 2004, 06:18 AM   #4
fractile
Junior Member
Member
 
Join Date: Sep 2004
Location: Tampere, Finland
Posts: 14
Default Related source code

Here comes the essential part of the source code.

FMeshBuilder is an object that generated my mesh format.
The line commented out in fmesh_export_triangle_surface() is the line that causes bad normals for vertices..

- - clip - -

Code:
static void fmesh_export_triangle_surface(Surface* surface, FMeshBuilder& builder, int materialindex)
{
   printf("exporting surface..\n");

   int vertexindex = -1;
   for(List* v = surface->vertlist; v != NULL; v = v->next)
   {
      SVertex* vertex = (SVertex*)v->data;

      FMeshVector3 pos(vertex->v->x, vertex->v->y, vertex->v->z);
//      FMeshVector3 n(vertex->v->normal.x, vertex->v->normal.y, vertex->v->normal.z);
      FMeshVector3 n(surface->normal.x, surface->normal.y, surface->normal.z);
      FMeshVector3 tc(vertex->tx, vertex->ty, 0);
      vertexindex = builder.addVertex(pos, &n, &tc);
   }   

   builder.addFace(materialindex, vertexindex - 2, vertexindex - 1, vertexindex);
}


static void fmesh_export_object(ACObject* obj, FMeshBuilder& builder)
{    
   if(ac_object_get_num_surfaces(obj) > 0)
   {      
      object_calc_normals_force(obj);
      printf("exporting object..\n");

      int materialindex = -1;
      char* data = ac_object_get_data(obj);
      if(data[0])
         materialindex = builder.addMaterial(data);
      else
         materialindex = builder.addMaterial("default");
      printf("materialindex = %u\n", materialindex);

      List* surfaces = ac_object_get_surfacelist(obj);
      for (List* s = surfaces; s != NULL; s = s->next)
      {
         Surface *surface = (Surface *)s->data;
//         if(surface->type == SURFACE_POLYGON)
         {
            if(surface->numvert == 3)
            {
               fmesh_export_triangle_surface(surface, builder, materialindex);
            }
            else if(surface->numvert > 3)
            {
               // Triangulate surface
               List* triangles = surface_get_triangulations(surface);
               for(List* t = triangles; t != NULL; t = t->next)
               {
                  Surface* triangle = (Surface*)t->data;
                  fmesh_export_triangle_surface(triangle, builder, materialindex);
                  surface_free(triangle);
               }
               list_free(&triangles);
            }
            else
               printf("skipping numvert = %d surface ..\n", (int)surface->numvert);
         }
//         else
//            printf("skipping non-polygon surface %d..\n", (int)surface->type);
      }
   }

   List* children = ac_object_get_childrenlist(obj);  
   for(List* c = children; c != NULL; c = c->next)
      fmesh_export_object((ACObject *)c->data, builder);
}

static void fmesh_export()
{
   char *filename;
   FILE *file;
   char *filter[] = {"fmesh files", ".fmesh", "All files", "*", NULL};
   filename = ac_get_save_filename("Export fmesh file", filter);

   if((filename == NULL) || (*filename =='\0'))
      return;   

   FMeshBuilder builder;
   builder.begin("exported", 1, 1, 0);

   fmesh_export_object(ac_get_world(), builder);
   builder.writeToFile(filename);
}

AC3D_PLUGIN_FUNC int AC3DPluginInit(AC3DPluginInitData *d)

{
   double ver = ac_get_version_number();
   ac_add_command("fmesh_export_plugin", fmesh_export);
   ac_add_export_menu_item("fmesh...", "ac3d fmesh_export_plugin");

   return(0);
}
fractile is offline   Reply With Quote
Old 21st November 2004, 12:22 PM   #5
Andy
Administrator
Professional user
 
Andy's Avatar
 
Join Date: Jun 2003
Posts: 4,379
Default

There was a bug in surface_get_triangulations where sv normals got messed up. This should have been in the release you have.

See my private message.

Andy
Andy is offline   Reply With Quote
Old 1st March 2005, 11:55 AM   #6
fractile
Junior Member
Member
 
Join Date: Sep 2004
Location: Tampere, Finland
Posts: 14
Default

Argh! I'm now using AC3D 5.0 for linux, and the situation isn't any better.

SVertex normals for triangulated surfaces are still at random values (most often [0,0,0]). Should I also update my SDK header and rebuild my plugin with that? The ac_plugin.h that I have seems to be dated 22nd July 1999..
fractile is offline   Reply With Quote
Old 1st March 2005, 12:06 PM   #7
fractile
Junior Member
Member
 
Join Date: Sep 2004
Location: Tampere, Finland
Posts: 14
Default

The problem isn't in the triangulation anymore. It seems that the vertex normals are bad, even if I triagulate the model manually before exporting. AC3D shows the model correctly smoothed.

I added a printf() in the code below to see, where the normals go bad. All I get is lots of "SVertex normal: (0.000000, 0.000000, 0.000000)" messages..

Code:
static void fmesh_export_triangle_surface(Surface* surface, FMeshBuilder& builder, int materialindex)
{
   printf("exporting surface..\n");

   int vertexindex = -1;
   for(List* v = surface->vertlist; v != NULL; v = v->next)
   {
      SVertex* vertex = (SVertex*)v->data;

      FMeshVector3 pos(vertex->v->x, vertex->v->y, vertex->v->z);
      //FMeshVector3 n(vertex->v->normal.x, vertex->v->normal.y, vertex->v->normal.z);      
      printf("SVertex normal: (%f, %f, %f)\n", vertex->v->normal.x, vertex->v->normal.y, vertex->v->normal.z);
      FMeshVector3 n(surface->normal.x, surface->normal.y, surface->normal.z);
      FMeshVector3 tc(vertex->tx, vertex->ty, 0);
      vertexindex = builder.addVertex(pos, &n, &tc);
   }   

   builder.addFace(materialindex, vertexindex - 2, vertexindex - 1, vertexindex);
}
Rest of the code is unchanged from what I posted earlier.
fractile is offline   Reply With Quote
Old 1st March 2005, 02:51 PM   #8
Andy
Administrator
Professional user
 
Andy's Avatar
 
Join Date: Jun 2003
Posts: 4,379
Default

The normal is part of the SVertex and not the Vertex.

You code appears to be accesing a normal which is part of the vertex. I can't see why this would compile, unless you have a very old header file.

In your code you should be using vertex->normal.

Aha - looked further down your message - a lot has changed since 1999...

I'll email you the new headers - which you will need for 5.0 anyway.

Andy
Andy is offline   Reply With Quote
Old 1st March 2005, 04:50 PM   #9
fractile
Junior Member
Member
 
Join Date: Sep 2004
Location: Tampere, Finland
Posts: 14
Default

Thanks a lot! I got the exporter plugin working. A six year update did help I haven't yet tried recompiling my other plugins..
fractile is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT -4. The time now is 03:54 AM.


AC3D Forum
(C) Inivis Limited 2020