Выбрать главу

 glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);

 glEnable(GL_LIGHT1);

}

Here we load LOTS of textures. Since auxDIBImageLoad() has an error-handler of it's own and since LoadBMP() wasn't much predictable without a try-catch-block, I just kicked it. But now to our loading-routine. First, we load the base-bitmap and build three filtered textures out of it ( GL_NEAREST, GL_LINEAR and GL_LINEAR_MIPMAP_NEAREST). Note that I only use one data-structure to hold bitmaps, since we only need one at a time to be open. Over that I introduced a new data-structure called alpha here. It is to hold the alpha-layer of textures, so that I can save RGBA Images as two bitmaps: one 24bpp RGB and one 8bpp greyscale Alpha. For the status-indicator to work properly, we have to delete the Image-block after every load to reset it to NULL.

Note also, that I use GL_RGB8 instead of just "3" when specifying texture-type. This is to be more conformant to upcoming OpenGL-ICD releases and should always be used instead of just another number. I marked it in orange for you.

int LoadGLTextures() { // Load Bitmaps And Convert To Textures

 bool status=true; // Status Indicator

 AUX_RGBImageRec *Image=NULL; // Create Storage Space For The Texture

 char *alpha=NULL;

 // Load The Tile-Bitmap for Base-Texture

 if (Image=auxDIBImageLoad("Data/Base.bmp")) {

  glGenTextures(3, texture); // Create Three Textures

  // Create Nearest Filtered Texture

  glBindTexture(GL_TEXTURE_2D, texture[0]);

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Image->sizeX, Image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Image->data);

  // Create Linear Filtered Texture

  glBindTexture(GL_TEXTURE_2D, texture[1]);

  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_RGB8, Image->sizeX, Image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Image->data);

  // Create MipMapped Texture

  glBindTexture(GL_TEXTURE_2D, texture[2]);

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, Image->sizeX, Image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, Image->data);

 }

 else status=false;

 if (Image) { // If Texture Exists

  if (Image->data) delete Image->data; // If Texture Image Exists

  delete Image;

  Image=NULL;

 }

Now we'll load the Bump Map. For reasons discussed later, it has to have only 50% luminance, so we have to scale it in the one or other way. I chose to scale it using the glPixelTransferf()-commands, that specifies how data from bitmaps is converted to textures on pixel-basis. I use it to scale the RGB components of bitmaps to 50%. You should really have a look at the glPixelTransfer()-command family if you're not already using them in your programs. They're all quite useful.

Another issue is, that we don't want to have our bitmap repeated over and over in the texture. We just want it once, mapping to texture-coordinates (s,t)=(0.0f, 0.0f) thru (s,t)=(1.0f, 1.0f). All other texture-coordinates should be mapped to plain black. This is accomplished by the two glTexParameteri()-calls that are fairly self-explanatory and "clamp" the bitmap in s and t-direction.

 // Load The Bumpmaps

 if (Image=auxDIBImageLoad("Data/Bump.bmp")) {

  glPixelTransferf(GL_RED_SCALE,0.5f); // Scale RGB By 50%, So That We Have Only

  glPixelTransferf(GL_GREEN_SCALE,0.5f); // Half Intenstity

  glPixelTransferf(GL_BLUE_SCALE,0.5f);

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); // No Wrapping, Please!

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

  glGenTextures(3, bump); // Create Three Textures

  // Create Nearest Filtered Texture

  >…<

  // Create Linear Filtered Texture

  >…<

  // Create MipMapped Texture

  >…<

You'll already know this sentence by now: For reasons discussed later, we have to build an inverted Bump Map, luminance at most 50% once again. So we subtract the bumpmap from pure white, which is {255, 255, 255} in integer representation. Since we do NOT set the RGB-Scaling back to 100% (took me about three hours to figure out that this was a major error in my first version!), the inverted bumpmap will be scaled once again to 50% luminance.

  for (int i=0; i<3*Image->sizeX*Image->sizeY; i++) // Invert The Bumpmap

   Image->data[i]=255-Image->data[i];

  glGenTextures(3, invbump); // Create Three Textures

  // Create Nearest Filtered Texture

  >…<

  // Create Linear Filtered Texture

  >…<

  // Create MipMapped Texture

  >…<

 } else status=false;

 if (Image) { // If Texture Exists

  if (Image->data) delete Image->data; // If Texture Image Exists

  delete Image;

  Image=NULL;

 }

Loading the Logo-Bitmaps is pretty much straightforward except for the RGB-A recombining, which should be self-explanatory enough for you to understand. Note that the texture is built from the alpha-memoryblock, not from the Image-memoryblock! Only one filter is used here.

 // Load The Logo-Bitmaps

 if (Image=auxDIBImageLoad("Data/OpenGL_ALPHA.bmp")) {

  alpha=new char[4*Image->sizeX*Image->sizeY];

  // Create Memory For RGBA8-Texture

  for (int a=0; a<Image->sizeX*Image->sizeY; a++)

   alpha[4*a+3]=Image->data[a*3]; // Pick Only Red Value As Alpha!

  if (!(Image=auxDIBImageLoad("Data/OpenGL.bmp"))) status=false;

  for (a=0; a<Image->sizeX*Image->sizeY; a++) {

   alpha[4*a]=Image->data[a*3]; // R

   alpha[4*a+1]=Image->data[a*3+1]; // G

   alpha[4*a+2]=Image->data[a*3+2]; // B

  }

  glGenTextures(1, &glLogo); // Create One Textures

  // Create Linear Filtered RGBA8-Texture

  glBindTexture(GL_TEXTURE_2D, glLogo);

  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, Image->sizeX, Image->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, alpha);

  delete alpha;

 } else status=false;

 if (Image) { // If Texture Exists

  if (Image->data) delete Image->data; // If Texture Image Exists