Total members 11892 |It is currently Fri Oct 18, 2024 7:28 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





ConnectDots.c :
Code:

/*
* ConnectDots.c
*
*     This program draws straight lines connecting dots placed with mouse clicks.
*
* Author: Samuel R. Buss
*
* Software accompanying the book
*      3D Computer Graphics: A Mathematical Introduction with OpenGL,
*      by S. Buss, Cambridge University Press, 2003.
*
* Software is "as-is" and carries no warranty.  It may be used without
*   restriction, but if you modify it, please change the filenames to
*   prevent confusion between different versions.
* Bug reports: Sam Buss, [email protected].
* Web page: http://math.ucsd.edu/~sbuss/MathCG
*
* Usage: 
*   Left click to place a control point. 
*      Maximum number of control points allowed is currently set at 64.
*    Press "f" to remove the first control point
*    Press "l" to remove the last control point.
*    Press escape to exit.
*/

#include "ConnectDots.h"
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>

#define maxNumberOfPoints 64
float ArrayOfPoints[maxNumberOfPoints][2];
int currentNumberPoints = 0;

// Window size in pixels
int frameHeight;
int frameWidth;

void keysFunction (unsigned char key, int x, int y)
{
   switch (key) {

   case 27:         // Escape key
      exit(0);
      break;
   }
}

void removeFirstPoint() {
   int i;
   if ( currentNumberPoints>0 ) {
      // Remove the first point, slide the rest down
      currentNumberPoints--;
      for ( i=0; i<currentNumberPoints; i++ ) {
         ArrayOfPoints[i][0] = ArrayOfPoints[i+1][0];
         ArrayOfPoints[i][1] = ArrayOfPoints[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)(frameWidth-1));
      float yPos = ((float)y)/((float)(frameHeight-1));

      yPos = 1.0f-yPos;         // Flip value since y position is from top row.

      addNewPoint( xPos, yPos );
      glutPostRedisplay();
   }
}

void removeLastPoint() {
   if ( currentNumberPoints>0 ) {
      currentNumberPoints--;
   }
}


void addNewPoint( float x, float y ) {
   if ( currentNumberPoints>=maxNumberOfPoints ) {
      removeFirstPoint();
   }
   ArrayOfPoints[currentNumberPoints][0] = x;
   ArrayOfPoints[currentNumberPoints][1] = y;
   currentNumberPoints++;
}

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 ( currentNumberPoints>1 ) {
      glBegin( GL_LINE_STRIP );
      for ( i=0; i<currentNumberPoints; i++ ) {
         glVertex2f( ArrayOfPoints[i][0], ArrayOfPoints[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<currentNumberPoints; i++ ) {
      glVertex2f( ArrayOfPoints[i][0], ArrayOfPoints[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.
   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)
{
   frameHeight = (h>1) ? h : 2;
   frameWidth = (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(keysFunction);
   glutMouseFunc(myMouseFunc);
   glutMainLoop();

   return 0;               // This line is never reached
}


ConnectDots.h:
Code:
/*
* ConnectDots.h
*
* Author: Samuel R. Buss
*
* Software accompanying the book
*      3D Computer Graphics: A Mathematical Introduction with OpenGL,
*      by S. Buss, Cambridge University Press, 2003.
*
* Software is "as-is" and carries no warranty.  It may be used without
*   restriction, but if you modify it, please change the filenames to
*   prevent confusion between different versions.
* Bug reports: Sam Buss, [email protected].
* Web page: http://math.ucsd.edu/~sbuss/MathCG
*/

// Function prototypes

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);






Attachments:
taaa.GIF
taaa.GIF [ 12.31 KiB | Viewed 10348 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Connect Dots
 How to connect two computers and connect them with Internet?     -  
 connect to MYSQL from ASP     -  
 connect SSIS package with ASP.net     -  
 How to connect database with Applets     -  
 How to Connect to MySql Server     -  
 HELP CONNECT SQL SERVER 2005 WITH PHP 5.2.6     -  
 how to connect applet with excel     -  
 problems to connect to active directory     -  
 how to solve that isssue i m nt able to connect cam and code     -  
 clients cannot connect to server on another machine     -  



Topic Tags

C++ Graphics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com