渲染3d球面的纹理坐标 [英] Texture coordinates for rendering a 3d sphere

查看:172
本文介绍了渲染3d球面的纹理坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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();

}


解决方案

问题是我只画了两个顶点,而且 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)
{


//顶点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);


//顶点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);


//顶点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();

}

希望遇到困难的人可能会觉得有用。 p>

I am drawing a 3d sphere using GL_QUAD_STRIP primitive and it works except I don't know how to set up the texture coordinates.

I have a certain number of division that divide my sphere into equal number of latitudes and longitudes. Hence, the sphere's vertices are approximated using the divisions as follows

   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() ;

    }

解决方案

Okay the problem was that I was only drawing 2 vertices and GL_QUAD_STRIP requires 4. The following code now has both vertices set up properly and texture working

   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() ;

    }

Hope people who get stuck may find this useful.

这篇关于渲染3d球面的纹理坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆