示例#1
0
static void GenTexture(
	const SDL_Surface * surface,
	const TextureInfo & info,
	unsigned & id,
	bool & alpha,
	std::ostream & error)
{
	// detect channels
	int internalformat, format;
	GetTextureFormat(surface, info, internalformat, format, alpha);

	// gen texture
	glGenTextures(1, &id);
	CheckForOpenGLErrors("Texture ID generation", error);

	// init texture
	glBindTexture(GL_TEXTURE_2D, id);

	SetSampler(info);

	glTexImage2D(GL_TEXTURE_2D, 0, internalformat, surface->w, surface->h, 0, format, GL_UNSIGNED_BYTE, surface->pixels);
	CheckForOpenGLErrors("Texture creation", error);

	// If we support generatemipmap, go ahead and do it regardless of the info.mipmap setting.
	// In the GL3 renderer the sampler decides whether or not to do mip filtering, so we conservatively make mipmaps available for all textures.
	GenerateMipmap(GL_TEXTURE_2D);
}
  /// \brief
  ///   Returns the format for the resolved texture.
  ///
  /// This is the format used when sampling the renderable texture.
  inline VTextureLoader::VTextureFormat_e GetResolvedTextureFormat() const
  {
#if defined(_VISION_XENON)
    return static_cast<VTextureLoader::VTextureFormat_e>(m_eResolvedTextureFormat);
#else
    return GetTextureFormat();
#endif
  }
already_AddRefed<gfxImageSurface>
TiledDeprecatedTextureHostOGL::GetAsSurface() {
  nsRefPtr<gfxImageSurface> surf = IsValid() ?
    mGL->GetTexImage(mTextureHandle,
                     false,
                     GetTextureFormat())
    : nullptr;
  return surf.forget();
}
already_AddRefed<gfxImageSurface>
GrallocDeprecatedTextureHostOGL::GetAsSurface() {
  gl()->MakeCurrent();

  GLuint tex = mCompositor->GetTemporaryTexture(LOCAL_GL_TEXTURE0);
  gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
  gl()->fBindTexture(mTextureTarget, tex);
  if (!mEGLImage) {
    mEGLImage = gl()->CreateEGLImageForNativeBuffer(mGraphicBuffer->getNativeBuffer());
  }
  gl()->fEGLImageTargetTexture2D(mTextureTarget, mEGLImage);

  nsRefPtr<gfxImageSurface> surf = IsValid() ?
    gl()->GetTexImage(tex,
                      false,
                      GetTextureFormat())
    : nullptr;
  return surf.forget();
}
示例#5
0
size_t DDSImage::Read(const char* pData, const size_t dataSize)
{
	// Read in header and decode
	if (!ReadHeader(pData, surfacedata_))
		return -1;

	if (surfacedata_.mipmapcount==0)
		surfacedata_.mipmapcount=1;

	imgdata_.height = surfacedata_.height;
	imgdata_.width = surfacedata_.width;

	if(surfacedata_.flags & DDS::DDSD_DEPTH)
		imgdata_.depth = surfacedata_.depth;
	else
		imgdata_.depth = 0;	// no depth to these images

	imgdata_.colourdepth = surfacedata_.pixelformat.RGBBitCount;
	imgdata_.numMipMaps = surfacedata_.mipmapcount;
	imgdata_.format = GetTextureFormat();
	imgdata_.numImages = GetNumImages();
	imgdata_.size = CalculateStoreageSize();
	if(0 >= imgdata_.size)
		return -1;
			
	if(-1 == imgdata_.format)
		return -1;

	const long headerSize=128;
	const size_t DDSStructSize = sizeof(DDS::DDSStruct)+4;
	// proceed with allocating memory and reading the file
	imgdata_.imgData = new byte[imgdata_.size];

	// Read in remaining data
	memcpy(imgdata_.imgData, pData + headerSize, dataSize-headerSize);

	return dataSize - headerSize;
}
示例#6
0
GLuint LoadTexture(std::string filename)
{
    SDL_Surface* rawTexture, *optimizedTexture;
    GLuint texture;

    rawTexture = IMG_Load(filename.c_str());

    if(!rawTexture)
    {
        std::cout << "Error loading image: " << filename << std::endl;
        return NULL;
    }

    optimizedTexture = SDL_DisplayFormatAlpha(rawTexture);

    bool lock = SDL_MUSTLOCK(optimizedTexture);
    if(lock)
    {
        SDL_LockSurface(optimizedTexture);
    }

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, optimizedTexture->w, optimizedTexture->h, 0, GetTextureFormat(optimizedTexture), GL_UNSIGNED_BYTE, optimizedTexture->pixels);

    if(lock)
    {
        SDL_UnlockSurface(optimizedTexture);
    }

    SDL_FreeSurface(rawTexture);
    SDL_FreeSurface(optimizedTexture);

    return texture;
}
 /// \brief
 ///   Returns the format for the resolved texture.
 ///
 /// This is the format used when sampling the renderable texture.
 inline VTextureLoader::VTextureFormat_e GetResolvedTextureFormat() const
 {
   return GetTextureFormat();
 }
示例#8
0
bool Texture::Load(const std::string & path, const TextureInfo & info, std::ostream & error)
{

	if (texid)
	{
		error << "Tried to double load texture " << path << std::endl;
		return false;
	}

	if (!info.data && path.empty())
	{
		error << "Tried to load a texture with an empty name" << std::endl;
		return false;
	}

	if (!info.data && LoadDDS(path, info, error))
	{
		return true;
	}

	if (info.cube)
	{
		return LoadCube(path, info, error);
	}

	SDL_Surface * surface = 0;
	if (info.data)
	{
		Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
		rmask = 0xff000000;
		gmask = 0x00ff0000;
		bmask = 0x0000ff00;
		amask = 0x000000ff;
#else
		rmask = 0x000000ff;
		gmask = 0x0000ff00;
		bmask = 0x00ff0000;
		amask = 0xff000000;
#endif
		surface = SDL_CreateRGBSurfaceFrom(
			info.data, info.width, info.height,
			info.bytespp * 8, info.width * info.bytespp,
			rmask, gmask, bmask, amask);
	}
	else
	{
		surface = IMG_Load(path.c_str());
	}

	if (!surface)
	{
		error << "Error loading texture file: " << path << std::endl;
		error << IMG_GetError() << std::endl;
		return false;
	}

	const unsigned char * pixels = (const unsigned char *)surface->pixels;
	const unsigned bytespp = surface->format->BytesPerPixel;
	unsigned pitch = surface->pitch;
	unsigned w = surface->w;
	unsigned h = surface->h;

	// downsample if requested by application
	std::vector<unsigned char> pixelsd;
	unsigned wd = w;
	unsigned hd = h;
	if (info.maxsize == TextureInfo::SMALL)
	{
		if (w > 256)
			wd = w / 4;
		else if (w > 128)
			wd = w / 2;

		if (h > 256)
			hd = h / 4;
		else if (h > 128)
			hd = h / 2;
	}
	else if (info.maxsize == TextureInfo::MEDIUM)
	{
		if (w > 256)
			wd = w / 2;

		if (h > 256)
			hd = h / 2;
	}
	if (wd < w || hd < h)
	{
		pixelsd.resize(wd * hd * bytespp);

		SampleDownAvg(
			bytespp, w, h, pitch, pixels,
			wd, hd, wd * bytespp, &pixelsd[0]);

		pixels = &pixelsd[0];
		pitch = wd * bytespp;
		w = wd;
		h = hd;
	}

	// store dimensions
	width = w;
	height = h;

	target = GL_TEXTURE_2D;

	// gen texture
	glGenTextures(1, &texid);
	CheckForOpenGLErrors("Texture ID generation", error);

	// setup texture
	glBindTexture(GL_TEXTURE_2D, texid);
	SetSampler(info);

	int internalformat, format;
	GetTextureFormat(surface, info, internalformat, format);

	// upload texture data
	glTexImage2D(GL_TEXTURE_2D, 0, internalformat, w, h, 0, format, GL_UNSIGNED_BYTE, pixels);
	CheckForOpenGLErrors("Texture creation", error);

	// If we support generatemipmap, go ahead and do it regardless of the info.mipmap setting.
	// In the GL3 renderer the sampler decides whether or not to do mip filtering,
	// so we conservatively make mipmaps available for all textures.
	if (GLC_ARB_framebuffer_object)
		glGenerateMipmap(GL_TEXTURE_2D);

	SDL_FreeSurface(surface);

	return true;
}