Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: question

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    19

    question

    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;
    }

  2. #2
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: question

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    19

    Re: question

    I made the changes but it still doesn't work

  4. #4
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: question

    I have tried your code with a 256x256 image and it work. Your problem seem outside of the LoadTGA function.

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    19

    Re: question

    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,&forma t);
    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);
    };

  6. #6
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: question

    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.

  7. #7
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    19

    Re: question

    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.

  8. #8
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: question

    This is my test code:
    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_STENCIL|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_REPLACE);
     
      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_FILTER,GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,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)!=sizeof(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);
     
    }

  9. #9
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    19

    Re: question

    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.

  10. #10
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: question

    Your last code work for me. What is your video card and driver version?


Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 04-23-2018, 12:03 AM
  2. Texture question and sphere question...
    By detzel in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-19-2001, 12:02 PM
  3. Matrix question (Not really an OGL Question)
    By KurtCob in forum OpenGL: Basic Coding
    Replies: 22
    Last Post: 04-04-2001, 07:55 AM
  4. Quake3 bsp question (not really openGL question)
    By ribblem in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 01-15-2001, 10:13 AM
  5. Replies: 2
    Last Post: 01-08-2001, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean