网课内容--解码BMP与创建纹理



texture.cpp

#include "texture.h"
#include "utils.h"
 
unsigned char* DecodeBMP(unsigned char*bmpFileData, int&width, int&height)
{
	if (0x4D42==*((unsigned short*)bmpFileData))
	{
		printf("yes");
		int pixelDataOffset = *((int*)(bmpFileData + 10));
		width = *((int*)(bmpFileData+18));
		height = *((int*)(bmpFileData + 22));
		unsigned char*pixelData = bmpFileData + pixelDataOffset;
		//bgr bgr bgr ....
		//rgb rgb rgb
		for (int i=0;i<width*height*3;i+=3)
		{
			unsigned char temp = pixelData[i];
			pixelData[i] = pixelData[i+2];
			pixelData[i+2]=temp;
		}
		return pixelData;
	}
	else
	{
		printf("no");
		return nullptr;
	}
}

void Texture::Init(const char*imagePath)
{
	//load image file from disk to memory
	unsigned char*imageFileContent = LoadFileContent(imagePath);
	//decode image
	int width = 0, height = 0;
	unsigned char* pixelData = DecodeBMP(imageFileContent, width, height);

	//generate an opengl texture
	glGenTextures(1, &mTextureID);
	glBindTexture(GL_TEXTURE_2D, mTextureID);
	/*glGenTextures(GLsizei n, GLuint *textures)函数说明 
	百度百科:
	n:用来生成纹理的数量
	textures:存储纹理索引的第一个元素指针
	glGenTextures函数根据纹理参数返回n个纹理索引。纹理名称集合不必是一个连续的整数集合。
	(glGenTextures就是用来产生你要操作的纹理对象的索引的,比如你告诉OpenGL,我需要5个纹理对象,它会从没有用到的整数里返回5个给你)
	glBindTexture实际上是改变了OpenGL的这个状态,它告诉OpenGL下面对纹理的任何操作都是对它所绑定的纹理对象的,比如glBindTexture(GL_TEXTURE_2D,1)告诉OpenGL下面代码中对2D纹理的任何设置都是针对索引为1的纹理的。
	产生纹理函数假定目标纹理的面积是由glBindTexture函数限制的。先前调用glGenTextures产生的纹理索引集不会由后面调用的glGenTextures得到,除非他们首先被glDeleteTextures删除。你不可以在显示列表中包含glGenTextures。*/
	
	
	//operation on current texture  可以解决绝大多数问题 glTexParameteri:http://blog.csdn.net/d04421024/article/details/5089641
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
	glBindTexture(GL_TEXTURE_2D, 0);
	delete imageFileContent;
}

texture.h

#pragma once
#include <windows.h>
#include <gl/GL.h>

class Texture
{
public:
	GLuint mTextureID;//gpu
	void Init(const char*imagePath);
};
加载文件
#include "utils.h"

unsigned char* LoadFileContent(const char*filePath)
{
	unsigned char* fileContent = nullptr;
	FILE*pFile = fopen(filePath, "rb");
	if (pFile)
	{
		//read
		fseek(pFile,0,SEEK_END);
		int nLen = ftell(pFile);
		if (nLen>0)
		{
			rewind(pFile);
			fileContent = new unsigned char[nLen+1];
			fread(fileContent,sizeof(unsigned char),nLen,pFile); 
			fileContent[nLen]='\0';
		}
		fclose(pFile);
	}
	return fileContent;
}
utlis.h
#pragma once
#include <stdio.h>

unsigned char* LoadFileContent(const char*filePath);




texture.cpp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值