PDA

View Full Version : a selection problem



romb
08-08-2001, 02:39 AM
here is a program: switch "a" to pass camera moving to selection. when you translate camera you can select the triangle and the
repere but when you rotate it only the repere is selectable. Can someone help me please ?

/************************************************** ************************************

#include <gl/glut.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

void MAT_Mult(float mat1[3][3],float mat2[3][3],float dest[3][3]);
void Vect_Mult(float vect[3],float mat2[3][3],float dest[3]);
void InitGL();
void Reshape(int width, int height);
void Draw();
void keyboard(unsigned char key,int x,int y);
void glutMotion(int x, int y) ;
void glutMouse(int button, int state, int x, int y) ;

//************************************************** ***
float rot[3] = {0,0,0};
float eye[3] = {0,0,50};
int oldX = -13;
int oldY = -13;
int mState = GLUT_UP;
int mButton = -1;
int Win1;
double a=0,b=0;
int active=1;
float FOV=45.0,coor[3]={0,0,0},view[3]={0,0,50};
float wheight,wwidth;
float d,ro,uux[3],uuy[3]={0,-1,0},uuz[3],mpas[3];
char str[256];
//************************************************** ***
void OutputString ( float x, float y, char *string )
{
int length;

length = strlen ( string );
glRasterPos2f( x, y );
for (int i = 0; i < length; i++ )
glutBitmapCharacter ( GLUT_BITMAP_9_BY_15, string[i] );
}
//************************************************** ************************************************** *
void Reshape(int width, int height)
{
wwidth=width;wheight=height;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,float(width)/float(height),0,2000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-0.5);
}
//************************************************** ************************************************** *

void Dessine(GLenum mode)
{
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glTranslatef(0,50,0);
if (mode==GL_SELECT) glLoadName(1);
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);
glVertex3f(0,0,0);glVertex3f(5,0,0);
glVertex3f(0,0,0);glVertex3f(0,10,0);
glVertex3f(0,0,0);glVertex3f(0,0,15);
glEnd();
glColor3f(0.0,1.0,1.0);

glPushMatrix();

glTranslatef(100,0,0);
if (mode==GL_SELECT) glLoadName(2);
glColor3f(1.0,1.0,0.0);
glBegin(GL_LINES);

glVertex3f(-10,-10,-10);glVertex3f(-5,0,0);
glVertex3f(-5,0,0);glVertex3f(0,-10,0);
glVertex3f(0,-10,0);glVertex3f(-10,-10,-10);
glEnd();
glPopMatrix();
glScalef(3,3,3);
if (mode==GL_SELECT) glLoadName(3);
glColor3f(0.0,1.0,0.0);
glutWireIcosahedron();
glPopMatrix();
}
//************************************************** ************************************************** *
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (-eye[0], -eye[1], -eye[2]-50);
glRotatef (rot[0], 1.0, 0.0, 0.0);
glRotatef (rot[1], 0.0, 1.0, 0.0);
glRotatef (rot[2], 0.0, 0.0, 1.0);
Dessine(GL_RENDER);
glFlush();
}
//************************************************** ************************************************** *
void InitGL()
{
glClearColor(0,0,0,0);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glDepthRange(0.0,1.0);

}
//************************************************** ************************************************** *
void afficheSel(GLint hits, GLuint buffer[])
{
/*unsigned*/ int i, j;
GLuint names, *ptr;
printf ("hits = %d\n", hits);
glColor3f(1.0,1.0,1.0);
if (hits==0) OutputString ( -10,-10,"Rien" );
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 %u;", *ptr); ptr++;
printf (" z2 is %u\n", *ptr); ptr++;
printf (" the name is ");
OutputString ( -10,-10," " );
for (j = 0; j < names; j++)
{ /* for each name */

if (*ptr==1) {glColor3f(1.0,0.0,0.0);OutputString ( -10,-10,"Repere" );}
if (*ptr==2) {glColor3f(1.0,1.0,0.0);OutputString ( -10,-10,"Triangle" );}
if (*ptr==3) {glColor3f(0.0,1.0,0.0);OutputString ( -10,-10,"Iso" );}
printf ("%d ", *ptr); ptr++;
}
printf ("\n");
}

}
//************************************************** ************************************************** *

#define BUFSIZE 512
void pickObjets(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(-1);

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);
gluPerspective(45.0,(wwidth)/(wheight),0,2000);
Dessine(GL_SELECT);
glPopMatrix ();
glFlush ();

hits = glRenderMode (GL_RENDER);
afficheSel (hits, selectBuf);
}
//************************************************** ************************************************** *
void keyboard(unsigned char key,int x,int y)
{
switch (key) {
case 27:
exit(0);
case 'a':
active=active*-1;
break;
case 'b':

break;
}
}
//************************************************** ************************************************** *
void glutMouse(int button, int state, int x, int y)
{
if(state == GLUT_DOWN)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
mState = GLUT_DOWN;
mButton = GLUT_LEFT_BUTTON;
oldX = x;
oldY = y;
if (active==-1)
{
OutputString ( 20,20,"romain" );
pickObjets(x,y);
}
break;
case GLUT_RIGHT_BUTTON:
mState = GLUT_DOWN;
mButton = GLUT_RIGHT_BUTTON;
oldX = x;
oldY = y;
break;

}
} else if (state == GLUT_UP)
{
mState = GLUT_UP;
mButton = -1;
}

}
//************************************************** ************************************************** *

void glutMotion(int x, int y)
{
if (active==1)
{
OutputString(10,10,"Motion");
if (mState == GLUT_DOWN && mButton == GLUT_LEFT)
{
rot[0] -= ((oldY - y) * 180.0f) / 100.0f;
rot[1] -= ((oldX - x) * 180.0f) / 100.0f;

}
if (mState == GLUT_DOWN && mButton == GLUT_RIGHT_BUTTON)
{
eye[2] -= ((oldY - y) * 180.0f) / 100.0f;
}
oldX = x;
oldY = y;
glutPostRedisplay();
}

}