Hello all,
I'm am trying to get this program to work for class tomorrow. Maybe someone would be kind enough to be able to check it out for me. I'm using the g++ compiler in Ubuntu trying to get this openGL application to work. It compiles fine but has a segmentation fault during runtime. I'm using;

g++ -lglut -lGL -lGLU -lGLEW main.cpp -o GSP380Lab3

to compile it.

Attachments
#ifdef _WINDOWS
#include <windows.h>
#endif

#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include <stdio.h>

#include "CGfxOpenGL.h"
#include "gmath.h"

// disable float-double conversion warnings
#pragma warning(disable:4305)
#pragma warning(disable:4244)

//extern bool WireFrame;

CGfxOpenGL::CGfxOpenGL()
{
}

CGfxOpenGL::~CGfxOpenGL()
{
    Shutdown();
}

bool CGfxOpenGL::Init()
{	
    // erase the background to black
	glClearColor(0.0, 0.0, 0.0, 0.0);

    // initialize a rotation angle
    m_angle = 0.0f;

    // create a quadric object for drawing spheres
    m_Quadric = gluNewQuadric();

    return true;
}

bool CGfxOpenGL::Shutdown()
{
    // delete the quadric object
    if (m_Quadric != NULL) {
        gluDeleteQuadric(m_Quadric);
        m_Quadric = NULL;
    }
	return true;
}

void CGfxOpenGL::SetupProjection(int width, int height)
{
	if (height == 0)					// don't want a divide by zero
	{
		height = 1;					
	}

	glViewport(0, 0, width, height);		// reset the viewport to new dimensions
	glMatrixMode(GL_PROJECTION);			// set projection matrix current matrix
	glLoadIdentity();						// reset projection matrix

	// calculate aspect ratio of window
	gluPerspective(52.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

	glMatrixMode(GL_MODELVIEW);				// set modelview matrix
	glLoadIdentity();						// reset modelview matrix

	m_windowWidth = width;
	m_windowHeight = height;
}

void CGfxOpenGL::Prepare(float dt)
{
    // update the rotation angle
    m_angle += 0.1f;
}

// draw a normal form the center of poly defined by p1, p2, p3
// using the normal value
// this function used to test/visualize the normal value
void DrawNormal(Vector3& p1, Vector3& p2, Vector3& p3, Vector3& normal) 
{
    Vector3 center, step;

    // compute the center of the poly break up this calculation into
    // two parts since g++ doesn't know how to add more than two numbers.
    step = p1 + p2;
    center = step + p3;
    center.scale(1.0/3.0);

    // temporarily disable lighting to draw the normal line
    glDisable(GL_LIGHTING);


    // draw a small white line to represent the normal
    glBegin(GL_LINES);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(center.x, center.y, center.z);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(center.x + normal.x, center.y + normal.y, center.z + normal.z);

    glEnd();

    // re-enable lighting
    glEnable(GL_LIGHTING);
}


// define a single sided face
struct Face {
    int i1, i2, i3;             // indexes into vertex data
};

void CGfxOpenGL::Render()
{

    bool Wireframe;
    
    int numFaces = 8;
    Vector3 p1, p2, p3;        // points or vertices of the triangle

    // define 5 vertices of a diamond shape - colors not being used now
    Vector3 vertexes[] = {
        Vector3(  0,  2,  0 ),     // vertex 0
        Vector3(  1,  0,  1 ),     // vertex 1
        Vector3(  1,  0, -1 ),     // vertex 2 ...
        Vector3( -1,  0, -1 ),
        Vector3( -1,  0,  1 ),
        Vector3(  0, -2,  0 )
    };

    // set up the face index data into the vertex array above
    // each triple will define a single face (triangle) of our shape
    Face faceData[] = {
        0, 4, 1,    // top front-facing face
        0, 3, 4,    // top left face
        0, 2, 3,    // top rear face
        0, 1, 2,    // top right face
        5, 1, 4,    // bottom front face ...
        5, 4, 3,
        5, 3, 2, 
        5, 2, 1,
    };

    // clear screen and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     
    glLoadIdentity();

	// enable z or depth buffer testing
    glEnable(GL_DEPTH_TEST);

    if (Wireframe) {
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    }
    else {
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }


    // move the whole scene back 
    glTranslatef(0.0, 0.0, -10.0f);

    // set up a temporary transform for the orbiting sphere
    glPushMatrix();
    
        // make light rotate about an odd axis
        glRotatef(m_angle, 1.0f, 1.0f, 0.0f);

        // move the light into upper left corner of the scene
        glTranslatef(-2.0, 2.0, 3.0f);

        // draw a small sphere for light position
        glColor3f(1.0, 0.0, 1.0);
        gluSphere(m_Quadric, 0.2, 10, 10);
        
    glPopMatrix();


    // rotate object about Y
    glRotatef(m_angle*2, 0.0f, 1.0f, 0.0f);

    // for each of the 8 faces
    for (int i=0; i < numFaces; i++) {
        // draw the faces that make up the diamond shape
        glBegin(GL_TRIANGLES);

            // draw 3 vertices with color, light and normal info
            // vertex 1
            p1 = vertexes[faceData[i].i1];
            glVertex3f(p1.x, p1.y, p1.z);

            // vertex 2
            p2 = vertexes[faceData[i].i2];
            glVertex3f(p2.x, p2.y, p2.z);

            // vertex 3
            p3 = vertexes[faceData[i].i3];
            glVertex3f(p3.x, p3.y, p3.z);
    
        glEnd();

    }
}
#ifndef __GL_COMPONENT
#define __GL_COMPONENT

bool Wireframe;

class CGfxOpenGL
{
public:
	int m_windowWidth;
	int m_windowHeight;
    GLUquadricObj* m_Quadric;

	float m_angle;

public:
	CGfxOpenGL();
	virtual ~CGfxOpenGL();

	bool Init();
	bool Shutdown();

	void SetupProjection(int width, int height);

	void Prepare(float dt);
	void Render();

};

#endif
// gmath.h

#ifndef GMATH_H
#define GMATH_H

#include<math.h>

const double DTOR        = 0.01745329251994;     // degrees to radians
const double RTOD        = 57.29577951308;       // radians to degrees

// function to round a real to an int
int Round(double x) {
    if (x < 0) {
        return int(x - 0.5);
    } else {
        return int(x + 0.5);
    }
}

// function to get the absolute value of an integer
int Abs(int x) {
    if (x < 0)  return -x;
    else        return x;
}

// function to get the sign (+1 or -1) of an integer
int Sign(int x) {
    if (x < 0)  return -1;
    else        return 1;
}

// a 3D vector with a homogeneous coordinate
// if not used set w = 1
class Vector3 {
public:
    double x, y, z, w;

    // default constructor
    Vector3() {
        x = y = z = 0.0;
        w = 1.0;
    }

    Vector3(double x, double y, double z, double w = 1.0) {
        this->x = x; 
        this->y = y; 
        this->z = z;   
        this->w = w;
    }

    // set this vector to a new value
    void set(double x, double y, double z, double w = 1.0) {
        this->x = x; 
        this->y = y; 
        this->z = z;   
        this->w = w;
    }

    // set this vector to a new value from an array
    void set(double v[]) {
        this->x = v[0]; 
        this->y = v[1]; 
        this->z = v[2];   
        this->w = 1.0;
    }

    // set this vector to a new value
    void set(Vector3& v) {
        this->x = v.x; 
        this->y = v.y; 
        this->z = v.z;   
        this->w = v.w;
    }

    // scale this vector uniformly
    void scale(double f) {
        this->x *= f; 
        this->y *= f; 
        this->z *= f;   
    }

    // return squared length of this vector
    double squaredLength() {
        return x * x + y * y + z * z;
    }

    // return length of this vector
    double length() {
        return sqrt(x * x + y * y + z * z);
    }

    // normalize this vector
    void normalize() {
        double len = length();
        scale(1.0/len);
    }

};

Vector3 Add(Vector3& a, Vector3& b) {
    // addition - returns result = a + b
    Vector3 result;
    result.x = a.x + b.x;
    result.y = a.y + b.y;
    result.z = a.z + b.z;
    return result;
}

Vector3 operator+(Vector3& a, Vector3& b) {
    // addition - returns result = a + b
    Vector3 result;
    result.x = a.x + b.x;
    result.y = a.y + b.y;
    result.z = a.z + b.z;
    return result;
}

Vector3 Subtract(Vector3& a, Vector3& b){
    // subtraction - returns result = a - b
    Vector3 result;
    result.x = a.x - b.x;
    result.y = a.y - b.y;
    result.z = a.z - b.z;
    return result;
}

Vector3 operator-(Vector3& a, Vector3& b){
    // subtraction - returns result = a - b
    Vector3 result;
    result.x = a.x - b.x;
    result.y = a.y - b.y;
    result.z = a.z - b.z;
    return result;
}

Vector3 CrossProduct(Vector3& a, Vector3& b) {
    // cross product - returns result = a X b
    Vector3 result;
    result.x = a.y * b.z - a.z * b.y;
    result.y = a.z * b.x - a.x * b.z;
    result.z = a.x * b.y - a.y * b.x;
    return result;
}

// return dot product of a & b
double DotProduct(Vector3& a, Vector3& b) {
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

// a 4x4 matrix class
class Matrix4 {
public:
    double m[4][4];

    Matrix4(){ 
        // default constructor set m = I
        m[0][0] = 1.0;  m[0][1] = 0.0;  m[0][2] = 0.0;  m[0][3] = 0.0;
        m[1][0] = 0.0;  m[1][1] = 1.0;  m[1][2] = 0.0;  m[1][3] = 0.0;
        m[2][0] = 0.0;  m[2][1] = 0.0;  m[2][2] = 1.0;  m[2][3] = 0.0;
        m[3][0] = 0.0;  m[3][1] = 0.0;  m[3][2] = 0.0;  m[3][3] = 1.0;
    }

    Matrix4(double m00, double m01, double m02, double m03, 
            double m10, double m11, double m12, double m13,
            double m20, double m21, double m22, double m23,
            double m30, double m31, double m32, double m33) {
        m[0][0] = m00;  m[0][1] = m01;  m02 = m02;  m03 = m03;
        m[1][0] = m10;  m[1][1] = m11;  m12 = m12;  m13 = m13;
        m[2][0] = m20;  m[2][1] = m21;  m22 = m22;  m23 = m23;
        m[3][0] = m30;  m[3][1] = m31;  m32 = m32;  m33 = m33;
    }

    void makeIdMatrix() {
        // makes an identity matrix
        m[0][0] = 1.0;  m[0][1] = 0.0;  m[0][2] = 0.0;  m[0][3] = 0.0;   
        m[1][0] = 0.0;  m[1][1] = 1.0;  m[1][2] = 0.0;  m[1][3] = 0.0;   
        m[2][0] = 0.0;  m[2][1] = 0.0;  m[2][2] = 1.0;  m[2][3] = 0.0;   
        m[3][0] = 0.0;  m[3][1] = 0.0;  m[3][2] = 0.0;  m[3][3] = 1.0;   
    }

    void makeProjectionMatrix(double d) {
        // makes a perspective projection matrix
        m[0][0] = 1.0;  m[0][1] = 0.0;  m[0][2] = 0.0;  m[0][3] = 0.0;   
        m[1][0] = 0.0;  m[1][1] = 1.0;  m[1][2] = 0.0;  m[1][3] = 0.0;   
        m[2][0] = 0.0;  m[2][1] = 0.0;  m[2][2] = 1.0;  m[2][3] = 0.0;   
        m[3][0] = 0.0;  m[3][1] = 0.0;  m[3][2] = 1/d;  m[3][3] = 0.0;   
    }

    void makeTranslationMatrix(double dx, double dy, double dz) {
        // makes a translation matrix
        m[0][0] = 1.0;  m[0][1] = 0.0;  m[0][2] = 0.0;  m[0][3] = dx;   
        m[1][0] = 0.0;  m[1][1] = 1.0;  m[1][2] = 0.0;  m[1][3] = dy;   
        m[2][0] = 0.0;  m[2][1] = 0.0;  m[2][2] = 1.0;  m[2][3] = dz;   
        m[3][0] = 0.0;  m[3][1] = 0.0;  m[3][2] = 0.0;  m[3][3] = 1.0;   
    }

    void makeScaleMatrix(double sx, double sy, double sz){
        // makes a scale matrix
        m[0][0] =  sx;  m[0][1] = 0.0;  m[0][2] = 0.0;  m[0][3] = 0.0;   
        m[1][0] = 0.0;  m[1][1] =  sy;  m[1][2] = 0.0;  m[1][3] = 0.0;   
        m[2][0] = 0.0;  m[2][1] = 0.0;  m[2][2] =  sz;  m[2][3] = 0.0;   
        m[3][0] = 0.0;  m[3][1] = 0.0;  m[3][2] = 0.0;  m[3][3] = 1.0;   
    }

    // makes rotation matrix about X-axis based on a given angle in degrees
    void makeRotationMatrixX(double angle) {
        double angleInRads = angle * DTOR;
        makeRotationMatrixX(sin(angleInRads), cos(angleInRads));
    }

    void makeRotationMatrixX(double sinA, double cosA){
        // makes rotation matrix about X-axis
        m[0][0] = 1.0; m[0][1] =   0.0;  m[0][2] =   0.0;  m[0][3] = 0.0;   
        m[1][0] = 0.0; m[1][1] =  cosA;  m[1][2] = -sinA;  m[1][3] = 0.0;   
        m[2][0] = 0.0; m[2][1] =  sinA;  m[2][2] =  cosA;  m[2][3] = 0.0;   
        m[3][0] = 0.0; m[3][1] =   0.0;  m[3][2] =   0.0;  m[3][3] = 1.0;   
    }

    // makes rotation matrix about Y-axis based on a given angle in degrees
    void makeRotationMatrixY(double angle) {
        double angleInRads = angle * DTOR;
        makeRotationMatrixY(sin(angleInRads), cos(angleInRads));
    }

    void makeRotationMatrixY(double sinA, double cosA){
        // makes rotation matrix about Y-axis
        m[0][0] =  cosA; m[0][1] = 0.0;  m[0][2] =  sinA;  m[0][3] = 0.0;   
        m[1][0] =   0.0; m[1][1] = 1.0;  m[1][2] =   0.0;  m[1][3] = 0.0;   
        m[2][0] = -sinA; m[2][1] = 0.0;  m[2][2] =  cosA;  m[2][3] = 0.0;   
        m[3][0] =   0.0; m[3][1] = 0.0;  m[3][2] =   0.0;  m[3][3] = 1.0;   
    }

    // makes rotation matrix about Z-axis based on a given angle in degrees
    void makeRotationMatrixZ(double angle) {
        double angleInRads = angle * DTOR;
        makeRotationMatrixZ(sin(angleInRads), cos(angleInRads));
    }

    void makeRotationMatrixZ(double sinA, double cosA){
        // makes rotation matrix about Z-axis
        m[0][0] =  cosA; m[0][1] = -sinA;  m[0][2] = 0.0;  m[0][3] = 0.0;   
        m[1][0] =  sinA; m[1][1] =  cosA;  m[1][2] = 0.0;  m[1][3] = 0.0;   
        m[2][0] =   0.0; m[2][1] =   0.0;  m[2][2] = 1.0;  m[2][3] = 0.0;   
        m[3][0] =   0.0; m[3][1] =   0.0;  m[3][2] = 0.0;  m[3][3] = 1.0;   
    }
};

// multiplies matrix m by vector a
Vector3 Multiply(Matrix4& m, Vector3& a) {
    // returns result = m * a
    Vector3 result;
    result.x = m.m[0][0] * a.x + m.m[0][1] * a.y + m.m[0][2] * a.z + m.m[0][3] * a.w;
    result.y = m.m[1][0] * a.x + m.m[1][1] * a.y + m.m[1][2] * a.z + m.m[1][3] * a.w;
    result.z = m.m[2][0] * a.x + m.m[2][1] * a.y + m.m[2][2] * a.z + m.m[2][3] * a.w;
    result.w = m.m[3][0] * a.x + m.m[3][1] * a.y + m.m[3][2] * a.z + m.m[3][3] * a.w;
    return result;
}

// multiplies matrix m by vector a
Vector3 operator*(Matrix4& m, Vector3& a) {
    // returns result = m * a
    Vector3 result;
    result.x = m.m[0][0] * a.x + m.m[0][1] * a.y + m.m[0][2] * a.z + m.m[0][3] * a.w;
    result.y = m.m[1][0] * a.x + m.m[1][1] * a.y + m.m[1][2] * a.z + m.m[1][3] * a.w;
    result.z = m.m[2][0] * a.x + m.m[2][1] * a.y + m.m[2][2] * a.z + m.m[2][3] * a.w;
    result.w = m.m[3][0] * a.x + m.m[3][1] * a.y + m.m[3][2] * a.z + m.m[3][3] * a.w;
    return result;
}

// multiplies vector a by matrix m
Vector3 Multiply(Vector3& a, Matrix4& m) {
    // returns result = a * m
    Vector3 result;
    result.x = a.x * m.m[0][0] + a.y * m.m[1][0] + a.z * m.m[2][0] + a.w * m.m[3][0];
    result.y = a.x * m.m[0][1] + a.y * m.m[1][1] + a.z * m.m[2][1] + a.w * m.m[3][1];
    result.z = a.x * m.m[0][2] + a.y * m.m[1][2] + a.z * m.m[2][2] + a.w * m.m[3][2];
    result.w = a.x * m.m[0][3] + a.y * m.m[1][3] + a.z * m.m[2][3] + a.w * m.m[3][3];
    return result;
}

// multiplies vector a by matrix m
Vector3 operator*(Vector3& a, Matrix4& m) {
    // returns result = a * m
    Vector3 result;
    result.x = a.x * m.m[0][0] + a.y * m.m[1][0] + a.z * m.m[2][0] + a.w * m.m[3][0];
    result.y = a.x * m.m[0][1] + a.y * m.m[1][1] + a.z * m.m[2][1] + a.w * m.m[3][1];
    result.z = a.x * m.m[0][2] + a.y * m.m[1][2] + a.z * m.m[2][2] + a.w * m.m[3][2];
    result.w = a.x * m.m[0][3] + a.y * m.m[1][3] + a.z * m.m[2][3] + a.w * m.m[3][3];
    return result;
}

// multiply x X y using loops
Matrix4 Multiply(Matrix4& x, Matrix4& y) {
    int i, j, k;
    Matrix4 result;

    for(i=0; i < 4; i++) {
        for(j=0; j < 4; j++) {
            result.m[i][j] = 0.0;
            for(k=0; k < 4; k++) {
                result.m[i][j] += x.m[i][k] * y.m[k][j];    
            }
/*
When creating your project, uncheck OWL,
uncheck Class Library, select Static
instead of Dynamic and change the target
model to Console from GUI.
Also link glut.lib to your project once its done.
*/

//#include <windows.h>   // Standard Header For Most Programs
#include <GL/glew.h>     // The GL Header File
#include <GL/glut.h>   // The GL Utility Toolkit (Glut) Header
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>

//main.cpp needs to see this to execute it
#include "CGfxOpenGL.h"

//main.cpp needs to see where this is to execute it
#include "CGfxOpenGL.cpp"

#include "gmath.h"

//give CGfxOpenGL a pointer so you can use it
CGfxOpenGL *g_glRender = NULL;


void init (void )     // Create Some Everyday Functions
{

  glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
   glEnable ( GL_COLOR_MATERIAL );
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display ( void )   // Create The Display Function
{
  g_glRender->Prepare(0.0f);
  g_glRender->Render();
  glutSwapBuffers ( );
  // Swap The Buffers To Not Be Left With A Clear Screen
}

void reshape ( int w, int h )   // Create The Reshape Function (the viewport)
{
  glViewport     ( 0, 0, w, h );
  glMatrixMode   ( GL_PROJECTION );  // Select The Projection Matrix
  glLoadIdentity ( );                // Reset The Projection Matrix
  if ( h==0 )  // Calculate The Aspect Ratio Of The Window
     gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
  else
     gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
  glMatrixMode   ( GL_MODELVIEW );  // Select The Model View Matrix
  glLoadIdentity ( );    // Reset The Model View Matrix
}

void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function
{
  switch ( key ) {
    case 27:        // When Escape Is Pressed...
      //exit ( 0 );   // Exit The Program
      break;        // Ready For Next Case
    default:        // Now Wrap It Up
      break;
  }
}

void arrow_keys ( int a_keys, int x, int y )  // Create Special Function (required for arrow keys)
{
  switch ( a_keys ) {
    case GLUT_KEY_UP:     // When Up Arrow Is Pressed...
      glutFullScreen ( ); // Go Into Full Screen Mode
      break;
    case GLUT_KEY_DOWN:               // When Down Arrow Is Pressed...
      glutReshapeWindow ( 500, 500 ); // Go Into A 500 By 500 Window
      break;
    default:
      break;
  }
}

int main ( int argc, char** argv )   // Create Main Function For Bringing It All Together
{
  //create a new space in memory for an instance of CGfxOpenGL in memory
  g_glRender = new CGfxOpenGL;
  
  glutInit            ( &argc, argv ); // Erm Just Write It =)
  init ();
  glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode
  glutInitWindowSize  ( 800, 600 ); // If glutFullScreen wasn't called this is the window size
  glutCreateWindow    ( "GSP 380 Lab1 Brian Perry" ); // Window Title (argv[0] for current directory as title)
  //glutFullScreen      ( );          // Put Into Full Screen
  glutDisplayFunc     ( display );  // Matching Earlier Functions To Their Counterparts
  glutReshapeFunc     ( reshape );
  glutKeyboardFunc    ( keyboard );
  glutSpecialFunc     ( arrow_keys );
  glutIdleFunc			 ( display );
  glutMainLoop        ( );          // Initialize The Main Loop
}

Try to cut out code until it is short enough to post here using code tags (< 30 lines). That will help you find the problem, as well as help us fix it if you can't.

Can you give a little more information about the segmentation fault you're getting.

When does it happen? What's happening on screen? What data is given to you with the fault?

I have eliminated all the compile errors but when I try to run the executible, it flashes the window on the screen and then shows segmentation fault on the console window. I posted the code in this post and it can be compiled with the command that I gave in my original post. I don't have any other errors other than that.

Unfortunately I don't have a Unix system here and translating the code to windows may actually remove the error.

There is a program for Unix/Linux called GNU Debugger (GDB)

You should be able to run your app through that. If a crash occurs, you will need to use the -bt (backtrace) command and it will tell you where your sigfault occurs.

EDIT: I've scanned your code and found the error. You've just forgotten to do something with your CGfxClass... =)
(I still suggest downloading GDB as it WILL help you a lot in future)

Edited 6 Years Ago by Ketsuekiame: n/a

Thanks for the info. It looks like this application show a little more detail than just running the compiler.

This article has been dead for over six months. Start a new discussion instead.