Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Normals for cylinder

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    5

    Normals for cylinder

    Hi i am not able to apply proper normals to my cylinder i have tried using the cross product of three vertices but it is not working for me, kindly help me

    this is my code.

    void normalize(float *xn, float *yn, float *zn)
    {
    float len = sqrt(*xn**xn+*yn**yn+*zn**zn);
    *xn = *xn / len;
    *yn = *yn / len;
    *zn = *zn / len;
    }



    struct Vert v0,v1,v2,v3,vn,edge1,edge2;
    float theta,theta1,x1,y1,z1,x22,y22,z22;

    for(int i=0;i<32;i++)
    {
    theta = 2*M_PI*i/32;
    x1= 1.0*cos(theta);
    y1=1.0*sin(theta);
    z1 = 0;

    theta1 = 2*M_PI*i+1/32;
    x22= 1.0*cos(theta);
    y22=1.0*sin(theta);
    z22 = 0;

    v0.x=x1;v0.y=y1;v0.z=z1;
    v1.x=x1,v1.y=y1,v1.z=z1+5.0;
    v2.x=x22;v2.y=y22;v2.z=z22;

    edge1.x = v1.x-v0.x;
    edge1.y = v1.y-v0.y;
    edge1.z = v1.z-v0.z;

    edge2.x = v2.x-v0.x;
    edge2.y = v2.y-v0.y;
    edge2.z = v2.z-v0.z;

    vn.x = edge1.y*edge2.z - edge1.z * edge2.y;
    vn.y = edge1.z*edge2.x - edge1.x * edge2.z;
    vn.z = edge1.x*edge2.y - edge1.y * edge2.x;

    normalize(&vn.x,&vn.y,&vn.z);
    //glNormal3f(vn.x,vn.y,vn.z);

    glVertex3f(x1,y1,z1);
    glVertex3f(x1,y1,z1+5.);


    }

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    723
    Code :
    theta1 = 2*M_PI*i+1/32;
        x22= 1.0*cos(theta);
        y22=1.0*sin(theta);
        z22 = 0;

    should this be theta1

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    5
    Sorry for the typing mistake,yes this should be theta1 but still i am not able to get proper normals for the cylinder

  4. #4
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421
    Quote Originally Posted by sumkang View Post
    Sorry for the typing mistake,yes this should be theta1 but still i am not able to get proper normals for the cylinder
    Normals for a cylinder is quite simple. For the body, I would do
    normal = normalize(vertex - center of cylinder); //Do that for all the body vertices

    and for the top, it would be {0.0, 0.0, 1.0}
    and the bottom, it would be {0.0, 0.0, -1.0}
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    723
    very clever V-man

  6. #6
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    5
    Thanks a lot V-man for the help but still it is not working for me all the normals are facing in top direction this is my code.

    struct Vert nm1,nm2;
    float theta,theta1,x1,y1,z1

    for(int i=0;i<32;i++)
    {

    theta = 2*M_PI*i/32;
    x1= 1.0*cosf(theta);
    y1=1.0*sinf(theta);
    z1 = 5;



    // calculation normal for top vertices
    nm1.x = x1-0;
    nm1.y=y1-0;
    nm1.z = 5.0-0;
    normalize(&nm1.x,&nm1.y,&nm1.z);
    glNormal3f(nm1.x,nm1.y,nm1.z);
    glVertex3f(x1,y1,5.0);


    // calculation normal for Bottom vertices

    nm2.x = x1-0;
    nm2.y = y1-0;
    nm2.z = -5.0-0;
    normalize(&nm2.x,&nm2.y,&nm2.z);
    glNormal3f(nm2.x,nm2.y,nm2.z);
    glVertex3f(x1,y1,-5.0);

  7. #7
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    5
    Thanks V-man it is working for me now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •