Results 1 to 10 of 10

Thread: textures showing me raw heap contents

  1. #1
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    20

    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

    Code :
         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);

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,103
    Quote Originally Posted by question2 View Post
    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?
    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).

  3. #3
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    20
    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.

  4. #4
    Senior Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    Posts
    890
    Your memset arguments are swapped.

  5. #5
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    20
    That is true although when I switch back to using the following I get white where there used to be textures.

    Code :
    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.

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,103
    Quote Originally Posted by question2 View Post
    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?
    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().

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,103
    Quote Originally Posted by question2 View Post
    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.
    It's actually only 33% more than a non-mipmapped texture.

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

  8. #8
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    20
    Quote Originally Posted by GClements View Post
    It's actually only 33% more than a non-mipmapped texture.
    1 + 1/4 + 1/16 + 1/64 + ... = 1+1/3
    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?

  9. #9
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674
    Quote Originally Posted by question2 View Post
    Yes but if I have 100 textures or even 500, that would use up all of the video RAM, I assume.
    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.

  10. #10
    Senior Member Regular Contributor
    Join Date
    Mar 2015
    Posts
    296
    Quote Originally Posted by GClements View Post
    It's actually only 33% more than a non-mipmapped texture.

    1 + 1/4 + 1/16 + 1/64 + ... = 1+1/3
    very good, the sum of the power series, 1/(1-1/x); where x = (-1,1);

Similar Threads

  1. Textures showing inverted (upside down) !
    By imported_fakhir in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-10-2013, 04:37 PM
  2. help visualising the contents of Integer textures
    By BionicBytes in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 10-14-2011, 03:10 PM
  3. Scale the Contents of pixelBuffer
    By Seagate123 in forum OpenGL: macOS
    Replies: 2
    Last Post: 01-18-2010, 09:45 PM
  4. Texture Object Contents
    By Zeno in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 06-01-2001, 12:07 AM
  5. Where's the drawing contents
    By danny1238 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 04-18-2001, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean