Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following is the complete code, the Point3D is a class only with three value:x,y,z represent the coordination.

First thing is draw some points on the screen with the mouse. Then connect them with line. Then I just want to pick up these lines
But it does not work.

Can somebody help me Please? I have struggled for several days. I am exhausted!!!


C++
#include "StdAfx.h"
#include <Windows.h>
#include <gl/GL.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<time.h>
#include<iostream>  
#include<math.h>
#include<vector>

#include "Point3D.h"

using namespace std;

#define BUFSIZE 512

int screenWidth=340;
int screenHeight=280;


vector<Point3D> P;



static int pickUpModel=0; //1: mouse pickup work  0:close the function of mouse pick up

 void  drawSpere(float x, float y,float z);
 void drawLine2D(GLenum);
  void pickUp(int x, int y);
  void processHits (GLint hits, GLuint buffer[]);
void myInit(void) 

{ 
	glClear (GL_COLOR_BUFFER_BIT);
      glClearColor(1.0, 1.0, 1.0, 0.0);
     glShadeModel(GL_SMOOTH);   
   
  
}
void myDisplay(void)
{
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
   glClearColor(1.0, 1.0, 1.0, 0.0);
   glColor3f (0.0, 1.0, 1.0);
 
}
void myReshape (int w, int h)
{

   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
 //  gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
   if(w<=h)
		  glOrtho(0,screenWidth*2, 0,  screenHeight*2*(GLfloat)h/w,-screenWidth,screenWidth);
	 else
		   glOrtho(0,screenWidth*2*(GLfloat)w/h,0,screenHeight*2,-screenWidth,screenWidth);
   glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   
}

void myMouse(int button,int state,int x,int y)
{
		
	Point3D X;
    if((state==GLUT_DOWN)&&(button==GLUT_LEFT_BUTTON)&&(pickUpModel==0))
    {

		         X.x=x;
			X.y=screenHeight*2-y; 
			X.z=0;
			P.push_back(X);   
			pointNum++;
			for(int i=0;i<pointNum;i++)
			   drawSpere(P[i].x, P[i].y, P[i].z);
		 
            
		   cout<<x<<"  "<<y<<endl;
	}

	 if((state==GLUT_DOWN)&&(button==GLUT_LEFT_BUTTON)&&(pickUpModel==1))
    {
		pickUp(x,y);
	 }
//	glutSwapBuffers();
	 glFlush();
}

void myMenu(int value){
   if(value==1)
   {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
	 
	  P.clear();
	  pointNum=0;
	  idx=0;
	  glFlush();
		
   }
   if(value==2)  
   {
	  drawLine2D(GL_RENDER);

   }
   if(value==3)
   {  
   }
   if(value==4)
	   pickUpModel=1;
   if(value==5)
	   pickUpModel=0;
	 
}
 void  drawSpere(float x, float y,float z)
 {
     glPushMatrix();
		 
   glTranslatef (x, y, 0.0);
   glRotatef (5, 0.0, 1.0, 0.0);
   glutWireSphere(5, 100, 80);    /* draw smaller planet */
   glPopMatrix();
  
 
 }

 void drawLine2D(GLenum mode)
 {
    glLineWidth(5);
	 for(int i=0;i<pointNum;i++){
		 if(mode==GL_SELECT)
		  glPushName(i+1);
		 glBegin(GL_LINE_LOOP);
	           glVertex2f(P[i%pointNum].x, P[i%pointNum].y);
		    glVertex2f(P[(i+1)%pointNum].x, P[(i+1)%pointNum].y);
		 glEnd();
 }
	 	glFlush();
 }

 
 void pickUp(int x, int y)
{
   GLuint selectBuf[BUFSIZE];
   GLint hits;
   GLint viewport[4];


   glGetIntegerv (GL_VIEWPORT, viewport);

   glSelectBuffer (BUFSIZE, selectBuf);
   (void) glRenderMode (GL_SELECT);

   glInitNames();
   glPushName(0);

   glMatrixMode (GL_PROJECTION);
   glPushMatrix ();
   glLoadIdentity ();
/*  create 5x5 pixel picking region near cursor location	*/
   gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 
                  5.0, 5.0, viewport);
   gluOrtho2D (0.0, screenWidth*2.0, 0.0, screenHeight*2.0);


    drawLine2D(GL_SELECT);

   glMatrixMode (GL_PROJECTION);
   glPopMatrix ();
   glFlush ();

   hits = glRenderMode(GL_RENDER);
   processHits (hits, selectBuf);


} 

void processHits (GLint hits, GLuint buffer[])
{
   unsigned int i, j;
   GLuint names, *ptr;

   printf ("hits = %d\n", hits);
   ptr = (GLuint *) buffer;
   for (i = 0; i < hits; i++) {	/*  for each hit  */
      names = *ptr;
      printf (" number of names for hit = %d\n", names); ptr++;
      printf("  z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
      printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
      printf ("   the name is ");
      for (j = 0; j < names; j++) {	/*  for each name */
         printf ("%d ", *ptr); ptr++;
      }
      printf ("\n");
   }
}



int main(int argc, char** argv)
{
   glutInit(&argc, argv);
  // glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   glutInitDisplayMode (GLUT_SINGLE| GLUT_RGB);
   glutInitWindowSize (screenWidth*2, screenHeight*2); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   myInit();
   glutDisplayFunc(myDisplay); 
   glutReshapeFunc(myReshape);
    glutMouseFunc(myMouse);
		int id=glutCreateMenu(myMenu);
	glutAddMenuEntry("Clear Screen",1);
	glutAddMenuEntry("InitialDraw",2);
	glutAddMenuEntry("iter",3);
	glutAddMenuEntry("pickUpOpen",4);
	glutAddMenuEntry("pickUpClose",5);
		glutAttachMenu(GLUT_RIGHT_BUTTON); 
   glutMainLoop();
   return 0;
}


What I have tried:

I just copy the OpenGL redbook(7th) chapter 13 code and modify to choose line,but it just doesn't work!
Posted
Updated 16-Aug-16 17:56pm
Comments
Beginner Luck 16-Aug-16 20:56pm    
got error message??

1 solution

I know what is the matter!
The gluOrtho2D (0.0, screenWidth*2.0, 0.0, screenHeight*2.0);should keep the same in the whole code, while I change it in the reshape funcion. When I uniform the code, it works!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900