This might be a dumb question. But here it goes. How would I project a 3D object to a place in screen coordinates?
I would be pleased to answer your question, but I don’t really understand it… Can you be more specific ? What do you want to achieve with this ?
Chman
Well I am doing a 3D scanner. Shows other objects relative position to whoever owns the scanner.
The scanner is a sphere with red blips inside of it. I would like to put this sphere say in lower right corner of the screen as a part of the HUD.
Well, if I understand correctly, I would proceed like that :
-
Get wanted objects coordinates (the ones to display on the scanner) in 3d space (x,y,z).
-
Get the length of the clip pane of the current frustrum.
-
Create the scanner sphere with a diameter of 1 unit (as example).
-
Create a virtual sphere with a radius of max_clip_frustrum centered on the player location.
-
For every ships (or what you want them to be), check if it’s in the sphere frustrum. If not, it won’t be visible in the scanner. Else, for each ship, divide the scanner diameter byt the (x,y,z) ship values.
You should get your ship position in scanner… I think it should work (I’m very very very tired, insomniac, so maybe my suggestion is a little dumb :-[…).
Chman 
You could use Ortho mode. It is only parallel projection, but it should be sufficient for what you want to do.
GL11.glPushMatrix(GL11.GL_PROJECTION);
GL11.glOrtho(0,800,600,0,-1.0,1.0);
GL11.glLoadIdentity();
GL11.glTranslate(700,500,0);
//draw object
GL11.glPopMatrix(GL11.GL_PROJECTION);
This may no be perfect, but you get the idea.
What ever numbers you specify in glOrtho command will give you the screen coordinates that you can use.
Not sure I get what you mean chman but i’ll ponder over it.
The scanner works with a range in world units. Everything within that range has a red dot. I am just checking range every update from this object to all others, may be ineffecient. But I have only one scanner and perhaps 10-20 objects checked every 0.5 sec.
I am realy looking for a way to render that 3D sphere in a nice GUI/HUD manner without using ortho 
CaptainJester I tried using ortho it looks like well… a parallel projection.
Okay been reading up Chman and trying to understand.
Sure done
front or back clipping plane?
sure sure
Virtual sphere whats that?
Well i stated another method to check if they are within scan range, but this could be faster?
My main problem is to render a sphere as a HUD, just a 3D one.
[quote]front or back clipping plane?
[/quote]
Frnot, because you must get the farest point that the player can see.
[quote]Virtual sphere whats that?
[/quote]
I must admit that I am not very good at explanations
! A virtual sphere would just be a center point and a radius (radius = front clipping plane). This sphere would not be drawn, you’d just keep it in memory and use it to test is ships are in the scanner area or not…
[quote]Well i stated another method to check if they are within scan range, but this could be faster?
[/quote]
Dunno, what was your first idea ?
Well in my first post, I haven’t explained how to solve your probleme but a way to draw a 3D sphere scanner… I think you already know how to do that.
About rendering the sphere as a hud object, there’s a common way to do it : use billboarding, so as the sphere will be in the 3D scene, but its location on screen will alwais be the same (facing the camera or wherever you want)…
Here’s a link for a pretty nice billboarding tutorial : http://www.lighthouse3d.com/opengl/billboarding/index.php3?billInt
Hope it’ll help…
Chman
K I was under the impresion that billboarding was based on how on objects faces a camera.
Talked to a guy yesterday who suggested that I’d use a combination of ortho projection and prespective projection.
Ortho to bring the sphere into my screen coordinates and glu.perspective will render it using a perspective projection. (it looks weird though so I am doing something wrong)
Another method would be to render the damn sphere before I rotate my scene, eg do GLU.lookat. That way the sphere wont be rotated in anyway.
I made it work
The solution is simple, make sure your HUD is drawn as the last object.
Clear the z buffer using GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
Use GLU.perspective
draw whatever you want and it will work
WEEEHAA
If anyone is intrested i’ll post my code here when its a bit more refined. ;D
- A smal step for mankind huge fall for me
Congrats !
It really interests me because I’ve never succeed in including a 3D object into a 2D HUD…
So if you could post some code it would be very great
!
Chman
I am still working on code. But I’ll post my stuff anyway
Along with a screenshot. The radar is not placed at the camera its placed in origo (0,0,0).
http://www.extorris.com/screenshots/Scanner01.png
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GLU.gluPerspective(45f,((float)Display.getDisplayMode().getWidth())/Display.getDisplayMode().getHeight(),0.1f,1000f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glPushMatrix();
GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK,GL11.GL_LINE);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glDisable(GL11.GL_TEXTURE_2D) ;
GL11.glEnable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_LINE_SMOOTH);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glScalef(0.5f,0.5f,0.5f);
//green color
GL11.glColor4f(0,1,0,0.1f);
//Draw scan sphere
Sphere s = new Sphere();
GL11.glTranslatef(-4.2f,-2.5f,-10); //FIXME:make this hack dependant on screen resolution
//rot +=0.1f;
GL11.glRotatef(85,1f,0f,0);
GL11.glScalef(0.05f,0.05f,0.05f);
s.draw(sso.get_scanRange(),10,10);
//draw scanned objects
for(PositionRotation pr : sso._scannedObjects){
GL11.glTranslatef(pr.get_position()._x ,pr.get_position()._y ,pr.get_position()._z);
GL11.glColor4f(1,0,0,1);
GL11.glPointSize(3);
GL11.glBegin(GL11.GL_POINTS);
GL11.glVertex3f(0,0,0);
GL11.glEnd();
}
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glColor4f(1,1,1,1);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPopMatrix();
Many thanks for your code !
And the screenshot looks very promising 
Chman