Texture only makes white square

I am trying to learn how to use textures. Unfortunately, all my attempts result in a white square. From what I understand, this is very common, but I can’t find any solutions online to solve my problem.

I set up two different sets of code: one loads a bitmap from file and displays it using glDrawPixels. This works, so I know the image is being loaded correctly. The second set of code generates, binds, and then uses glTexImage2D, but it only makes a white square. :frowning:

Here are the two sample codes. Is there anything obvious that is wrong about this code?


// THIS WORKS

// Object constructor
p_mybitmap = new MyBitmap;
p_mybitmap->load_24_bit_bitmap("C:\\CppPrograms\\New Interface\\Res\\OptionsButtonUp.bmp");

// In Render Function
glRasterPos2i(0,0);
glDrawPixels(p_mybitmap->width,p_mybitmap->height, GL_RGBA, GL_UNSIGNED_BYTE, p_mybitmap->p_data);


// THIS PRODUCES A WHITE SQUARE
// object constructor
glEnable(GL_TEXTURE_2D);
p_background_image = new MyBitmap();
if(p_background_image->load_24_bit_bitmap("C:\\CppPrograms\\New Interface\\Res\\OptionsButtonUp.bmp"))
{
	glGenTextures(1, &background_image);
	glBindTexture(GL_TEXTURE_2D, background_image);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, p_background_image->width, p_background_image->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_background_image->p_data);
// break points make it here, so I know the above 3 lines of code are called.
}

// In Render function
glEnable(GL_TEXTURE_2D);

glBegin(GL_QUADS);
	glBindTexture(GL_TEXTURE_2D, background_image);
	glTexCoord2f(0.0f,1.0f); glVertex3f(0, m_height, 0.0);
	glTexCoord2f(0.0f,0.0f); glVertex3f(0, 0.0, 0.0);
	glTexCoord2f(1.0f,0.0f); glVertex3f(m_width, 0.0, 0.0);
	glTexCoord2f(1.0f,1.0f); glVertex3f(m_width, m_height, 0.0);
glEnd();
// the polygon itself is appearing normally. . . I have another variant of the program that just fills the polygon with various colors, so I know it's being rendered appropriately.

http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture

I’ve made the following change, and it still gives me a white square.


p_background_image = new MyBitmap();
if(p_background_image->load_24_bit_bitmap("C:\\CppPrograms\\New Interface\\Res\\OptionsButtonUp.bmp"))
{
	glGenTextures(1, &background_image);
	glBindTexture(GL_TEXTURE_2D, background_image);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, p_background_image->width, p_background_image->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, p_background_image->p_data);
}

Does anyone know why this is happening?

Edit: I dunno know if this matters but the values of m_height and m_width in the rendering part of the code are both .15607427. Does that affect it?

From what I can tell, the following line sets the GL error to 1821 (GL_INVALID_VALUE):


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, p_mybitmap->width, p_mybitmap->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, p_mybitmap->p_data);

What does this mean? I’m a little confused since this line works perfectly:


glDrawPixels(p_mybitmap->width,p_mybitmap->height, GL_RGBA, GL_UNSIGNED_BYTE, p_mybitmap->p_data);

Check the list of errors at http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml

It’s likely an error with the values of width + height you are providing. Either they’re negative values, too big values (use glGetIntegerv to check GL_MAX_TEXTURE_SIZE), or non power of two values and you’re not using OpenGL 2.0/OpenGL 1.4 + the extension GL_ARB_texture_non_power_of_two.

Ok. I set a breakpoint and got this information. . .

glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_size) returns a size 2048. The height is +82 and the width is +84.

I’m using the version of OpenGL that comes with Visual Studio 2010. None of this seems to conflict. This makes no sense. :frowning:

Having up to date headers etc. does not mean you will automatically be using a high version of OpenGL - your hardware/drivers also need to support it(*).

Have you checked what version of OpenGL is being used at runtime + searched the extension string for GL_ARB_texture_non_power_of_two.


version = glGetString(GL_VERSION);
extensions = glGetString(GL_EXTENSIONS);

(*) For core profiles, the version also needs to be explicitly requested, but for compatibility profiles it may provide you with the highest version that is compatible with the version you requested. Creating a context with eg. wglCreateContext will give you the highest context type compatible with OpenGL 1.0, which if you have recent hardware + up to date drivers is currently OpenGL 4.2

Ok, for version I get:


1.3.3842 WinXP Release

(i.e. a dinosaur version, I presume)

for extensions, I get:


GL_ARB_multitexture
GL_EXT_texture_env_add
GL_EXT_compiled_vertex_array
GL_S3_s3tc
GL_ARB_point_parameters
GL_ARB_texture_border_clamp
GL_ARB_texture_compression
GL_ARB_texture_cube_map
GL_ARB_texture_env_add
GL_ARB_texture_env_combine
GL_ARB_texture_env_crossbar
GL_ARB_texture_env_dot3
GL_ARB_texture_mirrored_repeat
GL_ARB_transpose_matrix
GL_ARB_vertex_blend
GL_ARB_vertex_program
GL_ARB_window_pos
GL_ATI_element_array
GL_ATI_envmap_bumpmap
GL_ATI_fragment_shader
GL_ATI_map_object_buffer
GL_ATI_texture_env_combine3
GL_ATI_texture_mirror_once
GL_ATI_vertex_array_object
GL_ARB_vertex_buffer_object
GL_ATI_vertex_attrib_array_object
GL_ATI_vertex_streams
GL_ATIX_texture_env_combine3
GL_ATIX_texture_env_route
GL_ATIX_vertex_shader_output_point_size
GL_EXT_abgr
GL_EXT_bgra
GL_EXT_blend_color
GL_EXT_blend_func_separate
GL_EXT_blend_minmax
GL_EXT_blend_subtract
GL_EXT_clip_volume_hint
GL_EXT_draw_range_elements
GL_EXT_fog_coord
GL_EXT_multi_draw_arrays
GL_EXT_packed_pixels
GL_EXT_point_parameters
GL_EXT_rescale_normal
GL_EXT_secondary_colorGL_EXT_separate_specular_color 
GL_EXT_stencil_wrap
GL_EXT_texgen_reflection
GL_EXT_texture3D
GL_EXT_texture_compression_s3tc
GL_EXT_texture_cube_map
GL_EXT_texture_edge_clamp
GL_EXT_texture_env_combine
GL_EXT_texture_env_dot3
GL_EXT_texture_filter_anisotropic
GL_EXT_texture_lod_bias
GL_EXT_texture_object
GL_EXT_texture_rectangle
GL_EXT_vertex_array
GL_EXT_vertex_shader
GL_HP_occlusion_test
GL_NV_texgen_reflection
GL_NV_blend_square
GL_NV_occlusion_query
GL_SGI_color_matrix
GL_SGIS_texture_edge_clamp
GL_SGIS_texture_border_clamp
GL_SGIS_texture_lod
GL_SGIS_generate_mipmap
GL_SGIS_multitexture 

Since I don’t see the GL_ARB_texture_non_power_of_two extension, then I’m guessing that my computer currently doesn’t have that capacity.

How do I update this? If it matters, I’m using a Dell Latitude D600. It has a crank of the side that I need to turn once in a while to keep it going. I have Mobility Radeon 9000. It’s one step up from a Lite-Brite.

Also, why is the non-power-of-tow an issue when my image is 84 pixels wide? (each is 4 bytes)

Also, why is the non-power-of-tow an issue when my image is 84 pixels wide? (each is 4 bytes)

That should be explained in the book.
Power of 2 values are

1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, etc. up to whatever the maximum that the GPU can handle.

Oh geez. How embarrassing. I kept thinking it meant numbers divisible by 2. Yes, that is rather obvious now that you point it out. :smiley:

Thanks.