Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a mesh looking almost like a cylinder, the cylinder goes through a mesh and continues on the other side. What I want to do is to draw the projection of the cylinder on the mesh. So if you look at the images down below. I just want draw the green color on the surface. Any ideas? Im working in opengl.

enter image description here

enter image description here

share|improve this question

You're talking about the cylinder going through a mesh. But your picture shows a much simpler scenario where you're projecting into simple quad. So the main question is, can you represent your surface with a plane?

If that's the case, then I think you may be able to work with a shadow matrix to achieve this. A shadow matrix is one that typically takes a ground plane and a light position and projects any geometry that you render into the ground plane, from the point of view of the light.

It was normally used in conjunction with a stencil buffer to render planar shadows of objects, but it should also work in your case, since it's going to project your mesh into the plane. I found the following OpenGL implementation here which I have not tested:

void shadowMatrix(GLfloat shadowMat[4][4], GLfloat groundplane[4], GLfloat lightpos[4])
    GLfloat dot;

    /* Find dot product between light position vector and ground plane normal. */
    dot = groundplane[X] * lightpos[X] +
          groundplane[Y] * lightpos[Y] +
          groundplane[Z] * lightpos[Z] +
          groundplane[W] * lightpos[W];

    shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
    shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
    shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
    shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];

    shadowMat[0][1] = 0.f - lightpos[Y] * groundplane[X];
    shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
    shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
    shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];

    shadowMat[0][2] = 0.f - lightpos[Z] * groundplane[X];
    shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
    shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
    shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];

    shadowMat[0][3] = 0.f - lightpos[W] * groundplane[X];
    shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
    shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
    shadowMat[3][3] = dot - lightpos[W] * groundplane[W];

Push this matrix before rendering the cylinder, and pop it afterwards. Draw with backface culling enabled. And if you want to limit the projected shape inside the quad's boundaries, look into how to use the stencil buffer.

share|improve this answer
thank you, but I cant represent the mesh with a plane, I just draw the picture that way – Merni Apr 12 '12 at 13:34
@Merni Then it becomes a lot harder, and I'm not sure how to help. As a starting point I'd suggest perhaps looking into how to use decals. – David Gouveia Apr 12 '12 at 13:40
thank you, I will look into that:) – Merni Apr 12 '12 at 14:00

I have a thought which might get you going.

You could take each polygon from your mesh and test to see where each edge intersects with the plane defined by your surface (you may have to get fancy to avoid duplicate testing on shared edges). The collection of these intersection points will help define a bounding area on your surface. This can then be used as a new mesh to render the intersection or You can run a pixel shader on your surface (or just a function on a texture) and look for pixels which are inside this bounding area (or just inside your original mesh if that's easier). To do this you may want to transform your collision points into texels, but how you would do that will depend largely on what surface your using.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.