Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
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  
  A bug in ByteBuffers?  (Read 1541 times)
0 Members and 1 Guest are viewing this topic.
Offline Java Cool Dude

Senior Member




Java forever


« Posted 2004-02-15 05:37:11 »

I uploaded a demo of mine that I tested and positive is working on my pc to xith.org intending to run it as a webstart application.
My demo reads a 3D model from the jar file using a FileInputStream that fills a bytearray that I finally wrap into a bytebuffer to ease the byte to float/integer conversion.
Having noticed (it didn't take me that long  Roll Eyes ) that running as a webstart application my demo would display nothing on the screen (besides few sprites that are totally independant from the model that I just loaded) I started some trouble shooting trying to figure out what the f was happening.
So I created a Jframe that holds a JTextArea which was created using a StringBuffer containing all the data that I assumed was correctly loaded, and I scrolled down a bit to check the model info.
Well the readings seemed ok till I've reached down to 6 lines where all Tuple3fs were displayed as (0,0,0).
Question is what might have caused the bytebuffer to return 0s after doing a great job at loading the first few vertices? Huh
Offline Java Cool Dude

Senior Member




Java forever


« Reply #1 - Posted 2004-02-15 06:21:29 »

It looks like java can't read all the file at once through webstart.... Roll Eyes
Online kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2004-02-15 07:11:05 »

Is that the thing to do with reading from stream from the middle of jars. InputStream.available() doesn't return the total size of the file for streams retrieved ClassLoader.getResourceAsStream().

You got it to work now?

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Java Cool Dude

Senior Member




Java forever


« Reply #3 - Posted 2004-02-15 07:23:13 »

nope the size returned is correct...
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  
  public static IndexedTriangleArray readJCDModel(ArrayList list,
                                                  String    name,
                                                  float     scale){

    InputStream     fileStream     = JCD3DSFactory.class.getClassLoader().
                                                         getResourceAsStream(name);
    ByteBuffer      modelInfo      = null;
    byte            fileContents[] = null;

    try{
      if(fileStream == null)
        fileStream = new FileInputStream(name);
      fileContents = new byte[fileStream.available()];
     
      fileStream.read(fileContents);
      modelInfo = ByteBuffer.wrap(fileContents);
    }
    catch(Exception e){}
    int[]        indices     = new int       [modelInfo.getInt()];
    Point3f[]    vertices    = new Point3f   [modelInfo.getInt()];
    Vector3f[]   normals     = new Vector3f  [vertices.length  ];
    TexCoord3f[] tangents    = new TexCoord3f[vertices.length  ],
                 binormals   = new TexCoord3f[vertices.length  ],
                 texcoords3f = new TexCoord3f[vertices.length  ];

   for(int i = 0; i< vertices.length; i++){
      vertices   [i]= new Point3f   (scale*modelInfo.getFloat(),
                                     scale*modelInfo.getFloat(),
                                     scale*modelInfo.getFloat());
      normals    [i]= new Vector3f  (modelInfo.getFloat(),
                                     modelInfo.getFloat(),
                                     modelInfo.getFloat());
      tangents   [i]= new TexCoord3f(modelInfo.getFloat(),
                                     modelInfo.getFloat(),
                                     modelInfo.getFloat());
      binormals  [i]= new TexCoord3f(modelInfo.getFloat(),
                                     modelInfo.getFloat(),
                                     modelInfo.getFloat());
      texcoords3f[i]= new TexCoord3f(modelInfo.getFloat(),
                                     modelInfo.getFloat(),
                                     modelInfo.getFloat());
    }

    for(int i = 0; i< indices.length; i++)
      indices[i] = modelInfo.getInt();

    IndexedTriangleArray geometry = new IndexedTriangleArray(
                                        vertices.length,
                                        GeometryArray.TEXTURE_COORDINATE_3 |
                                        GeometryArray.COORDINATES          |
                                        GeometryArray.COLOR_3              |
                                        GeometryArray.NORMALS,
                                        2,
                                        new int[]{0,1},
                                        indices.length);
    geometry.setIndex(indices);
    geometry.setNormals(0, normals);
    geometry.setCoordinates(0,vertices);
    geometry.setValidIndexCount(indices.length);
    geometry.setTextureCoordinates(0,0, texcoords3f);
    geometry.setTextureCoordinates(1,0, texcoords3f);

    list.add(vertices);
    list.add(normals);
    list.add(tangents);
    list.add(binormals);
    list.add(indices);
    return geometry;
  }
Offline tom
« Reply #4 - Posted 2004-02-15 09:41:53 »

Quote
fileStream.read(fileContents);


Check out the javadoc on InputStream.read(byte[]). It is only required to block until one byte is read. It is not required to read the whole array. You have to use DataInputStream.readFully() if you need to read the whole array in one go.

Also, do not use available() to get the length of file. It might work now, but can fail at any moment. It do not give you the length of the file but the number of bytes that can be read without blocking.

Instead read the data into a temp buffer until inputstream returns -1.

Offline Java Cool Dude

Senior Member




Java forever


« Reply #5 - Posted 2004-02-15 17:27:27 »

Fixed using this approach
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
    InputStream     fileStream     = JCD3DSFactory.class.getClassLoader().
                                                         getResourceAsStream(name);
    ByteBuffer      modelInfo      = null;
    byte            fileContents[] = null;

    try{
      if(fileStream == null)
        fileStream = new FileInputStream(name);

      DataInputStream dataStream = new DataInputStream(fileStream);
      dataStream.readFully(fileContents = new byte[fileStream.available()]);
      modelInfo = ByteBuffer.wrap(fileContents);
    }
    catch(Exception e){}
Offline Mojomonkey

Senior Member




ooh ooh eee eeee


« Reply #6 - Posted 2004-02-16 21:55:48 »

This was a very timely thread, as I just ran into the same issue trying to get some webstarted demos working. So, I was able to solve one issue switching to readFully. However, I have a couple other model formats that are plain text. I'm using a BufferedReader to read them in. I'm experiencing the same issue. I use the readLine method as needed, works fine locally, doesn't work in webstart. Any ideas?

Don't send a man to do a monkey's work.
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.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (29 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (31 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!