level 7
第一张是没加透明通道的,第二张加了,第三张代码。。而且现在对比还发现加了透明通道之后纹理不会跟着正方体旋转了,这又是为何啊。。。
2015年11月05日 11点11分
3
level 7
没人?我上代码喽。。
unsigned char header[54]; // Each BMP file begins by a 54-bytes header
unsigned int dataPos; // Position in the file where the actual data begins
unsigned int width, height;
unsigned int imageSize; // = width*height*3
// Actual RGB data
unsigned char * data;
printf("texture");
// Open the file
FILE * file = fopen(imagepath, "rb");
if (!file) {
printf("Image could not be opened\n");
return 0;
}
if (fread(header, 1, 54, file) != 54) { // If not 54 bytes read : problem
printf("Not a correct BMP file\n");
return false;
}
if (header[0] != 'B' || header[1] != 'M') {
printf("Not a correct BMP file\n");
return 0;
}
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (imageSize == 0) imageSize = width*height * 3; // 3 : one byte for each Red, Green and Blue component
if (dataPos == 0) dataPos = 54; // The BMP header is done that way
data = new unsigned char[imageSize];
unsigned char *texture = new unsigned char[width*height * 4];
fread(data, 1, imageSize, file);
int j,k,t3,t4;
for (int i = 0;i < width;i++)
for (j = 0;j < height;j++)
{
t3 = (i*height + j)*3 ;
t4 = (i*height + j) * 4;
for (k = 0;k < 3;k++)
texture[t4+k] = data[t3+k];
texture[t4 + 3] = (data[t3 + 1]==255 && data[t3 + 2]==255 && data[t3]==255) ? 0 : 255;
}
fclose(file);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
//glPixelStorei(GL_PACK_ALIGNMENT, 1);
//glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, texture);
//gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_BGRA, GL_UNSIGNED_BYTE, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
return textureID;
2015年11月05日 11点11分
4
level 7
这里是初始化。为了避免影响吧光照关了但并没卵用
glShadeModel(GL_SMOOTH);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5);//
//glEnable(GL_LIGHTING);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, brightLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_LIGHT0);
glClearDepthf(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, brightLight);
glMaterialfv(GL_FRONT, GL_DIFFUSE, planet_diffuse);
glMateriali(GL_FRONT, GL_SHININESS, 20);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2015年11月05日 11点11分
5
level 1
glPixelStorei(GL_PACK_ALIGNMENT,4);
2015年11月05日 12点11分
8
level 1
// 读取图象的大小信息
fseek(pFile, 0x0012, SEEK_SET);
fread(&ImageWidth, sizeof(ImageWidth), 1, pFile);
fread(&ImageHeight, sizeof(ImageHeight), 1, pFile);
// 计算像素数据长度
PixelLength = ImageWidth * 3;
while( PixelLength % 4 != 0 )
++PixelLength;
PixelLength *= ImageHeight;
// 读取像素数据
PixelData = (GLubyte*)malloc(PixelLength);
if( PixelData == 0 )
exit(0);
fseek(pFile, 54, SEEK_SET);
fread(PixelData, PixelLength, 1, pFile);
2015年11月05日 12点11分
10
我读文件应该问题不大吧,因为我直接用原始数据画是彩色的哦
![[乖]](/static/emoticons/u4e56.png)
在我给他强制加上alpha通道之后就黑白了
2015年11月05日 12点11分
level 1
void readPixelFromBmp24(char* fileName,int* width,int* height, char **ppData,int *add)
{
FILE *pFile=fopen(fileName,"rb");
*ppData=NULL;
int c=0;
int pixelSize=0;
if(pFile!=NULL)
{
fseek(pFile,0x12,SEEK_SET);
fread(width,sizeof(int),1,pFile);
fread(height,sizeof(int),1,pFile);
if((*width>0)&&(*height>0))
{
pixelSize=(*width)*3;
while(pixelSize%4!=0)
{
c++;
pixelSize++;
}
*add=c;
pixelSize=pixelSize*(*height);
*ppData=( char*) malloc(sizeof( char)*pixelSize);
}
if(*ppData!=NULL)
{
fseek(pFile,54,SEEK_SET);
fread(*ppData,sizeof( char),pixelSize,pFile);
}
fclose(pFile);
}
}
2015年11月05日 15点11分
14
24位BMP读取函数
2015年11月05日 16点11分
level 1
char *pSource=NULL;
int imageWidth=0;
int imageHeight=0;
int i=0,j=0,k=0;
int t3,t4;
int add =0;//每行像素变成4的倍数所需填充的字节数
readPixelFromBmp24("source1.bmp",&imageWidth,&imageHeight,&pSource,&add);
ptest=(unsigned char*)malloc(imageWidth*imageHeight*4);
for(i=0;i<imageHeight;i++)
{
for(j=0;j<imageWidth;j++)
{
t3 = (i*imageWidth + j)*3+i*add;
t4 = (i*imageWidth + j)*4;
for(k=0;k<3;k++)
{
ptest[t4+k]=pSource[t3+2-k];
}
ptest[i*imageWidth*4+j*4+3]=255;
}
}
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,imageWidth,imageHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,ptest);
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_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glEnable(GL_TEXTURE_2D);
2015年11月05日 16点11分
15
level 1
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,imageWidth,imageHeight,0,GL_BGR,GL_UNSIGNED_BYTE,pSource);
//glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,imageWidth,imageHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,ptest);
经测试,两句效果一样,只不过后面一句带有ALPHA通道。后一句用的是GL_RGBA格式
2015年11月05日 16点11分
16
太感谢了
![[乖]](/static/emoticons/u4e56.png)
我明天就试试
2015年11月05日 16点11分