How to fix the 3D coordinate system?

edited March 2016 in Questions about Code

I want to fix the coordinate system to match the tutorial.

http://learnopengl.com/img/getting-started/coordinate_systems.png

However by default the results are totally wrong. X axis points down and Y axis points right.

PVector cameraPosition;
float rotation;

void setup()
{ 
    size (500, 500, P3D);
    colorMode(RGB, 1);     // a trick to color with 0f..1f
    setCameraPosition();   // set the camera position
}

void draw()
{ 
    background(0.1);
    updateCamera();
    updateExampleRotation();
    drawAxis();
}

void setCameraPosition()
{
    cameraPosition = new PVector();
    cameraPosition.z = 100f;
}

void updateCamera()
{
    float cx = cameraPosition.x;
    float cy = cameraPosition.y;
    float cz = cameraPosition.z;
    camera(cx, cy, cz, 0, 0, 0, 0, 1, 0);
}

void updateExampleRotation()
{
    // set the rotation
    if (mousePressed && mouseButton == LEFT)
    {
        rotation = map(mouseX, 0, width, 0, TWO_PI);
        println("Rotation " + rotation);
    }

    rotateY(rotation);

    // set the height
    if (mousePressed && mouseButton == RIGHT)
    {
       cameraPosition.y = map(mouseY, height, 0, 0, 50);
       println("Height " + cameraPosition.y);
    }
}

void drawAxis()
{
    /* http://learnopengl.com/img/getting-started/coordinate_systems.png  */

    float a = 10;  

    // X right
    stroke(1, 0, 0);
    line(0, a, 0, 0, 0, 0);

    // Y up
    stroke(0, 1, 0);
    line(0, 0, 0, a, 0, 0);

    // Z forward
    stroke(0, 0, 1);
    line(0, 0, 0, 0, 0, a);
}
Tagged:

Answers

  • edited March 2016

    It's mangled the link to that web page but lines 60 and 64 look wrong to me. Try swapping them.

  • edited March 2016

    I tried it but it did not work.

    For example in this case if you want to create a simple triangle, it will point down. The X coordinates of the triangle are -10 and 10, and the height will be 30.

    void setup()
    { 
        size (500, 500, P3D);
        colorMode(RGB, 1);
    }
    
    void draw()
    { 
        background(0.1);
        translate(width/2, height/2);
        rotateY(millis()*0.001);
        drawTriangle();
        drawAxis();
    }
    
    void drawTriangle()
    {
        noStroke();
        beginShape(TRIANGLES);
        vertex(-10, 0);
        vertex(0, 30);
        vertex(10, 0);
        endShape();
    }
    
    void drawAxis()
    {
        float a = 100;  
    
        // X axis points right
        stroke(1, 0, 0);
        line(0, a, 0, 0, 0, 0);
    
        // Y axis points up
        stroke(0, 1, 0);
        line(0, 0, 0, a, 0, 0);
    
        // Z axis points backwards
        stroke(0, 0, 1);
        line(0, 0, 0, 0, 0, a);
    }
    
  • Those x and y axes still look wrong in that code.

    There's a page about processing coordinate system which explains that it's a bit odd, but I think that's just the 2d case. I thought 3d was better because it just uses opengl space.

  • However by default the results are totally wrong. X axis points down and Y axis points right.

    If you draw a line in the Y direction and call the the X axis what do you expect. @koogs is right about the draw axis method, lines 32 and 36 need to be swapped.

    In OpenGL the Y axis points up but Processing makes the decision that the Y axis should point down so that 2D coordinates behave the same in P2D and P3D.

    If you want to match the OpenGL coordinate system you need to reverse the Y direction. Try this._ I have moved the triangle along the positive z axis for clarity_

    void setup()
    { 
      size (500, 500, P3D);
      colorMode(RGB, 1);
      strokeWeight(2);
    }
    
    void draw()
    { 
      background(0.1);
      translate(width/2, height/2);
      scale(1, -1, 1); // Revrese Y coordinates
      rotateY(millis()*0.0005);
      rotateX(0.02);
      drawTriangle();
      drawAxis();
    }
    
    void drawTriangle()
    {
      noStroke();
      beginShape(TRIANGLES);
      vertex(-10, 0, 45);
      vertex(0, 30, 45);
      vertex(10, 0, 45);
      endShape();
    }
    
    void drawAxis()
    {
      float a = 200;  
    
      // X axis points right
      stroke(1, 0, 0);
      line(a, 0, 0, 0, 0, 0);
    
      // Y axis points up
      stroke(0, 1, 0);
      line(0, 0, 0, 0, a, 0);
    
      // Z axis points forewards (towards viewer)
      stroke(0, 0, 1);
      line(0, 0, 0, 0, 0, a);
    }
    
  • OK after these corrections the example worked, however a problem that occurred was that the perspective was totally wrong, it looks like it's better to go fully 3D in that case.

    I updated the code to support the 3D camera, also I did the hacking to get proper axis coordinates.

    PVector rotation = new PVector();
    
    void setup()
    { 
        size (500, 500, P3D);
        colorMode(RGB, 1);
        strokeWeight(2);
    }
    
    void draw()
    { 
        background(0.1);
    
        pushMatrix();
        camera(0, 0, 300, 0, 0, 0, 0, -1, 0);
        scale(-1, 1, 1);
        rotation.y += (mouseX - pmouseX) * 0.01;
        rotation.x += (mouseY - pmouseY) * 0.01;
        rotateY(rotation.y);
        rotateX(rotation.x);
    
        //translate(width/2, height/2);
        //scale(1, -1, 1);
    
    
        drawTriangle();
        drawAxis();
        popMatrix();
    
    
    
        noStroke();
        fill(1);
        text("X " + rotation.x, 10, 10, 200, 20);
        text("Y " + rotation.y, 10, 20, 200, 20);
    }
    
    void drawTriangle()
    {
        noStroke();
        beginShape(TRIANGLES);
        vertex(-10, 0, 0);
        vertex(0, 30, 0);
        vertex(10, 0, 0);
        endShape();
    }
    
    void drawAxis()
    {
        float a = 100;  
    
        // X axis points right
        stroke(1, 0, 0);
        line(a, 0, 0, 0, 0, 0);
    
        // Y axis points up
        stroke(0, 1, 0);
        line(0, 0, 0, 0, a, 0);
    
        // Z axis points forewards (towards viewer)
        stroke(0, 0, 1);
        line(0, 0, 0, 0, 0, a);
    }
    
  • edited November 2017

    If it's any help, some relevant functions where Processing inverts the y axis from the standard OpenGL are frustum (a helper function to perspective) and ortho. And then there is camera itself. You could override Processing's version of these functions with your own, since each sketch you create is a child of PApplet. You have access to a majority of the properties in the sketch's graphics renderer through PGraphicsOpenGL rndr = (PGraphicsOpenGL)g;.

  • The last post of this thread happened in March 2016. Over a year ago.

    Why the hell are you replying to it now?

Sign In or Register to comment.