Well sir, I got something, but it ain’t right. Perhaps you can find my mistake? I’m stumped atm.
I’m using this for a dragged selection box. Mouse coordinates (@click and present) are input. It appears to have some of the correct points, but part of it draws off to the origin. I was going to use GL_TRIANGLE_STRIP, but this seemed just as good for testing for now. I could be wrong.
public void drawDragBox(double mX, double mY, double mX2, double mY2)
{
float lineWidth = 4.6f;
Vector2f V1 = new Vector2f((float)mX, (float)mY);
Vector2f V2 = new Vector2f((float)mX, (float)mY2);
Vector2f V3 = new Vector2f((float)mX2, (float)mY2);
Vector2f V4 = new Vector2f((float)mX2, (float)mY);
Vector2f L1 = (V1.subtract(V2)).normalize(); //subtract V1 [b]FROM[/b] V2 and then normalize
Vector2f L2 = (V2.subtract(V3)).normalize();
Vector2f L3 = (V3.subtract(V4)).normalize();
Vector2f L4 = (V4.subtract(V1)).normalize();
Vector2f G1 = rotate90Degrees(L1.add(L2).normalize());
Vector2f G2 = rotate90Degrees(L2.add(L3).normalize());
Vector2f G3 = rotate90Degrees(L3.add(L4).normalize());
Vector2f G4 = rotate90Degrees(L4.add(L1).normalize());
double x1 = 1 / Math.sqrt(L4.dot(L1)*0.5+0.5);
double y1 = 1 / Math.sqrt(L1.dot(L2)*0.5+0.5);
double x2 = 1 / Math.sqrt(L1.dot(L2)*0.5+0.5); //the line theagentd's example was built for
double y2 = 1 / Math.sqrt(L2.dot(L3)*0.5+0.5);
double x3 = 1 / Math.sqrt(L2.dot(L3)*0.5+0.5);
double y3 = 1 / Math.sqrt(L3.dot(L4)*0.5+0.5);
double x4 = 1 / Math.sqrt(L3.dot(L4)*0.5+0.5);
double y4 = 1 / Math.sqrt(L4.dot(L1)*0.5+0.5);
//B1 = V2 + G1*lineWidth/2 * x >> V2.add(G1.scale(lineWidth/2 * x)
//B2 = V2 - G1*lineWidth/2 * x
//and
//B3 = V3 + G2*lineWidth/2 * y
//B4 = V3 - G2*lineWidth/2 * y
//assemble vertices for drawing triangles
Vector2f A1 = V1.add(G4.scale((float)(lineWidth/2 * x1)));
Vector2f A2 = (G4.scale((float)(lineWidth/2 * x1))).subtract(V1); //subtract FROM V1
Vector2f A3 = V2.add(G1.scale((float)(lineWidth/2 * y1)));
Vector2f A4 = (G1.scale((float)(lineWidth/2 * y1))).subtract(V2);
Vector2f B1 = V2.add(G1.scale((float)(lineWidth/2 * x2)));
Vector2f B2 = (G1.scale((float)(lineWidth/2 * x2))).subtract(V2);
Vector2f B3 = V3.add(G2.scale((float)(lineWidth/2 * y2)));
Vector2f B4 = (G2.scale((float)(lineWidth/2 * y2))).subtract(V3);
Vector2f C1 = V3.add(G2.scale((float)(lineWidth/2 * x3)));
Vector2f C2 = (G2.scale((float)(lineWidth/2 * x3))).subtract(V3);
Vector2f C3 = V4.add(G3.scale((float)(lineWidth/2 * y3)));
Vector2f C4 = (G3.scale((float)(lineWidth/2 * y3))).subtract(V4);
Vector2f D1 = V4.add(G3.scale((float)(lineWidth/2 * x4)));
Vector2f D2 = (G3.scale((float)(lineWidth/2 * x4))).subtract(V4);
Vector2f D3 = V1.add(G4.scale((float)(lineWidth/2 * y4)));
Vector2f D4 = (G4.scale((float)(lineWidth/2 * y4))).subtract(V1);
glColor3f(1, 0, 0);
GL11.glLineWidth(lineWidth);
GL11.glBegin(GL_TRIANGLES);
glVertex2f(A1.x, A1.y);
glVertex2f(A2.x, A2.y);
glVertex2f(A3.x, A3.y);
glVertex2f(A3.x, A3.y);
glVertex2f(A4.x, A4.y);
glVertex2f(A2.x, A2.y);
glVertex2f(B1.x, B1.y);
glVertex2f(B2.x, B2.y);
glVertex2f(B3.x, B3.y);
glVertex2f(B3.x, B3.y);
glVertex2f(B4.x, B4.y);
glVertex2f(B2.x, B2.y);
glVertex2f(C1.x, C1.y);
glVertex2f(C2.x, C2.y);
glVertex2f(C3.x, C3.y);
glVertex2f(C3.x, C3.y);
glVertex2f(C4.x, C4.y);
glVertex2f(C2.x, C2.y);
glVertex2f(D1.x, D1.y);
glVertex2f(D2.x, D2.y);
glVertex2f(D3.x, D3.y);
glVertex2f(D3.x, D3.y);
glVertex2f(D4.x, D4.y);
glVertex2f(D2.x, D2.y);
glEnd();
}
//rotates a vector 90 degrees
public Vector2f rotate90Degrees(Vector2f vec)
{
Vector2f rotVec = new Vector2f(-vec.y, vec.x);
return rotVec;
}