纹理坐标用于渲染3d球体

时间:2012-04-24 08:29:18

标签: c++ opengl geometry texture-mapping

我正在使用GL_QUAD_STRIP基元绘制一个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() ;

    }

3 个答案:

答案 0 :(得分:1)

如果使用标准扭曲的球体纹理(如通常的地球地图),则应使用纹理坐标,如(lat / 180,lon / 360),即范围[0..1]中的标准化值。

答案 1 :(得分:0)

添加新变量:

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

然后添加到循环:    float x,y,z,dTheta = 180 / divisions,dLon = 360 / divisions,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() ;

}

答案 2 :(得分: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() ;

    }

希望陷入困境的人可能会觉得这很有用。

相关问题