#135877 - yellowstar - Thu Jul 26, 2007 10:50 pm
I need some help with loading TGA graphics,
with transparency(alpha).
The way I am doing it won't work.
EDIT:
This problem is solved.
But I have more questions.
See this post in this topic for details.
But I am having more problems, this time
with saving TGAs of captures of the screen(s).
See the above link for details.
<EDIT>
When I first posted this,
I forgot to post what my
problem was.
Here is my problem:
The converted TGA image,
looks nothing like
the TGA graphic.
Also,
It is transparent in
the wrong places.
NOTE:
This above is not the problem
I am having right now,
but it is similar.
See my below posts for details.
</EDIT>
<EDIT2>
I now have a image host.
This means I have posted links to these images.
Go to this post for details.
</EDIT2>
Here's how I am doing it:
The last piece of code is the whole thing,
and the ones before that are
important pieces of code.
Here is the function that loads the TGA graphic:
(The function that generates the texture from
the converted data is at the end of this piece of code.)
(The structs are at the top of the main piece of code,
and the code that does the drawing is at the bottom.)
The following is the whole thing:
Last edited by yellowstar on Sat Aug 04, 2007 12:10 am; edited 9 times in total
with transparency(alpha).
The way I am doing it won't work.
EDIT:
This problem is solved.
But I have more questions.
See this post in this topic for details.
But I am having more problems, this time
with saving TGAs of captures of the screen(s).
See the above link for details.
<EDIT>
When I first posted this,
I forgot to post what my
problem was.
Here is my problem:
The converted TGA image,
looks nothing like
the TGA graphic.
Also,
It is transparent in
the wrong places.
NOTE:
This above is not the problem
I am having right now,
but it is similar.
See my below posts for details.
</EDIT>
<EDIT2>
I now have a image host.
This means I have posted links to these images.
Go to this post for details.
</EDIT2>
Here's how I am doing it:
The last piece of code is the whole thing,
and the ones before that are
important pieces of code.
Here is the function that loads the TGA graphic:
(The function that generates the texture from
the converted data is at the end of this piece of code.)
(The structs are at the top of the main piece of code,
and the code that does the drawing is at the bottom.)
Code: |
bool loadTGA(u8 *DATA, TGAimage *tgaimage, int *texID) { int i; unsigned int temp; BYTE *stream = NULL; if(DATA==NULL){Error("Attempted to loaded null data!",-1);} stream = DATA; tgaimage->texID = texID; tgaimage->header.ID_Length = *stream;stream++; tgaimage->header.colorMapType = *stream;stream++; tgaimage->header.imageType = *stream;stream++; for(int a = 0; a < 5; a++) { tgaimage->header.ColorMapSpecification[a] = *stream;stream++; } SCpy(stream,&tgaimage->header.xOrigin);stream+=2; SCpy(stream,&tgaimage->header.yOrigin);stream+=2; SCpy(stream,&tgaimage->header.imageWidth);stream+=2; SCpy(stream,&tgaimage->header.imageHeight);stream+=2; tgaimage->header.pixelDepth = *stream;stream++; if(tgaimage->header.imageType !=2){ char str[256]; strcpy(str,""); sprintf(str,"%s\nSupported Type: Uncompressed, RGB, with orgin at top-left.(2)\nType: %d","Unsuported TGA type.",(int)tgaimage->header.imageType); Error(str,-2); } tgaimage->width = tgaimage->header.imageWidth; tgaimage->height = tgaimage->header.imageHeight; tgaimage->bpp = tgaimage->header.pixelDepth; if (tgaimage->width <= 0 || tgaimage->height <= 0 || (tgaimage->bpp!=24 && tgaimage->bpp!=32)){ printf("Width: %d\nHeight: %d\n BPP: %d\n",(int)tgaimage->width,(int)tgaimage->height,(int)tgaimage->bpp); Error("Wrong file type.",-3); } unsigned int bytesPerPixel = tgaimage->bpp / 8; unsigned int imageSize = ((tgaimage->width * tgaimage->height) * bytesPerPixel); tgaimage->type = GL_RGBA; if(tgaimage->bpp==24) { tgaimage->type = GL_RGB;printf("RGB\n"); } else { printf("RGBA\n"); } if(tgaimage->header.colorMapType!=0) { Error("Color Map Data detected! Stopping...",-4); } tgaimage->imageData = (BYTE*)malloc((size_t)((int)imageSize)); if(tgaimage->imageData==NULL) { Error("Failed to alloc memory for TGA data!",-5); } for(int I = 0; I < (int)imageSize; I++) { tgaimage->imageData[I] = *stream; stream++; } printf("Image data\n\n"); //Convert BGR to RGB /*for (i =0; i <(int)imageSize; i+=(int)bytesPerPixel) { temp = tgaimage->imageData[i]; tgaimage->imageData[i] = tgaimage->imageData[i+2]; tgaimage->imageData[i+2] = temp; }*/ tgaimage->image = (u16*)malloc(sizeof(u16)*(tgaimage->width*tgaimage->height)); int I = 0; for(int i=0; i < (int)(tgaimage->width*tgaimage->height); i++) { tgaimage->image[i] = RGB8((tgaimage->imageData[I + 0]), (tgaimage->imageData[I + 1]), (tgaimage->imageData[I + 2])) | BIT(15); I+=bytesPerPixel; } createTexture(tgaimage); printf("Success...\n\n"); return 1; } bool createTexture(TGAimage *tgaimage) { glGenTextures(1,tgaimage->texID); glBindTexture(GL_TEXTURE_2D, *tgaimage->texID); //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //GL_UNSIGNED_BYTE glTexImage2D(*tgaimage->texID, 0, GL_RGB, TEXTURE_SIZE_32, TEXTURE_SIZE_32,0, TEXGEN_TEXCOORD, (uint8*)tgaimage->image); return true; } |
The following is the whole thing:
Code: |
// include your ndslib #include <nds.h> #include <malloc.h> //needed to load pcx files #include <nds/arm9/image.h> #include "Pencil_0_tga.h" #include "APencil_0_tga.h" /*#include "Pencil_1_tga.h" #include "Pencil_2_tga.h" #include "Pencil_3_tga.h" #include "Pencil_4_tga.h" #include "Pencil_5_tga.h" */ #include <stdio.h> #include <string.h> #include <stdlib.h> #define Error(message, code) printf("ERROR: %s\n",message);return code; typedef unsigned char BYTE; struct TGA_header { BYTE ID_Length; BYTE colorMapType; BYTE imageType; BYTE ColorMapSpecification[5]; short xOrigin; short yOrigin; short imageWidth; short imageHeight; BYTE pixelDepth; BYTE ImageDesc; }; struct TGAimage { TGA_header header; BYTE *imageData; unsigned int bpp; unsigned int width; unsigned int height; int *texID; unsigned int type; u16 *image; }; bool createTexture(TGAimage *tgaimage); bool loadTGA(char *texFileName); int DrawGLScene(); float xrot; // X Rotation float yrot; // Y Rotation float zrot; // Z Rotation int texture[1]; // Storage For One Texture TGAimage image; int result=0; void SCpy(BYTE *source, short *target) { for(int i = 0; i < (int)sizeof(short); i++) target[i] = source[i]; } void WaitForKey(KEYPAD_BITS key) { if(key != KEY_TOUCH && key != KEY_LID) printf("Press "); if(key == KEY_LID) printf("Close the "); switch(key) { case KEY_A:printf("A");break; case KEY_B:printf("B");break; case KEY_SELECT:printf("SELECT");break; case KEY_START:printf("START");break; case KEY_RIGHT:printf("RIGHT");break; case KEY_LEFT:printf("LEFT");break; case KEY_UP:printf("UP");break; case KEY_DOWN:printf("DOWN");break; case KEY_R:printf("R");break; case KEY_L:printf("L");break; case KEY_X:printf("X");break; case KEY_Y:printf("Y");break; case KEY_TOUCH:printf("TOUCH");break; case KEY_LID:printf("LID");break; } printf(" To Continue...\n\n"); swiWaitForVBlank();swiWaitForVBlank();swiWaitForVBlank(); while(1) { scanKeys(); if(keysDown() & key)break; swiWaitForVBlank(); } consoleClear(); } void WaitForAnyKey() { bool found = false; printf("\nPress Any Key to continue...\n\n"); swiWaitForVBlank();swiWaitForVBlank();swiWaitForVBlank(); while(1) { scanKeys(); for(int i = 0; i <= 12; i++) { if(keysDown() & BIT(i)){found = true;break;} } if(found)break; swiWaitForVBlank(); } consoleClear(); } void DisplayPix(TGAimage *tgaimage, int X, int Y, bool pause) { int the_index = ( ( (int)tgaimage->width * (Y - 1) ) + (X - 1)) * (int)tgaimage->bpp; printf("Pix %d,%d =\nR = %d\nG = %d\nB = %d\n\n",(int)X,(int)Y, (int)tgaimage->imageData[the_index + 0],(int)tgaimage->imageData[the_index + 1], (int)tgaimage->imageData[the_index + 2]); WaitForAnyKey(); } bool loadTGA(u8 *DATA, TGAimage *tgaimage, int *texID) { int i; unsigned int temp; BYTE *stream = NULL; if(DATA==NULL){Error("Attempted to loaded null data!",-1);} stream = DATA; tgaimage->texID = texID; tgaimage->header.ID_Length = *stream;stream++; tgaimage->header.colorMapType = *stream;stream++; tgaimage->header.imageType = *stream;stream++; for(int a = 0; a < 5; a++) { tgaimage->header.ColorMapSpecification[a] = *stream;stream++; } SCpy(stream,&tgaimage->header.xOrigin);stream+=2; SCpy(stream,&tgaimage->header.yOrigin);stream+=2; SCpy(stream,&tgaimage->header.imageWidth);stream+=2; SCpy(stream,&tgaimage->header.imageHeight);stream+=2; tgaimage->header.pixelDepth = *stream;stream++; if(tgaimage->header.imageType !=2){ char str[256]; strcpy(str,""); sprintf(str,"%s\nSupported Type: Uncompressed, RGB, with orgin at top-left.(2)\nType: %d","Unsuported TGA type.",(int)tgaimage->header.imageType); Error(str,-2); } tgaimage->width = tgaimage->header.imageWidth; tgaimage->height = tgaimage->header.imageHeight; tgaimage->bpp = tgaimage->header.pixelDepth; if (tgaimage->width <= 0 || tgaimage->height <= 0 || (tgaimage->bpp!=24 && tgaimage->bpp!=32)){ printf("Width: %d\nHeight: %d\n BPP: %d\n",(int)tgaimage->width,(int)tgaimage->height,(int)tgaimage->bpp); Error("Wrong file type.",-3); } unsigned int bytesPerPixel = tgaimage->bpp / 8; unsigned int imageSize = ((tgaimage->width * tgaimage->height) * bytesPerPixel); tgaimage->type = GL_RGBA; if(tgaimage->bpp==24) { tgaimage->type = GL_RGB;printf("RGB\n"); } else { printf("RGBA\n"); } if(tgaimage->header.colorMapType!=0) { Error("Color Map Data detected! Stopping...",-4); } tgaimage->imageData = (BYTE*)malloc((size_t)((int)imageSize)); if(tgaimage->imageData==NULL) { Error("Failed to alloc memory for TGA data!",-5); } for(int I = 0; I < (int)imageSize; I++) { tgaimage->imageData[I] = *stream; stream++; } printf("Image data\n\n"); //Convert BGR to RGB /*for (i =0; i <(int)imageSize; i+=(int)bytesPerPixel) { temp = tgaimage->imageData[i]; tgaimage->imageData[i] = tgaimage->imageData[i+2]; tgaimage->imageData[i+2] = temp; }*/ tgaimage->image = (u16*)malloc(sizeof(u16)*(tgaimage->width*tgaimage->height)); int I = 0; for(int i=0; i < (int)(tgaimage->width*tgaimage->height); i++) { tgaimage->image[i] = RGB8((tgaimage->imageData[I + 0]), (tgaimage->imageData[I + 1]), (tgaimage->imageData[I + 2])) | BIT(15); I+=bytesPerPixel; } createTexture(tgaimage); printf("Success...\n\n"); return 1; } bool createTexture(TGAimage *tgaimage) { glGenTextures(1,tgaimage->texID); glBindTexture(GL_TEXTURE_2D, *tgaimage->texID); //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //GL_UNSIGNED_BYTE glTexImage2D(*tgaimage->texID, 0, GL_RGB, TEXTURE_SIZE_32, TEXTURE_SIZE_32,0, TEXGEN_TEXCOORD, (uint8*)tgaimage->image); return true; } int LoadGLTextures() { int R=loadTGA((u8*)APencil_0_tga,&image,&texture[0]); return R; } int main() { // Turn on everything powerON(POWER_ALL); /*videoSetMode(MODE_FB0); vramSetBankA(VRAM_A_LCD);*/ /*consoleDemoInit(); videoSetModeSub(MODE_FB0); vramSetBankB(VRAM_B_LCD); */ videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text vramSetBankC(VRAM_C_SUB_BG); //SUB_BG0_CR = BG_MAP_BASE(31); //consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(31), (u16*)CHAR_BASE_BLOCK_SUB(0), 16); consoleDemoInit(); // Setup the Main screen for 3D videoSetMode(MODE_0_3D); vramSetBankA(VRAM_A_TEXTURE); //NEW must set up some memory for textures // IRQ basic setup irqInit(); irqSet(IRQ_VBLANK, 0); // Set our viewport to be the same size as the screen glViewPort(0,0,255,191); // Specify the Clear Color and Depth glClearColor(0,0,0); glClearDepth(0x7FFF); result=LoadGLTextures(); while(1) { // Reset the screen and setup the view glReset(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(35, 256.0 / 192.0, 0.1, 100); glColor3f(1,1,1); glLight(0, RGB15(31,31,31) , 0, floattov10(-1.0), 0); glLight(1, RGB15(31,31,31) , 0, 0, floattov10(-1.0)); glLight(2, RGB15(31,31,31) , 0, 0, floattov10(1.0)); glPushMatrix(); glMatrixMode(GL_TEXTURE); glIdentity(); glMatrixMode(GL_MODELVIEW); //need to set up some material properties since DS does not have them set by default glMaterialf(GL_AMBIENT, RGB15(16,16,16)); glMaterialf(GL_DIFFUSE, RGB15(16,16,16)); glMaterialf(GL_SPECULAR, BIT(15) | RGB15(8,8,8)); glMaterialf(GL_EMISSION, RGB15(16,16,16)); //ds uses a table for shinyness..this generates a half-ass one glMaterialShinyness(); //ds specific, several attributes can be set here glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE | POLY_FORMAT_LIGHT0| POLY_FORMAT_LIGHT1| POLY_FORMAT_LIGHT2); // Set the current matrix to be the model matrix glMatrixMode(GL_MODELVIEW); //Push our original Matrix onto the stack (save state) glPushMatrix(); DrawGLScene(); // Pop our Matrix from the stack (restore state) glPopMatrix(1); // flush to screen glFlush(); swiWaitForVBlank(); } return 0; } int DrawGLScene() // Here's Where We Do All The Drawing { glLoadIdentity(); // Reset The View glTranslatef(0.0f,0.0f,-5.0f); glRotatef(xrot,1.0f,0.0f,0.0f); glRotatef(yrot,0.0f,1.0f,0.0f); glRotatef(zrot,0.0f,0.0f,1.0f); glBindTexture(GL_TEXTURE_2D, texture[0]); glBegin(GL_QUADS); // Front Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Back Face glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Face glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Bottom Face glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Right face glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Left Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glEnd(); xrot+=0.3f; yrot+=0.2f; zrot+=0.4f; return TRUE; } |
Last edited by yellowstar on Sat Aug 04, 2007 12:10 am; edited 9 times in total