Drawing an advance line

Started by zammbi, February 28, 2012, 09:54:32 PM

Previous topic - Next topic

zammbi

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:

EgonOlsen

Are the line coordinates given in world or in object space?


EgonOlsen

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...

zammbi

QuoteThere'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.

QuoteMaybe 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.

EgonOlsen

Quote from: zammbi on February 28, 2012, 11:34:04 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...

zammbi

#6
QuoteBut 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.

zammbi

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 :)

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;
}

EgonOlsen

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

zammbi

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?

EgonOlsen

Quote from: zammbi on February 29, 2012, 11:31:46 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.

zammbi

QuotePlease 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.

zammbi

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.

zammbi

#13
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' :)

EgonOlsen

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...