user3151321
user3151321

Reputation: 33

C++ Adding a texture to a GL_QUAD and it's coming out black

I have a series of rectangles of different colours and I'm trying to add a texture to one of them. However when I apply the texture to the given rectangle, it just turns black. Below is the function I use to load the texture.

GLuint GLWidget:: LoadTexture(const char * pic, int width, int height){

GLuint Texture;
BYTE * data;
FILE * picfile;

picfile = fopen(pic, "rb");
if (picfile == NULL)
    return 0;

data = (BYTE *)malloc(width * height * 3);

fread(data, width * height, 3, picfile);
fclose(picfile);

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

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_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB8, GL_UNSIGNED_BYTE, data);

return Texture;
}

In another function where the GL_QUADS are drawn, I then have...

    GLuint myTex = LoadTexture("texture.bmp", 500, 500);

    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glBindTexture(GL_TEXTURE_2D, myTex);

    glBegin(GL_QUADS);

    glTexCoord2f(1, 1); glVertex3f(42, 10, 42);
    glTexCoord2f(1, 0); glVertex3f(42, 10, -42);
    glTexCoord2f(0, 0); glVertex3f(-42,10,-42);
    glTexCoord2f(0, 1); glVertex3f(-42,10, 42);

    glEnd();

If anyone could let me know where I am going wrong that would be great, thanks!

Upvotes: 1

Views: 259

Answers (1)

derhass
derhass

Reputation: 45342

This call

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB8, GL_UNSIGNED_BYTE, data);

is invalid. GL_RGB8 is a valid internalFormat, but it is not a valid enum for format. Use GL_RGB, GL_UNSIGNED_BYTE as format and type if your client-side data is 3 channels with 8 but unsigned int data per channel.

Another thing is

LoadTexture("texture.bmp", 500, 500);

This suggests that you are dealing with BMP files, but your loader only deals with completely raw image data.

Upvotes: 2

Related Questions