Search code examples
opengltexture-mapping

Texturing an object with GL_TEXTURE_1D


I am working on a plug-in to an app that requires me to use OpenGL 2.1. As such, I'm using fixed-functionality and for debugging I'm currently using immediate mode drawing. I'm not using any shaders.

I'm trying to generate some simple terrain and texture it with a 1D gradient texture where the height of the terrain determines the color chosen from the texture.

What I'm seeing is that the texture isn't applied to the geometry at all, but I don't understand why.

Here's how I set up the texture:

// Set up gradient
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glEnable(GL_TEXTURE_1D);
GLuint  gradTex    = 0;
glGenTextures(1, &gradTex);
glBindTexture(GL_TEXTURE_1D, gradTex);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_FLOAT, samples);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
CheckGLError();

I have verified that the gradient texture is properly uploaded by using OpenGL Profiler on OS X to examine it. So I think that means the problem is with how I'm drawing the geometry or some other state set while drawing. Here's the drawing code:

glColor4f(1.0, 1.0, 1.0, 1.0);

glBegin(GL_QUADS);
int x, y;
for (y = 0; y < 32; y++)
{
    for (x = 0; x < 32; x++)
    {
        float   nextTexCoord = mesh [ y ][ x ].y + 0.5;
        glColor4f(nextTexCoord, nextTexCoord, nextTexCoord, 1.0);
        glTexCoord1d(nextTexCoord);
        glVertex3d(mesh [ y ][ x ].x, mesh [ y ][ x ].y * height, mesh [ y ][ x ].z);

        nextTexCoord = mesh [ y ][ x + 1 ].y + 0.5;
        glColor4f(nextTexCoord, nextTexCoord, nextTexCoord, 1.0);
        glTexCoord1d(nextTexCoord);
        glVertex3d(mesh [ y ][ x + 1 ].x, mesh [ y ][ x + 1 ].y * height, mesh [ y ][ x + 1 ].z);

        nextTexCoord = mesh [ y + 1 ][ x + 1 ].y + 0.5;
        glColor4f(nextTexCoord, nextTexCoord, nextTexCoord, 1.0);
        glTexCoord1d(nextTexCoord);
        glVertex3d(mesh [ y + 1 ][ x + 1 ].x, mesh [ y + 1 ][ x + 1 ].y * height, mesh [ y + 1 ][ x + 1 ].z);

        nextTexCoord = mesh [ y + 1 ][ x ].y + 0.5;
        glColor4f(nextTexCoord, nextTexCoord, nextTexCoord, 1.0);
        glTexCoord1d(nextTexCoord);
        glVertex3d(mesh [ y + 1 ][ x ].x, mesh [ y + 1 ][ x ].y * height, mesh [ y + 1 ][ x ].z);
    }
}
glEnd();

CheckGLError();

Just to be sure my texture coords are right, I've colored the vertexes with their texture coord. The texture coords appear to be correct - they go from 0 to 1, and my model varies from black to white as expected. My gradient does not have either white, gray, or black in it, yet my model shows up in shades of gray. What am I doing wrong? Calls to glGetError() returns no errors.


Solution

  • I figured it out. It turns out that not setting the min/mag filter causes this problem. By adding this:

    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    

    it started working. I have no idea why that would make a difference, but apparently it does!