PDA

View Full Version : glselection



Peacekeeper3
06-28-2000, 04:27 PM
Hi Im having trouble with glselection using ortho projection. I am developing a tile based engine. The code below renders a map 64 x 64. each tile is 32 x 32 pixals.

With this code I am able to correctly get the x coordinate for the tile in the map, but the y coordinate is messed up.

Im gonna try to explain it. the y axis for the mouse runs from top to bottom. 0 - 480.

the selection code interprets the mouse position as this.

0 10
1 9
2 8
3 7
4 6
5 5
6 4
7 3
8 2
9 1
10 0

anyway here is the code I am using..

void SelectObject(void)
{
int x,
y;

GLfloat tempx,
tempy;

//initialize name stack
glInitNames();
glPushName(0);

glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


//draw names map
for(y = 0 ; y < 65 ; y++)
{
for(x = 0 ; x < 65 ; x++)
{
tempx = x + camera.x;
tempy = y + camera.y;

//only draws viewable tiles
if((tempx >= -1) && (tempx <= 20) && (tempy >= -1) && (tempy <= 20))
{
//stores name as x.y
glLoadName(x * 100 + y);
glBegin(GL_QUADS);
glVertex3d(tempx * 32 , tempy * 32, 0); //Top Left
glVertex3d(tempx * 32 + 32, tempy * 32, 0); //Top Right
glVertex3d(tempx * 32 + 32, tempy * 32 + 32, 0); //Bottom Right
glVertex3d(tempx * 32 , tempy * 32 + 32, 0); //Bottom Left
glEnd();
}
}
}
}


#define BUFFER_LENGTH 64
void ProcessSelection(int xPos, int yPos)
{
float temp,
tempy;

// Space for selection buffer
GLuint selectBuff[BUFFER_LENGTH];

// Hit counter and viewport storage
GLint hits, viewport[4];

// Set up selection buffer
glSelectBuffer(BUFFER_LENGTH, selectBuff);

// Get the viewport
glGetIntegerv(GL_VIEWPORT, viewport);

// Switch to projection and save the matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();

// Change render mode
glRenderMode(GL_SELECT);

// Establish new clipping volume to be unit cube around
// mouse cursor point (xPos, yPos) and extending two pixels
// in the vertical and horizontal direction
glLoadIdentity();
gluPickMatrix(xPos, yPos, 2,2, viewport);

// Apply perspective matrix

glOrtho(0.0f,640,480,0.0f,-1.0f,1.0f);

// Draw the scene
SelectObject();

// Collect the hits
hits = glRenderMode(GL_RENDER);

// If a single hit occurred, display the info.
if(hits == 1)
{
temp = float(selectBuff[3]);
tileid.x = int(floor(temp / 100));
tempy = ((temp / 100) - tileid.x) * 100;
tileid.y = int(tempy);
}

// Restore the projection matrix
glMatrixMode(GL_PROJECTION);

// Go back to modelview for normal rendering
glMatrixMode(GL_MODELVIEW);

// Restore the matrix state
glPopMatrix(); // Modelview matrix

}


[This message has been edited by Peacekeeper3 (edited 06-28-2000).]