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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:38:51+00:00 2026-06-18T00:38:51+00:00

Following my attempt to learn CG and OpenGL I found the book Interactive Computer

  • 0

Following my attempt to learn CG and OpenGL I found the book “Interactive Computer Graphics”, which I think is a good one for beginners. Now, I’m trying to create the so called Sierpinski Gasket in 3D following the theory in the book. The project, which is in Xcode 4.5, builds fine and runs, but just when it arrives to the glEnable(GL_DEPTH_TEST) line, it crashes and Xcode shows the error Exc_bad_access.

Here’s what my code looks like:

//
//  main.cpp
//  SierpinskiGasket
//
//  Created by Federico Martinez on 24.01.13.
//  Copyright (c) 2013 ParallelDev. All rights reserved.
//

#include "Angel.h"

const int NumTimesToSubdivide = 3;
const int NumTriangles = 27;  // 3^5 triangles generated
const int NumVertices  = 3 * NumTriangles;

vec3 points[NumVertices];
vec3 base_colors[4] = {
    vec3(1.0, 0.0, 0.0),
    vec3(0.0, 1.0, 0.0),
    vec3(0.0, 0.0, 1.0),
    vec3(0.0, 0.0, 0.0)
};
vec3 colors[NumVertices];

int Index = 0;
int colorIndex;

//----------------------------------------------------------------------------

void triangle( const vec3& a, const vec3& b, const vec3& c ){
    colors[Index] = base_colors[colorIndex];
    points[Index++] = a;
    colors[Index] = base_colors[colorIndex];
    points[Index++] = b;
    colors[Index] = base_colors[colorIndex];
    points[Index++] = c;
}
//----------------------------------------------------------------------------

void tetra(vec3 a, vec3 b, vec3 c, vec3 d){
    colorIndex = 0;
    triangle(a, b, c);
    colorIndex = 1;
    triangle(a, c, d);
    colorIndex = 2;
    triangle(a, d, b);
    colorIndex = 3;
    triangle(b, d, c);
}
//----------------------------------------------------------------------------

void divide_tetra(const vec3& a, const vec3& b, const vec3& c, const vec3& d, int count){
    if ( count > 0 ) {
        vec3 mid[6];

        mid[0] = (a+b) / 2.0;
        mid[1] = (a+c) / 2.0;
        mid[2] = (a+d) / 2.0;
        mid[3] = (b+c) / 2.0;
        mid[4] = (c+d) / 2.0;
        mid[5] = (b+d) / 2.0;

        divide_tetra(a, mid[0], mid[1], mid[2], count-1);
        divide_tetra(mid[0], b, mid[3], mid[5], count-1);
        divide_tetra(mid[1], mid[3], c, mid[4], count-1);
        divide_tetra(mid[2], mid[5], mid[5], d, count-1);

    }else {
        tetra(a, b, c, d);    // draw tetrahedron at end of recursion
    }
}

//----------------------------------------------------------------------------

void
init( void )
{
    vec3 vertices[4] = {
        vec3(-1.0, -1.0, -1.0),
        vec3(1.0, -1.0, -1.0),
        vec3(0.0, 1.0, -1.0),
        vec3(0.0, 0.0, 1.0)
    };

    // Subdivide the original triangle
    divide_tetra(vertices[0], vertices[1], vertices[2], vertices[3], NumVertices);

    // Create a vertex array object
    GLuint vao;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );

    glBufferData( GL_ARRAY_BUFFER, sizeof(points)+sizeof(colors), NULL, GL_STATIC_DRAW );
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points);
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors);

    // Load shaders and use the resulting shader program
    GLuint program = InitShader( "vertex.glsl", "fragment.glsl" );
    glUseProgram( program );

    // Initialize the vertex position attribute from the vertex shader
    GLuint loc = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( loc );
    glVertexAttribPointer( loc, 3, GL_FLOAT, GL_FALSE, 0,
                          BUFFER_OFFSET(0) );


    GLuint loc2 = glGetAttribLocation(program, "vColor");
    glEnableVertexAttribArray(loc2);
    glVertexAttribPointer(loc2, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)));

    glClearColor( 1.0, 1.0, 1.0, 1.0 ); /* white background */
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);
    glFlush();
}

int main(int argc, char** argv){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_3_2_CORE_PROFILE | GLUT_DEPTH);
    glEnable(GL_DEPTH_TEST);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Sierpinski Gasket Recursive");

    init();

    glutDisplayFunc(display);

    glutMainLoop();

    return 0;
}

Most of the code comes from the book. The files Angel.h and InitShader.cpp provide most of the shaders and vector utilities and they are provided by the author of the book.

What does this Exc_bad_access means? Is there any error in the code that could be causing this problem?

UPDATE GDB Stacktrace

(gdb) t a a bt

Thread 5 (process 60424):
#0  0x00007fff8ecce6d6 in __workq_kernreturn ()
#1  0x00007fff8e5fdeec in _pthread_workq_return ()
#2  0x00007fff8e5fdcb3 in _pthread_wqthread ()
#3  0x00007fff8e5e8171 in start_wqthread ()

Thread 4 (process 60424):
#0  0x00007fff8ecce6d6 in __workq_kernreturn ()
#1  0x00007fff8e5fdeec in _pthread_workq_return ()
#2  0x00007fff8e5fdcb3 in _pthread_wqthread ()
#3  0x00007fff8e5e8171 in start_wqthread ()

Thread 3 (process 60424):
#0  0x00007fff8ecce6d6 in __workq_kernreturn ()
#1  0x00007fff8e5fdeec in _pthread_workq_return ()
#2  0x00007fff8e5fdcb3 in _pthread_wqthread ()
#3  0x00007fff8e5e8171 in start_wqthread ()

Thread 2 (process 60424):
#0  0x00007fff8ecced16 in kevent ()
#1  0x00007fff8d6b0dea in _dispatch_mgr_invoke ()
#2  0x00007fff8d6b09ee in _dispatch_mgr_thread ()

Thread 1 (process 60424):
#0  0x00007fff8ecf03d7 in glEnable ()
#1  0x000000010000212a in main (argc=1, argv=0x7fff5fbff800) at /Users/BRabbit27/Documents/COURSERA/ComputerGraphicsBook/SierpinskiGasket3D/SierpinskiGasket3D/main.cpp:129

UPDATE LLDB Stacktrace

(lldb) bt all
* thread #1: tid = 0x2e03, 0x00007fff8ecf03d7 libGL.dylib`glEnable + 15, stop reason = EXC_BAD_ACCESS (code=1, address=0x248)
    frame #0: 0x00007fff8ecf03d7 libGL.dylib`glEnable + 15
    frame #1: 0x000000010000212a SierpinskiGasket3D`main + 58 at main.cpp:129
    frame #2: 0x00007fff8fd5e7e1 libdyld.dylib`start + 1

  thread #2: tid = 0x3103, 0x00007fff8ecced16 libsystem_kernel.dylib`kevent + 10
    frame #0: 0x00007fff8ecced16 libsystem_kernel.dylib`kevent + 10
    frame #1: 0x00007fff8d6b0dea libdispatch.dylib`_dispatch_mgr_invoke + 883
    frame #2: 0x00007fff8d6b09ee libdispatch.dylib`_dispatch_mgr_thread + 54

  thread #3: tid = 0x3203, 0x00007fff8ecce6d6 libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #0: 0x00007fff8ecce6d6 libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #1: 0x00007fff8e5fdeec libsystem_c.dylib`_pthread_workq_return + 25
    frame #2: 0x00007fff8e5fdcb3 libsystem_c.dylib`_pthread_wqthread + 412
    frame #3: 0x00007fff8e5e8171 libsystem_c.dylib`start_wqthread + 13

  thread #4: tid = 0x3303, 0x00007fff8ecce6d6 libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #0: 0x00007fff8ecce6d6 libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #1: 0x00007fff8e5fdeec libsystem_c.dylib`_pthread_workq_return + 25
    frame #2: 0x00007fff8e5fdcb3 libsystem_c.dylib`_pthread_wqthread + 412
    frame #3: 0x00007fff8e5e8171 libsystem_c.dylib`start_wqthread + 13

  thread #5: tid = 0x3403, 0x00007fff8ecce6d6 libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #0: 0x00007fff8ecce6d6 libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #1: 0x00007fff8e5fdeec libsystem_c.dylib`_pthread_workq_return + 25
    frame #2: 0x00007fff8e5fdcb3 libsystem_c.dylib`_pthread_wqthread + 412
    frame #3: 0x00007fff8e5e8171 libsystem_c.dylib`start_wqthread + 13
  • 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-18T00:38:52+00:00Added an answer on June 18, 2026 at 12:38 am

    Short answer: move any OpenGL calls after glutCreateWindow.

    Rationale: OpenGL uses a data structure called a context, which stores all of its internal state: textures, shaders, state settings, etc. Every OpenGL function will access the current context before it starts its operation. Creating contexts is a function of the windowing system you’re using, or in the case of GLUT, the glutCreateWindow function. What’s happening in the OP is that the OpenGL implementation is effectively dereferencing a NULL pointer, and causing an exception.

    That’s bad form for an OpenGL implementation – most implementations will normally just have any OpenGL functions return immediately if there’s not a current context.

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

Sidebar

Related Questions

I am trying to learn cocos2d for iphone, following the following book http://www.apress.com/9781430233039 .
Moving on in my attempt to learn Android, I just read the following :
I am following a series of Java tutorials in an attempt to learn it.
In the book Learn Objective-C on the Mac' you can find the following code
I am trying to learn doxygen by running following Python file. A similar attempt
The following is my attempt at a prepare statement. It is causing the page
Im using the following code to attempt to update several static cells in my
I am using the following code to attempt to get the font color of
I am using the following code to attempt to redirect a dynamic URL to
I am using the following function to attempt to serialize an object to XML..

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.