We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
sphere texture (Read 520 times)
sphere texture
Apr 28th, 2008, 4:22pm
 
hi, this has been bugging me for days now and seems like a disproportional amount of stress for the simplicity of the task, so i sure hope someone can help me.

short of it is, im trying to texture a sphere.
the problem i run into is the sphere texture renders incomplete, sometimes missing a large portion of the sphere.

im using a texture in 2:1 ratio, so twice as long in width as it is in height. im suspecting its something to do with the texture, because if i change the texture dimensions but still maintaining the same ratio, i get different results, unfortunately all showing the sphere as not fully rendered.

here is the code ive got based on this tut.
http://ozviz.wasp.uwa.edu.au/~pbourke/texture_colour/spheremap/
 
public void setup()
{
size(800, 800, OPENGL);  

frameRate( 25 );
 
hint( ENABLE_OPENGL_4X_SMOOTH );

pgl = (PGraphicsOpenGL) g;
gl = pgl.gl;

texmap = loadImage("exp.png");

gl.glEnable( GL.GL_TEXTURE_2D );
gl.glTexEnvf( GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE );
gl.glEnable( GL.GL_DEPTH_TEST );
gl.glDepthFunc( GL.GL_LEQUAL );
gl.glHint( GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST );
pgl.bindTexture( texmap );

      }

public void draw()
{    
background(0);

renderSphere( 100, 60 );
}

public void renderSphere ( float r,int n )
{
  int i,j;
  float theta1, theta2, theta3;
  Vec3D e = new Vec3D();
  Vec3D p = new Vec3D();
 
  if (r < 0)
     r = -r;
  if (n < 0)
     n = -n;
  if (n < 4 || r <= 0)
  {
     gl.glBegin( GL.GL_POINTS );
     gl.glColor3f( 1, 1, 1 );
     gl.glVertex3f( 0.0f, 0.0f, 0.0f);
     gl.glEnd();
     
     return;
  }

  for (j=0;j<n/2;j++)
  {
     theta1 = j * 2*PI / n - PI/2;
     theta2 = (j + 1) * 2*PI / n - PI/2;

     gl.glBegin( GL.GL_TRIANGLE_STRIP );
     for (i=0;i<=n;i++)
     {
    theta3 = i * 2*PI / n;

        e.x = cos(theta2) * cos(theta3);
        e.y = sin(theta2);
        e.z = cos(theta2) * sin(theta3);
        p.x = r * e.x;
        p.y = r * e.y;
        p.z = r * e.z;

        gl.glNormal3f( e.x, e.y, e.z );
        gl.glTexCoord2f( i/(float)n, 2*(j+1)/(float)n);
        gl.glVertex3f(p.x,p.y,p.z);

        e.x = cos(theta1) * cos(theta3);
        e.y = sin(theta1);
        e.z = cos(theta1) * sin(theta3);
        p.x = r * e.x;
        p.y = r * e.y;
        p.z = r * e.z;

        gl.glNormal3f(e.x,e.y,e.z);
        gl.glTexCoord2f(i/(float)n,2*j/(float)n);
        gl.glVertex3f(p.x,p.y,p.z);
     }
     gl.glEnd();
  }
}
Page Index Toggle Pages: 1