Exemple #1
0
void LoadTexture()
{

    u32 * texture_mem = tiny3d_AllocTexture(64*1024*1024); // alloc 64MB of space for textures (this pointer can be global)    


    if(!texture_mem) return; // fail!

    texture_pointer = texture_mem;

    ResetFont();
    texture_pointer = (u32 *) AddFontFromBitmapArray((u8 *) font  , (u8 *) texture_pointer, 32, 255, 16, 32, 2, BIT0_FIRST_PIXEL);

    // here you can add more textures using 'texture_pointer'. It is returned aligned to 16 bytes

    jpg1.jpg_in= (void *) psl1ght_jpg_bin;
	jpg1.jpg_size= sizeof(psl1ght_jpg_bin);

    LoadJPG(&jpg1, NULL);

    jpg1_offset = 0;
       
    if(jpg1.bmp_out) {

        memcpy(texture_pointer, jpg1.bmp_out, jpg1.wpitch * jpg1.height);
        
        free(jpg1.bmp_out);

        jpg1.bmp_out= texture_pointer;

        texture_pointer += (jpg1.wpitch/4 * jpg1.height + 3) & ~3; // aligned to 16 bytes (it is u32) and update the pointer

        jpg1_offset = tiny3d_TextureOffset(jpg1.bmp_out);      // get the offset (RSX use offset instead address)
     }
}
Exemple #2
0
//
// Load a new image from an image file (PPM, JPEG
// and PNG formats are supported).
// Returns NULL on failure.
//
STImage::STImage(const std::string& filename)
    : mWidth(-1)
    , mHeight(-1)
    , mPixels(NULL)
{

    // Determine the right routine based on the file's extension.
    // The format-specific subroutines are each implemented in
    // a different file.
    std::string ext = STGetExtension( filename );
    if (ext.compare("PPM") == 0) {
        LoadPPM(filename);
    }
    else if (ext.compare("PNG") == 0) {
        LoadPNG(filename);
    }
    else if (ext.compare("JPG") == 0 || ext.compare("JPEG") == 0) {
        LoadJPG(filename);
    }
    else {
        fprintf(stderr,
                "STImage::STImage() - Unknown image file type \"%s\".\n",
                filename.c_str());
        throw new std::runtime_error("Error creating STImage");
    } 
}
Exemple #3
0
 /**
  * ファイルロード[拡張子自動判別]
  * @param filepath  ファイルフルパス 
  * @retval true 成功
  * @retval false 失敗
  */
 bool Load(const char* filename)
 {
     bool result = false;
     m_image.Clear();
     std::string path(filename);
     std::string::size_type pos = path.rfind('.');
     if (pos != std::string::npos)
     {
         const std::string ext = make_lowercase(path.substr(pos+1));
         if (ext == "png")
         {
             result = LoadPNG(path);
         }
         else if (ext == "jpg" || ext == "jpeg")
         {
             result = LoadJPG(path);
         }
         else if (ext == "tga")
         {
             result = LoadTGA(path);
         }
         else if (ext == "hdr")
         {
             result = LoadHDR(path);
         }
         else if (ext == "exr" || ext == "EXR")
         {
             result = LoadEXR(path);
         }
     }
     return result;
 }
Exemple #4
0
 	TTexture * LoadTexture(const std::string & parImg)
 	{
 		TImgType::Type typeImg = GetImgType(parImg);

 		TTexture * texture = NULL;
	    switch (typeImg)
	    {
	    	case TImgType::PNG:
                texture = LoadPNG(parImg.c_str());
	    	    break;
	    	case TImgType::JPG:
	    	    texture = LoadJPG(parImg.c_str());

	    	    break;
	    	case TImgType::BMP:
	    	    texture = LoadBMP(parImg.c_str());

	    	    break;
	    	case TImgType::TGA:
	    	    texture = NULL;
	    	    break;
    	    default:
    	        ASSERT_FAIL_MSG("Unhandled type "<<parImg);
	    };
	    return texture;
	}
Exemple #5
0
unsigned int CreateTexture(const char* strFileName)
{
 	if(!strFileName) 
		return false;

	unsigned int texture;

	tImage *pImage = NULL;

	if(strstr(strFileName, ".jpg"))
	{
		pImage = LoadJPG(strFileName);
	}

	// Make sure valid image data was given to pImage, otherwise return false
	if(pImage == NULL)		
	{
		LOGE("Failed to load %s", strFileName);
		return false;
	}

	// Generate a texture with the associative texture ID stored in the array
	glGenTextures(1, &texture);

	// This sets the alignment requirements for the start of each pixel row in memory.
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

	// Bind the texture to the texture arrays index and init the texture
	glBindTexture(GL_TEXTURE_2D, texture);

	// Assume that the texture is a 24 bit RGB texture (We convert 16-bit ones to 24-bit)
	int textureType = GL_RGB;

	// If the image is 32-bit (4 channels), then we need to specify GL_RGBA for an alpha
	if(pImage->channels == 4)
		textureType = GL_RGBA;
		
	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_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, textureType, pImage->sizeX, pImage->sizeY, 0, textureType, GL_UNSIGNED_BYTE, pImage->data);

	// Now we need to free the image data that we loaded since openGL stored it as a texture
	if (pImage)										// If we loaded the image
	{
		if (pImage->data)							// If there is texture data
		{
			free(pImage->data);						// Free the texture data, we don't need it anymore
		}

		free(pImage);								// Free the image structure

		LOGI("%s", strFileName);
	}

	return texture;
}
void CBitmap::Load(string filename)
{
	if(mem!=0)
		delete[] mem;

	if(filename.find(".jpg")!=string::npos)
		LoadJPG(filename);
	else if(filename.find(".pcx")!=string::npos)
		LoadPCX(filename);
	else
		LoadBMP(filename);
}
Exemple #7
0
void CreateTexture(UINT textureArray[], LPSTR strFileName, int textureID)
{
    if(!strFileName)									// Return from the function if no file name was passed in
        return;


/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *

    tImageJPG *pImage = LoadJPG(strFileName);			// Load the image and store the data

/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *


    if(pImage == NULL)									// If we can't load the file, quit!
        exit(0);

    // Generate a texture with the associative texture ID stored in the array
    glGenTextures(1, &textureArray[textureID]);

    // Bind the texture to the texture arrays index and init the texture
    glBindTexture(GL_TEXTURE_2D, textureArray[textureID]);

    // Build Mipmaps (builds different versions of the picture for distances - looks better)
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX, pImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);

    // Lastly, we need to tell OpenGL the quality of our texture map.  GL_LINEAR_MIPMAP_LINEAR
    // is the smoothest.  GL_LINEAR_MIPMAP_NEAREST is faster than GL_LINEAR_MIPMAP_LINEAR,
    // but looks blochy and pixilated.  Good for slower computers though.

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


    // Now we need to free the image data that we loaded since OpenGL stored it as a texture

    if (pImage)										// If we loaded the image
    {
        if (pImage->data)							// If there is texture data
        {
            free(pImage->data);						// Free the texture data, we don't need it anymore
        }

        free(pImage);								// Free the image structure
    }
}
Exemple #8
0
int TEXTURE_DESC::CreateTextureJPG(const char* strFileName) {
	if(!strFileName) return -1;

    // Load the image and store the data
    //
	tImageJPG *pImage = LoadJPG(strFileName);
	if(pImage == NULL) return -1;
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);
	glGenTextures(1, (GLuint*)&id);
	glBindTexture(GL_TEXTURE_2D, id);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX, pImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    xsize = pImage->sizeX;
    ysize = pImage->sizeY;

    if (pImage->data) {
        free(pImage->data);
    }
    free(pImage);
	return 0;
}
Exemple #9
0
void R_LoadDataImage( const char *name, byte **pic, int *width, int *height )
{
	int		len;
	char	work[MAX_QPATH];

	*pic = NULL;
	*width = 0;
	*height = 0;

	len = strlen(name);
	if(len >= MAX_QPATH)
	{
		return;
	}
	if (len < 5)
	{
		return;
	}

	strcpy(work, name);

	COM_DefaultExtension( work, sizeof( work ), ".jpg" );
	LoadJPG( work, pic, width, height );

	if (!pic || !*pic)
	{ //jpeg failed, try targa
		strcpy(work, name);
		COM_DefaultExtension( work, sizeof( work ), ".tga" );
		LoadTGA( work, pic, width, height );
	}

	if(*pic)
	{
		return;
	}

	// Dataimage loading failed
	ri->Printf(PRINT_WARNING, "Couldn't read %s -- dataimage load failed\n", name);
}
Exemple #10
0
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg) {
        HANDLE_MSG(hWnd, WM_CREATE, OnCreate);
        HANDLE_MSG(hWnd, WM_DESTROY, OnDestroy);
        HANDLE_MSG(hWnd, WM_PAINT, OnPaint);
        HANDLE_MSG(hWnd, WM_COMMAND, OnCommand);
        HANDLE_MSG(hWnd, WM_ERASEBKGND, OnEraseBkgnd);
    case WM_USER_IMG:
    {
        wchar_t *pwszFile = (wchar_t*)lParam;
        if (wParam == 0)
            LoadBMP(pwszFile, hWnd);
        else
            LoadJPG(pwszFile, hWnd);
        break;
    }
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

    return 0L;
}
bool CreateTexture(UINT &texture, LPSTR strFileName)
{
	if(!strFileName) 
		return false;

	// Define a pointer to a tImage
	tImage *pImage = NULL;

	// If the file is a jpeg, load the jpeg and store the data in pImage
	if(strstr(strFileName, ".jpg"))
	{
		pImage = LoadJPG(strFileName);
	}
	// If the file is a tga, load the tga and store the data in pImage
	else if(strstr(strFileName, ".tga"))
	{
		pImage = LoadTGA(strFileName);
	}
	// If the file is a bitmap, load the bitmap and store the data in pImage
	else if(strstr(strFileName, ".bmp"))
	{
		pImage = LoadBMP(strFileName);
	}
	// Else we don't support the file format that is trying to be loaded
	else
		return false;

	// Make sure valid image data was given to pImage, otherwise return false
	if(pImage == NULL)								
		return false;

	// Generate a texture with the associative texture ID stored in the array
	glGenTextures(1, &texture);

	// This sets the alignment requirements for the start of each pixel row in memory.
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

	// Bind the texture to the texture arrays index and init the texture
	glBindTexture(GL_TEXTURE_2D, texture);

	// Assume that the texture is a 24 bit RGB texture (We convert 16-bit ones to 24-bit)
	int textureType = GL_RGB;

	// If the image is 32-bit (4 channels), then we need to specify GL_RGBA for an alpha
	if(pImage->channels == 4)
		textureType = GL_RGBA;
		
	// Build Mipmaps (builds different versions of the picture for distances - looks better)
	gluBuild2DMipmaps(GL_TEXTURE_2D, pImage->channels, pImage->sizeX, 
					  pImage->sizeY, textureType, GL_UNSIGNED_BYTE, pImage->data);

	//Assign the mip map levels and texture info
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// Now we need to free the image data that we loaded since openGL stored it as a texture

	if (pImage)										// If we loaded the image
	{
		if (pImage->data)							// If there is texture data
		{
			free(pImage->data);						// Free the texture data, we don't need it anymore
		}

		free(pImage);								// Free the image structure
	}

	// Return a success
	return true;
}
Exemple #12
0
// now attempts to load a JPG if TGA load fails...
//
// (return value sent to "*format" param isn't actually used anywhere)
//
bool loadTGA (char *name, byte **pixels, unsigned int *width, unsigned int *height, unsigned int *format)
{
    int                             columns, rows, numPixels;
    byte                    *pixbuf;
    int                             row, column;
    FILE                    *fin;
    byte                    *targa_rgba;
    TargaHeader             targa_header;

    fin = fopen (name, "rb");
    if (!fin) //Debug( "Couldn't read textures" );
    {
        char sLine[256];

        strcpy(sLine,name);
        strlwr(sLine);
        char *p = strstr(sLine,".tga");
        if (p)
        {
            strcpy(p,".jpg");

            LoadJPG( sLine, pixels, (int*)width, (int*)height );

            if (*pixels)
                return true;
        }
        return false;
    }

    targa_header.id_length = fgetc(fin);
    targa_header.colormap_type = fgetc(fin);
    targa_header.image_type = fgetc(fin);

    targa_header.colormap_index = get16(fin);
    targa_header.colormap_length = get16(fin);
    targa_header.colormap_size = fgetc(fin);
    targa_header.x_origin = get16(fin);
    targa_header.y_origin = get16(fin);
    targa_header.width = get16(fin);
    targa_header.height = get16(fin);
    targa_header.pixel_size = fgetc(fin);
    targa_header.attributes = fgetc(fin);

    if (targa_header.image_type!=2
            && targa_header.image_type!=10)
        Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");

    if (targa_header.colormap_type !=0
            || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24))
        Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");

    *format = targa_header.pixel_size;

    columns = targa_header.width;
    rows = targa_header.height;
    numPixels = columns * rows;

    if (width)
        *width = columns;
    if (height)
        *height = rows;
    targa_rgba = new unsigned char[numPixels*4];
    *pixels = targa_rgba;

    if (targa_header.id_length != 0)
        fseek(fin, targa_header.id_length, SEEK_CUR);  // skip TARGA image comment

    if (targa_header.image_type==2) {  // Uncompressed, RGB images
        for(row=rows-1; row>=0; row--) {
            pixbuf = targa_rgba + row*columns*4;
            for(column=0; column<columns; column++) {
                unsigned char red,green,blue,alphabyte;
                switch (targa_header.pixel_size) {
                case 24:

                    blue = getc(fin);
                    green = getc(fin);
                    red = getc(fin);
                    *pixbuf++ = red;
                    *pixbuf++ = green;
                    *pixbuf++ = blue;
                    *pixbuf++ = 255;
                    break;
                case 32:
                    blue = getc(fin);
                    green = getc(fin);
                    red = getc(fin);
                    alphabyte = getc(fin);
                    *pixbuf++ = red;
                    *pixbuf++ = green;
                    *pixbuf++ = blue;
                    *pixbuf++ = alphabyte;
                    break;
                }
            }
        }
    }
    else if (targa_header.image_type==10) {   // Runlength encoded RGB images
        unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
        for(row=rows-1; row>=0; row--) {
            pixbuf = targa_rgba + row*columns*4;
            for(column=0; column<columns; ) {
                packetHeader=getc(fin);
                packetSize = 1 + (packetHeader & 0x7f);
                if (packetHeader & 0x80) {        // run-length packet
                    switch (targa_header.pixel_size) {
                    case 24:
                        blue = getc(fin);
                        green = getc(fin);
                        red = getc(fin);
                        alphabyte = 255;
                        break;
                    case 32:
                        blue = getc(fin);
                        green = getc(fin);
                        red = getc(fin);
                        alphabyte = getc(fin);
                        break;
                    }

                    for(j=0; j<packetSize; j++) {
                        *pixbuf++=red;
                        *pixbuf++=green;
                        *pixbuf++=blue;
                        *pixbuf++=alphabyte;
                        column++;
                        if (column==columns) { // run spans across rows
                            column=0;
                            if (row>0)
                                row--;
                            else
                                goto breakOut;
                            pixbuf = targa_rgba + row*columns*4;
                        }
                    }
                }
                else {                            // non run-length packet
                    for(j=0; j<packetSize; j++) {
                        switch (targa_header.pixel_size) {
                        case 24:
                            blue = getc(fin);
                            green = getc(fin);
                            red = getc(fin);
                            *pixbuf++ = red;
                            *pixbuf++ = green;
                            *pixbuf++ = blue;
                            *pixbuf++ = 255;
                            break;
                        case 32:
                            blue = getc(fin);
                            green = getc(fin);
                            red = getc(fin);
                            alphabyte = getc(fin);
                            *pixbuf++ = red;
                            *pixbuf++ = green;
                            *pixbuf++ = blue;
                            *pixbuf++ = alphabyte;
                            break;
                        }
                        column++;
                        if (column==columns) { // pixel packet run spans across rows
                            column=0;
                            if (row>0)
                                row--;
                            else
                                goto breakOut;
                            pixbuf = targa_rgba + row*columns*4;
                        }
                    }
                }
            }
breakOut:
            ;
        }
    }

    fclose(fin);
    return true;
}
Exemple #13
0
///////////////////BSP::LoadTextures////////
////////////////////////////////////////////
bool BSP::LoadTextures(FILE * file)
{
	//Calculate number of textures
	numTextures=header.directoryEntries[bspTextures].length/sizeof(BSP_LOAD_TEXTURE);

	//Create space for this many BSP_LOAD_TEXTUREs
	BSP_LOAD_TEXTURE * loadTextures=new BSP_LOAD_TEXTURE[numTextures];
	if(!loadTextures)
	{
		errorLog.OutputError("Unable to allocate space for %d BSP_LOAD_TEXTUREs", numTextures);
		return false;
	}

	//Load textures
	fseek(file, header.directoryEntries[bspTextures].offset, SEEK_SET);
	fread(loadTextures, 1, header.directoryEntries[bspTextures].length, file);

	//Create storage space for that many texture identifiers
	decalTextures=new GLuint[numTextures];
	if(!decalTextures)
	{
		errorLog.OutputError("Unable to create storage space for %d texture IDs", numTextures);
		return false;
	}
	
	//Create storage space for that many booleans to tell if texture has loaded
	isTextureLoaded=new bool[numTextures];
	if(!isTextureLoaded)
	{
		errorLog.OutputError("Unable to create storage space for %d booleans", numTextures);
		return false;
	}
	

	//Generate the texture identifiers
	glGenTextures(numTextures, decalTextures);

	//Loop through and create textures
	IMAGE textureImage;				//Image used to load textures

	for(int i=0; i<numTextures; ++i)
	{
		glBindTexture(GL_TEXTURE_2D, decalTextures[i]);
		
		//add file extension to the name
		char tgaExtendedName[68];
		char jpgExtendedName[68];
		strcpy(tgaExtendedName, loadTextures[i].name);
		strcat(tgaExtendedName, ".tga");
		strcpy(jpgExtendedName, loadTextures[i].name);
		strcat(jpgExtendedName, ".jpg");
		
		//Load texture image
		bool isJpgTexture=false;				//have we loaded a jpg?
		if(!textureImage.Load(tgaExtendedName))	//try to load .tga, if not
		{
			if(LoadJPG(&textureImage, jpgExtendedName))	//try to load jpg
			{
				isJpgTexture=true;
				isTextureLoaded[i]=true;
			}
			else
				isTextureLoaded[i]=false;
		}
		else
			isTextureLoaded[i]=true;
		
		//if a jpg texture, need to set UNPACK_ALIGNMENT to 1
		if(isJpgTexture)
			glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

		//Create texture
		gluBuild2DMipmaps(	GL_TEXTURE_2D, GL_RGBA8, textureImage.width, textureImage.height,
							textureImage.format, GL_UNSIGNED_BYTE, textureImage.data);

		glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

		//Set Parameters
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	}

	if(loadTextures)
		delete [] loadTextures;
	loadTextures=NULL;

	return true;
}