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 6806321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:41:47+00:00 2026-05-26T19:41:47+00:00

The code below uses an octahedron as a starting 3D shape, ( I found

  • 0

The code below uses an octahedron as a starting 3D shape, ( I found it on the web) and after some tweaking I can’t still make it work.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h> /* must include for the offsetof macro */
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdlib.h>
#include <vector>
#include <iostream>

using namespace std; 

struct XYZ {
    GLdouble x;
    GLdouble y;
    GLdouble z;
};

struct FACET3 {
    XYZ p1;
    XYZ p2;
    XYZ p3;
};


void Normalise(XYZ *p_input)
{
    double magnitude = 0;
    magnitude = sqrt((p_input->x * p_input->x )+ (p_input->y * p_input->y) + (p_input->z * p_input->z));
    p_input->x = p_input->x / magnitude;
    p_input->y = p_input->y / magnitude;
    p_input->z = p_input->z / magnitude;

}

int CreateNSphere(FACET3 *f,int iterations)
{

   int i,it;
   double a;
   XYZ p[6] = {0,0,1,  0,0,-1,  -1,-1,0,  1,-1,0,  1,1,0, -1,1,0};
   XYZ pa,pb,pc;
   int nt = 0,ntold;

   /* Create the level 0 object */
   a = 1 / sqrt(2.0);
   for (i=0;i<6;i++) {
      p[i].x *= a;
      p[i].y *= a;
   }
   f[0].p1 = p[0]; f[0].p2 = p[3]; f[0].p3 = p[4];
   f[1].p1 = p[0]; f[1].p2 = p[4]; f[1].p3 = p[5];
   f[2].p1 = p[0]; f[2].p2 = p[5]; f[2].p3 = p[2];
   f[3].p1 = p[0]; f[3].p2 = p[2]; f[3].p3 = p[3];
   f[4].p1 = p[1]; f[4].p2 = p[4]; f[4].p3 = p[3];
   f[5].p1 = p[1]; f[5].p2 = p[5]; f[5].p3 = p[4];
   f[6].p1 = p[1]; f[6].p2 = p[2]; f[6].p3 = p[5];
   f[7].p1 = p[1]; f[7].p2 = p[3]; f[7].p3 = p[2];
   nt = 8;

   if (iterations < 1)
      return(nt);

   /* Bisect each edge and move to the surface of a unit sphere */
   for (it=0;it<iterations;it++) {
      ntold = nt;
      for (i=0;i<ntold;i++) {
         pa.x = (f[i].p1.x + f[i].p2.x) / 2;
         pa.y = (f[i].p1.y + f[i].p2.y) / 2;
         pa.z = (f[i].p1.z + f[i].p2.z) / 2;
         pb.x = (f[i].p2.x + f[i].p3.x) / 2;
         pb.y = (f[i].p2.y + f[i].p3.y) / 2;
         pb.z = (f[i].p2.z + f[i].p3.z) / 2;
         pc.x = (f[i].p3.x + f[i].p1.x) / 2;
         pc.y = (f[i].p3.y + f[i].p1.y) / 2;
         pc.z = (f[i].p3.z + f[i].p1.z) / 2;
         Normalise(&pa);
         Normalise(&pb);
         Normalise(&pc);
         f[nt].p1 = f[i].p1; f[nt].p2 = pa; f[nt].p3 = pc; nt++;
         f[nt].p1 = pa; f[nt].p2 = f[i].p2; f[nt].p3 = pb; nt++;
         f[nt].p1 = pb; f[nt].p2 = f[i].p3; f[nt].p3 = pc; nt++;
         f[i].p1 = pa;
         f[i].p2 = pb;
         f[i].p3 = pc;
      }
   }

   return(nt);
}


int main()
{
    FACET3 facet[8];
    int facets = CreateNSphere(facet, 2);
    printf(" Result: %d", facets);
} 

When I try to compile it, it returns:

“Segmentation Error”

gdb debugger says:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004011df in CreateNSphere(FACET3*, int) ()

any help?

after compiling with “-g” option “gbd” returned this:

(gdb) run
Starting program: /home/alexander/Graphics/Code-Handouts/tut_3/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x00000000004011df in CreateNSphere (f=0x7fffffffdeb0, iterations=2) at sphere_model.cpp:86
86           f[nt].p1 = pb; f[nt].p2 = f[i].p3; f[nt].p3 = pc; nt++;
(gdb) where
#0  0x00000000004011df in CreateNSphere (f=0x7fffffffdeb0, iterations=2) at sphere_model.cpp:86
#1  0x0000000000401303 in main () at sphere_model.cpp:100
(gdb) 
  • 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-26T19:41:47+00:00Added an answer on May 26, 2026 at 7:41 pm

    For two iterations, you need many more than 8 vertices.

    The initial iteration, there are eight vertices.

    On iteration one, 24 are added.

    On iteration two, it quadruples again.

    So you should declare

    FACET3 facet[128];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below uses an octahedron as a starting 3D shape, ( I found
I've some simple code below that uses a ToggleButton.IsChecked property to set the Visibility
I have this javascript code below that uses jquery, it is suppoed to be
Say I have the code below that uses LINQ to SQL: MyDataContext dc =
I have a CSV file which the code below uses to put the contents
Does the code below smell? I'm refactoring some code and have discovered this circular
The below code which uses npoi to create excel documents displays images in open
I have some code that uses the CAtlRegExp to perform some validation. In the
We are running some code on a project that uses OpenMP and I've run
I've stress tested the code below and it seems to work fine - what

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.