⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 texture.cpp

📁 小型的3D游戏引擎
💻 CPP
字号:
#include <windows.h>
#include <gl/glu.h>
#include "loader/targa.h"
#include "loader/bitmap.h"
#include "global.h"
#include "texture.h"


/////////////////////////////////////////////////////////////////////////////////

GcTexture::GcTexture()
{
}

/////////////////////////////////////////////////////////////////////////////////

GcTexture::~GcTexture()
{
	glDeleteTextures(1, &texture);
}

/////////////////////////////////////////////////////////////////////////////////

bool GcTexture::Create(char *fileName)
{
	int colorMode;

	// Creat a targa to use for laoding the tecture
	GcLoader *bitmap;

	// Find out which file format to use
	if((fileName[strlen(fileName)-3] == 'b') && 
	   (fileName[strlen(fileName)-2] == 'm') && 
	   (fileName[strlen(fileName)-1] == 'p'))
	{
		bitmap = new GcBitmap;
	}
	else if((fileName[strlen(fileName)-3] == 't') && 
			(fileName[strlen(fileName)-2] == 'g') && 
			(fileName[strlen(fileName)-1] == 'a'))
	{
		bitmap = new GcTarga;
	}
	else
	{
		MessageBox(NULL, "Wrong texture file format", "Error", MB_OK);
		return false;
	}

	// Load the tga file
	if(!bitmap->Load(fileName)) {
		MessageBox(NULL, "Loading Texture failed", "Error", MB_OK);
		return false;
	}

	// Calculate the color mode (e.g. 32/8 == 4 == RGBA);
	if((bitmap->Bpp() / 8) == 4) {
		colorMode = GL_RGBA;
	}
	else {
		colorMode = GL_RGB;
	}

	// Save the width and height
	width = bitmap->Width();
	height = bitmap->Height();

	// Generate the texture object
	glGenTextures(1, &texture);

	// Sett the pixel alignment
	glPixelStorei(GL_UNPACK_ALIGNMENT, colorMode);
	
	// Bind the texture
	glBindTexture(GL_TEXTURE_2D, texture);

	// Settings
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// Creat a mip-map
	gluBuild2DMipmaps(GL_TEXTURE_2D, colorMode, bitmap->Width(), bitmap->Height(),
					  colorMode, GL_UNSIGNED_BYTE, bitmap->Image());

	// The texture has been loaded
	loaded = true;

	// Free the memory of the targa file
	bitmap->Destroy();
	delete bitmap;

	return true;
}

///////////////////////////////////////////////////////////////////////////////



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -