Hello, the following code samples will be refered to:
RESOURCES
...
//~Object Selection IDs
//The element specification screen
#define SELECT_FIRE 106
#define SELECT_WATER 107
#define SELECT_EARTH 108
#define SELECT_AIR 109
//Sphere view
#define SELECT_SPHERE 110
...
DRAWING SECTION
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glInitNames();
glPushName(0);
...
if(view == 0) //Intro View
{
glLoadName(SELECT_WATER);
glBegin(GL_LINES);
glEnd();
glPushMatrix();
RenderSphere(2, 0.0, 0.0, 0.0, 0.7, 35,-1.5, 1.0, -5);
glPopMatrix();
glEnd();
glLoadName(SELECT_FIRE);
glBegin(GL_LINES);
glEnd();
glPushMatrix();
RenderSphere(5, 0.0, 0.0, 0.0, 0.7, 35,-1.5, -1.0, -5);
glPopMatrix();
glEnd();
glLoadName(SELECT_EARTH);
glBegin(GL_LINES);
glEnd();
glPushMatrix();
RenderSphere(4, 0.0, 0.0, 0.0, 0.7, 35,1.5, -1.0, -5);
glPopMatrix();
glEnd();
glLoadName(SELECT_AIR);
glBegin(GL_LINES);
glEnd();
glPushMatrix();
RenderSphere(3, 0.0, 0.0, 0.0, 0.7, 35,1.5, 1.0, -5);
glPopMatrix();
glEnd();
glPopName();
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, texture[1]);
particle_generate(-1.9, 1.4, -6, 1.0, 0.0, 0.0);
particle_generate(-1.9, -1.4, -6, 0.5, 0.25, 0.0);
particle_generate(1.9, 1.4, -6, 0.0, 0.0, 1.0);
particle_generate(1.9, -1.4, -6, 1.0, 1.0, 1.0);
glDisable(GL_BLEND);
}
if(view == 1) //Sphere View
{
glLoadName(SELECT_SPHERE);
glBegin(GL_LINES);
glEnd();
glPushMatrix();
RenderSphere(0, 0.0, 0.0, 0.0, 1.5, 35,0.0, 0.0, -5);
glPopMatrix();
glEnd();
glPopName();
}
SELECTION EVALUATION SECTION
int select(int x, int y)
{
int objectsFound = 0;
int viewportCoords[8] = {0};
unsigned int selectBuffer[64] = {0};
glSelectBuffer(64, selectBuffer);
glGetIntegerv(GL_VIEWPORT, viewportCoords);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glRenderMode(GL_SELECT);
glLoadIdentity();
gluPickMatrix(x, viewportCoords[8] - y, 2, 2, viewportCoords);
gluPerspective(45.0f,(float)g_rRect.right/(float)g_rRect.bottom,0.1f,150.0f);
glMatrixMode(GL_MODELVIEW);
DrawGLScene();
objectsFound = glRenderMode(GL_RENDER);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
if (objectsFound > 0)
{
unsigned int lowestDepth = selectBuffer[1];
int selectedObject = selectBuffer[4];
for(int i = 1; i < objectsFound; i++)
{
if(selectBuffer[(i * 4) + 1] < lowestDepth)
{
lowestDepth = selectBuffer[(i * 4) + 1];
selectedObject = selectBuffer[(i * 4) + 3];
}
}
return objectsFound;
}
return 0;
}
LSRESULT CALLBACK CONTENTS FOR CLICKING
...
case WM_LBUTTONDOWN:
{
char what[15]={0};
mouse_x = LOWORD(lParam);
mouse_y = HIWORD(lParam);
objectID = select(mouse_x, mouse_y);
_itoa(objectID, numbuff, 10);
strcat(what, numbuff);
MessageBox(NULL, what, "Click", MB_OK);
switch(objectID)
{
case SELECT_FIRE:
{
MessageBox(NULL, "FIRE", "Click", MB_OK);
break;
}
case SELECT_WATER:
{
MessageBox(NULL, "WATER", "Click", MB_OK);
break;
}
case SELECT_EARTH:
{
MessageBox(NULL, "EARTH", "Click", MB_OK);
break;
}
case SELECT_AIR:
{
MessageBox(NULL, "AIR", "Click", MB_OK);
break;
}
case SELECT_SPHERE:
{
MessageBox(NULL, "The Sphere", "Click", MB_OK);
break;
}
}
break;
}
...
Problem: After compiling the above syntax, whenever I click anywhere on the screen, a zero ,contents of "objectID", is reported to me. Since that works, I decide to click on a sphere in order to get the following: fire, water, air, or earth message box, but a zero is still reported to me. The program doesn't seem to recoginize that an ID is assigned to whatever I drew, I don't know why. If I go to the second view, and click on another sphere, what supposly was to report the message box, "The Sphere", reported a 1 that indicates it is assigned to the ID of fire, but it doesn't activate the "fire" case. I tried to find out why this does not work for about 3 days now, but to no avail. I tried changing the IDs to 1,2,3,4, but they did not work, can anyone tell me why?
Thanks a lot!
[edited by - Xiachunyi on May 3, 2003 3:51:43 PM]
[edited by - Xiachunyi on May 3, 2003 3:53:01 PM]