tags:

views:

64

answers:

4

Hey Guys,

assume variables and the numbers in the 2 examples have the same numbers... Is there any difference in the end contents in these 2 different ways of populating arrays? am I wrong assuming it's the same?

Basically I'm trying to render graphics with OpenGL the first example doesn't work but the second does.

Example 1...

The global declaration is.. public static final float camObjCoord[] = new float[8000];

          public void addcube(float highx, float lowx, float highz, float lowz){
    //Constructing new cube...
    Global.cubes = Global.cubes + 1;
    float highy = 4.5f;
    float lowy = 2.5f;

    System.out.println("ADDING A CUBE!!");

    //FRONT
    Global.camObjCoord[Global.i] = highx;
    Global.camObjCoord[Global.i+1] = lowy;
    Global.camObjCoord[Global.i+2] = lowz;

    Global.camObjCoord[Global.i+3] = highx;
    Global.camObjCoord[Global.i+4] = lowy;
    Global.camObjCoord[Global.i+5] = lowz;

    Global.camObjCoord[Global.i+6] = highx;
    Global.camObjCoord[Global.i+7] = highy;
    Global.camObjCoord[Global.i+8] = lowz;

    Global.camObjCoord[Global.i+9] = highx;
    Global.camObjCoord[Global.i+10] = highy;
    Global.camObjCoord[Global.i+11] = lowz;

    //BACK
    Global.camObjCoord[Global.i+12] = highx;
    Global.camObjCoord[Global.i+13] = lowy;
    Global.camObjCoord[Global.i+14] = highz;

    Global.camObjCoord[Global.i+15] = highx;
    Global.camObjCoord[Global.i+16] = highy;
    Global.camObjCoord[Global.i+17] = highz;

    Global.camObjCoord[Global.i+18] = highx;
    Global.camObjCoord[Global.i+19] = lowy;
    Global.camObjCoord[Global.i+20] = highz;

    Global.camObjCoord[Global.i+21] = highx;
    Global.camObjCoord[Global.i+22] = highy;
    Global.camObjCoord[Global.i+23] = highz;

    //LEFT
    Global.camObjCoord[Global.i+24] = highx;
    Global.camObjCoord[Global.i+25] = lowy;
    Global.camObjCoord[Global.i+26] = lowz;

    Global.camObjCoord[Global.i+27] = highx;
    Global.camObjCoord[Global.i+28] = highy;
    Global.camObjCoord[Global.i+29] = lowz;

    Global.camObjCoord[Global.i+30] = highx;
    Global.camObjCoord[Global.i+31] = lowy;
    Global.camObjCoord[Global.i+32] = highz;

    Global.camObjCoord[Global.i+33] = highx;
    Global.camObjCoord[Global.i+34] = highy;
    Global.camObjCoord[Global.i+35] = highz;

    //RIGHT
    Global.camObjCoord[Global.i+36] = highx;
    Global.camObjCoord[Global.i+37] = lowy;
    Global.camObjCoord[Global.i+38] = highz;

    Global.camObjCoord[Global.i+39] = highx;
    Global.camObjCoord[Global.i+40] = highy;
    Global.camObjCoord[Global.i+41] = highz;

    Global.camObjCoord[Global.i+42] = highx;
    Global.camObjCoord[Global.i+43] = lowy;
    Global.camObjCoord[Global.i+44] = lowz;

    Global.camObjCoord[Global.i+45] = highx;
    Global.camObjCoord[Global.i+46] = highy;
    Global.camObjCoord[Global.i+47] = lowz;

    //TOP
    Global.camObjCoord[Global.i+48] = highx;
    Global.camObjCoord[Global.i+49] = highy;
    Global.camObjCoord[Global.i+50] = lowz;

    Global.camObjCoord[Global.i+51] = highx;
    Global.camObjCoord[Global.i+52] = highy;
    Global.camObjCoord[Global.i+53] = lowz;

    Global.camObjCoord[Global.i+54] = highx;
    Global.camObjCoord[Global.i+55] = highy;
    Global.camObjCoord[Global.i+56] = lowz;

    Global.camObjCoord[Global.i+57] = highx;
    Global.camObjCoord[Global.i+58] = highy;
    Global.camObjCoord[Global.i+59] = highz;

    //BOTTOM
    Global.camObjCoord[Global.i+60] = highx;
    Global.camObjCoord[Global.i+61] = lowy;
    Global.camObjCoord[Global.i+62] = lowz;

    Global.camObjCoord[Global.i+63] = highx;
    Global.camObjCoord[Global.i+64] = lowy;
    Global.camObjCoord[Global.i+65] = highz;

    Global.camObjCoord[Global.i+66] = highx;
    Global.camObjCoord[Global.i+67] = lowy;
    Global.camObjCoord[Global.i+68] = lowz;

    Global.camObjCoord[Global.i+69] = highx;
    Global.camObjCoord[Global.i+70] = lowy;
    Global.camObjCoord[Global.i+71] = highz;
}

Example 2...

        final static float camObjCoord[] = new float[] {
            // FRONT
             -2.0f, -1.5f,  2.0f,
              2.0f, -1.5f,  2.0f,
             -2.0f,  1.5f,  2.0f,
              2.0f,  1.5f,  2.0f,
             // BACK
             -2.0f, -1.5f, -2.0f,
             -2.0f,  1.5f, -2.0f,
              2.0f, -1.5f, -2.0f,
              2.0f,  1.5f, -2.0f,
             // LEFT
             -2.0f, -1.5f,  2.0f,
             -2.0f,  1.5f,  2.0f,
             -2.0f, -1.5f, -2.0f,
             -2.0f,  1.5f, -2.0f,
             // RIGHT
              2.0f, -1.5f, -2.0f,
              2.0f,  1.5f, -2.0f,
              2.0f, -1.5f,  2.0f,
              2.0f,  1.5f,  2.0f,
             // TOP
             -2.0f,  1.5f,  2.0f,
              2.0f,  1.5f,  2.0f,
             -2.0f,  1.5f, -2.0f,
              2.0f,  1.5f, -2.0f,
             // BOTTOM
             -2.0f, -1.5f,  2.0f,
             -2.0f, -1.5f, -2.0f,
              2.0f, -1.5f,  2.0f,
              2.0f, -1.5f, -2.0f,
        };

//edit I'm adding addcube(6, 2, 6, 2); to the first example and ARRAY is equal to.. [F@4499e290. Why would the array assignment be messing up?

+1  A: 

Your first example assumes that when addcube is called that the Global.i is initialized to some sensible value. Multiple calls to addcube could initialize different parts of the same array if Global.i changes. The addcube method also modifies the value of Glocal.cubes which you aren't doing in your second example.

Your second example directly creates an array of the exact size required without using the value of Global.i. This makes it much simpler and more robust, but also less configurable.

Mark Byers
How would I edit the first to remain able to dynamically create the variables yet work like the second?
Ulkmun
@Meowmix: You'll have to be more specific than "the first example doesn't work". What exactly doesn't work? What error / undesired output do you get?
Mark Byers
@Mark Byres There's no error, basically I just have them going through a `floatbuffer` then using `glDrawArrays` to draw the cube. The second example draws a cube the first does nothing.
Ulkmun
@Meowmix: I'm not sure what the problem is - you haven't posted enough code for me to be able to run it. Have you tried using a debugger to see if the contents of the array are as you expect them to be?
Mark Byers
@Mark Byres I'm adding `addcube(6, 2, 6, 2);` to the first example and ARRAY is equal to.. `[F@4499e290` Why would the value assignment be messing up?
Ulkmun
@Meowmix: Your parameters are wrong. To get a similar result as in your second example they should be "-2, 2, -2, 2".
Mark Byers
@Mark Byrnes I get that, but surely the Array results shouldn't be `[F@4499e290` what would cause that?
Ulkmun
A: 

In the second example (which is indeed less typing) the size of the array is also 'as needed' to contain the elements - rather than being 8000 floats in the first example - which is probably why the first doesn't work.

Will A
How would I fix my problem?
Ulkmun
Determine how many floats you'll need to add to the array for any particular run and then use:`camObjCoord = new float[numberRequired]`...you'll need to make the array non-final, though.
Will A
Did that, didn't work :(
Ulkmun
Didn't work (failed when run) or didn't compile?
Will A
Didn't work, same problem. I'm adding `addcube(6, 2, 6, 2);` to the first example and ARRAY is equal to.. `[F@4499e290` I'd assume thats the issue here.. any idea why it's returning such strange contents?
Ulkmun
camObjCoord.toString() won't return the content of the array, rather, it returns a string with some form of 'reference' (like a C++ pointer) - which is pretty meaningless and which isn't indicative of a problem.
Will A
A: 

I'm pretty sure I don't have it exact, but if I were trying to build a square kind of like this, I'd probably go:

for(float x : new float[]{-2.0f, 2.0f} ) 
    for(float y : new float[]{-1.5f, 1.5f} ) 
       create new Point(x,y); 

Which can easily be seen to give -2, -1.5; -2, 1.5; 2, -1.5 & 2, 1.5 which should be the 4 corner points of the square.

It seems like adding a z axis would simply be adding a z level after the y (and I'll clean up the syntax a little):

float[] xpts=new float[]{-2.0f, 2.0f}; 
float[] ypts=new float[]{-1.5f, 1.5f};
float[] zpts=new float[]{-2.0f, 2.0f} 

for(float x : xpts  ) 
    for(float y : ypts ) 
        for(float z : zpts ) 
           create_new_3D_Point(x,y,z);

I actually give this a pretty good chance of working after thinking about it for a while. If anyone tries it, please let me know.

I prefer NEVER listing out numbers like that, the repetition is bug prone and mind-numbing. To reduce it to a pattern eliminates typo-style bugs and helps you recognize patters for further optimization.

Bill K
A: 

I'm adding addcube(6, 2, 6, 2); to the first example and ARRAY is equal to.. [F@4499e290. Why would the array assignment be messing up?

It is not messing up! If you try to print an array that is the kind of thing that you get.

  • [F@4499e290 is the normal result of calling Object.toString() on a float[] object.
  • When you call System.out.print(...) on some variable or expression whose declared type is Object, the Object.toString() method is called. Similar for String concatenation.

If you want to print an array contents, you need to write some extra code to do it.

Incidentally, when you've figured out what your immediate problem, you should try to make your application design more object-oriented. All that use of globals / statics is ugly, and will lead you into trouble if you ever (EVER) want to run more than one instance of your application in the same JVM.

Stephen C