/*
  Copyright (C) 2010-2011  Charles Pence <charles@charlespence.net>
  Copyright (C) 1996-1997  Id Software, Inc.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "quakedef.h"

// Code common to both the Quake sky and the skybox

extern void R_LoadQSky(texture_t *mt);
extern void R_DrawQSky(void);

extern void R_LoadSkybox(void);
extern void R_DrawSkybox(void);


/*
=================
R_ClipSky

Set up the z-fail sky by writing the sky polygons' depth values into the
depth buffer
=================
*/
void R_ClipSky (msurface_t *s)
{
    msurface_t *fa;
    int i;
    float *v;

    // disable texturing and writes to the color buffer
    glDisable (GL_TEXTURE_2D);
    glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

    for (fa = s ; fa ; fa = fa->texturechain)
    {
        glBegin (GL_POLYGON);
        v = fa->polys->verts[0];
        for (i = 0 ; i < fa->polys->numverts ; i++, v+= VERTEXSIZE)
            glVertex3fv (v);
        glEnd ();
    }

    // revert
    glEnable (GL_TEXTURE_2D);
    glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

    // need to reset primary colour to full as well
    // as colormask can set it to black on some implementations
    glColor3f (1, 1, 1);
}


void R_InitSky(texture_t *mt)
{
    if (Cvar_VariableValue("r_skybox"))
        R_LoadSkybox();
    else
        R_LoadQSky(mt);
}

void R_DrawSky(void)
{
    if (Cvar_VariableValue("r_skybox"))
        R_DrawSkybox();
    else
        R_DrawQSky();
}

