Text() doesn't seem to be working

Here’s my project:

import com.jogamp.newt.opengl.GLWindow;
GLWindow r;

PGraphics w, u;
int R, L, F, B, U, D, S = 30;
float blockSize = 256, mouseSens = .003;
PVector move = new PVector(), dir = new PVector(), pos = new PVector();
ArrayList<PVector> chunks = new ArrayList<PVector>();
ArrayList<Byte> chunkData = new ArrayList<Byte>();

void setup() {
  fullScreen(P3D);
  r = (GLWindow)surface.getNative();
  r.setPointerVisible(false);
  r.confinePointer(true);
  w = createGraphics(width, height, P3D);
  u = createGraphics(width, height);
  addChunk(0, 0, 0);
  u.beginDraw();
  u.textSize(20);
  u.textAlign(LEFT, TOP);
  u.fill(0);
  u.endDraw();
}

void addChunk(int x, int y, int z) {
  chunks.add(new PVector(x, y, z));
  for (int j = 0; j < 512; j++) {
    byte thisByte = 0;
    for (int k = 0; k < 8; k++) {
      int thisBit = ((noise(j/32*0.1, j/2%16*0.1, (8*(j%2)+k)*0.1) > 0.5) ? 1 : 0) << k;
      thisByte |= (byte)thisBit;
    }
    chunkData.add(thisByte);
  }
}

void draw() {
  doPointerPhysics();
  tryInitChunks();
  drawWorld();
  drawUI();
}

void doPointerPhysics() {
  r.warpPointer(width/2, height/2);
  if (frameCount > 2) 
    dir.add((mouseX - width/2)*mouseSens, (mouseY - height/2)*mouseSens);
  if (dir.y < -PI/2) 
    dir.y = -PI/2;
  if (dir.y > PI/2) 
    dir.y = PI/2;
}

void tryInitChunks() {
}

void drawWorld() {
  w.beginDraw();
  w.background(124, 170, 255);
  w.lights();  
  w.perspective(TAU*60/360, (float)width/height, height/100*pow(3, 0.5)/2, height*100*pow(3, 0.5)/2);
  w.translate(width/2, height/2, height*pow(3, 0.5)/2);
  w.rotateX(-dir.y);
  w.rotateY(dir.x);
  pos.add(move.z*sin(dir.x)*S+move.x*cos(dir.x)*S, move.y*S, move.z*cos(dir.x)*S-move.x*sin(dir.x)*S);
  w.translate(-pos.x, pos.y, pos.z);
  for (int i = 0; i < chunks.size(); i++) {
    for (int j = 0; j < 512; j++) {
      byte thisByte = chunkData.get(512*i+j);
      for (int k = 0; k < 8; k++) {
        int thisBit = (thisByte >> k) & 1;
        w.pushMatrix();
        w.translate((chunks.get(i).x*16+j/32)*blockSize, (chunks.get(i).y*16+j/2%16)*blockSize, (chunks.get(i).z*16+j%2*8+k)*blockSize);
        if (thisBit != 0) {
          w.fill(0, 195, 0);
          w.box(blockSize);
        }
        w.popMatrix();
      }
    }
  }
  w.endDraw();
  image(w, 0, 0);
}

void drawUI() {
  u.beginDraw();
  u.clear();
  u.translate(width/2, height/2);
  u.line(0, -20, 0, 20);
  u.line(-20, 0, 20, 0);
  u.noStroke();
  u.resetMatrix();
  u.text(pos.x+" "+pos.y+" "+pos.z, 0, 0);
  image(u, 0, 0);
}

void keyPressed() {
  R = (keyCode == RIGHT || key == 'd') ? 1 : R;
  L = (keyCode == LEFT || key == 'a') ? 1 : L;
  F = (keyCode == UP || key == 'w') ? 1 : F;
  B = (keyCode == DOWN || key == 's') ? 1 : B;
  U = (keyCode == ENTER || key == ' ') ? 1 : U;
  D = (keyCode == CONTROL) ? 1 : D;
  S = (keyCode == SHIFT) ? 60 : S;
  if (abs(R-L)+abs(U-D)+abs(F-B) == 3)
    move.set(R-L, U-D, F-B).mult(pow(3, 0.5)/3);
  else if (abs(R-L)+abs(U-D)+abs(F-B) == 2)
    move.set(R-L, U-D, F-B).mult(pow(2, 0.5)/2);
  else 
  move.set(R-L, U-D, F-B);
}

void keyReleased() {
  R = (keyCode == RIGHT || key == 'd') ? 0 : R;
  L = (keyCode == LEFT || key == 'a') ? 0 : L;
  F = (keyCode == UP || key == 'w') ? 0 : F;
  B = (keyCode == DOWN || key == 's') ? 0 : B;
  U = (keyCode == ENTER || key == ' ') ? 0 : U;
  D = (keyCode == CONTROL) ? 0 : D;
  S = (keyCode == SHIFT) ? 30 : S;
  if (abs(R-L)+abs(U-D)+abs(F-B) == 3)
    move.set(R-L, U-D, F-B).mult(pow(3, 0.5)/3);
  else if (abs(R-L)+abs(U-D)+abs(F-B) == 2)
    move.set(R-L, U-D, F-B).mult(pow(2, 0.5)/2);
  else 
  move.set(R-L, U-D, F-B);
}

For some reason unknown to me, the line which reads u.text(pos.x+" "+pos.y+" "+pos.z, 0, 0); displays nothing but 0’s, when the variables themselves (the pos vector) are getting updated normally. The console prints the correct values just fine. What’s the issue?

1 Like

The drawUI is missing the endDraw();

void drawUI() {
  u.beginDraw();
  u.clear();
  u.translate(width/2, height/2);
  u.line(0, -20, 0, 20);
  u.line(-20, 0, 20, 0);
  u.noStroke();
  u.resetMatrix();
  u.text(pos.x+" "+pos.y+" "+pos.z, 0, 0);
  image(u, 0, 0);
  u.endDraw();//closes the draw loop to your UI
}

hope this helps

2 Likes

Oh… thanks! I wonder where it went… xD