Skip to main content
GameDev.net gamedev.net
🔒 Locked

Texture Problem

Started by Kris2456 Apr 11, 2006 at 5:24 AM 0 replies 600+ views
Original Post
Kris2456
Kris2456
Yep, its another "my texture wont appear" thread. Anyway, im trying to get this pic to appear (its a tga) but it simply wont appear. I am pretty sure that the texture loads fine. I just think that either ive set it up wrong, or that tCar1 isnt being assigned to a texture.


//textures
CIrisTexture texture;
//textID's
GLuint tCar1=0, tCar2=0;

//car coords
POINT Car1={50,150};
POINT Car2;



int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
{
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_TEXTURE_2D);	// Really Nice Perspective Calculation
    glEnable(GL_COLOR_MATERIAL);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    
    //Load textures
    texture.LoadTGA("car1.tga",tCar1);
    texture.LoadTGA("car2.tga",tCar2);
	return TRUE;										// Initialization Went OK
}

int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();									// Reset The Current Modelview Matrix
	
	//Draw Lines
	glPushMatrix();
	glLoadIdentity();
	glDisable(GL_TEXTURE_2D);
	for(int i=50;i<260;i+=7)
	{
            glBegin(GL_QUADS);
            glColor3f(0,1,0);
            glVertex2i(i,200);
            glVertex2i(i,194);
            glVertex2i(i+6,194);
            glVertex2i(i+6,200);
            glEnd();
    }
   	for(int i=260;i<410;i+=7)
	{
            glBegin(GL_QUADS);
            glColor3f(1,0,0);
            glVertex2i(i,200);
            glVertex2i(i,194);
            glVertex2i(i+6,194);
            glVertex2i(i+6,200);
            glEnd();
    }
    glPopMatrix();
    glEnable(GL_TEXTURE_2D);
    
    //Car1
   	glPushMatrix();
   	glLoadIdentity();
    glBindTexture(GL_TEXTURE_2D, tCar1);
    glBegin(GL_QUADS);
    glTexCoord2f(0,0);glVertex2i(Car1.x-101,Car1.y);
    glTexCoord2f(0,1);glVertex2i(Car1.x-101,Car1.y+34);
    glTexCoord2f(1,1);glVertex2i(Car1.x,Car1.y+34);
    glTexCoord2f(1,0);glVertex2i(Car1.x,Car1.y);
    glEnd();
    glPopMatrix();

    
    
	return TRUE;										// Everything Went OK
}

Ok this is the texture class:

int CIrisTexture::LoadTGA(LPSTR strName, GLuint TexID)
{
    hTGA        *pImage=NULL;
	GLubyte		TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
	GLubyte		TGAcompare[12];
	GLubyte		header[6];
	GLuint		bytesPerPixel;
	GLuint      imgSize;
	GLuint		temp;
	GLuint		type=GL_RGBA;
    
    if(!strName)
        return 1;     
     
    FILE *pFile = NULL;
    pFile = fopen(strName, "rb");
    
    //Check for errors
    if(pFile==NULL){
        return 2;
        fclose(pFile);
    }
    
    pImage = new hTGA;
    
    if(fread(TGAcompare,1,sizeof(TGAcompare),pFile)!=sizeof(TGAcompare))
        return 31;
    
    if(memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0)
        return 32;              
    if(fread(header,1,sizeof(header),pFile)!=sizeof(header))
        return 33;
              
    pImage->width = header[1] * 256 + header[0];
    pImage->height = header[3] * 256 + header[2];
    
    if(pImage->width <= 0 || pImage->height <= 0 || ((header[4] != 24) && (header[4] != 32))){
        fclose(pFile);
        return 4;
    }
    
    pImage->bpp = header[4];
    
    bytesPerPixel= pImage->bpp/8;
    imgSize = pImage->width * pImage->height * bytesPerPixel;
    pImage->pixels = (GLubyte *)malloc(imgSize);
    
    if(pImage->pixels==NULL)
        return 51;
    if(fread(pImage->pixels,1,imgSize,pFile) != imgSize)
    {
        if(pImage->pixels != NULL)
                free(pImage->pixels);
        fclose(pFile);
        return 52;
    }
    
    //BGR -> RGB conversion
    for(GLuint i =0;i<int(imgSize);i+=bytesPerPixel)
    {
        temp = pImage->pixels;                 //blue pixel goes in temp
        pImage->pixels = pImage->pixels[i+2];  //red pixel goes in first slot
        pImage->pixels[i+2] = temp;               //blue pixel from temp goes in last slot
    }                                             //green pixel remains unchanged
    fclose(pFile);
    
    //Create texture
    glGenTextures(1,&TexID);
    
    glBindTexture(GL_TEXTURE_2D, TexID);
    
    if(pImage->bpp==24)
        type=GL_RGB;
    
    glTexImage2D(GL_TEXTURE_2D, 0, type, pImage->width, pImage->height, 
                 0, type, GL_UNSIGNED_BYTE, pImage->pixels);
	
	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);
	
	glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
    free(pImage->pixels);
    delete[] pImage;	
	
    return 6;
}

------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
Gorax
Gorax
You're generating the texture ID, but you're not returning it in any form. The best way to fix this problem at the moment, would to pass the texture ID as a reference, like this: LoadTGA(LPSTR strName,GLuint& TexID). Alternatively, you could use pointers, but then you'd have to check to see if they were valid before trying to use them, but a reference works just as well.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.