Texture coords for a dome function

Started by
2 comments, last by Lyve 19 years, 6 months ago
i have a mdae a dome function for use in a game i am making, the only problme is im unsure of how to generate texture coords for it so one texture covers the whole thing, possilby even making itso i could make it a sweeping dphere instead (with a sweep angle for how much of a sphere to create) any help would be appreciated. here is the code
void glDome(unsigned int LAYERS, unsigned int SLICES, float RADIOUSX, float RADIOUSY, float RADIOUSZ) {
    unsigned int l;
    unsigned int s;
    long double x, y, z, w;
    long double PI = 3.1415926535897932384;
    long double PI2 = PI * 2;
    long double HALFPI = PI / 2;
    long double vx[LAYERS][SLICES];
    long double vy[LAYERS][SLICES];
    long double vz[LAYERS][SLICES];

    // Generate Verticies
    for (l = 0; l < LAYERS; l++) {
        y = sin(l * HALFPI / LAYERS);
        w = cos(l * HALFPI / LAYERS);

        for(s = 0; s < SLICES; s++) {
            x = sin(PI + s * PI2 / SLICES) * w;
            z = cos(PI + s * PI2 / SLICES) * w;
            vx[LAYERS - (l + 1)] = x * RADIOUSX;
            vy[LAYERS - (l + <span class="cpp-number">1</span>)] = y * RADIOUSY;
            vz[LAYERS - (l + <span class="cpp-number">1</span>)] = z * RADIOUSZ;
        }
    }

    <span class="cpp-comment">// Generate the triangle fan for the top</span>
    glBegin(GL_TRIANGLE_FAN);
        glVertex3f(vx[<span class="cpp-number">0</span>][<span class="cpp-number">0</span>],vy[<span class="cpp-number">0</span>][<span class="cpp-number">0</span>],vz[<span class="cpp-number">0</span>][<span class="cpp-number">0</span>]);
        <span class="cpp-keyword">for</span> (s = <span class="cpp-number">0</span>; s &lt;= SLICES; s++) {
            glNormal3f(vx[<span class="cpp-number">0</span>],vy[<span class="cpp-number">0</span>],vz[<span class="cpp-number">0</span>]);
            glVertex3f(vx[<span class="cpp-number">0</span>],vy[<span class="cpp-number">0</span>],vz[<span class="cpp-number">0</span>]);
        }
    glEnd();

    <span class="cpp-comment">// Generate the sides</span>
    <span class="cpp-keyword">for</span> (l = <span class="cpp-number">1</span>; l &lt; LAYERS; l++) {
        glBegin(GL_TRIANGLE_STRIP);
            <span class="cpp-keyword">for</span> (s = <span class="cpp-number">0</span>; s &lt;= SLICES; s++) {
                glNormal3f(vx[l],vy[l],vz[l]);
                glVertex3f(vx[l-<span class="cpp-number">1</span>],vy[l-<span class="cpp-number">1</span>],vz[l-<span class="cpp-number">1</span>]);
                glVertex3f(vx[l],vy[l],vz[l]);
            }
        glEnd();
    }
}
</pre></div><!–ENDSCRIPT–> 
- relpats_eht
Advertisement
I use the following function for my skybox (which is a textured sphere), and it works just fine. Hope it helps

r is the radius
n is the resolution

void RenderSphere( float c_x, float c_y, float c_z, float r, int n )
{
int i = 0;
int j = 0;

float theta1 = 0.0;
float theta2 = 0.0;
float theta3 = 0.0;

float e_x = 0.0f;
float e_y = 0.0f;
float e_z = 0.0f;

float p_x = 0.0f;
float p_y = 0.0f;
float p_z = 0.0f;

if( r < 0 )
r = -r;

if( n < 0 )
n = -n;

if( n < 4 || r <= 0 )
{
glBegin(GL_POINTS);
glVertex3f( c_x, c_y, c_z );
glEnd();
return;
}

for( j = 0; j < n/2; ++j )
{
theta1 = j * TWOPI / n - PIDIV2;
theta2 = (j + 1) * TWOPI / n - PIDIV2;

glBegin(GL_QUAD_STRIP);

for( i = 0; i <= n; i++ )
{
theta3 = i * TWOPI / n;

e_x = cosf(theta2) * cosf(theta3);
e_y = sinf(theta2);
e_z = cosf(theta2) * sinf(theta3);
p_x = c_x + r * e_x;
p_y = c_y + r * e_y;
p_z = c_z + r * e_z;

glNormal3f( e_x, e_y, e_z );
glTexCoord2f( i/(float)n,2*(j+1)/(float)n );
glVertex3f( p_x, p_y, p_z );

e_x = cosf(theta1) * cosf(theta3);
e_y = sinf(theta1);
e_z = cosf(theta1) * sinf(theta3);
p_x = c_x + r * e_x;
p_y = c_y + r * e_y;
p_z = c_z + r * e_z;

glNormal3f( e_x, e_y, e_z );
glTexCoord2f( i/(float)n,2*j/(float)n );
glVertex3f( p_x, p_y, p_z );
}
glEnd();
}
}
the only thing is that my radius and resolution are more than one variable, making you method of generating the correct texure coods unable to generate the correct coords for mine...
- relpats_eht
Projecting a rectangular texture onto a sphere will always result in distortion. Is there a specific reason why you don't use a sky box with a cube map instead?
_____________________________________http://www.winmaze.de, a 3D shoot em up in OpenGL, nice graphics, multiplayer, chat rooms, a nice community, worth visiting! ;)http://www.spheretris.tk, an upcoming Tetrisphere clone for windows, a 3D tetris game on a sphere with powerful graphics for Geforce FX and similar graphics cards.

This topic is closed to new replies.

Advertisement