2012-12-23 1 views
1

glutDrawSolidSphere를 대신 할 drawSphere 메소드로 클래스를 만들었습니다. 아래 코드를 참조하십시오.구 주위의 텍스처를 어떻게 랩핑합니까?

하지만 타일링하지 않고 주위에 텍스처를 어떻게 감쌀까요? 예를 들어, 입과 눈과 코를 그려야한다면 입이 1 개, 눈이 2 개, 코가 1 개만 있으면되고 구형 전체에 100 개가 아닌 타일이 있어야합니다.

일부 라이브러리에서 Jogl을 사용하고 있습니다.

class Shape { 

    public void drawSphere(double radius, int slices, int stacks) { 
     gl.glEnable(GL_TEXTURE_2D); 
     head.bind(gl); //This method is a shorthand equivalent of gl.glBindTexture(texture.getTarget(), texture.getTextureObject()); 
     gl.glBegin(GL_QUADS); 
     double stack = (2*PI)/stacks; 
     double slice = (2*PI)/slices; 
     for (double theta = 0; theta < 2 * PI; theta += stack) { 
      for (double phi = 0; phi < 2 * PI; phi += slice) { 
       Vector p1 = getPoints(phi, theta, radius); 
       Vector p2 = getPoints(phi + slice, theta, radius); 
       Vector p3 = getPoints(phi + slice, theta + stack, radius); 
       Vector p4 = getPoints(phi, theta + stack, radius); 
       gl.glTexCoord2d(0, 0); 
       gl.glVertex3d(p1.x(), p1.y(), p1.z()); 
       gl.glTexCoord2d(1, 0); 
       gl.glVertex3d(p2.x(), p2.y(), p2.z()); 
       gl.glTexCoord2d(1, 1); 
       gl.glVertex3d(p3.x(), p3.y(), p3.z()); 
       gl.glTexCoord2d(0, 1); 
       gl.glVertex3d(p4.x(), p4.y(), p4.z()); 
      } 
     } 
     gl.glEnd(); 
     gl.glDisable(GL_TEXTURE_2D); 
    } 

    Vector getPoints(double phi, double theta, double radius) { 
     double x = radius * cos(theta) * sin(phi); 
     double y = radius * sin(theta) * sin(phi); 
     double z = radius * cos(phi); 
     return new Vector(x, y, z); 
    } 
} 

답변

3

위도와 경도를 텍스처 좌표로 직접 매핑 할 수 있습니다. 당신의 TEXCOORD에서 0과 1 대신에 s0, s1, t0, t1을 사용하여 0과 1

double s0 = theta/(2 * PI); 
double s1 = (theta + stack)/(2 * PI); 
double t0 = phi/(2 * PI); 
double t1 = (phi + slice)/(2 * PI); 

그리고

for (double theta = 0; theta < 2 * PI; theta += stack) { 
    for (double phi = 0; phi < 2 * PI; phi += slice) { 

단지 규모 thetaphi)가 (로 호출합니다.

+0

고마워요! 그게 정말 도움이되었다 :) – Yatoom

관련 문제