LiFeng_Texture.cpp
资源名称:虚拟地形建模.rar [点击查看]
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:6k
源码类别:
OpenGL
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "LiFeng_Texture.h"
- #include "LiFeng.h"
- LiFeng_Texture::LiFeng_Texture()
- {
- }
- LiFeng_Texture::~LiFeng_Texture()
- {
- }
- GLuint LiFeng_Texture::TextureLoad(char *filename,
- GLboolean alpha,
- GLenum minfilter,
- GLenum magfilter,
- GLenum wrap)
- {
- int i; /* Looping var */
- BITMAPINFO *info; /* Bitmap information */
- GLubyte *bits; /* Bitmap RGB pixels */
- GLubyte *ptr; /* Pointer into bit buffer */
- GLubyte *rgba; /* RGBA pixel buffer */
- GLubyte *rgbaptr; /* Pointer into RGBA buffer */
- GLubyte temp; /* Swapping variable */
- GLenum type; /* Texture type */
- GLuint texture; /* Texture object */
- /* Try loading the bitmap file... */
- bits = LoadDIBitmap(filename, &info);
- if (bits == (GLubyte *)0)
- return (0);
- for (i = info->bmiHeader.biWidth * info->bmiHeader.biHeight, ptr = bits;
- i > 0;
- i --, ptr += 3)
- {
- /* Swap red and blue */
- temp = ptr[0];
- ptr[0] = ptr[2];
- ptr[2] = temp;
- }
- if (info->bmiHeader.biHeight == 1)
- type = GL_TEXTURE_1D;
- else
- type = GL_TEXTURE_2D;
- /* Create and bind a texture object */
- glGenTextures(1, &texture);
- glBindTexture(type, texture);
- /* Set texture parameters */
- glTexParameteri(type, GL_TEXTURE_MAG_FILTER, magfilter);
- glTexParameteri(type, GL_TEXTURE_MIN_FILTER, minfilter);
- glTexParameteri(type, GL_TEXTURE_WRAP_S, wrap);
- glTexParameteri(type, GL_TEXTURE_WRAP_T, wrap);
- glTexEnvi(type, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- if (alpha)
- {
- /* Create and use an RGBA image... */
- rgba =new GLubyte [info->bmiHeader.biWidth * info->bmiHeader.biHeight * 4];
- for (i = info->bmiHeader.biWidth * info->bmiHeader.biHeight,
- rgbaptr = rgba, ptr = bits;
- i > 0;
- i --, rgbaptr += 4, ptr += 3)
- {
- rgbaptr[0] = ptr[0];
- rgbaptr[1] = ptr[1];
- rgbaptr[2] = ptr[2];
- rgbaptr[3] = (ptr[0] + ptr[1] + ptr[2]) / 3;
- }
- /*
- * Set texture image; if the minification filter uses mip-mapping
- * then use gluBuild2D/1DMipmaps() to load the texture...
- */
- if (minfilter == GL_LINEAR || minfilter == GL_NEAREST)
- glTexImage2D(type, 0, 4, info->bmiHeader.biWidth,
- info->bmiHeader.biHeight, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, rgba);
- else if (type == GL_TEXTURE_1D)
- gluBuild1DMipmaps(type, 4, info->bmiHeader.biWidth,
- GL_RGBA, GL_UNSIGNED_BYTE, rgba);
- else
- gluBuild2DMipmaps(type, 3, info->bmiHeader.biWidth,
- info->bmiHeader.biHeight, GL_RGBA,
- GL_UNSIGNED_BYTE, rgba);
- /* Free the RGBA buffer */
- free(rgba);
- }
- else
- {
- /*
- * Set texture image; if the minification filter uses mip-mapping
- * then use gluBuild2D/1DMipmaps() to load the texture...
- */
- if (minfilter == GL_LINEAR || minfilter == GL_NEAREST)
- glTexImage2D(type, 0, 3, info->bmiHeader.biWidth,
- info->bmiHeader.biHeight, 0, GL_RGB,
- GL_UNSIGNED_BYTE, bits);
- else if (type == GL_TEXTURE_1D)
- gluBuild1DMipmaps(type, 3, info->bmiHeader.biWidth,
- GL_RGB, GL_UNSIGNED_BYTE, bits);
- else
- gluBuild2DMipmaps(type, 3, info->bmiHeader.biWidth,
- info->bmiHeader.biHeight, GL_RGB,
- GL_UNSIGNED_BYTE, bits);
- }
- /* Free the bitmap and return... */
- free(info);
- free(bits);
- return (texture);
- }
- GLubyte *LiFeng_Texture::LoadDIBitmap(const char *filename,BITMAPINFO **info)
- {
- FILE *fp; /* Open file pointer */
- GLubyte *bits; /* Bitmap pixel bits */
- int bitsize; /* Size of bitmap */
- int infosize; /* Size of header information */
- BITMAPFILEHEADER header; /* File header */
- /* Try opening the file; use "rb" mode to read this *binary* file. */
- if ((fp = fopen(filename, "rb")) == NULL)
- return (NULL);
- /* Read the file header and any following bitmap information... */
- if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
- {
- /* Couldn't read the file header - return NULL... */
- fclose(fp);
- return (NULL);
- }
- if (header.bfType != 'MB') /* Check for BM reversed... */
- {
- /* Not a bitmap file - return NULL... */
- fclose(fp);
- return (NULL);
- }
- infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
- if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
- {
- /* Couldn't allocate memory for bitmap info - return NULL... */
- fclose(fp);
- return (NULL);
- }
- if (fread(*info, 1, infosize, fp) < infosize)
- {
- /* Couldn't read the bitmap header - return NULL... */
- free(*info);
- fclose(fp);
- return (NULL);
- }
- /* Now that we have all the header info read in, allocate memory for *
- * the bitmap and read *it* in... */
- if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
- bitsize = ((*info)->bmiHeader.biWidth *
- (*info)->bmiHeader.biBitCount + 7) / 8 *
- abs((*info)->bmiHeader.biHeight);
- if ((bits = new GLubyte[bitsize]) == NULL)
- {
- /* Couldn't allocate memory - return NULL! */
- free(*info);
- fclose(fp);
- return (NULL);
- }
- if (fread(bits, 1, bitsize, fp) < bitsize)
- {
- /* Couldn't read bitmap - free memory and return NULL! */
- free(*info);
- delete(bits);
- fclose(fp);
- return (NULL);
- }
- /* OK, everything went fine - return the allocated bitmap... */
- fclose(fp);
- return (bits);
- }