Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (778)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Drawing texture on a 3d sphere[lwjgl 3]  (Read 3765 times)
0 Members and 1 Guest are viewing this topic.
Offline msacco

Senior Newbie





« Posted 2018-06-19 18:32:57 »

Hello, Im trying to draw a texture(earth, sun, doesn't really matter) on a 3d sphere, and I just can't really figure how to do it, whenever I try to do it I get the following result:

https://imgur.com/a/Ns31trj



I'll post some code that explains a bit what I do:

This is my drawSphere method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   public void drawSphere(float radius) {
      final float PI = 3.141592f;
      float x, y, z, alpha, beta; // Storage for coordinates and angles
      int gradation = 20;
      for (alpha = 0.0f; alpha < Math.PI; alpha += PI / gradation) {
         for (beta = 0.0f; beta < 2.01 * Math.PI; beta += PI / gradation) {
            x = (float) (radius * Math.cos(beta) * Math.sin(alpha));
            y = (float) (radius * Math.sin(beta) * Math.sin(alpha));
            z = (float) (radius * Math.cos(alpha));
            glVertex3f(x, y, z);
            x = (float) (radius * Math.cos(beta) * Math.sin(alpha + PI / gradation));
            y = (float) (radius * Math.sin(beta) * Math.sin(alpha + PI / gradation));
            z = (float) (radius * Math.cos(alpha + PI / gradation));
            glTexCoord2f(x, y);
            glVertex3f(x, y, z);
         }
         glEnd();
      }
   }


This is my rendering method from my "Star" class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   public void render() {
      rotation += rotationSpeed;
      glPushMatrix();
      glRotatef(90, 1, 0, 0);
       glRotatef(5, 0, 1, 0);
      glRotatef(rotation, 0, 0, rotation);
      glTranslatef(distance, distance, 0);
      if (translate) {
         glTranslatef(x, y, z);
      }
      glEnable(GL_TEXTURE_2D);
      drawSphere(radius);
      glPopMatrix();
   }


"glEnable(GL_TEXTURE_2D);" is the method that shows the actual texture, and I tried other things like earth.bind, glTexCoord2f, and things like that, but nothing actually worked. How do I map the texture on the sphere? Im using lwjgl 3, so using all the glu options are not really available for me, I've heard its bad using those old methods, tho I can't really understand why, but Im trying to stick with lwjgl 3.
I tried to not add too much irrelevant code so it will be easier, if more code is needed let me know. Thanks.
Offline KaiHH

JGO Kernel


Medals: 650



« Reply #1 - Posted 2018-06-19 18:45:53 »

First, you need a suitable projection of a spherical surface onto a 2D surface. Most people and also most textures you find use equirectangular projection. So there is that.

Next you need to know that texture coordinates in OpenGL are in the range ([0..1], [0..1]). So you need to map the texture coordinates in the range from zero to one in both dimensions u and v (also called 's' and 't') onto the sphere.

Equirectangular projection is suitable for you since it can simply use longitude/latitude angles, just like you are currently using to generate the 3D vertex positions in cartesian space.

So, try the following directly before the first glVertex3f call:
1  
glTexCoord2f(beta / (2.0f * PI), alpha / PI);


And the following directly before the second glVertex3f call:
1  
glTexCoord2f(beta / (2.0f * PI) + 0.5f / gradation, alpha / PI + 1.0f / gradation);
Offline msacco

Senior Newbie





« Reply #2 - Posted 2018-06-19 19:52:24 »

First, you need a suitable projection of a spherical surface onto a 2D surface. Most people and also most textures you find use equirectangular projection. So there is that.

Next you need to know that texture coordinates in OpenGL are in the range ([0..1], [0..1]). So you need to map the texture coordinates in the range from zero to one in both dimensions u and v (also called 's' and 't') onto the sphere.

Equirectangular projection is suitable for you since it can simply use longitude/latitude angles, just like you are currently using to generate the 3D vertex positions in cartesian space.

So, try the following directly before the first glVertex3f call:
1  
glTexCoord2f(beta / (2.0f * PI), alpha / PI);


And the following directly before the second glVertex3f call:
1  
glTexCoord2f(beta / (2.0f * PI) + 0.5f / gradation, alpha / PI + 1.0f / gradation);


You already answered me in the lwjgl forum, so there's no point answering here as well, thanks anyway Smiley
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (360 views)
2018-11-16 13:46:03

hadezbladez (194 views)
2018-11-16 13:41:33

hadezbladez (363 views)
2018-11-16 13:35:35

hadezbladez (91 views)
2018-11-16 13:32:03

EgonOlsen (2193 views)
2018-06-10 19:43:48

EgonOlsen (2228 views)
2018-06-10 19:43:44

EgonOlsen (1385 views)
2018-06-10 19:43:20

DesertCoockie (2023 views)
2018-05-13 18:23:11

nelsongames (1677 views)
2018-04-24 18:15:36

nelsongames (2313 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!