Author Topic: Drawing an advance line  (Read 2851 times)

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Drawing an advance line
« on: February 28, 2012, 09:54:32 pm »
Hi, it's been a while since I've visited here, this time it's for work :)

I'm here in behalf of CricHQ (http://www.crichq.com/) and looking if this library is suited for what they want for it. We are creating an Android version of CricHQ mobile. One of the things needed is a 3d wagon wheel just like the current iPhone version.

Everything has been going well however it seems that Jpct doesn't support primitive line drawing by default which has put a halt in my work and I don't want to use another library if possible.

I basically need one basic method like the iPhone engine 'populateAsLineStripWith', which basically you give it a width and an array of points. And of course allows to colour it with any colour.
Everything else I can port over.

Here is an example of the wagon wheel:

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: Drawing an advance line
« Reply #1 on: February 28, 2012, 10:44:30 pm »
Are the line coordinates given in world or in object space?

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Drawing an advance line
« Reply #2 on: February 28, 2012, 10:56:34 pm »
World.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: Drawing an advance line
« Reply #3 on: February 28, 2012, 11:09:58 pm »
There's no direct support for line primitives...the "usual" way is to fake them using small triangles. Maybe the code in this thread helps: http://www.jpct.net/forum2/index.php/topic,2567.0.html? It draws a trail behind an object in world space. It's hacky, but if you treat your line points in world space as the parts of the trail, it may be possible to use something like this. If won't work when looking at the lines from the side though...

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Drawing an advance line
« Reply #4 on: February 28, 2012, 11:34:04 pm »
Quote
There's no direct support for line primitives
Is there any reason? I personally think it would be a good to have simple support by default.

Quote
Maybe the code in this thread helps
Goes over my head atm :P but I'll dig into it and see if I can get what I need. Thanks.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: Drawing an advance line
« Reply #5 on: February 28, 2012, 11:42:45 pm »
Is there any reason? I personally think it would be a good to have simple support by default.
I guess you are right. There are several reasons, but none of them is a killer one. It all comes down to personal preference, i suppose. Actually, i don't think that lines belong in this kind of engine. They have no depth, they have no textures, no lighting, no shaders...they are simply no real 3d elements for me like triangles are. But as said, these are no real reasons...i'll see, if there's a way to add at least some basic support for them...

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Drawing an advance line
« Reply #6 on: February 28, 2012, 11:57:03 pm »
Quote
But as said, these are no real reasons...i'll see, if there's a way to add at least some basic support for them...
Cool, I can do any testing if needed.
The 2 graphics libraries we have been using for the wagon wheel for our flash stuff and iPhone both had line support. I think it would only help jpct.
« Last Edit: February 29, 2012, 12:00:34 am by zammbi »

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Drawing an advance line
« Reply #7 on: February 29, 2012, 03:37:16 am »
Found some code before on the forum, which I believe was from Paul. I've modified it to allow what I want. It seems to work, now to test it... Hopefully this will help anyone else looking for lines :)

Code: [Select]
public static Object3D createLine (SimpleVector[] points, float width, String textureName)
{
    Object3D line = new Object3D( 8 * points.length);
    float offset = width / 2.0f;
   
    int count = 0;
    while(count+1 < points.length){
    SimpleVector pointA = points[count];
    SimpleVector pointB = points[count+1];
   
    // Quad A:
    line.addTriangle( new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 0, 0,
                     new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 0, 1,
                     new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    line.addTriangle( new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 0, 0,
                     new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 0, 1,
                     new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    // Quad A, back-face:
    line.addTriangle( new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 0, 0,
                     new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 0, 1,
                     new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    line.addTriangle( new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 0, 0,
                     new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 0, 1,
                     new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    // Quad B:
    line.addTriangle( new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 0, 0,
                     new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 0, 1,
                     new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    line.addTriangle( new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 0, 0,
                     new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 0, 1,
                     new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    // Quad B, back-face:
    line.addTriangle( new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 0, 0,
                     new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 0, 1,
                     new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    line.addTriangle( new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 0, 0,
                     new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 0, 1,
                     new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    count++;
    }
   
    line.setLighting( Object3D.LIGHTING_NO_LIGHTS );
    line.setAdditionalColor(new RGBColor(255, 255, 255));
    line.setCulling(Object3D.CULLING_DISABLED);
//     line.setTransparency(0);
    line.forceGeometryIndices(false);
    line.build();

    return line;
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: Drawing an advance line
« Reply #8 on: February 29, 2012, 10:52:53 pm »
Ok, i did something...please give this version a try: http://jpct.de/download/beta/jpct_ae.jar

It adds a new class called Polyline. You can define static lines with that one and add them to the world. They will be rendered after all other objects.

Please let me know if it

  • works
  • does what you need

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Drawing an advance line
« Reply #9 on: February 29, 2012, 11:31:46 pm »
Awesome it works and looks a lot better than what I had.

Are you able to support width and transparency with the colour? Line width is quite important for us.

Other than the new Polyline class is the code stable?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: Drawing an advance line
« Reply #10 on: February 29, 2012, 11:46:03 pm »
Are you able to support width and transparency with the colour? Line width is quite important for us.

Other than the new Polyline class is the code stable?
Please re-download. I've added a setter for the width to Polyline. Transparency should be possible as the color already has alpha, but it's currently not enabled by the drawing method. I'll look into it, but not today.

The code is stable as far as i know. It's the code base of what i expect to be the next official release version. It adds some new features that might cause trouble, but if you don't use them, there shouldn't be any problem. As mentioned in the other thread, i'm using this for me RPG game, which is pretty demanding and it works fine so far.

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Drawing an advance line
« Reply #11 on: March 01, 2012, 12:19:37 am »
Quote
Please re-download. I've added a setter for the width to Polyline. Transparency should be possible as the color already has alpha, but it's currently not enabled by the drawing method. I'll look into it, but not today.
Great width works perfectly!

Thanks for your help.

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Drawing an advance line
« Reply #12 on: March 01, 2012, 04:13:54 am »
Performance seems to be good. 400 lines on a Motorola Flipout seems smooth enough. The X1 is like 1fps but that's ok :P Usually the user will use it as a static screen and not change the camera angle.

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Drawing an advance line
« Reply #13 on: March 08, 2012, 11:20:47 pm »
Is it possible to allow to modify current lines? That would allow me do pooling and would be friendlier for line animation if I needed to do it.
Or could even have a 'Polyline.create' method like 'SimpleVector.create' :)
« Last Edit: March 09, 2012, 12:27:26 am by zammbi »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: Drawing an advance line
« Reply #14 on: March 09, 2012, 08:32:04 pm »
It should be possible, but i decided not to add it until somebody asks for it (mainly because i was in a hurry when adding this feature). I'll look into it...