Beispiel #1
0
bool Texture::LoadXOR() {
	width_ = height_ = 256;
	unsigned char *buf = new unsigned char[width_*height_*4];
	for (int y = 0; y < 256; y++) {
		for (int x = 0; x < 256; x++) {
			buf[(y*width_ + x)*4 + 0] = x^y;
			buf[(y*width_ + x)*4 + 1] = x^y;
			buf[(y*width_ + x)*4 + 2] = x^y;
			buf[(y*width_ + x)*4 + 3] = 0xFF;
		}
	}
	GL_CHECK();
	glGenTextures(1, &id_);
	glBindTexture(GL_TEXTURE_2D, id_);
	SetTextureParameters(ZIM_GEN_MIPS);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0,
		GL_RGBA, GL_UNSIGNED_BYTE, buf);
	if(gl_extensions.FBO_ARB){
		glGenerateMipmap(GL_TEXTURE_2D);
	}else{
#ifndef USING_GLES2
		glGenerateMipmapEXT(GL_TEXTURE_2D);
#endif
	}
	GL_CHECK();
	delete [] buf;
	return true;
}
Beispiel #2
0
bool Texture::LoadXOR() {
	width_ = height_ = 256;
	unsigned char *buf = new unsigned char[width_*height_*4];
	for (int y = 0; y < 256; y++) {
		for (int x = 0; x < 256; x++) {
			buf[(y*width_ + x)*4 + 0] = x^y;
			buf[(y*width_ + x)*4 + 1] = x^y;
			buf[(y*width_ + x)*4 + 2] = x^y;
			buf[(y*width_ + x)*4 + 3] = 0xFF;
		}
	}
	GL_CHECK();
	ID3D11Device *ctx;
	D3D11_TEXTURE2D_DESC desc;
	desc.Width = width_;
	desc.Height = height_;
	desc.MipLevels = 0;
	desc.ArraySize = 1;
	desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	desc.Usage = D3D11_USAGE_DEFAULT;
	desc.SampleDesc.Count = 1;
	desc.SampleDesc.Quality = 0;
	desc.CPUAccessFlags = 0;
	desc.MiscFlags = 0;
	if (FAILED(ctx->CreateTexture2D(&desc, 0, &tex_))) {
		FLOG("Failed creating XOR texture");
	}
	SetTextureParameters(ZIM_GEN_MIPS);
	GL_CHECK();
	delete [] buf;
	return true;
}
Beispiel #3
0
bool Texture::LoadJPEG(const char *filename, bool genMips) {
	ILOG("Loading jpeg %s", filename);
	unsigned char *image_data;
	int actual_comps;
	image_data = jpgd::decompress_jpeg_image_from_file(filename, &width_, &height_, &actual_comps, 4);
	if (!image_data) {
		ELOG("jpeg: image data returned was 0");
		return false;
	}
	ILOG("Jpeg decoder failed to get RGB, got: %i x %i x %i", actual_comps, width_, height_);
	ILOG("First four bytes: %i %i %i %i", image_data[0], image_data[1], image_data[2], image_data[3]);

	GL_CHECK();
	glGenTextures(1, &id_);
	glBindTexture(GL_TEXTURE_2D, id_);
	SetTextureParameters(genMips ? ZIM_GEN_MIPS : ZIM_CLAMP);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
	if (genMips) {
		if (gl_extensions.FBO_ARB) {
			glGenerateMipmap(GL_TEXTURE_2D);
		} else {
#ifndef USING_GLES2
			glGenerateMipmapEXT(GL_TEXTURE_2D);
#endif
		}
	}
	GL_CHECK();
	free(image_data);
	return true;
}
Beispiel #4
0
GLuint CreateAlphaMap (U32 width, U32 height, U8* data) {
	GLuint textureID;

	glGenTextures(1, &textureID);
	glBindTexture(GL_TEXTURE_2D, textureID);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);

	SetTextureParameters(GL_CLAMP_TO_EDGE, GL_LINEAR);

	glBindTexture(GL_TEXTURE_2D, 0);
	return textureID;
}
Beispiel #5
0
GLuint CreateTextureFromPixels (U32 width, U32 height, U8* pixels, GLenum wrapMode) {
	assert(pixels != nullptr);
	assert(width > 0 && height > 0);
	GLuint textureID;
	glGenTextures(1, &textureID);
	glBindTexture(GL_TEXTURE_2D, textureID);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
	glGenerateMipmap(GL_TEXTURE_2D);

	SetTextureParameters(wrapMode, GL_LINEAR_MIPMAP_LINEAR);
	return textureID;
}
Beispiel #6
0
bool Texture::LoadPNG(const char *filename) {
	unsigned char *image_data;
	if (1 != pngLoad(filename, &width_, &height_, &image_data, false)) {
		return false;
	}
	GL_CHECK();
	glGenTextures(1, &id_);
	glBindTexture(GL_TEXTURE_2D, id_);
	SetTextureParameters(ZIM_GEN_MIPS);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, 
							 GL_RGBA, GL_UNSIGNED_BYTE, image_data);
	glGenerateMipmap(GL_TEXTURE_2D);
	GL_CHECK();
	free(image_data);
	return true;
}
Beispiel #7
0
bool Texture::LoadPNG(const uint8_t *data, size_t size, bool genMips) {
	unsigned char *image_data;
	if (1 != pngLoadPtr(data, size, &width_, &height_, &image_data, false)) {
		return false;
	}
	GL_CHECK();
	// TODO: should check for power of 2 tex size and disallow genMips when not.
	glGenTextures(1, &id_);
	glBindTexture(GL_TEXTURE_2D, id_);
	SetTextureParameters(genMips ? ZIM_GEN_MIPS : ZIM_CLAMP);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
	if (genMips) {
		if (gl_extensions.FBO_ARB) {
			glGenerateMipmap(GL_TEXTURE_2D);
		} else {
#ifndef USING_GLES2
			glGenerateMipmapEXT(GL_TEXTURE_2D);
#endif
		}
	}
	GL_CHECK();
	free(image_data);
	return true;
}
Beispiel #8
0
bool Texture::LoadPNG(const char *filename, bool genMips) {
	unsigned char *image_data;
	if (1 != pngLoad(filename, &width_, &height_, &image_data, false)) {
		return false;
	}
	GL_CHECK();
	glGenTextures(1, &id_);
	glBindTexture(GL_TEXTURE_2D, id_);
	SetTextureParameters(genMips ? ZIM_GEN_MIPS : ZIM_CLAMP);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0,
		GL_RGBA, GL_UNSIGNED_BYTE, image_data);
	if (genMips) {
		if (gl_extensions.FBO_ARB) {
			glGenerateMipmap(GL_TEXTURE_2D);
		} else {
#ifndef USING_GLES2
			glGenerateMipmapEXT(GL_TEXTURE_2D);
#endif
		}
	}
	GL_CHECK();
	free(image_data);
	return true;
}
Beispiel #9
0
bool Texture::LoadZIM(const char *filename) {
	uint8_t *image_data[ZIM_MAX_MIP_LEVELS];
	int width[ZIM_MAX_MIP_LEVELS];
	int height[ZIM_MAX_MIP_LEVELS];

	int flags;
	int num_levels = ::LoadZIM(filename, &width[0], &height[0], &flags, &image_data[0]);
	if (!num_levels)
		return false;
	width_ = width[0];
	height_ = height[0];
	int data_type = GL_UNSIGNED_BYTE;
	int colors = GL_RGBA;
	int storage = GL_RGBA;
	bool compressed = false;
	switch (flags & ZIM_FORMAT_MASK) {
	case ZIM_RGBA8888:
		data_type = GL_UNSIGNED_BYTE;
		break;
	case ZIM_RGBA4444:
		data_type = DXGI_FORMAT_B4G4R4A4_UNORM;
		break;
	case ZIM_RGB565:
		data_type = DXGI_FORMAT_B5G6R5_UNORM;
		colors = GL_RGB;
		storage = GL_RGB;
		break;
	case ZIM_ETC1:
		compressed = true;
		break;
	}

	GL_CHECK();
	//glGenTextures(1, &id_);
	//glBindTexture(GL_TEXTURE_2D, id_);
	SetTextureParameters(flags);

	if (compressed) {
		for (int l = 0; l < num_levels; l++) {
			int data_w = width[l];
			int data_h = height[l];
			if (data_w < 4) data_w = 4;
			if (data_h < 4) data_h = 4;
#if defined(USING_GLES2)
			int compressed_image_bytes = data_w * data_h / 2;
			glCompressedTexImage2D(GL_TEXTURE_2D, l, GL_ETC1_RGB8_OES, width[l], height[l], 0, compressed_image_bytes, image_data[l]);
			GL_CHECK();
#else
			//image_data[l] = ETC1ToRGBA(image_data[l], data_w, data_h);
			//glTexImage2D(GL_TEXTURE_2D, l, GL_RGBA, width[l], height[l], 0,
			//						GL_RGBA, GL_UNSIGNED_BYTE, image_data[l]);
#endif
		}
		GL_CHECK();
#if !defined(USING_GLES2)
		//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, num_levels - 2);
#endif
	} else {
		for (int l = 0; l < num_levels; l++) {
			//glTexImage2D(GL_TEXTURE_2D, l, storage, width[l], height[l], 0,
			//						 colors, data_type, image_data[l]);
		}
		if (num_levels == 1 && (flags & ZIM_GEN_MIPS)) {
			//glGenerateMipmap(GL_TEXTURE_2D);
		}
	}
	SetTextureParameters(flags);

	GL_CHECK();
	// Only free the top level, since the allocation is used for all of them.
	delete [] image_data[0];
	return true;
}
Beispiel #10
0
bool Texture::LoadZIM(const char *filename) {
	uint8_t *image_data[ZIM_MAX_MIP_LEVELS];
	int width[ZIM_MAX_MIP_LEVELS];
	int height[ZIM_MAX_MIP_LEVELS];

	int flags;
	int num_levels = ::LoadZIM(filename, &width[0], &height[0], &flags, &image_data[0]);
	if (!num_levels)
		return false;
	if (num_levels >= ZIM_MAX_MIP_LEVELS)
		return false;
	width_ = width[0];
	height_ = height[0];
	int data_type = GL_UNSIGNED_BYTE;
	int colors = GL_RGBA;
	int storage = GL_RGBA;
	bool compressed = false;
	switch (flags & ZIM_FORMAT_MASK) {
	case ZIM_RGBA8888:
		data_type = GL_UNSIGNED_BYTE;
		break;
	case ZIM_RGBA4444:
		data_type = GL_UNSIGNED_SHORT_4_4_4_4;
		break;
	case ZIM_RGB565:
		data_type = GL_UNSIGNED_SHORT_5_6_5;
		colors = GL_RGB;
		storage = GL_RGB;
		break;
	case ZIM_ETC1:
		compressed = true;
		break;
	}

	GL_CHECK();

	glGenTextures(1, &id_);
	glBindTexture(GL_TEXTURE_2D, id_);
	SetTextureParameters(flags);

	if (compressed) {
		for (int l = 0; l < num_levels; l++) {
			int data_w = width[l];
			int data_h = height[l];
			if (data_w < 4) data_w = 4;
			if (data_h < 4) data_h = 4;
#if defined(USING_GLES2) && !defined(IOS)
			int compressed_image_bytes = data_w * data_h / 2;
			glCompressedTexImage2D(GL_TEXTURE_2D, l, GL_ETC1_RGB8_OES, width[l], height[l], 0, compressed_image_bytes, image_data[l]);
			GL_CHECK();
#else
			// TODO: OpenGL 4.3+ accepts ETC1 so we should not have to do this anymore on those cards.
			// Also, iOS does not have support for ETC1 compressed textures so we just decompress.
			// TODO: Use PVR texture compression on iOS.
			image_data[l] = ETC1ToRGBA(image_data[l], data_w, data_h);
			glTexImage2D(GL_TEXTURE_2D, l, GL_RGBA, width[l], height[l], 0,
				GL_RGBA, GL_UNSIGNED_BYTE, image_data[l]);
#endif
		}
		GL_CHECK();
#if !defined(USING_GLES2)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, num_levels - 2);
#endif
	} else {
		for (int l = 0; l < num_levels; l++) {
			glTexImage2D(GL_TEXTURE_2D, l, storage, width[l], height[l], 0,
				colors, data_type, image_data[l]);
		}
		if (num_levels == 1 && (flags & ZIM_GEN_MIPS)) {
			if(gl_extensions.FBO_ARB) {
				glGenerateMipmap(GL_TEXTURE_2D);
			}else{
#ifndef USING_GLES2
				glGenerateMipmapEXT(GL_TEXTURE_2D);
#endif
			}
		}
	}
	SetTextureParameters(flags);

	GL_CHECK();
	// Only free the top level, since the allocation is used for all of them.
	free(image_data[0]);
	return true;
}