0

I've got the following float array public static float camObjCoord[] = new float[8000]; declared as a global variable. I'm then adding content to it by calling the following method:

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

I'm defining i in Global as 0 and it always remains 0... why? I'm calling:

GLLayer layers = new GLLayer(this);
    layers.addcube(-6, -2, 2, 6);

...in another class.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Skizit
  • 43,506
  • 91
  • 209
  • 269
  • 2
    Please post a short but *complete* program which demonstrates the problem. – Jon Skeet Jul 13 '10 at 11:26
  • Asides from that fact that this is really horrible design, the assignments should work. How are you checking the values? Is there any chance that some other code will change the value of `Global.camObjCoord`? – Andrzej Doyle Jul 13 '10 at 11:28
  • 4
    How can you write something like this and not cry uncontrollably??? Your programmer's instinct should've kicked in immediately and said, "Hey, there's something horribly wrong here; there must be a MUCH better way to write this!". – polygenelubricants Jul 13 '10 at 11:28
  • @Andrzej I'm just doing a System.out.println(Global.camObjCoord[0]); @polygenelubricants I can't think of another way of dynamically declaring a float array to go through a float buffer to render an OpenGL cube. – Skizit Jul 13 '10 at 11:31
  • 2
    A start is that every third value in the array is highx. :-) – Thomas Jung Jul 13 '10 at 11:38
  • @Meowmix - what's the value of `Global.i` when you make this call? Where it is set? What code can change it? If you're running with multiple threads, how is the access to it synchronized? – Andrzej Doyle Jul 13 '10 at 11:41
  • Seems like "i" is never changed, but only read. Also, you may look for vertex indices. They would reduce redundancy in your code like by i+15 and i+33, where same vertex is used. – Nebril Jul 13 '10 at 12:31

3 Answers3

4

(I can't believe I'm saying this, but...)

Did you perhaps forgot to Global.i += 72; after adding a cube?

Alternately you could've also done Global.camObjCoord[Global.i++] = ... for each of the assignments instead. This is a very slight "improvement", because at least you won't have to write +1, +2, +3, ... and then += at the end.

I fear that this advise will do more harm than good, though. If at all possible, the design should be reworked immediately. Generally speaking there are much better alternatives than using Global and float[], but given that this is for OpenGL, those options may or may not be applicable.

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
2

Bleh. This code is going to be absolutely littered with problems if everything is allowed access to the same, mutable, global(!) variables. How about something like the following, to encapsulate what you want:

public class Cube
{
    private final float highX;
    private final float lowX;
    private final float highY;
    private final float lowY;
    private final float highZ;
    private final float lowZ;

    // TODO give domain-meaningful names to magic defaults for Y axis
    public Cube(float highx, float lowx, float highz, float lowz)
    {
        this(highx, lowx, 4.5f, 2.5f, highz, lowz);
    }

    public Cube(float highx, float lowx, float highy, float lowy, float highz, float lowz)
    {
       // TODO - assert the "high" parameters are strictly greater than the "lows"
       this.highX = highx;
       this.lowX = lowx;
       this.highY = highy;
       this.lowY = lowy;
       this.highZ = highz;
       this.lowZ = lowZ;
    }

   // TODO add relevant getter methods, and interesting methods like:
   public float getVolume() 
   {
       return (highX - lowX) * (highY - lowY) * (highZ - lowZ);
   }

   ...
}


public void addcube(Collection<? super Cube> cubes, float highx, float lowx, float highz, float lowz){
   //Constructing new cube...
   final Cube cube = new Cube(highx, lowx, highz, lowz);
   System.out.println("ADDING A CUBE!!"); // FIXME is this really necessary?

   // Note, adding to a specific collection of cubes rather than a
   // hard-coded global variable
   cubes.add(cube);
}

"Cube" is a strange word when you stare at it long enough.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
1

i+1 is an expression, not an assignment.

Consider this:

int i = 0; int x = i + 1; // <-- i does not increase by 1

Blub
  • 13,014
  • 18
  • 75
  • 102