Problème avec TGA Loader

psykocrash Messages postés 240 Date d'inscription vendredi 14 juin 2002 Statut Membre Dernière intervention 17 mars 2009 - 8 janv. 2006 à 17:22
psykocrash Messages postés 240 Date d'inscription vendredi 14 juin 2002 Statut Membre Dernière intervention 17 mars 2009 - 15 janv. 2006 à 21:58
Bonjour,

J'ai commencé depuis peut la programmation avec OpenGL/GLut et j'ai
trouvé la classe TGA Loader de Funto66 mais je n'arrive pas à afficher
les textures.

Voila le code source de mon application, j'espère que vous trouverez d'où vient l'erreur...




<hr size="2" width="100%">/*

* Apprendre l'OpenGL / Glut

* par Psykocrash

*

* Lexique :

* - Vertex : ? (Liste de sommets ou Point)

*/



// -Inclusion des fichiers en-tête-

#ifdef WIN32

#include <windows.h>

#endif



#include <stdio.h>



#include <gl/gl.h>

#include <gl/glut.h>



// Classe codée par Funto (funto66@gmail.com)

#include <TGALoader/TGALoader.cpp>



// -Constantes-

#define TEXTURE_HERO 1



// -Déclaration des prototypes-

void Display(void);

void Reshape(int, int);

void ChargerTexture(char *Chemin, int id);

void DessinerTexture(int Id, int X, int Y, int Largeur, int Hauteur,
int X_src, int Y_src, int Largeur_src, int Hauteur_src, float
Transparence);



// -Fonctions-

int main(int argc, char *argv[])

{

// Initialisation

glutInit(&argc, argv); // Initialisation de GLUT

glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE); // Couleurs vraies, un seul tampon



// Paramètres de la fenêtre

glutInitWindowSize(640, 480); // Taille 640*480

//-> Pour un affichage en plein écran, remplacer glutInitWindowSize() par glutFullScreen()

glutInitWindowPosition(50, 50); // Position

glutCreateWindow("Fenetre OpenGL"); // Création de la fenêtre



// Chargement des textures

ChargerTexture("C:\\hero.tga", TEXTURE_HERO);



// Callbacks

glutDisplayFunc(Display); // Affichage

//glutReshapeFunc(Reshape); // Création et redimension de la fenêtre



// Lancement du gestionnaire GLUT

glutMainLoop();



// Fin du programme

return 0;

}



void ChargerTexture(char *Chemin, int id)

{

TGALoader tga_loader;

tga_loader.LoadOpenGLTextureWithID(Chemin, id, TGA_LINEAR);

}



void DessinerTexture(int Id, int X, int Y, int Largeur, int Hauteur,
int X_src, int Y_src, int Largeur_src, int Hauteur_src, float
Transparence)

{

glEnable(GL_BLEND);

glDisable(GL_DEPTH_TEST);

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glColor4f(1.0f, 1.0f, 1.0f, Transparence);



glBindTexture(GL_TEXTURE_2D, Id);

glBegin(GL_QUADS);

glTexCoord2d(X_src, Y_src);

glVertex2i(X, Y);

glTexCoord2d(X_src + Largeur_src, Y_src);

glVertex2i(X+Largeur, Y);

glTexCoord2d(X_src + Largeur_src, Y_src + Hauteur_src);

glVertex2i(X+Largeur, Y+Hauteur);

glTexCoord2d(X_src, Y_src + Hauteur_src);

glVertex2i(X, Y+Hauteur);

glEnd();



glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

glDisable(GL_BLEND);

glEnable(GL_DEPTH_TEST);

}



void Display()

{ // Chargée de l'affichage

glClearColor(0, 0, 0, 0); // Définit une couleur (Rouge,Vert,Bleu,Alpha)

glClear(GL_COLOR_BUFFER_BIT); // Remplis le buffer
d'une valeur définie (par défaut, la valeur 0 => couleur noir)



//----------------------

// Elements de la scène

//----------------------

DessinerTexture(TEXTURE_HERO, 0, 0, 100, 100, 0, 0, 1, 1, 1);

//----------------------



//Sleep(5); // Pause pour ne pas trop utiliser le CPU



glFlush(); // Envoit le flux de données vers le buffer d'affichage

}



void Reshape(int width, int height)

{

// Définit la zone rectangulaire de la fenêtre de résultat qui servira à afficher la scène

glViewport(0,0,width,height); // x,y => coin inférieur gauche


// width,height => dimensions de la fenetre



// Définit la matrice de projection comme matrice courante

glMatrixMode(GL_PROJECTION);



// Réinitialisation de la matrice

glLoadIdentity();



// Calculs pour rendre le repère orthonormal

float L; // Longueur entre les 2 plans de coupe verticaux

float H; // Hauteur entre les 2 plans de coupe horizontaux

if (width <= height)

{

H=(GLfloat) (10*height/width);

L=10.0;

}

else

{

H=10.0;

L=(GLfloat) (10*width/height);

}



// Définit la matrice de projection (place la caméra)

gluOrtho2D(-L/2,L/2,-H/2,H/2); // left,right => coordonnées des plans de coupe verticaux droite et gauche


// bottom,top => coordonnées des plans de coupe horizontaux

}

<hr size="2" width="100%">

8 réponses

niketou Messages postés 295 Date d'inscription dimanche 4 mai 2003 Statut Membre Dernière intervention 6 décembre 2010
8 janv. 2006 à 18:30
textureTGA.h
#ifndef TEXTURETGA_H
#define TEXTURETGA_H


#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include
#include <fstream>
#include "GL/gl.h"
#include "GL/glu.h"


typedef unsigned char byte;


typedef struct{
byte idLength;
byte colorMapType;
byte imageType;
byte colorMapInfo[5];
byte x[2];
byte y[2];
byte width[2];
byte height[2];
byte bpp;
}headerTga;


class textureTGA{
private:
int m_width;
int m_height;
int m_bpp;
int m_format;

public:
textureTGA(const char *filename, const int textureId);
textureTGA(std::string filename, const int textureId){textureTGA(filename.c_str(),textureId);}

int getWidth(){return m_width;}
int getHeight(){return m_width;}
int getBpp(){return m_bpp;}
};


#endif //TEXTURETGA_H

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////::
textureTGA.cpp

#include "textureTGA.h"


textureTGA::textureTGA(const char *filename, const int textureId){
byte *imageData;
int numPixels;
int bytesInPixel;
int imageDataSize;
headerTga fileHeader;
byte temp;

// Open file
std::ifstream textureFile(filename, std::ios::in | std::ios::binary | std::ios::ate);

if(!textureFile.is_open()){
std::cout<<"[TGA] ERROR: Could not open '"<<filename<<"'"<<std::endl;
return;
}
if(int(textureFile.tellg()) == 0){
std::cout<<"[TGA] ERROR: Texture '"<<filename<<"' is empty"<<std::endl;
textureFile.close();
return;
}

// Read TGA header
textureFile.seekg(0, std::ios::beg);
textureFile.read((char*)&fileHeader, sizeof(fileHeader));

m_width = fileHeader.width[0] + (fileHeader.width[1] << 8);
m_height = fileHeader.height[0] + (fileHeader.height[1] << 8);
m_bpp = fileHeader.bpp;


// We only support uncompressed 24 or 32 bits per pixel TGAs
if(fileHeader.colorMapType == 1 || int(fileHeader.imageType) != 2){
std::cout<<"[TGA] ERROR: '"<<filename<<"' is an texture invalid format\n[TGA] ERROR: It should be an uncompressed 24/32bpp TGA"<<std::endl;
textureFile.close();
return;
}
if(m_bpp != 32 && m_bpp != 24){
std::cout<<"[TGA] ERROR: Invalid texture color depth, '"<<filename<<"' must be uncompressed 24/32bpp TGA"<<std::endl;
textureFile.close();
return;
}

// OpenGL textures must be power-of-two
/*if((m_width&(m_width-1)) || (m_height&(m_height-1))){
std::cout<<"[TGA] ERROR: Texture '"<<filename<<"' must have power-of-two width & height"<<std::endl;
textureFile.close();
return;
}*/

// Determine format
switch(m_bpp){ case 24:m_format GL_RGB; bytesInPixel 3; break; case 32:m_format GL_RGBA; bytesInPixel 4; break;
default:
std::cout<<"[TGA] ERROR: Invalid texture color depth, '"<<filename<<"' must be uncompressed 24/32bpp TGA"<<std::endl;
textureFile.close();
return;
break;
}

// Allocate memory for image
numPixels = m_width * m_height;
imageDataSize = numPixels * bytesInPixel;
imageData = new byte[imageDataSize];
if(!imageData){
std::cout<<"[TGA] ERROR: Out of memory"<<std::endl;
textureFile.close();
return;
}

// Read image data
textureFile.seekg(18, std::ios::beg);
textureFile.read((char*)imageData, imageDataSize);
textureFile.close();

// TGAs are BGRA, convert to RGBA
for(int pixel=0; pixel < numPixels*bytesInPixel; pixel+=bytesInPixel){
temp = imageData[pixel+2];
imageData[pixel+2] = imageData[pixel];
imageData[pixel] = temp;
}

// Bind texture ID to load
glBindTexture(GL_TEXTURE_2D, textureId);

// Set texture parameters
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);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

// Upload texture to card with bound texture ID
//glTexImage2D(GL_TEXTURE_2D, 0, m_format, m_width, m_height, 0, m_format, GL_UNSIGNED_BYTE, imageData);
gluBuild2DMipmaps(GL_TEXTURE_2D, m_format, m_width, m_height, m_format, GL_UNSIGNED_BYTE, imageData);

// Texture's uploaded, don't need data any more
delete imageData;

std::cout<<"[TGA] Texture '"<<filename<<"' loaded"<<std::endl;
}
0
psykocrash Messages postés 240 Date d'inscription vendredi 14 juin 2002 Statut Membre Dernière intervention 17 mars 2009
8 janv. 2006 à 18:33
Je voudrais réussir à utiliser TGA Loader, sinon j'aurais déjà utilisé d'autres classes trouvées sur le net...
0
niketou Messages postés 295 Date d'inscription dimanche 4 mai 2003 Statut Membre Dernière intervention 6 décembre 2010
8 janv. 2006 à 18:36
La taille de ton image est bien un multiple de 2?
0
psykocrash Messages postés 240 Date d'inscription vendredi 14 juin 2002 Statut Membre Dernière intervention 17 mars 2009
8 janv. 2006 à 18:38
Oui. Pour les tests j'ai récupéré un tga de funto66 (qu'il a utilisé dans son jeu) pour justement éviter ce genre de problèmes..
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
neodelphi Messages postés 442 Date d'inscription jeudi 4 avril 2002 Statut Membre Dernière intervention 11 août 2008
9 janv. 2006 à 08:56
J'ai pas vu de glEnable(GL_TEXTURE_2D), vérifi que tu l'as pas oublié...
0
Alucards Messages postés 40 Date d'inscription mercredi 26 mars 2003 Statut Membre Dernière intervention 3 septembre 2008
15 janv. 2006 à 20:32
j'ai codé un loader dynamique tga/bmp/pcx/jpg je l'ameliore au fur et a
mesure mais pour le tga je me suis inspirer du loader de funtoo donc si
ca t'intersee regarde a ce lien





Tous les chemins mènent au RHUM !!!!!!!!!!!!!!!!!!!..............................
0
Alucards Messages postés 40 Date d'inscription mercredi 26 mars 2003 Statut Membre Dernière intervention 3 septembre 2008
15 janv. 2006 à 20:32
http://www.cppfrance.com/code.aspx?ID=27341 dsl le ctrl + c avez pas marcher ^^


Tous les chemins mènent au RHUM !!!!!!!!!!!!!!!!!!!..............................
0
psykocrash Messages postés 240 Date d'inscription vendredi 14 juin 2002 Statut Membre Dernière intervention 17 mars 2009
15 janv. 2006 à 21:58
Ok je vais l'essayer, puisqu'apparement, il n'y a pas de solution au problème du loader de funto66
0
Rejoignez-nous