Jump to content
  • Advertisement
Sign in to follow this  
Alessandro

OpenGL OpenGL ES picking: something is going wrong

This topic is 3591 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

First of all Merry Xmas everybody ! I'm trying to build a routine to pick objects in OpenGL ES 1.x which, unfortunately, doesn't support the selection buffer. Here is my routine code, which I tried to comment as most as I could: the problem is that it doesn't pick anything, i.e. the ray never intersects the bounding sphere around my object. I'd be glad if someone could really help, because it's quite some time I'm crashing my head on this: Definitions
[source code="cpp"]
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 320
#define NEAR -1
#define FAR 1
#define FOV 45
#define ASPECT  (float)SCREEN_WIDTH/(float)SCREEN_HEIGHT

Initial setup
[source code="cpp"]
(void)setupView
{
/* Sets up matrices and transforms for OpenGL ES */
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
// Clears the view with black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

Draw routine
[source code="cpp"]
(void)drawView
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0,0,-0.5);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);	/* draw my object */

/* get centered mouse coordinates between -1, 1 */
float centered_y = (SCREEN_HEIGHT - mouseX) - SCREEN_HEIGHT/2;
float centered_x = mouseY - SCREEN_WIDTH/2;
float unit_x = centered_x/(SCREEN_WIDTH/2);
float unit_y = centered_y/(SCREEN_HEIGHT/2);	

/* define ray start and ray end */
float near_height = (float) NEAR * (tan( FOV * 3.14 / 360.0 ));
float ray[4] = { (float)unit_x * near_height * ASPECT, (float)unit_y*near_height, 1.0, 0.0 };
float ray_start_point[4] = {0.0, 0.0, 0.0, 1.0};

/* Read the current modelview matrix into the array the_modelview */
GLfloat the_modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX, the_modelview);

/* invert the modelview */
inv_mv(inv_modelview, the_modelview);

/* build ray start vector */
vector3_t RayStart;
RayStart.x = inv_modelview[0]*ray_start_point[0] + inv_modelview[1]*ray_start_point[1] + inv_modelview[2]*ray_start_point[2] ;
RayStart.y = inv_modelview[4]*ray_start_point[0] + inv_modelview[5]*ray_start_point[1] + inv_modelview[6]*ray_start_point[2] ;
RayStart.z = inv_modelview[8]*ray_start_point[0] + inv_modelview[9]*ray_start_point[1] + inv_modelview[10]*ray_start_point[2] ;	

/* build ray end vector */
vector3_t RayEnd;
RayEnd.x = inv_modelview[0]*ray[0] + inv_modelview[1]*ray[1] + inv_modelview[2]*ray[2] ;
RayEnd.y = inv_modelview[4]*ray[0] + inv_modelview[5]*ray[1] + inv_modelview[6]*ray[2] ;
RayEnd.z = inv_modelview[8]*ray[0] + inv_modelview[9]*ray[1] + inv_modelview[10]*ray[2] ;		
	
/* subtract the two vectors to get the final ray direction vector */
vector3_t finalRay = RayEnd - RayStart;

/* test intersection with bounding sphere */
float sphere_radius = 0.25;	
GLfloat b = 2.0f * scalar_product(RayEnd ,finalRay);
GLfloat c = scalar_product(finalRay, finalRay) - sphere_radius * sphere_radius;
GLfloat d = b * b - 4.0f * c;
if ( d < 0.0f ) dbgString = [NSString stringWithFormat: @"NO"];
else
{
d= sqrtf(d);
GLfloat s0 = (d-b)*0.5f;
GLfloat s1 = (-d-b)*0.5f;
if ( s0 >=0.0f || s1 >= 0.0f ) 	dbgString = [NSString stringWithFormat: @"YES PICKED ! %f, %f", s0, s1];
else 	dbgString = [NSString stringWithFormat: @"NO PICKING %f, %f", s0, s1];
}
}

Function used to invert the matrix
[source code="cpp"]
void inv_mv(GLfloat ret[16], GLfloat mat[16] )
{
	ret[0 ] = mat[0];
	ret[1 ] = mat[4];
	ret[2 ] = mat[8];
	ret[3 ] = 0.0;
	ret[4 ] = mat[1];
	ret[5 ] = mat[5];
	ret[6 ] = mat[9];
	ret[7 ] = 0.0;
	ret[8 ] = mat[2];
	ret[9 ] = mat[6];
	ret[10] = mat[10];
	ret[11] = 0.0;
	ret[12] = -(mat[12] * ret[0] + mat[13] * ret[4] + mat[14] * ret[8]);
	ret[13] = -(mat[12] * ret[1] + mat[13] * ret[5] + mat[14] * ret[9]);
	ret[14] = -(mat[12] * ret[2] + mat[13] * ret[6] + mat[14] * ret[10]);
	ret[15] = 1.0;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!