unscreenX and unscreenY
in
Programming Questions
•
4 months ago
IF you look at the image, there are 3 sphere's of different size.
In the result they all look the same cause sphere's further away are drawn bigger.
Here I have a small example, the first sphere looks as big as the 2nd sphere but the 2nd is a lot larger.
You can rotate with 'a' and 'd' to see it (i have to click the window first in processing 2.09).
- int rotY;
- void setup() {
- size(400, 400, OPENGL);
- smooth();
- }
- // . . . . . . . . . . . . . . . . . . .
- void draw() {
- if (keyPressed) {
- if (key == 'a') {
- rotY -= 1;
- } else if (key == 'd') {
- rotY += 1;
- }
- }
- background(255);
- lights();
- noStroke();
- fill(200);
- rotateY(radians(rotY));
- pushMatrix();
- translate(width*0.25, height/2, 0);
- sphere(50);
- popMatrix();
- pushMatrix();
- translate(width*0.5, height/2, -500);
- sphere(120);
- popMatrix();
- pushMatrix();
- translate(width, height/2, -237);
- sphere(map(mouseY, 0, height, 1, 100));
- popMatrix();
- }
- // . . . . . . . . . . . . . . . . . . .
I did the example above by just trying out some numbers.
If you move the mouse, you can also see a 3th sphere. The x for the sphere is drawn at width but as you can see it's more to the left since the z is not zero.
What i would like is the inverse of screenX, screenY, lets call it unscreenX and unscreenY.
For example, i want to know where i have to place a sphere to make it look like it appears on x 50 and y 100 on the screen. The perfect code for me would be:
- float x = unscreenX(50, 100, 500);
- float y = unscreenY(50, 100, 500);
- translate(x, y);
- // sphere appears for the screen on x 50 and y 100 while the actual x y values are way different
- sphere(10);
The screenX etc. can be found here:
https://github.com/processing/processing/blob/master/core/src/processing/opengl/PGraphicsOpenGL.java
I only have no clue on how to reverse this.
Could someone please help me with this (asap if possible, i really need this to graduate my year). There can be real cool things done with this.
2