Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (791)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  libGDX How can I draw texture parallel to edge in polygon?  (Read 937 times)
0 Members and 1 Guest are viewing this topic.
Offline MossyForest

Junior Devvie


Medals: 4
Exp: 1 year



« Posted 2019-04-06 13:43:09 »

I'm trying to make polyline with thickness.
From point I need to connect I generate polygon(image 1~5)
I want texture to be filled in polygon.
So I used EarClippingTriangulator to cut polygon and declare 'PolygonRegion'.(image 6)
But when I draw PolygonRegion result is not what I want.(image 7-a)
I want polygon to be filled with texture parallel to edge (image 7-b)
How can I do this in libgdx?
Offline LiquidNitrogen
« Reply #1 - Posted 2019-04-06 22:19:49 »

you need to give specific texture coordinates to each vertex, based on their distance from the center of the line rather than their x,y position. although that triangulated polygon might give deformed texturing, so you also might need to triangulate it yourself with more evenly placed triangles - fairly easy by extruding triangles on either side of the center line. (ShapeRenderer is a useful class for testing/drawing triangles if it is awkward to work with PolygonRegion in this way)
Offline MossyForest

Junior Devvie


Medals: 4
Exp: 1 year



« Reply #2 - Posted 2019-04-06 23:55:32 »

Ok, I've changed how it is triangulated.


But How can I give specific coordinate to each vertex?
I assume method
PolygonSpriteBatch.draw(texture, polygonVertices, verticesOffset, verticesCount, polygonTriangles, trianglesOffset, trianglesCount);
is how it's done but, googling this method doesn't get any result.
API says 'There must be 4 vertices, each made up of 5 elements in this order: x, y, color, u, v.'
But I don't understand what are those vertices and how it should be generated.
Any simple guideline on how to approach this would be appreciated.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline LiquidNitrogen
« Reply #3 - Posted 2019-04-07 23:12:01 »

have a look at the source code for some of the SpriteBatch.draw() methods, they basically work the same way - by putting the vertex data into a float[] array which is eventually sent to the gpu for rendering, except they calculate the vertex data automatically for you instead of requiring you to specify the data for each vertex yourself (which is what you do want to do in this case).
Offline MossyForest

Junior Devvie


Medals: 4
Exp: 1 year



« Reply #4 - Posted 2019-04-09 18:02:34 »

It took longer than expected but I finally figured out how to do it.
Thanks to your answer, my game can now have beautiful Rainbow Beam attack!





For anyone may have similar problem with me, I'll leave simple code here.

'vertices' is float array of vertices from given polygon I'm try to fill in.
vertices is generated top-left corner to bottom-left corner in clockwise order.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
   private float[] testVertices;
   private short[] testTriangles;
   private Texture testImage;
   public void manualTriangulor() {      
      testVertices=new float[(vertices.length-4)*5];
      testTriangles=new short[(vertices.length/2-2)*3];  
      for(int i=0; i<vertices.length/4-1; i++) {      
         testVertices[i*20]=vertices[((vertices.length/2)-i-1)*2];
         testVertices[i*20+1]=vertices[((vertices.length/2)-i-1)*2+1];
         testVertices[i*20+2]=Color.WHITE.toFloatBits();
         testVertices[i*20+3]=0;
         testVertices[i*20+4]=1;
         
         testVertices[i*20+5]=vertices[i*2];
         testVertices[i*20+6]=vertices[i*2+1];
         testVertices[i*20+7]=Color.WHITE.toFloatBits();
         testVertices[i*20+8]=0;
         testVertices[i*20+9]=0;
         
         testVertices[i*20+10]=vertices[(i+1)*2];
         testVertices[i*20+11]=vertices[(i+1)*2+1];
         testVertices[i*20+12]=Color.WHITE.toFloatBits();
         testVertices[i*20+13]=1;
         testVertices[i*20+14]=0;

         testVertices[i*20+15]=vertices[((vertices.length/2)-i-2)*2];
         testVertices[i*20+16]=vertices[((vertices.length/2)-i-2)*2+1];
         testVertices[i*20+17]=Color.WHITE.toFloatBits();
         testVertices[i*20+18]=1;
         testVertices[i*20+19]=1;

         
         testTriangles[i*6]=(short)(i*4);
         testTriangles[i*6+1]=(short)(i*4+1);
         testTriangles[i*6+2]=(short)(i*4+2);

         testTriangles[i*6+3]=(short)(i*4+2);
         testTriangles[i*6+4]=(short)(i*4+3);
         testTriangles[i*6+5]=(short)(i*4);
      }
   }
   

   public void draw(PolygonSpriteBatch batch) {
      batch.begin();
      batch.draw(testImage,testVertices,0,testVertices.length,testTriangles,0, testTriangles.length);        
      batch.end();  
   }
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (2847 views)
2018-11-16 13:46:03

hadezbladez (1030 views)
2018-11-16 13:41:33

hadezbladez (2814 views)
2018-11-16 13:35:35

hadezbladez (553 views)
2018-11-16 13:32:03

EgonOlsen (3826 views)
2018-06-10 19:43:48

EgonOlsen (4241 views)
2018-06-10 19:43:44

EgonOlsen (2545 views)
2018-06-10 19:43:20

DesertCoockie (3357 views)
2018-05-13 18:23:11

nelsongames (3471 views)
2018-04-24 18:15:36

nelsongames (4470 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!