Converting 3D coordinates to 2D and back?

Molmasepic
  • Converting 3D coordinates to 2D and back? Molmasepic

    I'm wondering if there is a simple way to convert 3D coordinates to 2D coordinates. Also, if it's possible, to convert in the reverse direction.

    I'm using OpenGL(GLUT) in my C++ project.

    I am also using SFML for the 2D information (sprites text etc.)

    I found out that I can use gluProject(), but I have no idea how to use this.

    I'm asking for a simple example of using gluProject() or another example to convert 3D coordinates (such as from the player) to 2D coordinates.

    If I can't get the simple process I'm confident that I can figure out the rest.

  • You can convert between 3D and 2D coordinates in a variety of ways, some of which have more meaning than others. For example you could just drop the Z coordinate, yielding an (x,y) pair, but that probably doesn't do what you want semantically.

    But you're specifically asking how to convert between model, world or view space 3D and screen space 2D as used by your graphics API, it sounds like. This primarily involves application of the appropriate transformation matrix, as well as performing the perspective division by W and any viewport transformations that may be warranted. gluProject does exactly this operation, all you have to do is provide it with input coordinates and matrices.

    gluProject assumes the input coordinates objX, objY, and objZ to be in model space, but they can be anywhere as long as the matrices you provide it via the model and proj parameters have associated meaningful values. The format of the model and proj matrices is an array of 16 floating point values, the same kind of data you'd get from glGetFloatv and such. The view parameter is an array of 4 integers representing the position and size of your viewport (from glGetIntegerv for example).

    To perform the operation in reverse, you can use gluUnproject. Note that since you'll have a 2D coordinate you'll have manually specify an arbitrary depth value. The 3D → 2D conversion perforce loses data, so you must invent new data when going in the other direction.

Tags
opengl c++ coordinates sfml glut
Related questions and answers
  • I know that if you want to display a sprite on screen (in 2D) you can use glOrtho and essentially make 1 gl unit equal to 1 pixel, so when I plot out the vertices for say a 128x128 image (on a quad), I can define the vertices as -64/64, -64-64, etc and then when I map my texture coords to that quad, the image is displayed at a 1:1 ratio. However, lets say I wanted to not use glOrtho and wanted to have a perspective view, so I can combine 2D sprites with 3D models and whatnot? I'm at a loss on how to convert/set up the coordinates for the planes/quads I want to draw images to, in a way

  • I have a very simple effect file shown below. I am using this to draw 2D lines, however it is not behaving how I expected and I can't seem to get my head round why. If I draw a line that goes from 0,0 to 100, 100 for example, I would expect it to draw a line from one corner of the screen to a little way in. I would expect it to treat these numbers as screen coordinates. Instead, the line is huge! A line of about 2 long fills the whole screen. Why is this? How can I modify my shader to 'think' in screen coordinates? // a struct for the vertex shader return value struct VSOut { float4 Col

  • I'm programming a 3D game (using C++ and OpenGL), and I have a few 3D objects in the scene, we can say they are boxes for this example. I want to let the player click on those boxes to select them... coordinates. I guess I could then do some sort of ray-box collision test to see if any objects lie as the path of the mouse click. I'm afraid I might be over complicating it. Any better methods out there? ... to do this? The fact that these objects go through several transforms before getting to window coordinates is what makes this a bit tricky. One approach I thought about was that if the player clicks

  • examples/sites that could give me the specifics needed to actually make this such as equations and required physics knowledge. It would be great if anyone out there also would give me their attempts or ideas. finally I was wondering if it was possible to... use the source code of an existing 3D engine such as Bullet and transform it to be 2D based? use the source code of a 2D Rigid body physics...hey so I've decided to Code my own 2D soft-body physics engine in C++ since apparently none exist and I'm starting only with a general idea/understanding on how physics work and could be simulated

  • with C/C#. Most important to me would be that I could lear stuff that I can use later on for example in other Frameworks. This is important to me because I want to get deep into one thing I... engine I could use here. OGRE-engine: Always wanted to get a little deeper into C++ and the engine seems to be well documented. Cryengine/Sandbox 2 or higher: Well this one also needs C++ knowledge, seems...I've been working with the XNA-Framework(only in 2D) for over a year now. Now I want to try and start a bigger 3D-project. But somehow I have difficulties to decide which engine/Framework I should

  • I'm try to convert the 3D Objects created in the Maya or 3D Max to Simple OpenGl object anyone please guide me how can I do so???

  • that make really hard to work with when coding some functions that use them. I was thinking of making ie. SimpleMesh and HierarchyMesh objects, which will also require that the renderer can deal... them to be attached to the node in most cases, even if I allow setting global lights to the scene. @Nicol: Yes that's what I'm trying to figure out. You can see the code doesn't rely on any hardware... don't want to find out in the end that I have to rewrite everything again, as a lot of other objects will be working with these data. Sorry if it's a too subjective matter, but I need some insight. I'm

  • I've been using OpenGL for a short time now- and I'd like some clarification on VBOs. As I understand it, a VBO is an object stored on VRAM. In immediate mode, to apply a texture we simply bind it and specify the texture coordinates before the vertex coordinates, however it doesn't seem that way with VBOs. Question 1: Can I specify texture data in a VBO? What portion of the data? Normally when I make a game in 2d I'd store all of my objects in instances of different classes, then when my drawing routine comes up- just draw the objects that are on screen. With a 3D environment it would

  • I'm looking into building a cross-platform opensource 2D RPG style game engine for ChaiScript. I want to be able to do all of the graphics with SVG and need joystick input. I also need the libraries I use to be opensource and compatible with the BSD license. I'm familiar with allegro, ClanLib, and SDL. As far as I can tell, none of these libraries have built in or obvious integration for SVG. Also, I'm aware of the previous conversations on this site regarding Qt for SVG game development. I'm hoping to avoid Qt because of the size and complexity of making it a requirement. Also, Qt does

Data information