Jump to content

Rendering help for a TESR: a laser beam


Recommended Posts

I would like to render a thick beam of light. So essentially this should be a quad that is oriented in such a way that it always faces the camera. But it should start at some point in 3D and end at another point. And it should also be perspective correct. I have this code so far:


    private void drawLine(Coordinate c1, Coordinate c2, float width) {
        Tessellator tessellator = Tessellator.instance;

        tessellator.setColorRGBA(255, 255, 255, 128);

        GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);

        // Calculate the start point of the laser
        float mx1 = c1.getX() + .5f;
        float my1 = c1.getY() + .5f;
        float mz1 = c1.getZ() + .5f;
        Vector start = new Vector(mx1, my1, mz1);

        // Calculate the end point of the laser
        float mx2 = c2.getX() + .5f;
        float my2 = c2.getY() + .5f;
        float mz2 = c2.getZ() + .5f;
        Vector end = new Vector(mx2, my2, mz2);
        // Given the two points above I would like to render a quad that
        // always faces the camera. i guess I need to use things like
        // this.renderManager.playerViewY


        Vector p1 = ...;
        Vector p2 = ...;
        Vector p3 = ...;
        Vector p4 = ...;

        drawQuad(tessellator, p1, p2, p5, p6);
        drawQuad(tessellator, p2, p1, p6, p5);
        drawQuad(tessellator, p8, p7, p4, p3);
        drawQuad(tessellator, p7, p8, p3, p4);




So basically I need something for the '...'. Anyone who can help me with the correct formula to get a correct looking laser beam like that?



Link to comment
Share on other sites



What you're trying to find is the plane which is normal (perpendicular) to the line of vision from the player and which also includes the laser beam.  This is a bit like holding a ruler so that the flat face of the ruler is directly facing you so you can read the numbers along the long edge.  You know the vector direction that the long edge of the ruler is facing in, that's the laser beam start and end points, but in order to draw the ruler properly you need to calculate the vector direction of the short edge of the ruler.  (The thickness of the ruler is ignored)


So you have three points:

player eye position at P [xp, yp, zp]

start of beam at S [xs, ys, zs]

end of beam at E [xe, ye, ze]


You need to create the two vectors PS and SE

PS = [xs-xp, ys-yp, zs-zp]

SE = [xe-xs, ye-ys, ze-zs]

then find the cross product of PS and SE (Vec3 class has a crossproduct function) and normalise it (also a method of Vec3)

this gives you a unit length vector that is perpendicular to both the line of sight from the player as well as the laser beam itself.

If you call this vector say "acrossBeam" you draw a quad with the four points:

S plus half beam width * acrossBeam

S minus half beam width * acrossBeam

E plus half beam width * acrossBeam

E minus half beam width * acrossBeam

The larger your value of half beam width, the wider the laser beam will appear.


How much sense this makes to you probably depends on whether you've done much vector math before...  a bit of google will help if necessary :)










Link to comment
Share on other sites

  • 7 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.