void rglSetCombiner(rglRenderChunk_t & chunk, int format)
{
    static char _1ma[64];
    static char t1[64];
    static char t1a[64];
    static char t2[64];
    static char t2a[64];
    static char prim_lod_frac[64];

    static const char *saRGB[] = {
        "c",        t1,         t2,             "p/*PRIM*/", 
        "gl_Color", "e",        "1.0/*NOISE*/", "1.0",
        "0.0",      "0.0",      "0.0",          "0.0",
        "0.0",      "0.0",      "0.0",          "0.0"
    };

    static const char *mRGB[] = {
        "c",                t1,                 t2,                 "p/*PRIM*/", 
        "gl_Color/*SHADE*/","e",                "0.0/*SCALE*/",     "c.a/*COMBINED_A*/",
        "t1.a/*TEXEL0_A*/", "t2.a/*TEXEL1_A*/", "p.a/*PRIM_A*/",    "gl_Color.a/*SHADEA*/",
        "e.a/*ENV_ALPHA*/", "0.5/*LOD_FRACTION*/","0.5/*PRIM_LOD_FRAC*/","k5/*K5*/",
        "0.0",              "0.0",              "0.0",              "0.0",
        "0.0",              "0.0",              "0.0",              "0.0",
        "0.0",              "0.0",              "0.0",              "0.0",
        "0.0",              "0.0",              "0.0",              "0.0"
    };

    static const char *aRGB[] = {
        "c",                t1,             t2,         "p/*PRIM*/", 
        "gl_Color/*SHADE*/","e/*ENV*/",     "1.0",      "0.0",
    };

    static const char *saA[] = {
        "c.a",          t1a,        t2a,        "p.a/*PRIM*/", 
        "gl_Color.a",   "e.a",      "1.0",      "0.0",
    };

    static const char *sbA[] = {
        "c.a",          t1a,        t2a,        "p.a/*PRIM*/", 
        "gl_Color.a",   "e.a",      "1.0",      "0.0",
    };

    static const char *mA[] = {
        "0.5/*LOD_FRACTION*/",      t1a,        t2a,            "p.a/*PRIM*/", 
        "gl_Color.a/*SHADE*/",      "e.a",      prim_lod_frac,  "0.0",
    };

    static const char *aA[] = {
        "c.a",                      t1a,        t2a,            "p.a/*PRIM*/", 
        "gl_Color.a/*SHADE*/",      "e.a",      "1.0",          "0.0",
    };

    const static char * bRGB[] =
    { "c/*PREV*/", "f", "b", "fog/*FOG*/" };
    const static char * bA[2][4] =
    { {"c.a/*PREVA*/", "fog.a/*FOGA*/", "gl_Color.a/*SHADEA*/", "0.0/*ZERO*/"},
    {_1ma/*"(1.0-c.a/ *PREVA)"*/, "0.0/*f.a*//*FRAGA*/", "1.0", "0.0"}}; // need clamping on 1-alpha ?


    rdpState_t & state = chunk.rdpState;
    static rglCombiner_t * c;
    uint32_t cycle = RDP_GETOM_CYCLE_TYPE(state.otherModes);
    int i; //, fmt, size;
    char * p;
    const char * alphaTest;
    const char * alphaTest2;
    const char * write;
    static char src[4*4096];

    float env[4];
    env[0] = RDP_GETC32_R(state.envColor)/255.0f;
    env[1] = RDP_GETC32_G(state.envColor)/255.0f;
    env[2] = RDP_GETC32_B(state.envColor)/255.0f;
    env[3] = RDP_GETC32_A(state.envColor)/255.0f;
    glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, env);

    env[0] = RDP_GETC32_R(state.blendColor)/255.0f;
    env[1] = RDP_GETC32_G(state.blendColor)/255.0f;
    env[2] = RDP_GETC32_B(state.blendColor)/255.0f;
    env[3] = RDP_GETC32_A(state.blendColor)/255.0f;
    glLightfv(GL_LIGHT0, GL_AMBIENT, env);

    env[0] = RDP_GETC32_R(state.fogColor)/255.0f;
    env[1] = RDP_GETC32_G(state.fogColor)/255.0f;
    env[2] = RDP_GETC32_B(state.fogColor)/255.0f;
    env[3] = RDP_GETC32_A(state.fogColor)/255.0f;
    glLightfv(GL_LIGHT0, GL_DIFFUSE, env);

    glActiveTextureARB(GL_TEXTURE1_ARB);
    env[0] = state.k5/255.0f;
    glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, env);
    if (cycle == RDP_CYCLE_TYPE_FILL) {
        if (0/*fb_size == 3*/) { // FIXME
            env[0] = RDP_GETC32_R(state.fillColor)/255.0f;
            env[1] = RDP_GETC32_G(state.fillColor)/255.0f;
            env[2] = RDP_GETC32_B(state.fillColor)/255.0f;
            env[3] = RDP_GETC32_A(state.fillColor)/255.0f;
        } else {
            env[0] = RDP_GETC16_R(state.fillColor)/31.0f;
            env[1] = RDP_GETC16_G(state.fillColor)/31.0f;
            env[2] = RDP_GETC16_B(state.fillColor)/31.0f;
            env[3] = RDP_GETC16_A(state.fillColor);
        }
    } else {
        env[0] = RDP_GETC32_R(state.primColor)/255.0f;
        env[1] = RDP_GETC32_G(state.primColor)/255.0f;
        env[2] = RDP_GETC32_B(state.primColor)/255.0f;
        env[3] = RDP_GETC32_A(state.primColor)/255.0f;
    }
    glLightfv(GL_LIGHT0, GL_SPECULAR, env);
    glActiveTextureARB(GL_TEXTURE0_ARB);
    rglAssert(glGetError() == GL_NO_ERROR);

    //   if (c && rglNbCombiners &&
    //       RDP_GETOM_CYCLE_TYPE(c->otherModes) == cycle &&
    //       (RDP_GETOM_CYCLE_TYPE(c->otherModes) >= 2 ||
    //        (!memcmp(&c->combineModes, &state.combineModes, sizeof(rdpCombineModes_t)) &&
    //         !memcmp(&c->otherModes, &state.otherModes, sizeof(rdpOtherModes_t))))) {
    //     return;
    //   }

    for (i=0; i<rglNbCombiners; i++) {
        c = rglCombiners + i;
        if (c->format == format &&
            RDP_GETOM_CYCLE_TYPE(c->otherModes) == cycle &&
            (RDP_GETOM_CYCLE_TYPE(c->otherModes) >= 2 ||
            (!memcmp(&c->combineModes, &state.combineModes, sizeof(rdpCombineModes_t))
            && !memcmp(&c->otherModes, &state.otherModes, sizeof(rdpOtherModes_t))
            ))) {
#ifdef RDP_DEBUG
                chunk.shader = c->shader;
#endif
                rglUseShader(c->shader);
                goto ok;
        }
    }

    if (rglNbCombiners == RGL_MAX_COMBINERS)
        rglClearCombiners();

    c = rglCombiners + rglNbCombiners++;
    c->otherModes = state.otherModes;
    c->combineModes = state.combineModes;
    c->format = format;
#ifndef RGL_EXACT_BLEND
    c->srcBlend = GL_ONE;
    c->dstBlend = GL_ZERO;
#endif

    switch (format & RGL_COMB_FMT) {
case RGL_COMB_FMT_RGBA:
    write = "gl_FragColor = c;";
    break;
case RGL_COMB_FMT_I:
    write = "gl_FragColor = vec4(c[0]);";
    break;
case RGL_COMB_FMT_DEPTH:
    write = "gl_FragDepth = c[0];";
    break;
    }

    if (cycle == RDP_CYCLE_TYPE_FILL) {
        sprintf(
            src, 
            "void main()                       \n"
            "{                                 \n"
            //"  c = gl_TextureEnvColor[1];\n"
            "  vec4 c = gl_LightSource[0].specular;\n"
            "  %s\n"
            "}                                 \n",
            write);
        c->shader = rglCreateShader(
            "void main()                                                    \n"
            "{                                                              \n"
            "  gl_Position = ftransform();                                  \n"
            "  gl_FrontColor = gl_Color;                                    \n"
            "  gl_BackColor = gl_Color;                                     \n"
            "  gl_TexCoord[0] = gl_MultiTexCoord0;                          \n"
            "}                                                              \n"
            ,
            src
            );
#ifdef RDP_DEBUG
        chunk.shader = c->shader;
#endif
        rglUseShader(c->shader);
        goto ok;
    }

    alphaTest = "";
    alphaTest2 = "";

    if (//cycle < 2 && // CHECK THIS
        RDP_GETOM_CVG_TIMES_ALPHA(chunk.rdpState.otherModes)
        //&& rglT1Usage(chunk.rdpState)
        ) {
            if (RDP_GETOM_ALPHA_CVG_SELECT(chunk.rdpState.otherModes))
                alphaTest = "if (c.a < 0.5) discard; \n";
            else
                alphaTest = "if (t1.a < 0.5) discard; \n";
            alphaTest2 = "if (c.a < 0.5) discard; \n";
    }
    else if (RDP_GETOM_ALPHA_COMPARE_EN(chunk.rdpState.otherModes) &&
        !RDP_GETOM_ALPHA_CVG_SELECT(chunk.rdpState.otherModes)) {
            if (RDP_GETC32_A(chunk.rdpState.blendColor) > 0) {
                alphaTest = "if (c.a < b.a) discard; \n";
                alphaTest2 =
                    "  vec4 b = gl_LightSource[0].ambient;  \n"
                    "  if (c.a < b.a) discard; \n";
                //alphaTest2 = "if (c.a < 0.5) discard; \n";
            } else {
                alphaTest = "if (c.a == 0.0) discard; \n";
                alphaTest2 = "if (c.a == 0.0) discard; \n";
            }
    }

    if (cycle == RDP_CYCLE_TYPE_COPY) {
        sprintf(
            src, 
            "uniform sampler2D texture0;       \n"
            "                                  \n"
            "void main()                       \n"
            "{                                 \n"
            "  vec4 c = texture2D(texture0, vec2(gl_TexCoord[0])); \n"
            "  %s"
            "  %s\n"
            "}                                 \n",
            alphaTest2,
            write
            );
        c->shader = rglCreateShader(
            "void main()                                                    \n"
            "{                                                              \n"
            "  gl_Position = ftransform();                                  \n"
            "  gl_FrontColor = gl_Color;                                    \n"
            "  gl_BackColor = gl_Color;                                    \n"
            "  gl_TexCoord[0] = gl_MultiTexCoord0;                          \n"
            "}                                                              \n"
            ,
            src
            );
#ifdef RDP_DEBUG
        chunk.shader = c->shader;
#endif
        rglUseShader(c->shader);
        goto ok;
    }


    p = src;
    p +=
        sprintf(p,
        "uniform sampler2D texture0;       \n"
        "uniform sampler2D texture2;       \n"
#ifdef RGL_EXACT_BLEND
        "uniform sampler2D texture1;       \n"
#endif
        "                                  \n"
        "void main()                       \n"
        "{                                 \n"
        "vec4  c = vec4(0,0,0,0);\n"
        "vec4  e = gl_TextureEnvColor[0];\n"
        "float k5 = gl_TextureEnvColor[1][0];\n"
        "vec4  p = gl_LightSource[0].specular;\n"
#ifdef RGL_EXACT_BLEND
        "vec4  f = texture2D(texture1, vec2(gl_FragCoord.x/(2048.0*gl_TexCoord[1].x), gl_FragCoord.y/(2048.0*gl_TexCoord[1].y))); \n"
#endif
        "vec4  fog = gl_LightSource[0].diffuse;    \n"
        "vec4  b = gl_LightSource[0].ambient;  \n");

    switch (format & RGL_COMB_IN0) {
case 0:
    p +=
        sprintf(p,
        "vec4 t1 = texture2D(texture0, vec2(gl_TexCoord[0]));\n");
    break;
case RGL_COMB_IN0_DEPTH:
    p +=
        sprintf(p,
        "vec4 t1 = vec4(texture2D(texture0, vec2(gl_TexCoord[0]))[0]);\n");
    break;
    }
    switch (format & RGL_COMB_IN1) {
case 0:
    p +=
        sprintf(p,
        "vec4 t2 = texture2D(texture2, vec2(gl_TexCoord[2]));\n");
    break;
case RGL_COMB_IN1_DEPTH:
    p +=
        sprintf(p,
        "vec4 t2 = vec4(texture2D(texture2, vec2(gl_TexCoord[2]))[0]);\n");
    break;
    }

    const char * comb, * comb2;
    comb2 = 0;
    //   switch (RDP_GETOM_CVG_DEST(state.otherModes))
    //   {
    //     case 3:
    //       comb = "c = clamp(vec4((vec3(%s) - vec3(%s)) * vec3(%s) + vec3(%s), (%s - %s) * %s + %s), 0.0, 1.0);\n";
    //       break;
    //     case 2:
    //       comb = "c = vec4((vec3(%s) - vec3(%s)) * vec3(%s) + vec3(%s), (%s - %s) * %s + %s);\n";
    //       //comb = "c = vec4((vec3(%s) - vec3(%s)) * vec3(%s) + vec3(%s), t1.a*((%s - %s) * %s + %s));\n";
    //       break;
    //     case 0:
    //       //comb2 = "c = vec4((vec3(%s) - vec3(%s)) * vec3(%s) + vec3(%s), t1.a);\n";
    //     case 1:
    //       comb = "c = vec4((vec3(%s) - vec3(%s)) * vec3(%s) + vec3(%s), (%s - %s) * %s + %s);\n";
    //       break;
    //   }
    comb = "c = clamp(vec4((vec3(%s) - vec3(%s)) * vec3(%s) + vec3(%s), (%s - %s) * %s + %s), 0.0, 1.0);\n";
    strcpy(prim_lod_frac, "0.5/*PRIM_LOD_FRAC*/");
    strcpy(t1, "t1");
    strcpy(t1a, "t1.a");
    if (format & RGL_COMB_TILE7) {
        strcpy(t2, "t1");
        strcpy(t2a, "t1.a");
    } else {
        strcpy(t2, "t2");
        strcpy(t2a, "t2.a");
    }
    p +=
        sprintf(p,
        comb
        ,
        saRGB[RDP_GETCM_SUB_A_RGB0(state.combineModes)],
        saRGB[RDP_GETCM_SUB_B_RGB0(state.combineModes)],
        mRGB[RDP_GETCM_MUL_RGB0(state.combineModes)],
        aRGB[RDP_GETCM_ADD_RGB0(state.combineModes)],
        saA[RDP_GETCM_SUB_A_A0(state.combineModes)],
        sbA[RDP_GETCM_SUB_B_A0(state.combineModes)],
        mA[RDP_GETCM_MUL_A0(state.combineModes)],
        aA[RDP_GETCM_ADD_A0(state.combineModes)]
    );

    if (cycle == RDP_CYCLE_TYPE_2) {
        if (!(format & RGL_COMB_TILE7)) {
            strcpy(t1, "t2");
            strcpy(t1a, "t2.a");
            strcpy(t2, "t1");
            strcpy(t2a, "t1.a");
        }
        //strcpy(prim_lod_frac, "0.0/*PRIM_LOD_FRAC*/");
        //     if (!RDP_GETOM_ALPHA_CVG_SELECT(chunk.rdpState.otherModes))
        //       p +=
        //         sprintf(p, "  c.a = t1.a; \n");

        p +=
            sprintf(p,
            comb2? comb2 : comb
            ,
            saRGB[RDP_GETCM_SUB_A_RGB1(state.combineModes)],
            saRGB[RDP_GETCM_SUB_B_RGB1(state.combineModes)],
            mRGB[RDP_GETCM_MUL_RGB1(state.combineModes)],
            aRGB[RDP_GETCM_ADD_RGB1(state.combineModes)],
            saA[RDP_GETCM_SUB_A_A1(state.combineModes)],
            sbA[RDP_GETCM_SUB_B_A1(state.combineModes)],
            mA[RDP_GETCM_MUL_A1(state.combineModes)],
            aA[RDP_GETCM_ADD_A1(state.combineModes)]
        );
    }

    //   if (!RDP_GETOM_CVG_TIMES_ALPHA(state.otherModes))
    //     p += sprintf(p, "c.a = t1.a; \n");

    p += sprintf(p, "%s", alphaTest);


    const char * blender;
    blender = "c = vec4(float(%s)*vec3(%s) + float(%s)*vec3(%s), 1.0); \n";
#ifdef RGL_EXACT_BLEND
    const char * noblender = "c.a = 1.0;\n";
#endif

    int m1b, m1a, m2b, m2a;

    //LOG("New combiner / blender :\n%s", rglCombiner2String(state));

    if (cycle == RDP_CYCLE_TYPE_2) {
        if (RDP_GETOM_FORCE_BLEND(state.otherModes)) {
#ifndef RGL_EXACT_BLEND
            if (RDP_GETOM_BLEND_M1A_0(state.otherModes) != 1 &&
                RDP_GETOM_BLEND_M2A_0(state.otherModes) != 1) {
#endif
                    sprintf(_1ma, "(1.0 - %s)", bA[0][RDP_GETOM_BLEND_M1B_0(state.otherModes)]);
                    p +=
                        sprintf(
                        p,
                        "c = vec4(float(%s)*vec3(%s) + float(%s)*vec3(%s), c.a); \n"
                        ,bA[0][RDP_GETOM_BLEND_M1B_0(state.otherModes)],
                        bRGB[RDP_GETOM_BLEND_M1A_0(state.otherModes)],
                        bA[1][RDP_GETOM_BLEND_M2B_0(state.otherModes)],
                        bRGB[RDP_GETOM_BLEND_M2A_0(state.otherModes)]
                    );
#ifndef RGL_EXACT_BLEND
            } else {
                LOG("Blender error : fragment in cycle 1\n%s", rglCombiner2String(state));
            }
#endif

            m1b = RDP_GETOM_BLEND_M1B_1(state.otherModes);
            m1a = RDP_GETOM_BLEND_M1A_1(state.otherModes);
            m2b = RDP_GETOM_BLEND_M2B_1(state.otherModes);
            m2a = RDP_GETOM_BLEND_M2A_1(state.otherModes);
        } else {
            m1b = RDP_GETOM_BLEND_M1B_0(state.otherModes);
            m1a = RDP_GETOM_BLEND_M1A_0(state.otherModes);
            m2b = RDP_GETOM_BLEND_M2B_0(state.otherModes);
            m2a = RDP_GETOM_BLEND_M2A_0(state.otherModes);
        }
    } else {
        m1b = RDP_GETOM_BLEND_M1B_0(state.otherModes);
        m1a = RDP_GETOM_BLEND_M1A_0(state.otherModes);
        m2b = RDP_GETOM_BLEND_M2B_0(state.otherModes);
        m2a = RDP_GETOM_BLEND_M2A_0(state.otherModes);
    }

    if (RDP_GETOM_FORCE_BLEND(state.otherModes) || cycle == RDP_CYCLE_TYPE_2) {
#ifndef RGL_EXACT_BLEND
        if (m1a == 1 || m2a == 1) {
            if (/*(m1a != 1 || m1b == 3) &&*/ (m2a == 1 || m2b == 3)) {
                int src = GL_ZERO, dst = GL_ONE;
                const char * alpha = "c.a";
                switch (m1b) {
case 0: // c.a
    src = GL_SRC_ALPHA;
    break;
case 1: // fog.a
    src = GL_SRC_ALPHA;
    alpha = "fog.a";
    //             LOGERROR("Unsupported src alpha : FOG\n");
    //             LOGERROR(rglCombiner2String(state));
    break;
case 2: // shade.a
    src = GL_SRC_ALPHA;
    alpha = "gl_Color.a";
    //             LOGERROR("Unsupported src alpha : SHADE\n");
    //             LOGERROR(rglCombiner2String(state));
    break;
case 3: // 0
    src = GL_ZERO;
    break;
                }
                switch (m1a) {
case 0: // c
    if (m1b != 0 /* c.a */)
        p += sprintf(
        p, "c.a = %s; \n", alpha);
    break;
case 1: // f
    LOGERROR("Unsupported src color : FRAG\n");
    LOGERROR("%s", rglCombiner2String(state));
    break;
case 2: // b
    p += sprintf(
        p, "c = vec4(vec3(b), %s); \n", alpha);
    break;
case 3: // fog
    p += sprintf(
        p, "c = vec4(vec3(fog), %s); \n", alpha);
    break;
                }
                switch (m2b) {
case 0:
    switch (m1b) {
case 3:
    dst = GL_ONE;
    break;
default:
    dst = GL_ONE_MINUS_SRC_ALPHA;
    break;
    }
    break;
case 1:
    dst = GL_DST_ALPHA;
    break;
case 2:
    dst = GL_ONE;
    break;
case 3:
    dst = GL_ZERO;
    break;
                }

                c->srcBlend = src;
                c->dstBlend = dst;
            } else {
                LOGERROR("Unsuported blender :\n");
                LOGERROR("%s", rglCombiner2String(state));
            }
        }
        else
#endif
        {
            sprintf(_1ma, "(1.0 - %s)", bA[0][m1b]);
            p +=
                sprintf(p, blender, bA[0][m1b], bRGB[m1a], bA[1][m2b], bRGB[m2a]);
        }
    } else {
#ifdef RGL_EXACT_BLEND
        p +=
            sprintf(p,
            noblender
            );
#endif
    }

    p +=
        sprintf(
        p,
        "%s \n"
        "}                                 \n"
        ,write
        );

    rglAssert(p < src+sizeof(src));

    c->shader = rglCreateShader(
        "void main()                                                    \n"
        "{                                                              \n"
        "  gl_Position = ftransform();                                  \n"
        "  gl_FrontColor = gl_Color;                                    \n"
        "  gl_BackColor = gl_FrontColor;                                \n"
        "  gl_TexCoord[0] = gl_MultiTexCoord0;                          \n"
#ifdef RGL_EXACT_BLEND
        "  gl_TexCoord[1] = gl_MultiTexCoord1;                          \n"
#endif
        "  gl_TexCoord[2] = gl_MultiTexCoord2;                          \n"
        "}                                                              \n"
        ,
        src);

#ifdef RDP_DEBUG
    chunk.shader = c->shader;
#endif
    rglUseShader(c->shader);
    rglAssert(glGetError() == GL_NO_ERROR);

    int location;
    location = glGetUniformLocationARB(c->shader->prog, "texture0");
    glUniform1iARB(location, 0);
#ifdef RGL_EXACT_BLEND
    location = glGetUniformLocationARB(c->shader->prog, "texture1");
    glUniform1iARB(location, 1);
#endif
    location = glGetUniformLocationARB(c->shader->prog, "texture2");
    glUniform1iARB(location, 2);
    rglAssert(glGetError() == GL_NO_ERROR);

ok:;
#ifndef RGL_EXACT_BLEND
    if ((format & RGL_COMB_FMT) == RGL_COMB_FMT_DEPTH ||
        (c->srcBlend == GL_ONE && c->dstBlend == GL_ZERO))
        glDisable(GL_BLEND);
    else {
        glEnable(GL_BLEND);
        if ((format & RGL_COMB_FMT) == RGL_COMB_FMT_RGBA)
            glBlendFuncSeparate(c->srcBlend, c->dstBlend, GL_ZERO, GL_ONE);
        else
            glBlendFunc(c->srcBlend, c->dstBlend);
    }
#endif
}
Example #2
0
File: gltest.cpp Project: cxd4/z64
int main(int argc, char * * argv)
{
  const SDL_VideoInfo *videoInfo;
  Uint32 videoFlags = 0;
   
  /* Initialize SDL */
  printf("(II) Initializing SDL video subsystem...\n");
  if(SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
  {
    printf("(EE) Error initializing SDL video subsystem: %s\n", SDL_GetError());
    return -1;
  }
   
  /* Video Info */
  printf("(II) Getting video info...\n");
  if(!(videoInfo = SDL_GetVideoInfo()))
  {
    printf("(EE) Video query failed: %s\n", SDL_GetError());
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
    return -1;
  }
   
  /* Setting the video mode */
  videoFlags |= SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE;
  
  if(videoInfo->hw_available)
    videoFlags |= SDL_HWSURFACE;
  else
    videoFlags |= SDL_SWSURFACE;
  
  if(videoInfo->blit_hw)
    videoFlags |= SDL_HWACCEL;
  
  //videoFlags |= SDL_FULLSCREEN;

  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  //SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);
//   SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
//   SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
//   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
//   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
//   SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   
  printf("(II) Setting video mode %dx%d...\n", screen_width, screen_height);
  if(!(sdl_Screen = SDL_SetVideoMode(screen_width, screen_height, 0, videoFlags)))
  {
    printf("(EE) Error setting videomode %dx%d: %s\n", screen_width, screen_height, SDL_GetError());
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
    return -1;
  }
   
  char caption[500];
  sprintf(caption, "z64, LLE video plugin by Ziggy");
  SDL_WM_SetCaption(caption, caption);



  static char pixels[256*256*4];
  ilInit();
  glewInit();

  ilLoadImage("tex1.png");
  glBindTexture(GL_TEXTURE_2D, 1);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE,
               ilGetData());
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                   GL_LINEAR );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                   GL_LINEAR );
  ilLoadImage("tex2.png");
  glBindTexture(GL_TEXTURE_2D, 2);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE,
               ilGetData());
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                   GL_LINEAR );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                   GL_LINEAR );

  glDisable(GL_DEPTH_TEST);
  glDisable(GL_BLEND);

  glViewport(0, 0, screen_width, screen_height);

  glLoadIdentity();
  glScalef(2, 2, 1);
  glTranslatef(-0.5, -0.5, 0);

  rglShader_t * shader = rglCreateShader(
    "void main()                                                    \n"
    "{                                                              \n"
    "  gl_Position = ftransform();                                  \n"
    "  gl_FrontColor = gl_Color;                                    \n"
    "  gl_TexCoord[0] = gl_MultiTexCoord0;                          \n"
    "}                                                              \n"
    ,
    "uniform sampler2D texture0;       \n"
    "uniform sampler2D texture1;       \n"
    "                                  \n"
    "void main()                       \n"
    "{                                 \n"
    "  gl_FragColor = gl_Color * (texture2D(texture0, vec2(gl_TexCoord[0])) + texture2D(texture1, vec2(gl_TexCoord[0]))); \n"
    "}                                 \n"
  );
  rglUseShader(shader);

  int location;
  location = glGetUniformLocationARB(shader->prog, "texture0");
  glUniform1iARB(location, 0);
  location = glGetUniformLocationARB(shader->prog, "texture1");
  glUniform1iARB(location, 1);
  
  //glEnable(GL_TEXTURE_2D);

  glActiveTextureARB(GL_TEXTURE0_ARB);
  glBindTexture(GL_TEXTURE_2D, 1);
  glActiveTextureARB(GL_TEXTURE1_ARB);
  glBindTexture(GL_TEXTURE_2D, 2);
  

  glColor4ub(255,255,255,255);
  glEnable(GL_TEXTURE_2D);
  glBegin(GL_QUADS);
  
  glTexCoord2f(0, 0);
  glVertex2f  (0, 0);
  
  glTexCoord2f(0, 1);
  glVertex2f  (0, 1);
  
  glTexCoord2f(1, 1);
  glVertex2f  (1, 1);
  
  glTexCoord2f(1, 0);
  glVertex2f  (1, 0);
  
  glEnd();

  SDL_GL_SwapBuffers();

  getchar();

  return 0;
}