Skip to content

Instantly share code, notes, and snippets.

@patriciogonzalezvivo
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patriciogonzalezvivo/c2d0fa8706586b64a5ea to your computer and use it in GitHub Desktop.
Save patriciogonzalezvivo/c2d0fa8706586b64a5ea to your computer and use it in GitHub Desktop.

Simple Quad

glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(width, 0); glVertex3f(width, 0, 0);
glTexCoord2f(width, height); glVertex3f(width, height, 0);
glTexCoord2f(0,height);  glVertex3f(0,height, 0);
glEnd();

Dot line

    glEnable(GL_LINE_STIPPLE);
    glLineStipple(1, 0x1111);
	...
    glDisable(GL_LINE_STIPPLE);

Unproject

glm::vec3 unProject(const glm::vec3 &_loc){
	GLdouble x = 0.0;
	GLdouble y = 0.0;
	GLdouble z = 0.0;
    
    	GLint viewport[4];
	GLdouble mvmatrix[16], projmatrix[16];
    
	glGetIntegerv(GL_VIEWPORT, viewport);
	glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
    
	gluProject(_loc.x, _loc.y, _loc.z,
			   mvmatrix, projmatrix, viewport,
			   &x, &y, &z);
    
    	return glm::vec3(x,y,z);
}

Reference:

First do this (copied from OpenGL_Programming):

glGetIntegerv( GL_VIEWPORT, viewport );
glGetDoublev( GL_MODELVIEW_MATRIX, mvmatrix );
glGetDoublev( GL_PROJECTION_MATRIX, projmatrix );
realy = viewport[3] - (GLint) y - 1;
gluUnProject( (GLdouble) x, (GLdouble) realy, 0.0, mvmatrix,
projmatrix, viewport, &wx1, &wy1, &wz1 );
gluUnProject( (GLdouble) x, (GLdouble) realy, 1.0, mvmatrix,
projmatrix, viewport, &wx2, &wy2, &wz2 );

You then have the endpoints of a line that represents all 3D places that mouse click could have been. Then, for each point, calculate the distance between this line and the point. Find the minimum, and you're done. In the case where two points tie, or are both reasonably close to zero distance from the line, then simply find which of the two are closest to (wx1, wy1, wz1), which represents the point on the hither plane (the closest one to the eye is probably the one you clicked on.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment