[For David] Half Dynamic Range

Luminary
Posts: 5,143
Joined: 2002.04
Post: #1
Here's how to compute (tex - 0.5) * 2 using texture_env_combine.

Executive summary:

Code:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
    GLfloat subtractColor[4] = { 0.5, 0.5, 0.5, 0.0 };
    glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, subtractColor);
    glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT);
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT);
    glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE, 2.0f);

Full working program:

Code:
#include <stdlib.h>

#if defined(__APPLE_CC__)
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
#else
#include <GL/glut.h>
#include <GL/glext.h>
#endif

GLubyte texture[16] =
{
    192, 0, 0, 255,
    0, 192, 0, 255,
    0, 0, 192, 255,
    192, 192, 0, 255
};

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    static GLdouble angle = 0.0;
    angle += 0.001;
    if (angle > 6.283185308)
    {
        angle -= 6.283185308;
    }

    glLoadIdentity();
    gluLookAt(5.0, 5.0, 0.0,
              0.0, 0.0, 0.0,
              0.0, 1.0, 0.0);
    glRotatef(180 * angle / 3.141592654, 0, 1, 0);
    
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(-1, -1);
    glTexCoord2f(1, 0);
    glVertex2f(1, -1);
    glTexCoord2f(1, 1);
    glVertex2f(1, 1);
    glTexCoord2f(0, 1);
    glVertex2f(-1, 1);
    glEnd();

    glutSwapBuffers();
    glutReportErrors();
}

void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLdouble)width/(GLdouble)height, 0.01, 100);
    glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(640, 480);

    (void)glutCreateWindow("GLUT Program");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_CULL_FACE);
    
    glBindTexture(GL_TEXTURE_2D, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
    GLfloat subtractColor[4] = { 0.5, 0.5, 0.5, 0.0 };
    glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, subtractColor);
    glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT);
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
    glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT);
    glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE, 2.0f);

    glutMainLoop();

    return EXIT_SUCCESS;
}
Quote this message in a reply
Member
Posts: 145
Joined: 2002.06
Post: #2
Not sure what this is for, but if you're looking for something like Unreal's Modulate mode, you want something like this:

glBlendFunc(GL_DST_COLOR,GL_SRC_COLOR);

Drawing with that blend mode, black will turn what it's drawn on top of black, 50% gray will leave the background unaltered, and white will double the brightness of what it's drawn on top of.

"He who breaks a thing to find out what it is, has left the path of wisdom."
- Gandalf the Gray-Hat

Bring Alistair Cooke's America to DVD!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Vertex Array Range bonanza 3 3,789 Jun 19, 2007 12:42 PM
Last Post: OneSadCookie
  Texture filter only works half the time Jake 10 3,868 Sep 6, 2004 03:05 PM
Last Post: Jake