简体   繁体   中英

How to load a hardcoded image into an OpenGL texture

I'm attempting to load an image that I have stored as a large array of unsigned chars. More specifically, an array of structs made of unsigned chars

typedef struct {
    unsigned char
        r,
        g,
        b;
}RGB;

The image data itself is exported from gimp as a very large c source file which looks something like this

RGB Image[25000]={
    {0,0,0},{0,0,0},{0,0,0},{174,174,174},{175,175,175},{177,177,177},
    {180,180,180},{183,183,183},{187,187,187},{192,192,192},{196,196,196},
    {202,202,202},{207,207,207},{213,213,213},{239,239,239},
    ...
}

I have the image width, height and know that it's in 8-bit format RGB. I'm trying to pass it to glTexImage2D, but it always renders all black.

Say something like

unsigned int imageWidth = 960;
unsigned int imageHeight = 540;

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, &Image);

The problem is not my shader code or anywhere else in the program, as everything works if I load a texture from a file. I just don't know if this data can be read in as is or if I have to prep it somehow, or anything else really, I'm newbish with openGL.

My goal is to hard code a couple textures into the app so they can't be swapped or modded out easily. I'm using Gimp plugins to export the texture to c source. There's another Gimp export plugin which provides source that I have working on gcc/g++ compiler but when I try to compile from Visual Studio it complains that the string is to long

compiler limit: string exceeds 65535 bytes in length

It works on gcc/g++ however. The source looks like this in that case

static const struct Image{
  unsigned int       width;
  unsigned int       height;
  unsigned int       bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */ 
  unsigned char      pixel_data[960 * 540 * 4 + 1];
} ImageImage = {
  960, 540, 4,
  "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000"
  "\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000"
  "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000"
  "\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377"
  "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000"
  "\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000"
  "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000"
  "\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377"
...
}

That works on gcc/g++, but visual studio complains. It is a really really long file, so I'm not to surprised, but It's a real bummer I can't get it to work. I've tried parsing through it by making each max limit string into a std::string and pass each unsigned char from those into a vector of unsigned chars, which I know I shouldn't do, but I'm grasping at straws at this point

std::vector<unsigned char> _charVec;
std::string tempString = "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000"
      "\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000"
      "\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000"
      "\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377"
      "\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000\000\000\377\000"
...
_charVec.insert(_charVec.end(), tempString.begin(), tempString.end());

that doesn't do the trick anyway. It stops the compiler error, but it causes a crash when I try to access any class object the data is stored in, or pass the container to another method.

I don't have a another way to convert my textures into source, and there are one or two that I would definitely prefer not be distributed raw, so I would like to get one of these two texture sources loading in openGL. The first source format is probably what I'll need to go with as long as I'm building with Visual Studio, as it can't handle the long strings in the second method and attempting to fill vectors or arrays from it seems like more extra steps than needed. I'm probably going about this all wrong as I'm not very familiar with openGL's nuances yet. Any advice would be greatly appreciated.

The first method of exporting the image as usable source data does indeed work, my problem was in my Visual Studio code base I forgot to set the texture parameters in my "hardcoded" loading method

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Thanks for reminding me in the comments, derhass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM