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

  • SEARCH
  • Home
  • 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 8051033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:13:05+00:00 2026-06-05T07:13:05+00:00

Given the code below. Taken from http://pastie.org/pastes/764327/text void CreateSphere(int PointRows, int PointsPerRow) { NumVertices

  • 0

Given the code below. Taken from http://pastie.org/pastes/764327/text

    void CreateSphere(int PointRows, int PointsPerRow)
{
    NumVertices = (PointRows-2)*PointsPerRow + 2;
    Vertices = new SVertex[NumVertices];
    IndexVect.clear();  //to be sure it is empty
    float x,y,z;
    int i,j;
    double r;
    for (i = 1; i < (PointRows-1); i++)
    {
        for (j = 0; j < PointsPerRow; j++)
        {
            y = 1.0 - float(i) / float(PointRows-1)*2.0;
            r = sin (acos(y));  //radius of the row
            x = r * sin(float(j) / float(PointsPerRow)*PI*2.0);     
            z = r * cos(float(j) / float(PointsPerRow)*PI*2.0);
        Vertices[(i-1)*PointsPerRow+j].x = x;
        Vertices[(i-1)*PointsPerRow+j].y = y;
        Vertices[(i-1)*PointsPerRow+j].z = z;
        Vertices[(i-1)*PointsPerRow+j].r = (float)(i) / float(PointRows);
        Vertices[(i-1)*PointsPerRow+j].g = 0.7;
        Vertices[(i-1)*PointsPerRow+j].b = (float)(j) / float(PointsPerRow);
    }

}
//The highest and deepest vertices:
Vertices[(PointRows-2)*PointsPerRow].x = 0.0;
Vertices[(PointRows-2)*PointsPerRow].y = 1.0;
Vertices[(PointRows-2)*PointsPerRow].z = 0.0;
Vertices[(PointRows-2)*PointsPerRow].r = 1.0;
Vertices[(PointRows-2)*PointsPerRow].g = 0.7;
Vertices[(PointRows-2)*PointsPerRow].b = 1.0;
Vertices[(PointRows-2)*PointsPerRow+1].x = 0.0;
Vertices[(PointRows-2)*PointsPerRow+1].y = -1.0;
Vertices[(PointRows-2)*PointsPerRow+1].z = 0.0;
Vertices[(PointRows-2)*PointsPerRow+1].r = 1.0;
Vertices[(PointRows-2)*PointsPerRow+1].g = 0.7;
Vertices[(PointRows-2)*PointsPerRow+1].b = 1.0;

for (i = 1; i < (PointRows-2); i++)
{
    for (j = 0; j < (PointsPerRow-1); j++)
    {
        IndexVect.push_back((i-1)*PointsPerRow+j);
        IndexVect.push_back((i-1)*PointsPerRow+j+1);
        IndexVect.push_back((i)*PointsPerRow+j);

        IndexVect.push_back((i-1)*PointsPerRow+j+1);
        IndexVect.push_back((i)*PointsPerRow+j+1);
        IndexVect.push_back((i)*PointsPerRow+j);
    }

    IndexVect.push_back((i-1)*PointsPerRow+PointsPerRow-1);
    IndexVect.push_back((i-1)*PointsPerRow);
    IndexVect.push_back((i)*PointsPerRow+j);

    IndexVect.push_back((i)*PointsPerRow);
    IndexVect.push_back((i-1)*PointsPerRow);
    IndexVect.push_back((i)*PointsPerRow+j);
}       

//The triangles to the highest and deepest vertices:
for (j = 0; j< (PointsPerRow-1); j++)
{
    IndexVect.push_back(j);
    IndexVect.push_back(j+1);
    IndexVect.push_back((PointRows-2)*PointsPerRow);
}
IndexVect.push_back(j);
IndexVect.push_back(0);
IndexVect.push_back((PointRows-2)*PointsPerRow);

for (j = 0; j< (PointsPerRow-1); j++)
{
    IndexVect.push_back((PointRows-3)*PointsPerRow+j);
    IndexVect.push_back((PointRows-3)*PointsPerRow+j+1);
    IndexVect.push_back((PointRows-2)*PointsPerRow+1);
}
IndexVect.push_back((PointRows-3)*PointsPerRow+j);
IndexVect.push_back((PointRows-3)*PointsPerRow);
IndexVect.push_back((PointRows-2)*PointsPerRow+1);
Indices = new GLuint[IndexVect.size()];  //allocate the required memory
for (i = 0; i < IndexVect.size(); i++)
{
    Indices[i] = IndexVect[i];
}
NumIndices = IndexVect.size();
IndexVect.clear();  //no longer needed, takes only memory

}

How would you calculate the normals using the resultant Vertices….?

I would then hope to use glEnableClientState(GL_NORMAL_ARRAY) , and glNormalPointer(GL_FLOAT,0, Normals) , along with glDrawElements to draw the resultant indices together with the normals.

I’ve had a go but it just looks wrong. The lighting appears to the left of the sphere, rather than below.

Apologies. I’ve not given the full picture here. Basically I’m trying to generate a random shape, by entering noise into the data.

If I use the following code at the end of the first nested loop:

Normals[(i-1)*PointsPerRow+j].x = x;
Normals[(i-1)*PointsPerRow+j].y = y;
Normals[(i-1)*PointsPerRow+j].z = z;

I am able to generate the correct normals for the sphere and it all looks fine.

However if I do this:

x=x+(noise3(x,y,z));
y=y+(noise3(x,y,z));
z=z+(noise3(x,y,z));

And then try to use:

Normals[(i-1)*PointsPerRow+j].x = x;
Normals[(i-1)*PointsPerRow+j].y = y;
Normals[(i-1)*PointsPerRow+j].z = z;

The normals look wrong for some faces. I want to loop through the resultant indices once the full shape has been created and then calculate the normal for each point, if that makes any sense….?

OK, here is what I got. It does not appear to work.It’s probably completely wrong so go easy.

  for (j = 0; j < NumIndices-2; j=j+3)
   {

Ax = Vertices[Indices[j]].x;    Bx = Vertices[Indices[j+1]].x;   Cx = Vertices[Indices[j+2]].x;
Ay = Vertices[Indices[j]].y;   By = Vertices[Indices[j+1]].y;   Cy = Vertices[Indices[j+2]].y;
Az = Vertices[Indices[j]].z;   Bz = Vertices[Indices[j+1]].z;   Cz = Vertices[Indices[j+2]].z;

dms::Vector3 p1(Ax,Ay,Az);
dms::Vector3 p2(Bx,By,Bz);
dms::Vector3 p3(Cx,Cy,Cz);
dms::Vector3 V1= (p2 - p1);
dms::Vector3 V2 = (p3 - p1);
dms::Vector3 normal = V1.cross(V2);

Normals[j].x = normal[0];
Normals[j].y = normal[1];
Normals[j].z = normal[2];;

Normals[j+1].x = normal[0];
Normals[j+1].y = normal[1];
Normals[j+1].z = normal[2];;


Normals[j+2].x = normal[0];
Normals[j+2].y = normal[1];
Normals[j+2].z = normal[2];;

}

Edit —

I was able to get a much better result by re-arranging the section where the 2 vectors are calculated. I changed the following:

dms::Vector3 V1= (p2 - p1);
dms::Vector3 V2 = (p3 - p1);

To:

dms::Vector3 V1= (p2 - p1);
dms::Vector3 V2 = (p1 - p3);

It still doesn’t look quite right however, there is dark strip that runs along the front of the sphere, and the top of the sphere looks strange.

Many thanks for Kaganar’s excellent answer which I will work through to resolve my remaining issues. Many thanks also to Bart!

  • 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-06-05T07:13:06+00:00Added an answer on June 5, 2026 at 7:13 am

    Based on the comments, this is one possible answer:

    • Step 1: Set all vertex normals to the zero vector.
    • Step 2: Calculate the normal of each triangle and add it to all vertices in each triangle.
    • Step 3: Normalize all vertex normals.

    Regarding the code you’ve posted at the time of writing this answer:

    • It’s not clear if the normals are initialized to the zero vector.
    • You’re assigning, not adding the normals to the vertex normals. (So step 2 isn’t working right.) Furthermore, the normals you are adding to the vertex normals aren’t themselves normalized. (A cross product is not automatically normalized — you must normalize the result of the cross product.)
    • You’re not doing step 3 at all.

    One further note: When you’re normalizing, beware of close-to-zero-length vectors passed to your normalization function, especially if you’re using noisily generated shapes. Since normalization is just dividing the components of the length of a vector, it’s common to check to see if this length is close to zero before doing the division you’d normally expect. If it is close to zero, the output of such a normalize function is typically just the up vector (since a zero vector would cause spuriously dark results in most shading models.)

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

Sidebar

Related Questions

Code given below is taken from the stackoverflow.com !!! Can anyone tell me how
Given the code below ... Net::HTTP.start('localhost', 4000) do |http| # # usual stuff omitted
Given the code below, how would you create/implement SR.h so that it produces the
Given the code below, how do I compare a List of objects's values with
I was given the code below to disable a button on an ASP.Net page
Any ideas given the code below why the highlight is being triggered to run
Given the Java code below, what's the closest you could represent these two static
In the code given below, I need to position the userid box in the
In the code given below, I am trying to modify it in such a
Hello friends i am running code given below which contains the setLogTimeEntery function and

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.