2012-04-24 43 views
0

我正在使用GL_QUAD_STRIP原始图绘制3d球体,它的工作原理除了我不知道如何设置纹理坐标。用于渲染3d球体的纹理坐标

我有一定数量的division将我的球体划分为相等的纬度和经度数。因此,球体的顶点所使用的划分如下

float x, y, z, dTheta=180/divisions, dLon=360/divisions, degToRad=3.14/180 ; 

    for(float lat =0 ; lat <=180 ; lat+=dTheta) 
    { 
     glBegin(GL_QUAD_STRIP) ; 
     for(float lon = 0 ; lon <=360; lon+=dLon) 
     { 

      x = r*cosf(lat * degToRad) * sinf(lon * degToRad) ; 
      y = r*sinf(lat * degToRad) * sinf(lon * degToRad) ; 
      z = r*cosf(lon * degToRad) ; 

      glNormal3f(x, y, z) ; 
      glVertex3f(x, y, z) ; 

      x = r*cosf((lat + dTheta) * degToRad) * sinf(lon * degToRad) ; 
      y = r*sinf((lat + dTheta) * degToRad) * sinf(lon * degToRad) ; 
      z = r*cosf(lon * degToRad) ; 

      glNormal3f(x, y, z) ; 
      glVertex3f(x, y, z) ; 
     } 
     glEnd() ; 

    } 

回答

0

好了,问题是,我只是画2个顶点和GL_QUAD_STRIP要求4.下面的代码现已顶点都正确设置和纹理工作

double x, y, z, dTheta=180/divisions, dLon=360/divisions, degToRad=3.141592665885/180 ; 

    for(double lat =0; lat <=180; lat+=dTheta) 
    { 

     glBegin(GL_QUAD_STRIP) ; 
     for(double lon =0 ; lon <=360 ; lon+=dLon) 
     { 


      //Vertex 1 
      x = r*cos(lon * degToRad) * sin(lat * degToRad) ; 
      y = r*sin(lon * degToRad) * sin(lat * degToRad) ; 
      z = r*cos(lat * degToRad) ; 
      glNormal3d(x, y, z) ; 
      glTexCoord2d(lon/360-0.25, lat/180); 
      glVertex3d(x, y, z) ; 


      //Vetex 2 
      x = r*cos(lon * degToRad) * sin((lat + dTheta)* degToRad) ; 
      y = r*sin(lon * degToRad) * sin((lat + dTheta) * degToRad) ; 
      z = r*cos((lat + dTheta) * degToRad) ; 
      glNormal3d(x, y, z) ; 
      glTexCoord2d(lon/360-0.25, (lat + dTheta-1)/(180)); 
      glVertex3d(x, y, z) ; 


      //Vertex 3 
      x = r*cos((lon + dLon) * degToRad) * sin((lat) * degToRad) ; 
      y = r*sin((lon + dLon) * degToRad) * sin((lat) * degToRad) ; 
      z = r*cos((lat) * degToRad) ; 
      glNormal3d(x, y, z) ; 
      glTexCoord2d((lon + dLon)/(360)-0.25 ,(lat)/180); 
      glVertex3d(x, y, z) ; 


      //Vertex 4 
      x = r*cos((lon + dLon) * degToRad) * sin((lat + dTheta)* degToRad) ; 
      y = r*sin((lon + dLon)* degToRad) * sin((lat + dTheta)* degToRad) ; 
      z = r*cos((lat + dTheta)* degToRad) ; 
      glNormal3d(x, y, z) ; 
      glTexCoord2d((lon + dLon)/360-0.25, (lat + dTheta)/(180)); 
      glVertex3d(x, y, z) ; 


     } 
     glEnd() ; 

    } 

谁卡住希望人们可能会发现这很有用。

1

如果使用标准地distortioned球质地(如通常的地球地图)应该使用纹理坐标等(纬度/ 180,LON/360),即,归一化的近似值范围[0..1]。

0

添加新的变量:

float texX = 0.0; 
float texy = 0.0; 
float dTex = 1/ divisions; 

然后再加入到循环: 浮子X,Y,Z,dTheta = 180 /司,dLon = 360 /司,degToRad = 3.14/180;

for(float lat =0 ; lat <=180 ; lat+=dTheta, texX += dTex) 
{ 
    texY = 0.0; 
    glBegin(GL_QUAD_STRIP) ; 
    for(float lon = 0 ; lon <=360; lon+=dLon, texY += dTex) 
    { 

     x = r*cosf(lat * degToRad) * sinf(lon * degToRad) ; 
     y = r*sinf(lat * degToRad) * sinf(lon * degToRad) ; 
     z = r*cosf(lon * degToRad) ; 

     glNormal3f(x, y, z) ; 
     glTexCoord2f(texX, texY); 
     glVertex3f(x, y, z) ; 
    } 
    glEnd() ; 

} 
+0

这没有奏效。它画出了整个棕色的颜色。这是不是所有包装我的抽样图像这是一个检查(国际象棋棋盘布局)图片 – jmishra 2012-04-24 09:47:25

+0

glVertex3f调用需要在glTexCoord2f调用.... – ltjax 2012-04-24 09:52:02

+0

@ltjax doh权利。谢谢。现在修复 – 2012-04-24 09:57:47