Archived

This topic is now archived and is closed to further replies.

Selection, Name stack, Hits - Newbie question

This topic is 5781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I''ve tested my code pretty thoroughly and hits and everything is working. The only thing that isn''t working is the name stack.
  
GLint viewport[4];
	double ratio;

	glSelectBuffer(BUFSIZE,selectBuf);

	glGetIntegerv(GL_VIEWPORT,viewport);

	glRenderMode(GL_SELECT);

	glInitNames();

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	gluPickMatrix(mouse_x,viewport[3]-mouse_y,5,5,viewport);
	ratio = (viewport[2]+0.0) / viewport[3];
	gluPerspective(45,ratio,0.1,1000);
	glMatrixMode(GL_MODELVIEW);
  
that is more or less from a book. Next I draw ''tiles'' each with its own name using
  
	glPushName(GROUND);

	for ( X = 0; X < MAP_SIZE; X += STEP_SIZE )
	{
		for ( Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )
		{
			glPushName(X*MAP_SIZE+Y);

			x = X;					
			y = 0;	
			z = Y;							
			glColor3f(1.0f, 1.0f, 1.0f);
			glVertex3i(x, y, z);		

			...etc...etc

			//glPopName();

		}
	}
	glEnd();

	//glPopName();

  
Is there anything wrong with that ? As far as I can tell from the examples PopName should be used but if I use it it will erase the name on top of the stack which I don''t want (I don''t think). Now when performing to see if I have any hits, I get 1 hit, but the names in the hit are always 0. If I put glPushName(-1); during initialisation it will return 1 name but it is always -1, so why does that count during initialisation but not during rendering ? and yes I''ve set the Render mode to SELECT. Any idea''s ? thanks in advance!

Share this post


Link to post
Share on other sites
Does it actually only return one name (GROUND) or two names (GROUND & whatever) per hit ?

Once you have pushed your first name, you can try using glLoadName()

Could you post a copy of your hit record ?

And... do you remember that nothing is returned until you come back to glRenderMode(GL_RENDER) ?

Edit: once you are done, you can erase the last name on the stack.

Edited by - Fruny on February 18, 2002 4:16:11 AM

Share this post


Link to post
Share on other sites
It is returning 1 hit with 0 names (if that is possible).

I''ll quickly try using glLoadName() and see if that makes a difference, thanks.

and yeah I''m switching back to GL_Render

Share this post


Link to post
Share on other sites
Okay when I inistialise I PushName(-1), then I use glLoadName to set the name for each quad/polygon and I always get the same results no matter what I click.

Hits = 1
number of names for hit = 1

z1 is 4248731479;
z2 is 4250548850

the name is -1


  
void processHits (GLint hits, GLuint buffer[])
{
GLint i, j;

GLuint names, *ptr;
Output("Hits = %d", hits);
ptr = (GLuint *) buffer;


for (i = 0; i < hits; i++)
{ /* for each hit */
names = *ptr;

Output(" number of names for hit = %d\n", names); ptr++;
Output(" z1 is %u;", *ptr); ptr++;
Output(" z2 is %u\n", *ptr); ptr++;

for (j = 0; j < names; j++)
{ /* for each name */

Output(" the name is %d ", *ptr); ptr++;
Output(" ");
Output(" ");
}

}
}


Share this post


Link to post
Share on other sites
More checking

Structure of the hit record (Red Book, p536)
for each hit
- number of names N
- zmin
- zmax
- N names

The number of hits is the value returned by glRenderMode()

Edit: ok, you got it ;p

Edited by - Fruny on February 18, 2002 4:43:05 AM

Share this post


Link to post
Share on other sites
I don't know, you code seems ok...

more stupid questions
- are you indeed going through the for loops ?
- is your projection matrix really the same as in render mode ? (i.e. are you clicking on an object)
- do you have bad karma ?

correction : your projection matrix looks fine...

Edited by - Fruny on February 18, 2002 4:53:35 AM

Share this post


Link to post
Share on other sites
a) Yeah going through the loops as I can see them rendered and am pretty sure they are being rendered in SELECT mode too.

b)I believe so, because when I click on nothing, black background.. no hits are returned only when I click on a ground tile.

c) God yes! You have no idea what kind of problems I''ve been having. All of a sudden my news server has randomly started dropping posts, been getting 404 errors all over the place, and Gravity freezes on splash screen.

Share this post


Link to post
Share on other sites
Here is an overview of my process

  
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (mode == SELECT) {
startPicking();
}

glLoadIdentity();


// Set Camera Angle.

gluLookAt(cameraPos[0],
cameraPos[1],
cameraPos[2], // camera location

cameraLookAt[0],
cameraLookAt[1],
cameraLookAt[2], // looking at

0.0f,
1.0f,
0.0f); // up vector



// Update Keyboard input

// -----------------------

sinput.DInputUpdate(); //Update DirectInput to make sure it''s still alive


if(KEY_DOWN(DIK_ESCAPE)) //Check to see if the user wants to quit

PostQuitMessage(0);
// -----------------------



// Handle keyboard input

Game_Input();

Game_DrawScene();

//Game_DrawHUD();


glFlush();

if (mode == SELECT)
stopPicking();
else
s3d.BufferSwap(); //Finally, swap the buffers

Share this post


Link to post
Share on other sites
Just another test : try clicking at the edge of several tiles so that your 5x5 picking window gets several hits... and post the results back.

Share this post


Link to post
Share on other sites
Okay I found 1 problem, I messed up a stupid PopMatrix, PushMatrix which was messing up the naming during the tile rendering.

Now it is always returning 0.

Also I tried clicking on the cornor of a tile it only returned 1 result, but I think I have an idea now!

Ill let you know how it goes in a few mins.


Edit:

Nope, still always returning 1.
I may have my tree naming setup incorrectly, either that or I'm not naming correctly for each individual tile ?

Edited by - Darjk on February 18, 2002 5:23:47 AM


Edit #2:

Okay I'm not getting these results

Hits = 1
number of names for hit = 2

z1 is 4198027884;
z2 is 4216201336

the name is -1

the name is 0

It seems to be returning the -1 is just something I push onto the name stack and the 0 is the ground, still not giving me the tile name though

Edited by - Darjk on February 18, 2002 5:29:01 AM

Share this post


Link to post
Share on other sites
Honestly, I don''t know. As long as you are placing a name on the stack before drawing your primitives, that name should show up in the hit record if that primitive is rendered (within the restricted window from gluPickMatrix).

Which leads to a last test : remove the call to gluPickMatrix and let it render the whole scene... you should have N hits, where N is the total number of names you picked.

Share this post


Link to post
Share on other sites
Okay I finally figured it out.

The way I was drawing my tiles was incorrectly and I did not pick up on it

Basically I needed to put the glBegin(QUADS); inside the For loop instead of begining, drawing all tiles, then ending it.

I now get the following results:

Hits = 4
number of names for hit = 3

z1 is 4208480774;
z2 is 4216927619

the name is -1


the name is 0


the name is 5


number of names for hit = 3

z1 is 4198754167;
z2 is 4208480774

the name is -1


the name is 0


the name is 9


number of names for hit = 3

z1 is 4208480774;
z2 is 4216927619

the name is -1


the name is 0


the name is 1029


number of names for hit = 3

z1 is 4198754167;
z2 is 4208480774

the name is -1


the name is 0


the name is 1033


Thanks for all the help and testing suggestions.. very much appreciated!

You''ll probably hear from me in a couple days trying to work out what ever comes next
Thanks again Fruny!

Share this post


Link to post
Share on other sites