Creating Texture2d from a byte array

Hey guys,

I am trying to read in a photoshop .raw file and use its data to create an OpenGl texture. Here is the code I have at the minute


GLuint _texture;

LoadTestTexture(int width, int height, string fileName) {

	BYTE * data = new BYTE[width * height * 3];
	FILE * file;

	//read texture data
	fopen_s(&file, fileName.c_str(), "rb");
	fread(data, width * height * 3, 1, file);
	fclose(file);

	glGenTextures(1, &_texture);
	glBindTexture(GL_TEXTURE_2D, _texture);

	 // elect modulate to mix texture with color for shading
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    //when texture area is small, bilinear filter the closest mipmap
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    // when texture area is large, bilinear filter the first mipmap
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //the texture wraps over at the edges
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_BYTE, data);

//build texture mipmaps
	//gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_BYTE, data);
}

However, when I go to draw the texture there only seems to be a red square appearing. I looked into what values were being extracted from the .raw file, and they were along the lines of 255, 157, 38 etc ie They weren’t all 255 (which would explain the solid red texture).

The image file is a white image with black lines drawn over it. Does anyone have any ideas what I’m doing wrong? Thanks in advance!

I’m posting this as a follow up, because everyone hates the guy who just abandons his own help thread when he gets the problem solved.

I’m not sure how I fixed this to be honest. I gave up and went on to other things until today, when I started a new project. The code is mostly the same except it is in a new project.

Upon drawing the scene I see a white texture, which is more like the image I was hoping to display. When I moved the camera back the correct white image with black lines appeared for me.

So, if anyone out there is having a similar sort of issue, try moving the camera out a bit to see what you can see.


GLuint Texture::NumOfTextures = 0;

GLuint Texture::LoadTexture(int width, int height, std::string fileName) {

	unsigned char * data =  (unsigned char *)malloc( width * height * 3 );
	FILE * file;

	//read texture data
	fopen_s(&file, fileName.c_str(), "rb");
	fread(data, width * height * 3, 1, file);
	fclose(file);

	glGenTextures(1, &NumOfTextures);
	glBindTexture(GL_TEXTURE_2D, NumOfTextures);

	// select modulate to mix texture with color for shading
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    // when texture area is small, bilinear filter the closest mipmap
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    // when texture area is large, bilinear filter the first mipmap
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//clamp the texture when stretched
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    // build our texture mipmaps
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

	//free the allocated memory
	free(data);

	NumOfTextures++;

	return NumOfTextures - 1;
}

Though I now have a problem where my image appears to be tiled 3x3

The black lines appearing is probably because of the sampling being used - the smaller the area to render the texture the more pixels that have to be discarded.