Hello, everyone.
I'm having some trouble getting

glutMouseFunc

to work.

#include <GL/glut.h>  //GL/glut.h includes GL/gl.h and GL/glu.h so there is no need to declare them
#include <ctime>   // std::time
#include <cstdlib> // std::srand
#include "./colorgl.h"  // contains my ColorGL class that handles colors.
#include "./pathgl.h"  // contains my NodeGL, PathGL, BezierGL, and HermiteGL classes

float winDX, winDY;
int winWidth = 600;
int winHeight = 400;
int clxct = 0;		// number of clicks

ColorGL bgl, pen;	// background color (default is set to black), and foreground color.
BezierGL bzc, bzcr, bzru;  // Three BezierGL objects.  BezierGL is a subclass of PathGL

void reshapeFcn(int dx, int dy){
 float aspectRatio;
 if(dy == 0){dy = 1;}		// prevent divbyzero
 /* void glViewport(int x, int y, int width, int height); */
 glViewport(0,0,dx,dy);		// set viewport to window dimensions
 // reset coordinate system
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 // establish clipping volumne (left,right,bottom,top,near,far)
 aspectRatio = static_cast<float>(dx)/static_cast<float>(dy);
 if(dy <= dx)
 {
  winDX = 200;
  winDY = 200 / aspectRatio;
 }
 else
 {
  winDX = 200 * aspectRatio;
  winDY = 200;
 };
 glOrtho(-winDX,winDX,-winDY,winDY,1.0,-1.0);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
};

void displayFcn()
{
 glClear(GL_COLOR_BUFFER_BIT);
 pen.pick(bgl).use();  // pick a color except the background color and use it as the foreground color.
 glPointSize(3.0);  // Set the point size to 3.0 pixels
 glutSwapBuffers();	// Flush and execute
}

/*
void timerFunc(int val)
{
 // Animation would go here, but it is also good for getting the keyboard functions to work.
 glutPostRedisplay();
 glutTimerFunc(33,timerFunc,1);
}
*/

/* This is working.  */
void keyFcn( unsigned char key, int x, int y )

{

 switch(key)

 {
  case 'c':	pen.pick(bgl).use(); break;  // pick a new color
  case 'q':

  case 27:	std::exit(0);		// ESCape to quit

  default:	break;

 };

};

/* This is correct but it is not working.  */
void mouseFcn(int button, int action, int x, int y)
{
 if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
 {
  if(clxct < 4)  // If less than four points are ploted, plot a point.
  {
   bzc.set(clxct,x,y);	// y = winHeight - y in windows
   glBegin(GL_POINTS);
   bzc.plot(clxct);
   glEnd();
   clxct++;
  }
  else{    // Otherewise plot a path.
   pen.pick(bgl).use();		// the color to draw the line.
   glPointSize(1.0);
   glBegin(GL_LINE_STRIP);
   bzc.plot(clxct);
   glEnd();
   pen.pick(bgl).use();		// the color to draw the next set of points
   glPointSize(3.0);
   clxct = 0;
  }
 }
};

void init()
{
 bgl.useClear();   // glClearColor(0.0,0.0,0.0,1.0);
};

int main(int argc, char** argv)
{
 std::srand(std::time(NULL));	// Random number generator (RNG)
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
 glutInitWindowPosition(100,100);  // left, top
 glutInitWindowSize(winWidth,winHeight);
 glutCreateWindow(argv[0]);   // Create a new window with the name of the program as the title.
 glutDisplayFunc(displayFcn);
 glutReshapeFunc(reshapeFcn);
 glutKeyboardFunc(keyFcn);
 glutMouseFunc(mouseFcn);
 //glutTimerFunc(33,timerFunc,1);
 init();
 glutMainLoop();
 return 0;
};

The program shuts down when I click and returns a

Segmentation fault

message.
What is causing this to happen?

Recommended Answers

All 9 Replies

There is probably something wrong in your not-shown classes ColorGL &| BezierGL, since the code runs fine with all reference to those classes commented.

Have you got a debugger? I'm guessing if your PC is saying 'Segmentation fault' that you're using some breed of Linux? If so, run your application from the command like like this... gdb ./a.out ...and when it segfaults, do... backtrace ...and you'll have a much better idea where your error is ( since gdb is an interactive debugger, and 'backtrace' gives you the callstack before the error ).

If you're not on Linux.. er.. dno, what are you on? I've never seen Windows say 'Segmentation fault'... Visual Studio has an integrated debugger, but you'll have to work out how to use it yourself.

The program name is hex3

$ g++ -c hex3.cpp -o hex3.o
$ g++ -Wl--start-group hex3.o colorgl.o pathgl.o -lglut -Wl--end-group -o hex3
$ ./hex3

It is at this point that I click something and...

Segmentation fault
$ gdb ./hex3
GNU gdb Red Hat Linux (6.6-16.fc7rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...
(no debugging symbols found)
Using host libthread_db library "/lib64/libthread_db.so.1".
(gdb) backtrace
No stack.
(gdb) quit

>_< oops, sorry;

$ gdb ./hex3
[b](gdb) run[/b]
..wait for error..
(gdb) backtrace

That helps. Turns out BezierGL::plot() is a fault.

I guess I can show some of my code.

/* pathgl.h */
//...
class NodeGL
{
 public:
 //...
  void plot() const;
 //...
 private:
  double *p;
};
//..
class PathGL
{
 public:
 //...
  virtual void plot() const;
  virtual void plot(int) const;
  //...
 private:
  NodeGL *p;
  int size;
};
//...
class BezierGL : public PathGL
{
 public:
  //...
  virtual void plot() const;
  virtual void plot(int) const;
  //...
};
/* pathgl.cpp */
//...
void NodeGL::plot() const
{
 glVertex3d(p[0],p[1],p[2]);
};
//...
void PathGL::plot() const
{
 for(int i = 0; i < size; i++){p[i].plot();};
};
//...
void PathGL::plot(int n) const {p[n].plot();};
//...
void BezierGL::plot() const {
 //..
 NodeGL tmp;
 //...
  tmp.plot();
 //...
};
//...
void BezierGL::plot(int n) const {this->plot(n);};

Perhaps I should use this code instead for BezierGL::plot(int)

void BezierGL::plot(int n) const {PathGL::plot(n);};

Modifying that function work. I managed to make my four points before Segmentation fault occurs. :-)

I think I can handle it from here. Thanks for the help.

OK, after replacing most instances of this->someMethod() in my BezierGL class, a new challenge appears.

$ gdb ./hex3
GNU gdb Red Hat Linux (6.6-16.fc7rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...
(no debugging symbols found)
Using host libthread_db library "/lib64/libthread_db.so.1".
(gdb) run
Starting program: /home/bushidohacks/newexamples/hex3 
(no debugging symbols found)
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7fff549fd000
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
---Type <return> to continue, or q <return> to quit---
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
---Type <return> to continue, or q <return> to quit---

Program received signal SIGSEGV, Segmentation fault.
0x00000000004050bb in NodeGL::plot ()
(gdb) backtrace
#0  0x00000000004050bb in NodeGL::plot ()
#1  0x0000000000405563 in PathGL::plot ()
#2  0x000000000040559f in BezierGL::plot ()
#3  0x0000000000401711 in mouseFcn ()
#4  0x0000003609c1f928 in glutMainLoopEvent () from /usr/lib64/libglut.so.3
#5  0x0000003609c1ffaa in glutMainLoop () from /usr/lib64/libglut.so.3
#6  0x0000000000401647 in main ()
(gdb) quit
The program is running.  Exit anyway? (y or n) y
$

What could it be now?

Fixed it. Turns Out I was using the wrong function in my mouse function.

Ok, the method NodeGL:plot is very small, the only thing that could be wrong in that method itself is the array not being instantiated ( or being too short ), however, there could be something wrong higher up ( the pointer 'this' can be 0 sometimes! ).

To get more useful info from the debugger, you need to add an option when compiling: recompile all of your files with the option -g3 to emit debugging information ( I hope that works on your G++ version, I'm using G++4 ).. after recompiling -- you need to recompile ALL of the files with this option, so not just the hex3.cpp file, but the file(s) for bezier, node, etc aswell -- after doing that, run the application with GDB again, and post the output of backtrace if you dont mind.. that will tell me ( and I can tell you ) whether or not it's an error within the code in the method NodeGL:plot, or an error higher in the call chain.

Fixed it. Turns Out I was using the wrong function in my mouse function.

Cool; good to know it's working :)

Disregard my last post; although, the -g3 flag is quite useful to remember ( with full debug info you can see all the values passed into methods aswell as the method names ).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.