In C++, OpenGL, Glut, how to bind image.c to a texture, where image.c comes from Gimp>Export>C source code

StackOverflow https://stackoverflow.com/questions/22224897

Question

So I've spent past two days looking through different kinds of 'solutions' to my question via google, there aren't all that many and the ones I've find don't seem to work.

I'm exporting a small test image as .c resource file from Gimp, it's size is 64x64 and it has an alpha channel. Basically looks like:

    static const struct {
    unsigned int     width;
    unsigned int     height;
    unsigned int     bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */ 
    char            *comment;
    unsigned char    pixel_data[64 * 64 * 4 + 1];
    } ship = {
    64, 64, 4,
    (char*) 0,
    "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
    "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
    "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
    "\0\0\0\0\0\0\0\0\237\237\237\377\237\237\237\377\237\237\237\377\237\237"
    "\237\377\237\237\237\377\237\237\237\377\237\237\237\377vZI\0vZI\0vZI\0\0"
    "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

and goes on like that for quite a while, as you might expect, until finally ending with

    "\237\237\377\237\237\237\377\237\237\237\377",
    };

So how can I actually use this resource file? If anyone could provide an example, a bare minimum that is needed to create a square with the texture stamped on it, I'd be most appreciative.

Was it helpful?

Solution

Looking at the reference page for glTexImage2D, it is done like (from here) :

GLuint texName1 = 0;

glGenTextures(1, &texName1);
glBindTexture(GL_TEXTURE_2D, texName1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, ship.bytes_per_pixel, ship.width, ship.height, 0, GetDataFormat(), GetDataType(), ship.pixel_data);

glColor3f(1, 1, 0);
glBindTexture(GL_TEXTURE_2D, texName1);
glBegin(GL_QUADS);
    glTexCoord2f (0.0, 0.0);
    glVertex3f (0.0, 0.0, -5.0f);
    glTexCoord2f (1.0, 0.0);
    glVertex3f (.5, 0.0, -5.0f);
    glTexCoord2f (1.0, 1.0);
    glVertex3f (.5, .5, -5.0f);
    glTexCoord2f (0.0, 1.0);
    glVertex3f (0.0, .5, -5.0f);
glEnd();

The key line is this :

glTexImage2D(GL_TEXTURE_2D, 0, ship.bytes_per_pixel, ship.width, ship.height, 0, GetDataFormat(), GetDataType(), ship.pixel_data);

You need to implement GetDataFormat() and GetDataType() yourself, and it returns the data format and type.

One possible implementation :

GLenum GetDataFormat(){
  return  GL_BGRA;
}
GLenum GetDataType(){
  return  GL_UNSIGNED_BYTE;
}

OTHER TIPS

add the file to your source code. ship structure object is created with all info needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top