Volodymyr B.
Volodymyr B.

Reputation: 3441

transform code from OpenGL 2.1 to OpenGL 3.2

I want to understand this old code and translate it for newest version of OpenGL with using shaders:

        if (channel == Alpha) {
            glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        } else {
            // replicate color into alpha
            if (GL_ARB_texture_env_dot3) {
                switch (channel) {
                case Red: 
                    glColor3f(1.0, 0.5, 0.5); 
                    break;                
                case Green: 
                    glColor3f(0.5, 1.0, 0.5); 
                    break;
                case Blue: 
                    glColor3f(0.5, 0.5, 1.0); 
                    break;
                default:
                    // should not happen!
                    assert(0);
                }
            } else {
                // should not happen!
                assert(0);
            }

            glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
            glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGBA_ARB);
            glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
            glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
            glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_PRIMARY_COLOR);
            glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_EXT, GL_SRC_COLOR);
        }
      <draw models>

My ideas whats happening:

if channe = Alpha
   just replace alpha
else
   // no idea why used glColor3f
   looks like it makes one of the colors 100% bright
   and then in magical 6 glTexEnvi lines it transforms to alpha

Upvotes: 2

Views: 368

Answers (1)

datenwolf
datenwolf

Reputation: 162164

It set's up a normal mapping texture. The channel switch defines, which axis the "normal" vector points to and sets the color accordingly; today you'd use a uniform for that.

The equation the texture environment for the case that the texture does not consist of merely an alpha channel sets something, that looks like the following fragment shader

uniform vec3 primary_direction; // instead of primary color
uniform sampler… tex;

in vec2 tex_coord;

void main()
{
    gl_FragColor = vec4( dot(primary_direction, texture(tex, tex_coord)), 1);
}

Upvotes: 3

Related Questions