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)!=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];
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,pheight,0,format,GL_UNSIGNED_BYTE,pData);
free(pData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,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_REPLACE);
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.