texturing a sphere primitive
I am trying to wrap a texture on a sphere primitive, but I get
multiple instances of the texture wrapped all around. My code
snippet is:
glTexGeni tcS, tgTextureGenMode, tgmObjectLinear
glTexGeni tcT, tgTextureGenMode, tgmObjectLinear
glEnable glcTextureGenS
glEnable glcTextureGenT
''Draw textured sphere
glPushMatrix
glBindTexture glTexture2D, gaTextures(0)
glTranslated 0, 0, 0
glScaled 0.3, 0.3, 0.3
gluSphere QuadObj, 10#, 16, 20
glPopMatrix
Also, I have a really nice earth texture I want to use, but
it is a 2160x1080 resolution. How do I use a texture that
is not square?
Thanks for your help.
I think i can do it, without using glu** Func
As we know, the sphere''s parameter function is
x = R * cos(phai)*cos(thita)
y = R * sin(phai)*cos(thita)
z = R * cos(thita)
here, (x,y,z)is a point in the sphere, R is the radius of the sphere, and thita is longitude angle, phai is altitude angle
we convert the texture coordinate from( phai,thita) to (s, t)
where
0 <= phai < 2*PI , -PI/2 <= thita <= PI/2
and
0 <= s <= 1, 0 <= t <= 1
with this equation, I think we can using QUADS or TRIANGLE_STRIP to draw a sphere, and set the proper coordinate
As we know, the sphere''s parameter function is
x = R * cos(phai)*cos(thita)
y = R * sin(phai)*cos(thita)
z = R * cos(thita)
here, (x,y,z)is a point in the sphere, R is the radius of the sphere, and thita is longitude angle, phai is altitude angle
we convert the texture coordinate from( phai,thita) to (s, t)
where
0 <= phai < 2*PI , -PI/2 <= thita <= PI/2
and
0 <= s <= 1, 0 <= t <= 1
with this equation, I think we can using QUADS or TRIANGLE_STRIP to draw a sphere, and set the proper coordinate
I''m a newb so I''m not sure if this will be of any help, but I think I remember my instructor saying bitmaps had to be have a height and width of a power of 2 (in pixels) due to some bitmap-related algorithm''s great improvement when dealing with powers of 2. Can do this in MS Paint really quick.
-rw
-rw
i hope this will be posted correctly because i ''ve had some probelm in the past with the code posted anyway this is it
void CSphere::Build( int n )
{
int i,j;
float t1,t2,t3;
float deltaT,deltaF,c1,s1,c2,s2,c3,s3,u,du,dv,v,v1;
float phi1=__PI/2.0f;
SphereSize=n;
Lenght=n/2;
///////////////////////////////////////////////
// Handle special cases
if (n < 0 ) n = -n;
if (n <= 4 || n==0) n=4;
//////////////////////////////////////////////
// calculate constants for faster performance
deltaF=(phi1+phi1)/(float)Lenght;
deltaT=(2*__PI)/(float)n;
t1=phi1+deltaF;
t2=phi1;
du=1.0f/(float)n;
dv=2.0f*du;
v=0.0f;
v1=dv;
Verts=0;
for ( j=0; j {
c1=FastCos(t1);
s1=FastSin(t1);
c2=FastCos(t2);
s2=FastSin(t2);
t3=0.0f;
u=0.0f;
for ( i=0; i<=n; i++ )
{
c3=FastCos(t3);
s3=FastSin(t3);
Vert[Verts].p[2]=c2*c3;
Vert[Verts].p[1]=s2;
Vert[Verts].p[0]=c2*s3;
Vert[Verts].t[0]=u;
Vert[Verts].t[1]=v;
Verts++;
Vert[Verts].p[2]=c1*c3;
Vert[Verts].p[1]=s1;
Vert[Verts].p[0]=c1*s3;
Vert[Verts].t[0]=u;
Vert[Verts].t[1]=v1;
Verts++;
t3+=deltaT;
u+=du;
}
t1+=deltaF;
t2+=deltaF;
v+=dv;
v1+=dv;
}
}
you can use the vertex.p both cor coordinate and normal since its a sphere with a radius of exactly 1 , this is the code taht generates a sphere in my engine , accordingly to the distance, you may vary the factor ''n'' to enhance the tessellation or lower it and more you need this function to draw it
k=0;
glBegin(GL_TRIANGLE_STRIP);
for (j=0;j for (i=0;i<=SphereSize;i++)
{
glTexCoord2f(Vert[k].t[0],Vert[k].t[1]);
glNormal3f(Vert[k].p[0],Vert[k].p[1],Vert[k].p[2]);
glVertex3f(Vert[k].p[0],Vert[k].p[1],Vert[k].p[2]);k++;
glTexCoord2f(Vert[k].t[0],Vert[k].t[1]);
glNormal3f(Vert[k].p[0],Vert[k].p[1],Vert[k].p[2]);
glVertex3f(Vert[k].p[0],Vert[k].p[1],Vert[k].p[2]);k++;
}
glEnd();
byez.
void CSphere::Build( int n )
{
int i,j;
float t1,t2,t3;
float deltaT,deltaF,c1,s1,c2,s2,c3,s3,u,du,dv,v,v1;
float phi1=__PI/2.0f;
SphereSize=n;
Lenght=n/2;
///////////////////////////////////////////////
// Handle special cases
if (n < 0 ) n = -n;
if (n <= 4 || n==0) n=4;
//////////////////////////////////////////////
// calculate constants for faster performance
deltaF=(phi1+phi1)/(float)Lenght;
deltaT=(2*__PI)/(float)n;
t1=phi1+deltaF;
t2=phi1;
du=1.0f/(float)n;
dv=2.0f*du;
v=0.0f;
v1=dv;
Verts=0;
for ( j=0; j {
c1=FastCos(t1);
s1=FastSin(t1);
c2=FastCos(t2);
s2=FastSin(t2);
t3=0.0f;
u=0.0f;
for ( i=0; i<=n; i++ )
{
c3=FastCos(t3);
s3=FastSin(t3);
Vert[Verts].p[2]=c2*c3;
Vert[Verts].p[1]=s2;
Vert[Verts].p[0]=c2*s3;
Vert[Verts].t[0]=u;
Vert[Verts].t[1]=v;
Verts++;
Vert[Verts].p[2]=c1*c3;
Vert[Verts].p[1]=s1;
Vert[Verts].p[0]=c1*s3;
Vert[Verts].t[0]=u;
Vert[Verts].t[1]=v1;
Verts++;
t3+=deltaT;
u+=du;
}
t1+=deltaF;
t2+=deltaF;
v+=dv;
v1+=dv;
}
}
you can use the vertex.p both cor coordinate and normal since its a sphere with a radius of exactly 1 , this is the code taht generates a sphere in my engine , accordingly to the distance, you may vary the factor ''n'' to enhance the tessellation or lower it and more you need this function to draw it
k=0;
glBegin(GL_TRIANGLE_STRIP);
for (j=0;j for (i=0;i<=SphereSize;i++)
{
glTexCoord2f(Vert[k].t[0],Vert[k].t[1]);
glNormal3f(Vert[k].p[0],Vert[k].p[1],Vert[k].p[2]);
glVertex3f(Vert[k].p[0],Vert[k].p[1],Vert[k].p[2]);k++;
glTexCoord2f(Vert[k].t[0],Vert[k].t[1]);
glNormal3f(Vert[k].p[0],Vert[k].p[1],Vert[k].p[2]);
glVertex3f(Vert[k].p[0],Vert[k].p[1],Vert[k].p[2]);k++;
}
glEnd();
byez.
AAAAhhh it is happened again!!!
beetween the glBegin.... and the cycle for there is another cycle
for ( j=0 ; j < Lenght ; j++ )
put this beetween glBegin ..... and the first cycle.
beetween the glBegin.... and the cycle for there is another cycle
for ( j=0 ; j < Lenght ; j++ )
put this beetween glBegin ..... and the first cycle.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement