Skip to main content
GameDev.net gamedev.net
🔒 Locked

glselection

Started by Peacekeeper3 Jun 28, 2000 at 2:04 PM 0 replies 650+ views
Original Post
Peacekeeper3
Peacekeeper3
Hello I am developing an RTS engine in OpenGL. I hit a roadblock with my selection engine. This code is able to properly return the x tile coordinate. (the coordinate in the array not screen) but not the y tile coordinate. I have no idea what is wrong
    
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);
    glPopMatrix();

    // Go back to modelview for normal rendering

    glMatrixMode(GL_MODELVIEW);
       
    // Restore the matrix state

    glPopMatrix(); // Modelview matrix

}
    
Peacekeeper3
Peacekeeper3
Wont anyone help this extreme newbie?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.