darkante's picture

"GL.ShadeModel (ShadingModel.Smooth)" doesnt seem to have any effect

Hi.

I am working on a loader for AC3D models (www.ac3d.org).

Everything works fine except that my models are flat and not smooth :-P

Here's my InitGL function:
-----------------------------

        private void InitGL()
        {
            /* Enable texture mapping */
            GL.Enable(EnableCap.Texture2D);
 
            /* Enable smooth shading */
            <b>GL.ShadeModel(ShadingModel.Smooth);</b>
 
            GL.Disable(EnableCap.ColorMaterial);
 
            GL.Enable(EnableCap.Lighting);
 
            GL.Enable(EnableCap.Light0);
 
            /* Set the background black */
            GL.ClearColor(0.0f, 0.5f, 0.0f, 0.0f);
 
            /* Depth buffer setup */
            GL.ClearDepth(1.0f);
 
            /* Enable depth testing */
            GL.DepthFunc(DepthFunction.Lequal);
 
            GL.Enable(EnableCap.DepthTest);
 
            /* Really nice perspective calculations */
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
        }

-----------------------------

Using OpenTK, version 0.9.5, and the GLControl inside a Windows Form. Running Windows XP. Developing in Visual Studio 2008.

I've attached example code. You have to add OpenTK.dll and OpenTK.Utilities.dll to the bin\Debug folder manually (sorry, the zip file was to big to upload with them...)

Any help or pointers is much appreciated! :)

Thanks in advance,
Andreas

[Moderator edit: word wrapping in title]

AttachmentSize
ExampleCode.zip121.07 KB

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

I skimmed through the code and nothing seemed out of place. How did you create the models? Did you enable smooth normals in your 3d editor?

darkante's picture

Thanks for the reply.

I've enabled smooth models in my 3d editor...

I calculate surface normals with the following function (from three points on the surface):
--------------------------------------------------------------

        private ACNormal tri_calc_normal(ACVertex v1, ACVertex v2, ACVertex v3)
        {
            ACNormal n = new ACNormal();
            double len;
 
            n.x = (v2.y - v1.y) * (v3.z - v1.z) - (v3.y - v1.y) * (v2.z - v1.z);
            n.y = (v2.z - v1.z) * (v3.x - v1.x) - (v3.z - v1.z) * (v2.x - v1.x);
            n.z = (v2.x - v1.x) * (v3.y - v1.y) - (v3.x - v1.x) * (v2.y - v1.y);
            len = Math.Sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
 
            if (len > 0)
            {
                n.x /= (float)len;
                n.y /= (float)len;
                n.z /= (float)len;  
            }
 
            return n;
        }

--------------------------------------------------------------

And here's what the ACNormal and ACVertex classes look like:
--------------------------------------------------------------

    public class ACNormal
    {
        public float x, y, z;
 
        public ACNormal()
            : this(0.0f, 0.0f, 0.0f)
        {
        }
 
        public ACNormal(float xx, float yy, float zz)
        {
            x = xx;
            y = yy;
            z = zz;
        }
    }
 
    public class ACVertex
    {
        public float x, y, z;
        public ACNormal normal;
 
        public ACVertex()
        {
            normal = new ACNormal();
        }
    }

--------------------------------------------------------------

Hmm...

/Andreas

the Fiddler's picture

I'm pretty sure this calculation will give you flat normals. What you can do is add a second pass which interpolates the normals of adjustent triangles, if their angle is less than 30 degrees. While this will give you smooth normals, they won't be as high quality as "true" normals, which are set by the artist in the editor.

I am not familiar with the AC3D file format: does it contain per-vertex normals? If it does, why don't you use them directly, instead of calculating face normals in code?

Edit: Try putting code in <code><code/> tags, it's easier to read. ;)

darkante's picture

Unfortunately, there are no pre-calculated normals. They have to be done in code...

I am considering moving on to another format. I want a easy format with skeletal animations and pre-calculated stuff like you mentioned :)

Got any recommendations? Whats your experience with Milkshape 3D?

Thanks for your help so far.

/Andreas

(Had a look at the AC3D format. Apparently there's a crease angle that can be used in my calculations................
http://www.inivis.com/forum/showthread.php?t=1821&highlight=crease+angle)