Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  Generating Texture Coordinates for Indexed Arrays  (Read 1910 times)
0 Members and 1 Guest are viewing this topic.
Offline shochu

Junior Devvie




Java games rock!


« Posted 2003-11-07 01:35:40 »

I've been having problems getting my textures to appear and I'm not sure if If I'm making the mistake in the actual generation of the texture coordinates or in applying them to the geom.

The following code generates the coordinates

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
      private void generateTextureCoordinates(int sizeX, int sizeZ){
            //SizeX is the number of coordinates in the X Direction
            //SizeZ is the number of coordinates in the Z Direction
           
            texCoords = new TexCoord2f[MAX_COORDINATES];
            int index=0;
            for(int z = 0; z < sizeZ; z++){
             for(int x = 0; x < sizeX; x++){
                    texCoords[index++] =  new TexCoord2f((float)x / (float)sizeX, (float)z / (float)sizeZ);
              }
             }
      }


When I create the geometry I do the following.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
      private void generateGeometry()      {
            geo = new IndexedTriangleArray(MAX_COORDINATES, TriangleArray.COORDINATES |
                                                                                    TriangleArray.NORMALS |
                                                                                  TriangleArray.COLOR_3 |
                                                                                  TriangleArray.TEXTURE_COORDINATE_2, MAX_INDICES);
            geo.setValidVertexCount(MAX_COORDINATES);
            geo.setCoordinates(0, coordinates);
            geo.setValidIndexCount(MAX_INDICES);
            geo.setIndex(indices);
            geo.setNormals(0, normals);
            geo.setColors(0, colors);
            geo.setTexCoordRef2f(0,texCoords);
      }


Finally when I set up the texture I do the following ...

1  
2  
3  
4  
5  
6  
7  
8  
                    TextureLoader.tf.registerPath(".");
                    TextureLoader.tf.registerPath("..");
                    texture = (Texture2D) TextureLoader.tf.getMinMapTexture("checkerboard.jpg");
                    TextureAttributes ta = new TextureAttributes();
                    ta.setTextureMode(TextureAttributes.MODULATE);
                    app = new Appearance();
                    app.setTexture(texture);
                                      app.setTextureAttributes(ta);


Of course I realize being just code fragments its probably difficult to figure out what is wrong.  Hopefully, it's something very obvious and someone can catch it right away.  If not I'll try to extract the relevant code into something executable.  Thanks for the help!
Offline shochu

Junior Devvie




Java games rock!


« Reply #1 - Posted 2003-11-07 17:07:49 »

I went ahead and took all the relevant code and made it it's own program.  You should be able to just copy and paste the following code.  Right now it just displays the cube (From the getting started demo) and a plane which is an indexedTriangleArray.  I can't seem to get the stone texture to appear correctly on the plane.

Does anyone have any ideas?  Does it look I'm calculating the texture coordinates correctly?  Since it's just a square arranged in a grid I figured it wasn't that complicated.

I'm positive the indices and vertices are being calculated properly since they show up correctly when I just render lines.  THanks for any help you can provide.

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  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
import javax.vecmath.*;

// Xith3D
import com.xith3d.scenegraph.*;
import com.xith3d.test.*;
import com.xith3d.loaders.texture.*;

// use Jogl
import com.xith3d.render.*;
import com.xith3d.render.jogl.*;

public class indexTest
{
      public Shape3D shape;
      IndexedTriangleArray geo;
     
      private Point3f[] coordinates; // = new int[];
      private Vector3f[] normals;
      private Color3f[] colors;
     
      private int[] indices;
      private TexCoord2f[] texCoords;
           
      private int MAX_INDICES;
      private int MAX_COORDINATES;
     
      public PolygonAttributes pa;
      private Texture2D texture;
      private ColoringAttributes ca;
      private Material mat;
      private Appearance app;
     
     
      public static void main(String[] args)
      {
            new indexTest();
      }
     
      public Shape3D createShape(){
           
            int size = 5; //Shape is a square with length 'size'
            MAX_COORDINATES = size * size;
            MAX_INDICES = (size-1)*(size-1) * 6;
                             
            this.generateCoordinates(size);
            this.generateIndices(size);
            this.generateTextureCoordinates(size);
            this.generateGeometry();

//Create Shape based on above created geometry      
            shape = new Shape3D(geo);
           
//Load Texture
            TextureLoader.tf.registerPath(".");
            TextureLoader.tf.registerPath("..");
            texture = (Texture2D) TextureLoader.tf.getMinMapTexture("stone.jpg");
           
//Polygon Attributes                        
            pa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE,PolygonAttributes.CULL_NONE,0);
                     
//Creating Appearance
            app = new Appearance();
            app.setTexture(texture);
            app.setPolygonAttributes(pa);
            shape.setAppearance(app);
            shape.setBoundsAutoCompute(false);
            shape.setBounds(new BoundingSphere(new Point3f(0,0,0),100000));
           
            return shape;
      }
     
      /**
       * This function generates the Indices which are in the following format
       *       v1          v2
       *(x,0,z)    (x+1,0,z)
       *       0-----------0
       *       |                 |
       *       |                   |
       *       |           |
       *       |                |
       *       |             |
       *       0-----------0
       * v3                  v4
       * @param sizeX, Size of the Terrain in the X dimension
       * @param sizeZ, Size of the Terrain in the Z dimension
       */

      private void generateIndices(int size){
            indices = new int[MAX_INDICES];
            int index=0;
            for(int z = 0; z < size-1; z++){
                  for(int x = 0; x < size-1; x++){
                         indices[index++] = x +     ( z * size );                              //v1
                         indices[index++] = x + 1 + ( z * size);                              //v2
                          indices[index++] = x + 1 + ((z + 1) * (size));                  //v4

                          indices[index++] = x +     (z * size);                              //v1
                          indices[index++] = x + 1 + ((z + 1) * (size));                  //v4
                          indices[index++] = x +     ((z + 1) * (size));                  //v3
                   }
             }
      }
     
      private void generateTextureCoordinates(int size){
            texCoords = new TexCoord2f[MAX_COORDINATES];
            int index=0;
            for(int z = 0; z < size; z++)
                  for(int x = 0; x < size; x++)
                        texCoords[index++] =  new TexCoord2f((float)x / (float)(size-1), (float)z / (float)(size-1));
      }
           
      private void generateCoordinates(int size){
            coordinates = new Point3f[MAX_COORDINATES];
            int index=0;
            for(int z=0; z<size; z++)
                  for(int x=0; x<size; x++)
                        coordinates[index++] = new Point3f((float)x, 0.0f, (float)z);
      }      
     
      private void generateGeometry()      {
            geo = new IndexedTriangleArray(MAX_COORDINATES, TriangleArray.COORDINATES |
                                                                                    TriangleArray.TEXTURE_COORDINATE_2,
                                                                                    MAX_INDICES);
            geo.setValidVertexCount(MAX_COORDINATES);
            geo.setCoordinates(0, coordinates);
            geo.setValidIndexCount(MAX_INDICES);
            geo.setIndex(indices);
            geo.setTexCoordRef2f(0,texCoords);
      }


      public void start(View view) {
            while (true){
                  Transform3D viewT = new Transform3D();
                  viewT.lookAt(new Point3f(5,5,-5),new Point3f(5,0,0),new Point3f(0,1,0));
                  view.setTransform(viewT);
                 
                  view.renderOnce();
                  try      {
                        Thread.sleep(10L);
                  }
                  catch (Exception e)      {
                  }
            }
      }

      public indexTest() {
            // create the virtual univers
            VirtualUniverse universe = new VirtualUniverse();

            // add a view to the universe
            View view = new View();
            universe.addView(view);

            // add a locale
            Locale locale = new Locale();
            universe.addLocale(locale);

            // create a BranchGroup
            BranchGroup scene = new BranchGroup();
            locale.addBranchGraph(scene);
                 
            // create Cube
            Shape3D plane = createShape();
            Geometry geo = Cube.createCubeViaTriangles(0, 0, 0, 1, true);
            Shape3D sh = new Shape3D(geo, new Appearance());
            scene.addChild(sh);
            scene.addChild(plane);

            // create a canvas for our graphics
            RenderPeer rp = new RenderPeerImpl();
            CanvasPeer cp = rp.makeCanvas(null, 640, 480, 32, false);
            Canvas3D canvas = new Canvas3D();
            canvas.set3DPeer(cp);

            // modify our view so we can see the cube
            view.addCanvas3D(canvas);
            start(view);
      }
}
Offline Daath

Junior Devvie




Java games rock!


« Reply #2 - Posted 2003-11-07 18:29:27 »

use geo.setTextureCoordinates(0, 0,texCoords); instead of geo.setTexCoordRef2f(0,texCoords);  - that should do it ( at  least that is what I am seeing here )
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline shochu

Junior Devvie




Java games rock!


« Reply #3 - Posted 2003-11-07 18:30:40 »

Yep!
 I just figured it out like 10 seconds ago and then came to the boards.  

geo.setTextureCoordinates(0,0,texCoords);

That's the key.  Thanks for your help Daath!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!