Results 1 to 7 of 7

Thread: Help with rendering a 3D surface

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2007
    Location
    San Francisco, CA
    Posts
    2

    Help with rendering a 3D surface

    Hello,

    I am physicist doing computational modeling. Over the years I have learned to code by "imitating" examples that people gave me or that I found online. I am reasonably confident using basic OpenGL, until it comes to drawing smooth surfaces, where I become quickly lost.

    I have to display a 3D smooth surface for which I have 3D points describing it, arranged such that XY come in a NxM grid, and then Z is the height for each XY point.

    I have been reading about polygons and face and vertex normals, but I am really at a loss as to what algorythms to use to compute vertex normals, and then how to produce a nice and smooth surface.

    I have found a few examples online, but in all the normals are given, not computed. I need to compute them on the fly, as my surface changes continuously over the simulation.

    Could anyone point to an example code I could again imitate?

    Thanks infinitely

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,574

    Re: Help with rendering a 3D surface

    If I recall, the standard algorithm for computing a nice normal for a surface is to perform a weighted average the face normals for each of the faces connected to that vertex. The weight should be the area of the face.

  3. #3
    Junior Member Newbie
    Join Date
    May 2007
    Posts
    21

    Re: Help with rendering a 3D surface

    Exactly right. Calculate the normals for each of the faces in your model first, then to get the normal for each vertex simply add up the x,y,z normals for each face that surrounds it and divide by the number of surrounding faces. This gives the 'average' (i.e. weighted) normal for your vertex.

    So for three surrounding faces, add up normalX1+normalX2+normalX3 and divide by 3. This gives the average 'x' value for your vertex. And so on for your y and z values.

    To calculate the normal for a face, you only need to use the first three vertices of that face:


    // Calculate vectors
    v1x = faceVertex1X - faceVertex2X;
    v1y = faceVertex1Y - faceVertex2Y;
    v1z = faceVertex1Z - faceVertex2Z;

    v2x = faceVertex2X - faceVertex3X;
    v2y = faceVertex2Y - faceVertex3Y;
    v2z = faceVertex2Z - faceVertex2Z;

    // Get cross product of vectors
    cx = (v1y * v2z) - (v1z * v2y);
    cy = (v1z * v2x) - (v1x * v2z);
    cz = (v1x * v2y) - (v1y * v2x);

    // Normalise final vector
    vLen = (float)sqrt((cx * cx) + (cy * cy) + (cz * cz));

    faceNormalX = (cx / vLen);
    faceNormalY = (cy / vLen);
    faceNormalZ = (cz / vLen);

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,574

    Re: Help with rendering a 3D surface

    Actually, that sparked a memory.

    Don't normalize the vector. Only normalize the sum of the face normals. Because the cross-product is proportional to the face area, you don't have to compute the area manually.

  5. #5
    Junior Member Newbie
    Join Date
    May 2007
    Posts
    21

    Re: Help with rendering a 3D surface

    OK, I didn't realise that.

    I usually normalise the face normals first, then normalise again after calculating the sum for each vertex. Still seems to work though.

  6. #6
    Senior Member Regular Contributor
    Join Date
    Jul 2003
    Location
    Bangalore, Karnataka, India
    Posts
    123

    Re: Help with rendering a 3D surface

    Dear Opengl Experts,

    Please correct me if I my understanding is wrong.

    Ro calculate face normal of polygon, I will do the following
    ( 1 ) Calculate vector from the vertices for each edge of the ploygon
    ( 2 ) Calculate the crossproduct to compute normals
    ( 3 ) Repeat 1 and 2 for each edge of the polygon
    ( 4 ) Take average of the normals, This will obtain the face normal.

    If I am wrong, Please correct me

    RAJESH.R

  7. #7
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: Help with rendering a 3D surface

    (4) sum the normals
    (5) normalize it.

Similar Threads

  1. Replies: 0
    Last Post: 10-05-2008, 10:44 PM
  2. Surface Rendering form Volume data
    By jacobl in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 11-22-2003, 01:57 PM
  3. Water surface rendering
    By ioquan in forum OpenGL: Advanced Coding
    Replies: 19
    Last Post: 09-18-2003, 02:17 AM
  4. Help on Rendering a 3D SURFACE
    By ajit_v_r in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-24-2003, 01:18 AM
  5. surface Rendering
    By Rucha in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 01-16-2001, 09:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean