Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:13:08+00:00 2026-05-26T18:13:08+00:00

I am drawing a sphere using quads. I plot an extra vertex, just to

  • 0

I am drawing a sphere using quads. I plot an extra vertex, just to divide the quad into 2 triangles. So it goes like this:

1 ----> 2
|       |
|       |
4 ----> 3

but after 3 I plot 1 again. So imagine an extra line from 3–>1.

I am now trying to calculate each vertex’ normal.
Here is my code:

//calculate normals
   for (no_vertice=0; no_vertice<12887; no_vertice+=1) 
   {

    //getting the sphere's vertices
    x=sphere_vertices[no_vertice].position[0];
    y=sphere_vertices[no_vertice].position[1];
    z=sphere_vertices[no_vertice].position[2];

    //normalising vector "norm(Vertex - Center)"
    magnitude = sqrt((x*x) + (y*y) + (z*z));

    sphere_vertices[no_vertice].normal[0] = (x/magnitude);
    sphere_vertices[no_vertice].normal[1] = (y/magnitude);
    sphere_vertices[no_vertice].normal[2] = (z/magnitude);

    printf("Normal at vertice %d = X:%f, Y:%f, Z:%f. \n", no_vertice, sphere_vertices[no_vertice].normal[0], sphere_vertices[no_vertice].normal[1], sphere_vertices[no_vertice].normal[2]);

    }

I am calculating the magnitude for each vertex, and then dividing each component of that vertex with the magnitude so I get a unit vector. The problem is that I get a lot of zero vectors. that is vertices with x=0, y=0, z=0…
When I pass the normal to the vertex shader,

//my vertex structure
struct Vertex {

    GLdouble position[3];
    GLfloat color[3];
    GLdouble normal[3];
};

....
..
.

/* Enable attribute index 2 as being used */
    glEnableVertexAttribArray ( 2 );
    glVertexAttribPointer ( ( GLuint ) 2, 3, GL_FLOAT, GL_FALSE, sizeof ( struct Vertex ), ( const GLvoid* )
    offsetof(struct Vertex, normal) );

...
..
.

    //pass the normal to vertex shader  
    glBindAttribLocation(shaderprogram, 2, "in_Normal");

and do my light calculation I get all weird kind of effects.

Am I doing anything wrong?

The most confusing part is I am asked to do this:

“For the sphere, work out the surface normal direction and augment your wire-frame
drawing with short lines representing the normal direction of each vertex The sphere
should now appear to be a hedge hog.”

“Note: The surface normal is the unit vector at right angles to the surface patch, assuming it is flat.”

So is it basically the normal to a vertex, or to the quad surface that I have to draw?
I am confused because it says,

“work out the surface normal direction”

and then

“drawing with short lines representing the normal direction of each vertex”

So where the lines should be drawn??? on the vertex? or in the middle of the quad? Thanks

EDIT: Vertex Calculation

   for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
      for (phi=0;phi<=360-dphi;phi+=dphi) {


    //calculating Vertex 1
     x = cos(theta*DTOR) * cos(phi*DTOR);
     y = cos(theta*DTOR) * sin(phi*DTOR);
     z = sin(theta*DTOR);

    no_vertice+=1;
    sphere_vertices[no_vertice].position[0] = x;
    sphere_vertices[no_vertice].position[1] = y;
    sphere_vertices[no_vertice].position[2] = z;

    //calculating Vertex 2
    x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
    y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
    z = sin((theta+dtheta)*DTOR);

    no_vertice+=1;
    sphere_vertices[no_vertice].position[0] = x;
    sphere_vertices[no_vertice].position[1] = y;
    sphere_vertices[no_vertice].position[2] = z;

    //calculating Vertex 3
    x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
    y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
    z = sin((theta+dtheta)*DTOR);

    no_vertice+=1;
    sphere_vertices[no_vertice].position[0] = x;
    sphere_vertices[no_vertice].position[1] = y;
    sphere_vertices[no_vertice].position[2] = z;

    //adding Vertex_1 again to divide the Quad into 2 triangles 
    //calculating Vertex 1
     x = cos(theta*DTOR) * cos(phi*DTOR);
     y = cos(theta*DTOR) * sin(phi*DTOR);
     z = sin(theta*DTOR);

    no_vertice+=1;
    sphere_vertices[no_vertice].position[0] = x;
    sphere_vertices[no_vertice].position[1] = y;
    sphere_vertices[no_vertice].position[2] = z;


        if (theta > -90 && theta < 90) {

            //calculating Vertex 4
            x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
            y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
            z = sin(theta*DTOR);

            no_vertice+=1;
            sphere_vertices[no_vertice].position[0] = x;
            sphere_vertices[no_vertice].position[1] = y;
            sphere_vertices[no_vertice].position[2] = z;


             }
        }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:13:09+00:00Added an answer on May 26, 2026 at 6:13 pm

    Your problem(s), as I see them is, that you don’t really have a grip for what’s actually going on. Instead of looking at code from some tutorials, you should get a grip of the mathematical concepts (trying to filter those from code is counterproductive, since code doesn’t tell the full story and also may omit or simplify things for a good enough approximation).

    First of all you got the order of vertices, well not wrong, but unhappy. OpenGL assumes right handedness in vector calculations (unless you swap one axis at the end of the transformation pipeline). That means, vertices should be counted counterclockwise. You can do it clockwise, but things get much easier the other way round. Next you should start counting with 0, at least if you’re in a C like language that addresses arrays by offset index, i.e. first element is at index 0.

    3--2
    | /|
    |/ |
    0--1
    

    The normals of a origin centered sphere are something special, since normalized (don’t confuse a “normal” with the normal of normal-ized, they’re different things!) vertex position vectors are the normal of the vertex in question.

    In the general case a normal can be evaluated by taking the cross product of the tangent space of the vertex, i.e. for a triangle the cross product of the edge vectors at a corner. In your quad’s case the normal at [0] would be

    normalize( ([1]-[0]) × ([2]-[0]) )
    

    for the triangle 0,1,2 and

    normalize( ([2]-[0]) × ([3]-[0]) )
    

    Notice, that this is the cross product of the partial derivatives of the analytical representation of the surface. You already know the analytical representation of a sphere’s surface (see Paul Bourke’s tutorial). I recommend as an exercise to test this, i.e. proof that for a unit radius sphere around the origin, the position of a point of the surface equals the surface’s normal at that point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need the fastest sphere mapping algorithm. Something like Bresenham's line drawing one. Something
Drawing on a QPicture should update its bounding rect. Like this: >>> picture =
I'm drawing points on a map with OpenLayers like in this example: http://dev.openlayers.org/examples/draw-feature.html Now
I am drawing a line on a control on my Windows form like this:
Drawing a blank on this, and google was not helpful. Want to make a
I'm working on drawing an SVG grid using Javascript. I've managed to get the
I'm using System.Drawing.Color, is there anything in Visual Studio that can display the color
I am drawing graphs into a WinForms Picturebox. Now I am searching for a
I'm drawing polygons using the Graphics View framework. I added a polygon to the
Currently I'm drawing an NSImage in a custom NSCell like so: - (void)drawInteriorWithFrame:(NSRect)theCellFrame inView:(NSView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.