shot.h
#ifndef CTGALOADER_H
#define CTGALOADER_H
class CTGALoader
{
public:
CTGALoader(); ~CTGALoader();
bool LoadTGAFile(char *filename); void SaveTGAScreenShot(char *filename,
int width,
int height); void FreeImage();
unsigned int ID; int imageWidth; int imageHeight;
protected:
void GenerateTexture(); bool LoadTGA(char *filename); bool WriteTGA(char *file, short int width,
short int height,
unsigned char *image);
unsigned char *image; bool textureExist; int type; };
#endif
shot.cpp
#include"shot.h"
#include <windows.h>
#include <./gl/GL.H>
#include <./gl/GLU.H>
#include <STDIO.H>
#pragma comment(lib,"OPENGL32.LIB")
#pragma comment(lib,"Glu32.lib")
CTGALoader::CTGALoader()
{
image = 0;
textureExist = false;
type = 0;
}
CTGALoader::~CTGALoader()
{
FreeImage(); }
bool CTGALoader::LoadTGAFile(char *file)
{
if(textureExist)
FreeImage();
if(!LoadTGA(file))
return false;
if(image == 0)
{
return false;
}
GenerateTexture();
textureExist = true;
return true;
}
void CTGALoader::GenerateTexture()
{
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_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);
gluBuild2DMipmaps(GL_TEXTURE_2D, type, imageWidth, imageHeight,
type, GL_UNSIGNED_BYTE, image);
glTexImage2D(GL_TEXTURE_2D, 0, type, imageWidth, imageHeight,
0, type, GL_UNSIGNED_BYTE, image);
}
bool CTGALoader::LoadTGA(char* file)
{
FILE *pfile;
unsigned char tempColor; unsigned char uselessChar; short int uselessInt; int colorMode; long tgaSize; long index; unsigned char imageTypeCode;
unsigned char bitCount;
pfile = fopen(file, "rb");
if (!pfile)
return false;
fread(&uselessChar, sizeof(unsigned char), 1, pfile);
fread(&uselessChar, sizeof(unsigned char), 1, pfile);
fread(&imageTypeCode, sizeof(unsigned char), 1, pfile);
if ((imageTypeCode != 2) && (imageTypeCode != 3))
{
fclose(pfile);
return false;
}
fread(&uselessInt, sizeof(short int), 1, pfile);
fread(&uselessInt, sizeof(short int), 1, pfile);
fread(&uselessChar, sizeof(unsigned char), 1, pfile);
fread(&uselessInt, sizeof(short int), 1, pfile);
fread(&uselessInt, sizeof(short int), 1, pfile);
fread(&imageWidth, sizeof(short int), 1, pfile);
fread(&imageHeight, sizeof(short int), 1, pfile);
fread(&bitCount, sizeof(unsigned char), 1, pfile);
fread(&uselessChar, sizeof(unsigned char), 1, pfile);
colorMode = bitCount / 8;
tgaSize = imageWidth * imageHeight * colorMode;
image = (unsigned char*)malloc(sizeof(unsigned char)*tgaSize);
fread(image, sizeof(unsigned char), tgaSize, pfile);
for (index = 0; index < tgaSize; index += colorMode)
{
tempColor = image[index];
image[index] = image[index + 2];
image[index + 2] = tempColor;
}
fclose(pfile);
if(bitCount == 32)
type = GL_RGBA;
else
type = GL_RGB;
return true;
}
void CTGALoader::FreeImage()
{
if(image)
{
free(image);
image = 0;
textureExist = false;
type = 0;
}
}
bool CTGALoader::WriteTGA(char *file, short int width, short int height, unsigned char *outImage)
{
FILE *pFile; unsigned char uselessChar; short int uselessInt; unsigned char imageType; int index; unsigned char bits; long Size; int colorMode;
unsigned char tempColors;
pFile = fopen(file, "wb");
if(!pFile) { fclose(pFile); return false; }
imageType = 2; colorMode = 3; bits = 24;
uselessChar = 0; uselessInt = 0;
fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
fwrite(&imageType, sizeof(unsigned char), 1, pFile);
fwrite(&uselessInt, sizeof(short int), 1, pFile);
fwrite(&uselessInt, sizeof(short int), 1, pFile);
fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
fwrite(&uselessInt, sizeof(short int), 1, pFile);
fwrite(&uselessInt, sizeof(short int), 1, pFile);
fwrite(&width, sizeof(short int), 1, pFile);
fwrite(&height, sizeof(short int), 1, pFile);
fwrite(&bits, sizeof(unsigned char), 1, pFile);
fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
Size = width * height * colorMode;
for(index = 0; index < Size; index += colorMode)
{
tempColors = outImage[index];
outImage[index] = outImage[index + 2];
outImage[index + 2] = tempColors;
}
fwrite(outImage, sizeof(unsigned char), Size, pFile);
fclose(pFile);
return true;
}
void CTGALoader::SaveTGAScreenShot(char *filename, int w, int h)
{
unsigned char *outputImage = 0;
outputImage = (unsigned char*)malloc(w * h * 3);
memset(outputImage, 0, w * h * 3);
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, outputImage);
WriteTGA(filename, w, h, (unsigned char*)outputImage);
free(outputImage);
}
main.cpp
#include <windows.h>
#include "shot.h"
void main()
{
CTGALoader tga;
tga.SaveTGAScreenShot("123.TGA",1024,768);
}
the tga is whole black, why is it ?
thanks for your reply !
|