Thread: Creating OpenGL Texture

  1. #1
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61

    Creating OpenGL Texture

    I'm writing a pixel shader for a class, and the final step is to use a 1D texture to map whatever color the fragment would be under phong shading to another color to produce a banding effect. When I run the program the object appears completely black, so I think that I am creating the texture wrong. Here is the code to generate the texture

    Code:
    // definitions actually located in a header file
    #define COLOR_MAP_WIDTH 256
    unsigned char color_map[COLOR_MAP_WIDTH][3];
    #define RED_STEP 13
    #define BLUE_STEP 27
    #define GREEN_STEP 33
    unsigned int color_map_id;
    
    void gen_texture_map(void){
    	int i;
    	for(i = 0; i < 256; ++i){
    		color_map[i][0] = (i/RED_STEP)*RED_STEP;
    		color_map[i][1] = (i/GREEN_STEP)*GREEN_STEP;
    		color_map[i][2] = (i/BLUE_STEP)*BLUE_STEP;
    	}               
    
    	glGenTextures(1, &color_map_id);
    	glBindTexture(GL_TEXTURE_1D, color_map_id);
    	glTexImage1D(GL_TEXTURE_1D, 0, 3, COLOR_MAP_WIDTH, 0, GL_RGB, GL_UNSIGNED_BYTE, color_map);
    }
    It's a 1D, RGB texture with no border, so I think I have the glTexImage1D parameters right. When the object is drawn, I pass color_map_id to it as a sampler1D and perform a texture1D lookup with it. Am I setting it up wrong? Am I doing the lookup/id passing wrong? Why is the texture not loading? Thanks in advance for any help you can give

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    Code:
    (i/RED_STEP)*RED_STEP;
    I'm a little confused, you divide by RED_STEP and then multiply by it, leaving just i?

    Apart from that, your texture generating looks fine to me.

    PS, what does '3' as the 3rd argument to TexImage1D do? I use GL_RGB.

  3. #3
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by Noise View Post
    Code:
    (i/RED_STEP)*RED_STEP;
    I'm a little confused, you divide by RED_STEP and then multiply by it, leaving just i?

    Apart from that, your texture generating looks fine to me.

    PS, what does '3' as the 3rd argument to TexImage1D do? I use GL_RGB.
    Those are integers, so it makes the values in the texture go up in steps. For example, if RED_STEP is 14, 0-13 maps to 0, 14-27 maps to 14, 28-41 maps to 28, etc.

    I changed the 3 to GL_RGB, but still no luck. After looking at some tutorials, I changed the funciton to this

    Code:
    void gen_texture_map(void){
    	int i;
    	for(i = 0; i < 256; ++i){
    		color_map[i][0] = (i/RED_STEP)*RED_STEP;
    		color_map[i][1] = (i/GREEN_STEP)*GREEN_STEP;
    		color_map[i][2] = (i/BLUE_STEP)*BLUE_STEP;
    	}               
    
    	glEnable(GL_TEXTURE_1D);
    	glGenTextures(1, &color_map_id);
    	glBindTexture(GL_TEXTURE_1D, color_map_id);
    	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    	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_MAG_FILTER, GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    	glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, COLOR_MAP_WIDTH, 0, GL_RGB, GL_UNSIGNED_BYTE, color_map);
    }
    And call glBindTexture(GL_TEXTURE_1D, color_map_id) before rendering the object. Still nothing

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    You will need those glTexParameteri calls to set those parameters for the texture, but you shouldn't need to do that other stuff. You would need to call glBindTexture before rendering if the active texture might have changed.

    I believe that the problem is very likely elsewhere in the program.

  5. #5

    Join Date
    May 2005
    Posts
    1,042
    I'm wondering if integer casting is screwing up your code. Try:

    -ceil and floor functions
    -manually put in (int) and (float) where you're casting

    Those are the first few things I'd try.
    I'm not immature, I'm refined in the opposite direction.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    Wait shouldn't color_map be a char array? Ints are 4 bytes long and chars are 1 byte, so it will sort of just be getting the first quarter of the array when you pass it to glTexImage1D. Make color_map an unsigned char.
    Last edited by Noise; 12-07-2008 at 09:17 PM.

  7. #7

    Join Date
    May 2005
    Posts
    1,042
    Yeah good point, OpenGL expects it to be byte data based on this line's GL_UNSIGNED_BYTE


    Code:
    glTexImage1D(GL_TEXTURE_1D, 0, 3, COLOR_MAP_WIDTH, 0, GL_RGB, GL_UNSIGNED_BYTE, color_map);
    I'm not immature, I'm refined in the opposite direction.

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    color_map is declared as an unsigned char already.

    >When I run the program the object appears completely black,

    Can you see the object if you just color it (without texturing?). Maybe the object is not being drawn in a viewable area.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    Oh yes sorry I was seeing color_map_id.

    Yes you can see an object without texturing it. Unless there are no lights. I suggest turning both texturing and lighting off. Then if you still can't see it, it may be out of the camera's view so try turning the camera around.

  10. #10
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Sorry for the late reply - this was due a while ago

    Thanks for the suggestions all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Texture Binding with OpenGL
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 07-01-2008, 11:02 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM