textures showing me raw heap contents

Hi folks,
I’m trying to build an understanding of how to use textures. I’ve been scouring the various webpages that claim to explain how to do this but many of the examples actually don’t work.
Here’s some code that I’m trying to use but for some reason it does not work. Can anyone diagnose what is wrong with this code? It’s virtually identical to the many examples I’ve seen on the web.
Thanks

     GLuint texture;
        glGenTextures (1, &texture);

        glBindTexture (GL_TEXTURE_2D, texture);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

        glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        bool wrap = false;
        glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP);
        glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP);

        uint32_t n_bytes = 3 *  textureWidth *  textureHeight;
        _pixels = malloc (n_bytes);
        if (!_pixels) {
                return false;
        }

	// Fill it with a random grayscale value.
        memset (_pixels, n_bytes, rand()&255);

        glTexImage2D (GL_TEXTURE_2D, 0, 3, textureWidth, textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, _pixels);
		 glEnable (GL_TEXTURE_2D); // UNINTUITIVE: This has to be here, not during initialization.
        free (_pixels);
		          
        glPushMatrix();
        glTexCoord2d(0.0,0.0); glVertex3d(x0,y0,depth);
        glTexCoord2d(1.0,0.0); glVertex3d(x1,y0,depth);
        glTexCoord2d(1.0,1.0); glVertex3d(x1,y1,depth);
        glTexCoord2d(0.0,1.0); glVertex3d(x0,y1,depth);
        glEnd();

        glPopMatrix();
	//glDeleteTextures (1, &texture);

You’re setting the minification filter to GL_LINEAR_MIPMAP_NEAREST, but you’re only uploading mipmap level zero.

If you use a minifaction filter which requires mipmaps but you don’t provide all of the mipmap levels, the result is as if texturing is disabled.

Either:

  1. change the minification filter to one which doesn’t require mipmaps,
  2. build the mipmap levels with glGenerateMipmap(),
  3. use gluBuild2DMipmaps() instead of glTexture2D(), or
  4. create and upload the additional mipmap levels manually.

Note that option #2 requires OpenGL 3.0 or later, while option #3 is deprecated (along with the rest of GLU).

For this project I don’t really need mipmaps, since the textures are all going onto flat rectangular surfaces.

Here’s a question: Am I supposed to be creating new textures for each frame i.e. each call to draw_scene? And then deleting the textures at the end of draw_scene?
Thanks.

Your memset arguments are swapped.

That is true although when I switch back to using the following I get white where there used to be textures.

glTexImage2D (GL_TEXTURE_2D, 0, 3, _textureWidth, _textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, _pixels);
free (_pixels);
glEnable (GL_TEXTURE_2D);

I hope I’m not somehow required to use mipmaps just to get this to work, because I gather they use a lot more video RAM.

No.

Typically, you create them either on startup, or at a convenient point prior to their use, then either delete them when you know that you won’t be needing them for a while, or just allow them to be deleted automatically when the program terminates. E.g. a game might change the set of existing textures when changing between maps.

You select which texture(s) to use for a drawing operation with glBindTexture().

[QUOTE=question2;1266777]
I hope I’m not somehow required to use mipmaps just to get this to work, because I gather they use a lot more video RAM.[/QUOTE]
It’s actually only 33% more than a non-mipmapped texture.

1 + 1/4 + 1/16 + 1/64 + … = 1+1/3

[QUOTE=GClements;1266779]It’s actually only 33% more than a non-mipmapped texture.
1 + 1/4 + 1/16 + 1/64 + … = 1+1/3[/QUOTE]

Yes but if I have 100 textures or even 500, that would use up all of the video RAM, I assume. Does OpenGL swap out less-used textures into main memory?

It rather depends on the sizes of those textures and the size of video memory.

Also, using mipmaps improves performance in most cases of texture mapping. So it’s a memory vs. performance tradeoff.

Most important of all… if you don’t want to use mipmaps, do what GClements said: stop using a GL_TEXTURE_MIN_FILTER mode that requires mipmaps.

[QUOTE=GClements;1266779]It’s actually only 33% more than a non-mipmapped texture.

1 + 1/4 + 1/16 + 1/64 + … = 1+1/3[/QUOTE]

very good, the sum of the power series, 1/(1-1/x); where x = (-1,1);