I need a line that would be part of the user interface, but it always pointed to a specific place in 3D space.
To do this, I try:
double camY, camX;
camX = cameraX * -1;
if(camX > 90)
camX = 180 - camX;
if(camX < -90)
camX = -180 - camX;
camY = cameraY;
double camY2 = camY;
if(camY > 90)
camY2 = 180 - camY;
if(camY < -90)
camY2 = -180 - camY;
double x1;
double y1;
double x2 = x * (90.0 - Math.abs(camY)) / 90.0,
y2 = (y * (90.0 - Math.abs(camY)) / 90.0);
if(vertical) {
x1 = x2 + (y * (camY2 / 90.0) * ((90 - camX) / 90));
y1 = (y2 * ((90 - camX) / 90)) - (x * (camY2 / 90.0));
}else{
x1 = x2 + (y * (camY2 / 90.0));
y1 = y2 - (x * (camY2 / 90.0));
y1 = y1 * (camX / 90.0);
}
GL11.glVertex2d(x1, y1);
GL11.glVertex2d(toX, toY);
GL11.glVertex2d(toX, toY);
GL11.glVertex2d(max, toY);
Where x
and y
- coordinate of point on 3D space. cameraX
and cameraY
- angle of camera rotate. toX`` and
toY``` destination point on the camera plane (user interface).
All this code runs before the camera (
GL11.glOrtho(-max, max, -1, 1, 10, -10);
glRotatef(cameraX, 1f, 0f, 0);
glRotatef(cameraY, 0f, 1f, 0);
) and before GL11.glMatrixMode(GL11.GL_MODELVIEW);
. Therefore, it ignores the Z coordinate.
Initially, I had only the last 4 lines of code, but then when the camera was rotated, the entire line moved behind it. So I added the rest of the calculations.
This partially solves my problem.
Initial position:
Camera rotation on the y axis:
Small line deviations are already visible.
Camera rotation on the x and y axis:
As you can see in the screenshots, the red line still shifts when the camera rotates. How can I make it always be at the point I need in spaces? (In the center of the red circle in the screenshots)
You should:
Project the 3D point you want the line to end to the screen manually. For that
glGetFloatv
, GL_MODELVIEW_MATRIX
and GL_PROJECTION_MATRIX
.Draw a 2d line from the UI location you want it to begin to the projected 2d location you want it to end.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.