|
|
#include "utils.h"
unsigned char * DecodeBMP(unsigned char * bmpFileData, int & width, int & height) { if (0x4D42 == *((unsigned short*)bmpFileData)) {//ͨ��ǰ�����ֽ�ȥ�ж������Ƿ���BMP����
int pixelDataOffset = *((int*)(bmpFileData + 10));//ȡ��λͼ�������ݵ�ƫ�Ƶ�ַ��
width = *((int*)(bmpFileData + 18));//��ȡͼƬ����
height = *((int*)(bmpFileData + 22));//��ȡͼƬ����
/*��ȡͼƬ����*/ unsigned char* pixelData = bmpFileData + pixelDataOffset; for (int i = 0; i < width * height * 3; i += 3) { unsigned char temp = pixelData[i]; //BGR ת���� RGB
pixelData[i] = pixelData[i + 2]; pixelData[i + 2] = temp; } return pixelData; } return nullptr; }
GLuint CreateTexture2D(unsigned char * pixelData, int width, int height, GLenum type) { GLuint texture; glGenTextures(1, &texture);
//��������
glBindTexture(GL_TEXTURE_2D, texture);
//���������Ŵ�/��Сʱ�����ݲɼ����㷨
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//ʹ�����Թ���
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//ʹ�����Թ���
//���õ�������������1.0ʱ�������ݲɼ���ʽ
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//�����ϴ����Կ�
glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixelData);
//�ͷ�����
glBindTexture(GL_TEXTURE_2D, 0); return texture; }
GLuint CreateTexture2DFromBMP(const char * bmpPath) { int nFileSize = 0; unsigned char* bmpFileContent = LoadFileContent(bmpPath, nFileSize);
if (bmpFileContent == nullptr) { return 0; }
int bmpWidth = 0; int bmpHeight = 0; unsigned char* pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight);
if (bmpWidth == 0) { delete bmpFileContent; return 0; }
GLuint textrue = CreateTexture2D(pixelData, bmpWidth, bmpHeight, GL_RGB);
return textrue; }
GLuint CreateDisplayList(std::function<void()> foo) { GLuint displayList = glGenLists(1);//����һ����ʾ�б�
glNewList(displayList, GL_COMPILE); foo(); glEndList();
return displayList; }
|