PDA

View Full Version : question



Sergiu
02-09-2009, 02:29 AM
Hy.Why does the following function only load tga files with 128*128 pixels or lower?

GLubyte *LoadTGA(char *filename,GLshort *iwidth,GLshort *iheight,GLenum *iformat)
{
GLubyte TGAcompare[12];
GLubyte header[6];
GLuint bytesPerPixel;
GLuint imageSize;
GLuint temp;
GLshort width,height;
FILE *file = fopen(filename, "rb");

if( file==NULL ||
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeo f(TGAcompare) ||
fread(header,1,sizeof(header),file)!=sizeof(header ))
{

if (file == NULL)
return false;
else
{
fclose(file);
return false;
}
}

width = header[1] * 256 + header[0];
height = header[3] * 256 + header[2];

if( width <=0 || height<=0 || (header[4]!=24 && header[4]!=32))
{
fclose(file);
return NULL;
}
bytesPerPixel = header[4]/8;
imageSize = width*height*bytesPerPixel;

GLubyte *imageData;
imageData=(GLubyte*)malloc(imageSize);

if(imageData==NULL ||
fread(imageData, 1, imageSize, file)!=imageSize)
{
if(imageData!=NULL)
free(imageData);
fclose(file);
return NULL;
}

for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)
{
temp=imageData[i];
imageData[i] = imageData[i + 2];
imageData[i + 2] = temp;
}

if (bytesPerPixel==4)
*iformat=GL_RGBA;
else *iformat=GL_RGB;
*iwidth=width;
*iheight=height;
fclose (file);
return imageData;
}

trinitrotoluene
02-09-2009, 08:07 AM
Change the type of width and height from GLshort to GLint. The following instructions: width = header[1] * 256 + header[0]; and
height = header[3] * 256 + header[2]; overflow for GLshort type when the image width and height is greater than 128.

Sergiu
02-09-2009, 08:32 AM
I made the changes but it still doesn't work :(

trinitrotoluene
02-09-2009, 10:02 AM
I have tried your code with a 256x256 image and it work. Your problem seem outside of the LoadTGA function.

Sergiu
02-09-2009, 11:35 AM
Here is the code that uses this function.

void SetupRC(void)
{
GLubyte *pData;
GLint pwidth;
GLint pheight;
GLenum format;

glClearColor(0.0,0.0,0.0,1.0);
pData=LoadTGA("Baner3.tga",&pwidth,&pheight,&format);
glTexImage2D(GL_TEXTURE_2D,0,format,pwidth,pheight ,0,format,GL_UNSIGNED_BYTE,pData);
free(pData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MO DULATE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
};

trinitrotoluene
02-09-2009, 12:55 PM
My code is almost the same as you except that I call glEnable(GL_TEXTURE_2D); and glBindTexture(GL_TEXTURE_2D,texID) just before glTexImage2D. Notice that your LoadTGA function only work with uncompressed TGA file. If I choose to save a TGA image with RLE compression (with gimp) I cannot load the image with your code. Maybe your Baner3.tga image is in a TGA compressed format. If you want, I can post my test code so you can verify with your image.

Sergiu
02-09-2009, 01:02 PM
It would be very kindful of you if you could post your code.The function LoadTGA does indeed only load uncompressed files,but this is no problem because for the moment I only use uncompressed images :).

ps: baner3.tga is a uncompressed file.The application just doesn't loads it until I scale it to a dimension of 128*128.

trinitrotoluene
02-09-2009, 01:05 PM
This is my test code:


#include <iostream>
#include <stdio.h>

#include <GL/glut.h>

GLuint texID;

GLvoid display(GLvoid);
GLvoid init(GLvoid);
GLubyte *LoadTGA(char *filename,GLint *iwidth,GLint *iheight,GLenum *iformat);

GLvoid reshape(GLsizei w,GLsizei h);

int main(int argc,char **argv)
{

glutInit(&amp;argc,argv);

glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_STEN CIL|GLUT_DOUBLE);
glutCreateWindow("LoadTGA");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();

return (EXIT_SUCCESS);
}

/*-----------------------------------------------------------------------------*/
GLvoid display(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL _STENCIL_BUFFER_BIT);

glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texID);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_RE PLACE);

glBegin(GL_TRIANGLE_STRIP);

glTexCoord2f(0.0,0.0);
glVertex4f(-1.0,-1.0,0.0,1.0);
glTexCoord2f(1.0,0.0);
glVertex4f(1.0,-1.0,0.0,1.0);
glTexCoord2f(0.0,1.0);
glVertex4f(-1.0,1.0,0.0,1.0);
glTexCoord2f(1.0,1.0);
glVertex4f(1.0,1.0,0.0,1.0);

glEnd();

glDisable(GL_TEXTURE_2D);

glutSwapBuffers();
}

/*-----------------------------------------------------------------------------*/
GLvoid init(GLvoid)
{
GLenum format;
GLint width,height;
GLubyte *data;
GLubyte data2[3] = {128,0,0};
data = LoadTGA("256.tga",&amp;width,&amp;height,&amp;format);
std::cout<<sizeof(data)<<std::endl;
glEnable(GL_DEPTH_TEST);

glGenTextures(1,&amp;texID);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texID);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,width,height, 0,format,GL_UNSIGNED_BYTE,data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _CLAMP_TO_EDGE);
// glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,1,1,0,GL_RGB, GL_UNSIGNED_BYTE,data2);

glDisable(GL_TEXTURE_2D);


}

/*-----------------------------------------------------------------------------*/
GLubyte *LoadTGA(char *filename,GLint *iwidth,GLint *iheight,GLenum *iformat)
{
GLubyte TGAcompare[12];
GLubyte header[6];
GLuint bytesPerPixel;
GLuint imageSize;
GLuint temp;
GLint width,height;
FILE *file = fopen(filename, "rb");

if( file==NULL ||
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeo f(TGAcompare) ||
fread(header,1,sizeof(header),file)!=sizeof(header ))
{

if (file == NULL)
return false;
else
{
fclose(file);
return false;
}
}

width = header[1] * 256 + header[0];
height = header[3] * 256 + header[2];

std::cout<<width<<std::endl;
std::cout<<height<<std::endl;

if( width <=0 || height<=0 || (header[4]!=24 &amp;&amp; header[4]!=32))
{
fclose(file);
return NULL;
}
bytesPerPixel = header[4]/8;
std::cout<<bytesPerPixel<<std::endl;
imageSize = width*height*bytesPerPixel;

GLubyte *imageData = 0;
imageData=(GLubyte*)malloc(imageSize);

if(imageData==NULL ||
fread(imageData, 1, imageSize, file)!=imageSize)
{
if(imageData!=NULL)
free(imageData);
fclose(file);
return NULL;
}

for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)
{
temp=imageData[i];
imageData[i] = imageData[i + 2];
imageData[i + 2] = temp;
}

if (bytesPerPixel==4)
{
*iformat=GL_RGBA;
}
else
{
*iformat=GL_RGB;
}
*iwidth=width;
*iheight=height;
fclose (file);
return imageData;
}

/*------------------------------------------------------------*/
GLvoid reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);

}

Sergiu
02-09-2009, 01:36 PM
I have made some adjustments to my code but still it won't work.This is becoming very frustrating...I lost a lot of time with this Tga thing.
Anyway, here's my code(amateur structuring):


#include <GL\gl.h>
#include <GL\glu.h>
#include <GL\glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

GLuint texID;

/////////////////////////////////TGA LOADER /////////////////////////////////
GLubyte *LoadTGA(char *filename,GLint *iwidth,GLint *iheight,GLenum *iformat)
{
GLubyte TGAcompare[12];
GLubyte header[6];
GLuint bytesPerPixel;
GLuint imageSize;
GLuint temp;
GLint width,height;
FILE *file = fopen(filename, "rb");

if( file==NULL ||
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeo f(TGAcompare) ||
fread(header,1,sizeof(header),file)!=sizeof(header ))
{

if (file == NULL)
return false;
else
{
fclose(file);
return false;
}
}

width = header[1] * 256 + header[0];
height = header[3] * 256 + header[2];

if( width <=0 || height<=0 || (header[4]!=24 && header[4]!=32))
{
fclose(file);
return NULL;
}
bytesPerPixel = header[4]/8;
imageSize = width*height*bytesPerPixel;

GLubyte *imageData;
imageData=(GLubyte*)malloc(imageSize);

if(imageData==NULL ||
fread(imageData, 1, imageSize, file)!=imageSize)
{
if(imageData!=NULL)
free(imageData);
fclose(file);
return NULL;
}

for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)
{
temp=imageData[i];
imageData[i] = imageData[i + 2];
imageData[i + 2] = temp;
}

if (bytesPerPixel==4)
*iformat=GL_RGBA;
else *iformat=GL_RGB;
*iwidth=width;
*iheight=height;
fclose (file);
return imageData;
}
///////////////////////////////////////////////////////////////////////


/////////////////////////////FUNCTION THAT CALCULATES THE NORMAL OF A TRIANGLE////////
void calcNormala(GLfloat normal[3],GLfloat V1[3],GLfloat V2[3],GLfloat V3[3])
{

GLfloat ar1[3],ar2[3];
ar1[0]=V2[0]-V1[0];
ar1[1]=V2[1]-V1[1];
ar1[2]=V2[2]-V1[2];

ar2[0]=V3[0]-V1[0];
ar2[1]=V3[1]-V1[1];
ar2[2]=V3[2]-V1[2];

normal[0]=(ar1[1]*ar2[2])-(ar1[2]*ar2[1]);
normal[1]=-(ar2[2]*ar1[0])-(ar2[0]*ar1[2]);
normal[2]=(ar1[0]*ar2[1])-(ar1[1]*ar2[0]);

float factor=sqrt((normal[0]*normal[0])+(normal[1]*normal[1])+(normal[2]*normal[2]));

normal[0]=normal[0]/factor;
normal[1]=normal[1]/factor;
normal[2]=normal[2]/factor;

}
////////////////////////////////////////////////////////////////////////




void SetupRC(void)
{
GLubyte *pData;
GLint pwidth;
GLint pheight;
GLenum format;
glClearColor(0.0,0.0,0.0,1.0);
glGenTextures(1,&texID);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texID);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
pData=LoadTGA("first_Win_Photo_Gal_02.tga",&pwidth,&pheight,&format);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,pwidth,pheigh t,0,format,GL_UNSIGNED_BYTE,pData);
free(pData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glEnable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
//glEnable(GL_DEPTH_TEST);
};



void RenderScene(void)
{
GLfloat p1[3]={1.0f,0.0f,1.0f},p2[3]={1.0f,0.0f,-1.0f},p3[3]={-1.0f,0.0f,-1.0f}
,p4[3]={-1.0f,0.0f,1.0f},p5[3]={0.0f,2.0f,0.0f};
GLfloat normala[3];
glClear(GL_COLOR_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texID);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_RE PLACE);
glColor3f(1.0f,1.0f,1.0f);
glPushMatrix();
glTranslatef(0.0f,0.0f,-5.0f);
gluLookAt(1.0f,1.0f,1.0f,0.0f,1.0f,0.0f,0.0f,1.0f, 0.0f);
glBegin(GL_TRIANGLES);

//base of piramid
calcNormala(normala,p1,p2,p4);
glNormal3fv(normala);
glTexCoord2f(1.0f,0.0f);
glVertex3fv(p1);
glTexCoord2f(1.0f,1.0f);
glVertex3fv(p2);
glTexCoord2f(0.0f,0.0f);
glVertex3fv(p4);

calcNormala(normala,p2,p3,p4);
glNormal3fv(normala);
glTexCoord2f(1.0f,1.0f);
glVertex3fv(p2);
glTexCoord2f(0.0f,1.0f);
glVertex3fv(p3);
glTexCoord2f(0.0f,0.0f);
glVertex3fv(p4);

//right face of piramid
calcNormala(normala,p1,p2,p5);
glNormal3fv(normala);
glTexCoord2f(0.0f,0.0f);
glVertex3fv(p1);
glTexCoord2f(1.0f,0.0f);
glVertex3fv(p2);
glTexCoord2f(0.5f,1.0f);
glVertex3fv(p5);
//front face of piramid ...
calcNormala(normala,p1,p5,p4);
glNormal3fv(normala);
glTexCoord2f(1.0f,0.0f);
glVertex3fv(p1);
glTexCoord2f(0.5f,1.0f);
glVertex3fv(p5);
glTexCoord2f(0.0f,0.0f);
glVertex3fv(p4);
glEnd();
glPopMatrix();
glutSwapBuffers();
}


void ChangeSize(GLsizei w,GLsizei h)
{
if (h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(GLfloat)w/(GLfloat)h,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(600,600);
glutCreateWindow("Simplu");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
return 0;
}

Also note that I canot set this : glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _CLAMP_TO_EDGE);
The compiler generates an error saying that it doesn't recognize what GL_CLAM_TO_EDGe is.Probably my OpenGL version is out-of-date.

trinitrotoluene
02-09-2009, 02:09 PM
Your last code work for me. What is your video card and driver version?

http://img25.imageshack.us/img25/931/texturesd4.png

Sergiu
02-09-2009, 02:22 PM
NVIDIA RIVA TNT2 Model 64/Model 64 Pro
driver version: 6.1
OpenGL version: 1.1
My computer is 5 years old..

trinitrotoluene
02-09-2009, 02:32 PM
I have almost the same video card, a NVidia TNT2 (Diamond Viper 770). You don't have the latest drivers. Try to download the latest driver on the NVidia web site (www.nvidia.com). My OpenGL version is: 1.5.3 NVIDIA 71.86.07 on Linux

My computer is 9 years old!