POV-Ray : Newsgroups : povray.general : Rendering texture for use in SkySphere : Re: Rendering texture for use in SkySphere Server Time
4 Aug 2024 12:20:04 EDT (-0400)
  Re: Rendering texture for use in SkySphere  
From: Warp
Date: 14 May 2003 16:42:12
Message: <3ec2aa24@news.povray.org>
Xilo Musimene <xil### [at] hotpopcom> wrote:
> It's for use in a backgound sphere for a new opengl program I'm doing...

  You know what? I have done *exactly* this about a week ago for a
school project (ie. render an environment map with povray and use it
in a "sky sphere" in OpenGL).

> Now, I wonder how to map this texture on an OpenGL sphere...

  That's easy. Here is the code I used for my sky sphere.

  Firstly it's handiest to make a display list for the sky sphere:

    glNewList(SKY_ID, GL_COMPILE);
    {   
        glDisable(GL_LIGHTING);
        glDisable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, SKY_ID);

        generateSkyDome();

        glEnable(GL_DEPTH_TEST);
    }
    glEndList();

  This assumed you have bound the environment map to the texture number
SKY_ID (I use enum constant for all display lists and textures; it makes
things a lot more clear).
  You can then display the sky sphere like this (note that this should be
executed *before* drawing anything else in the scene):

    // Sky (remove translation from transf matrix):
    GLdouble m[16];
    glGetDoublev(GL_MODELVIEW_MATRIX, m);
    m[12]=m[13]=m[14]=0;
    glPushMatrix();
    glLoadMatrixd(m);
    glCallList(SKY_ID);
    glPopMatrix();

  The idea is that the sky sphere is a unit sphere around the camera (in
the same way as in povray). Since it disables depth-testing during its
drawing, everything else will be drawn over it, giving the impression
that the sky sphere is infinitely away (this is why it's important that
the sky sphere is drawn before everything else).

  The generateSkyDome() function which is called above looks like this:

void generateSkyDome()
{
    const unsigned steps = 16;

    glColor3d(1,1,1);

    for(unsigned vInd = 0; vInd < steps; ++vInd)
    {
        double
            v1 = .001+.998*(double(vInd)/steps),
            v2 = .001+.998*(double(vInd+1)/steps);
        double
            vsin1 = std::sin(v1*M_PI),
            vsin2 = std::sin(v2*M_PI),
            vcos1 = std::cos(v1*M_PI),
            vcos2 = std::cos(v2*M_PI);
        double
            vTexCoord1 = double(vInd)/steps,
            vTexCoord2 = double(vInd+1)/steps;

        glBegin(GL_QUAD_STRIP);

        for(unsigned hInd = 0; hInd <= steps*2; ++hInd)
        {
            double
                hsin = std::sin(hInd*M_PI/steps + M_PI/2),
                hcos = std::cos(hInd*M_PI/steps + M_PI/2);

            glTexCoord2d(double(hInd)/(steps*2), vTexCoord1);
            glVertex3d(-hsin*vsin1, vcos1, hcos*vsin1);
            glTexCoord2d(double(hInd)/(steps*2), vTexCoord2);
            glVertex3d(-hsin*vsin2, vcos2, hcos*vsin2);
        }

        glEnd();
    }
}


-- 
#macro N(D)#if(D>99)cylinder{M()#local D=div(D,104);M().5,2pigment{rgb M()}}
N(D)#end#end#macro M()<mod(D,13)-6mod(div(D,13)8)-3,10>#end blob{
N(11117333955)N(4254934330)N(3900569407)N(7382340)N(3358)N(970)}//  - Warp -


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.