0

I've got a 2D Texture on a 3D Sphere and I want to know how to transfer a 2D coordinate on the Texture into a 3D coordinate. I know it has to do with the clipping of the texture : I'm using the auto clipping function of OpenGL to put the texture on the Sphere.


Edit: To clarify the problem:
I have a 2D plane which is an image containing borders drawn in red now I put objects on this plane, that have a collision radius and are wildly moving around. Whenever the objects collide with the red border they bounce back.

Now I take this 2D plane and warp it around a 3D sphere. At the position of the circles I want to put 3D-Models that move on the sphere. The problem now is to get from the "simple" 2D coordinates on the plane to the more complicates 3D coordinates on the sphere to position the 3D-Models correctly.

My first approach would be to map 2D coordinates to spherical coordinates which can easily be transferred into 3D coordinates but how would I do this?

SilentStorm
  • 149
  • 1
  • 2
  • 10

2 Answers2

2

You don't "convert" the 2D coordinate to a 3D coordinate. The 2D coordinates you have are UV coordinates (from 0 to 1) and they represent a position in the texture space. What you do is to map these UV coordinates to the vertices. UV Mapping. Source: wikipedia

You can read more about UV mapping here.

In OpenGL, it depends on which version are you using. Either you use glTexCoord calls before the glVertex calls (for old versions of OpenGL), or you set it in a VBO to be processed at the fragment shader on newer versions of OpenGL.

gbuzogany
  • 1,911
  • 16
  • 17
  • So how do I get from the UV coordinates to the 3D coordinates. Do I need to know, how OpenGL maps UV coordinates onto the Sphere? – SilentStorm Apr 22 '15 at 09:49
  • You assign a UV coordinate to each vertex, and when rendering (fragment shader, or OpenGL fixed shader if using older versions) it interpolates the UV coordinates and vertex positions to define the color of the pixel to be rendered. A very good and detailed explanation of how it works can be found [here](http://acko.net/files/fullfrontal/fullfrontal/webglmath/online.html). – gbuzogany Apr 22 '15 at 09:59
0

If you are planning to use gluSphere() function, you don't need to worry about calculating UV texture coordinates since opengl does it for you with the right functions.

If you are planning to render your own sphere, check this question

Community
  • 1
  • 1
Berke Cagkan Toptas
  • 1,034
  • 3
  • 21
  • 31
  • I'm already using gluSphere, but I'm using the texture as a 2D play field which I want to wrap around a Sphere. So what I need is a way to tell where in the 3D space a pixel of the texture is located. – SilentStorm Apr 22 '15 at 12:13
  • @Backfighter As I understood you have a 2D mesh MxN Grid and you want your mesh to wrap around a gluSphere. Is it correct? If so, you don't need gluSphere first of all. Because that means you want to build your own sphere using a 2D MxN grid.However, if you have a texture (e.g a jpeg image instead of 2D plane) You can cover it using UV coordinates (check the links I provided) . If you are interrested in the idea behind texture covering, it is explained at [wiki page here](http://en.wikipedia.org/wiki/UV_mapping). – Berke Cagkan Toptas Apr 22 '15 at 13:09