Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenGL : Draw lines with mouse click - sample program
#1
Draw lines with mouse click - sample program

#include <iostream>>
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
using namespace std;
#define MaxNumPts 64
float PointArray[MaxNumPts][2];
int NumPts = 0;
// Window size in pixels
int WindowHeight;
int WindowWidth;
void myKeyboardFunc( unsigned char key, int x, int y );
void myMouseFunc( int button, int state, int x, int y );
void displayLines(void);
void removeFirstPoint();
void removeLastPoint();
void addNewPoint( float x, float y );
void initRendering();
void resizeWindow(int w, int h);
void myKeyboardFunc (unsigned char key, int x, int y)
{
switch (key) {
case 'f':
removeFirstPoint();
glutPostRedisplay();
break;
case 'd':
removeLastPoint();
glutPostRedisplay();
break;
case 27: // Escape key
exit(0);
break;
}
}
void removeFirstPoint() {
int i;
if ( NumPts>0 ) {
// Remove the first point, slide the rest down
NumPts--;
for ( i=0; i<NumPts; i++ ) {
PointArray[i][0] = PointArray[i+1][0];
PointArray[i][1] = PointArray[i+1][1];
}
}
}
// Left button presses place a control point.
void myMouseFunc( int button, int state, int x, int y ) {
if ( button==GLUT_LEFT_BUTTON && state==GLUT_DOWN ) {
float xPos = ((float)x)/((float)(WindowWidth-1));
float yPos = ((float)y)/((float)(WindowHeight-1));
yPos = 1.0f-yPos; // Flip value since y position is from top row.
Page 1
New Text Document.txt
addNewPoint( xPos, yPos );
glutPostRedisplay();
}
}
// Add a new point to the end of the list.
// Remove the first point in the list if too many points.
void removeLastPoint() {
if ( NumPts>0 ) {
NumPts--;
}
}
// Add a new point to the end of the list.
// Remove the first point in the list if too many points.
void addNewPoint( float x, float y ) {
if ( NumPts>=MaxNumPts ) {
removeFirstPoint();
}
PointArray[NumPts][0] = x;
PointArray[NumPts][1] = y;
NumPts++;
}
void displayLines(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
// Draw the line segments
glColor3f(1.0f, 0.0f, 0.8f); // Reddish/purple lines
if ( NumPts>1 ) {
glBegin( GL_LINE_STRIP );
for ( i=0; i<NumPts; i++ ) {
glVertex2f( PointArray[i][0], PointArray[i][1] );
}
glEnd();
}
// Draw the interpolated points second.
glColor3f( 0.0f, 0.0f, 0.0f); // Draw points in black
glBegin( GL_POINTS );
for ( i=0; i<NumPts; i++ ) {
glVertex2f( PointArray[i][0], PointArray[i][1] );
}
glEnd();
glFlush();
}
void initRendering() {
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
// Make big points and wide lines. (This may be commented out if desired.)
glPointSize(8);
glLineWidth(5);
// The following commands should induce OpenGL to create round points and
// antialias points and lines. (This is implementation dependent unfortunately,
and
// may slow down rendering considerably.)
// You may comment these out if you wish.
Page 2
New Text Document.txt
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); // Make round points, not square points
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Antialias the lines
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void resizeWindow(int w, int h)
{
WindowHeight = (h>1) ? h : 2;
WindowWidth = (w>1) ? w : 2;
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, 1.0f, 0.0f, 1.0f); // Always view [0,1]x[0,1].
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
initRendering();
glutDisplayFunc(displayLines);
glutReshapeFunc(resizeWindow);
glutKeyboardFunc(myKeyboardFunc);
glutMouseFunc(myMouseFunc);
glutMainLoop();
return 0; // This line is never reached
}
Page 3

Download PDF

.pdf   line-draw-with-mouse.pdf (Size: 14.79 KB / Downloads: 2)
Reply
#1
Draw lines with mouse click - sample program

#include <iostream>>
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
using namespace std;
#define MaxNumPts 64
float PointArray[MaxNumPts][2];
int NumPts = 0;
// Window size in pixels
int WindowHeight;
int WindowWidth;
void myKeyboardFunc( unsigned char key, int x, int y );
void myMouseFunc( int button, int state, int x, int y );
void displayLines(void);
void removeFirstPoint();
void removeLastPoint();
void addNewPoint( float x, float y );
void initRendering();
void resizeWindow(int w, int h);
void myKeyboardFunc (unsigned char key, int x, int y)
{
switch (key) {
case 'f':
removeFirstPoint();
glutPostRedisplay();
break;
case 'd':
removeLastPoint();
glutPostRedisplay();
break;
case 27: // Escape key
exit(0);
break;
}
}
void removeFirstPoint() {
int i;
if ( NumPts>0 ) {
// Remove the first point, slide the rest down
NumPts--;
for ( i=0; i<NumPts; i++ ) {
PointArray[i][0] = PointArray[i+1][0];
PointArray[i][1] = PointArray[i+1][1];
}
}
}
// Left button presses place a control point.
void myMouseFunc( int button, int state, int x, int y ) {
if ( button==GLUT_LEFT_BUTTON && state==GLUT_DOWN ) {
float xPos = ((float)x)/((float)(WindowWidth-1));
float yPos = ((float)y)/((float)(WindowHeight-1));
yPos = 1.0f-yPos; // Flip value since y position is from top row.
Page 1
New Text Document.txt
addNewPoint( xPos, yPos );
glutPostRedisplay();
}
}
// Add a new point to the end of the list.
// Remove the first point in the list if too many points.
void removeLastPoint() {
if ( NumPts>0 ) {
NumPts--;
}
}
// Add a new point to the end of the list.
// Remove the first point in the list if too many points.
void addNewPoint( float x, float y ) {
if ( NumPts>=MaxNumPts ) {
removeFirstPoint();
}
PointArray[NumPts][0] = x;
PointArray[NumPts][1] = y;
NumPts++;
}
void displayLines(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
// Draw the line segments
glColor3f(1.0f, 0.0f, 0.8f); // Reddish/purple lines
if ( NumPts>1 ) {
glBegin( GL_LINE_STRIP );
for ( i=0; i<NumPts; i++ ) {
glVertex2f( PointArray[i][0], PointArray[i][1] );
}
glEnd();
}
// Draw the interpolated points second.
glColor3f( 0.0f, 0.0f, 0.0f); // Draw points in black
glBegin( GL_POINTS );
for ( i=0; i<NumPts; i++ ) {
glVertex2f( PointArray[i][0], PointArray[i][1] );
}
glEnd();
glFlush();
}
void initRendering() {
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
// Make big points and wide lines. (This may be commented out if desired.)
glPointSize(8);
glLineWidth(5);
// The following commands should induce OpenGL to create round points and
// antialias points and lines. (This is implementation dependent unfortunately,
and
// may slow down rendering considerably.)
// You may comment these out if you wish.
Page 2
New Text Document.txt
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); // Make round points, not square points
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Antialias the lines
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void resizeWindow(int w, int h)
{
WindowHeight = (h>1) ? h : 2;
WindowWidth = (w>1) ? w : 2;
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, 1.0f, 0.0f, 1.0f); // Always view [0,1]x[0,1].
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
initRendering();
glutDisplayFunc(displayLines);
glutReshapeFunc(resizeWindow);
glutKeyboardFunc(myKeyboardFunc);
glutMouseFunc(myMouseFunc);
glutMainLoop();
return 0; // This line is never reached
}
Page 3

Download PDF

.pdf   line-draw-with-mouse.pdf (Size: 14.79 KB / Downloads: 2)
Reply
CAA


Forum Jump:


Users browsing this thread: 1 Guest(s)