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?