glapp
Class GLLine

java.lang.Object
  extended by glapp.GLLine

public class GLLine
extends java.lang.Object

GLLine creates a quad-strip line from a series of points. To use:

 draw() {
    GLLine L = new GLLine();
    L.setWidth(20);
    L.point(0,0);
    L.point(5,5);
    L.point(0,10);
    L.point(5,15);
    L.close();   // if you want a closed shape
    L.draw();
 }
 

NOTE: gets ugly when points are very close, or reverse direction sharply. Uses my home-grown trigonometry... probably not the best way to do this, but it works in most cases.

ALSO NOTE: the line will be drawn with the active texture. To draw an untextured line, call activateTexture(0) or disable textures (GL11.glDisable(GL11.GL_TEXTURE_2D) before calling line.draw().


Constructor Summary
GLLine()
           
GLLine(float lineWidth)
           
 
Method Summary
 void clear()
          reset the line
 void close()
          Close the line by adding a line segment that connects the last point to the first.
 void draw()
          draw the mesh geometry created by rebuild()
 void draw3D(float height)
          draw the mesh geometry created by rebuild() as a 3D shape (extrude the line shape along the Z axis).
 void drawV(GL_Vector v, float x, float y)
          for debugging
 GL_Vector getNormal(GL_Vector a, GL_Vector b, float width)
          Make an average normal to the two vectors with the right length to make a join for a line wide.
 float getNormalLen(GL_Vector v1, GL_Vector v2, float width)
          Return the length of the normal needed to make a join in a line of the given width.
 int getNumPoints()
          return number of points in this line
 GL_Vector getPoint(int n)
          return the given xy point
 float getWidth()
          return line width
static GLLine makeCircle(float x, float y, float radius, int numSegments, float linewidth)
          return a GLLine shaped into a circle.
 int makeDisplayList()
          render the line into a display list and return the list ID.
 void makeLine()
          create the line geometry from point positions
static GLLine makeRectangle(float x, float y, float w, float h, float linewidth)
          return a GLLine shaped into a rectangle.
 void makeSegment(GL_Vector p1, GL_Vector p2, GL_Vector prevV, GL_Vector postV)
          Draw one segment from a line.
 void point(float x, float y)
          Add an xy point to the line.
 void rebuild()
          regenerate the line geometry from the added points.
 void setUVfactor(float tilefactor)
          set texture tiling factor.
 void setWidth(float w)
          set line width
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GLLine

public GLLine()

GLLine

public GLLine(float lineWidth)
Method Detail

clear

public void clear()
reset the line


point

public void point(float x,
                  float y)
Add an xy point to the line.


close

public void close()
Close the line by adding a line segment that connects the last point to the first.


setWidth

public void setWidth(float w)
set line width


getWidth

public float getWidth()
return line width


getNumPoints

public int getNumPoints()
return number of points in this line


getPoint

public GL_Vector getPoint(int n)
return the given xy point


setUVfactor

public void setUVfactor(float tilefactor)
set texture tiling factor. Texture will repeat every units. If a line is 50 units long, and uvfactor is 10, then the texture will repeat five times along the length of the line.


rebuild

public void rebuild()
regenerate the line geometry from the added points.


makeLine

public void makeLine()
create the line geometry from point positions


makeSegment

public void makeSegment(GL_Vector p1,
                        GL_Vector p2,
                        GL_Vector prevV,
                        GL_Vector postV)
Draw one segment from a line. The segment is a quad created by finding normals to the line vector. The line is drawn in the XY plane. In the diagram below, qv1 thru qv4 are the four quad vectors. N1 and N2 are normals to the line vector and Z axis. The normals are averaged with the previous and next line segments so that this segment will join gracefully with the previous and next.
      qv4   p2    qv3
        +----o----+
         -N2 |  N2
             |
             |          line segment p1-p2
             |
         -N1 |  N1
        +----o----+
      qv1   p1    qv2
 

Parameters:
p1 - start point of line segment
p2 - end point of line segment
prevV - direction of previous line segment
postV - direction of next line segment

getNormal

public GL_Vector getNormal(GL_Vector a,
                           GL_Vector b,
                           float width)
Make an average normal to the two vectors with the right length to make a join for a line wide.
           + - - - - - -
           |\ normal
           | \
           |  o-----------o line b
              |
              |
              |
              o line a
 

Parameters:
a -
b -
Returns:

getNormalLen

public float getNormalLen(GL_Vector v1,
                          GL_Vector v2,
                          float width)
Return the length of the normal needed to make a join in a line of the given width.

Parameters:
v1 -
v2 -
width -
Returns:

drawV

public void drawV(GL_Vector v,
                  float x,
                  float y)
for debugging

Parameters:
v -
x -
y -

draw

public void draw()
draw the mesh geometry created by rebuild()


draw3D

public void draw3D(float height)
draw the mesh geometry created by rebuild() as a 3D shape (extrude the line shape along the Z axis).


makeDisplayList

public int makeDisplayList()
render the line into a display list and return the list ID.


makeRectangle

public static GLLine makeRectangle(float x,
                                   float y,
                                   float w,
                                   float h,
                                   float linewidth)
return a GLLine shaped into a rectangle.


makeCircle

public static GLLine makeCircle(float x,
                                float y,
                                float radius,
                                int numSegments,
                                float linewidth)
return a GLLine shaped into a circle. Can also make triangle, square, pentagon, etc. by giving a low number for numSegments.