PDA

View Full Version : Texture Mapping



dvwood
11-02-2008, 08:04 AM
Hi All,
I am doing texture mapping for the first time in my life, and i am a bit stuck, as i am a beginner and dont know much about programming hehe.. but so far so good. I made 2 planes, one blue and one white (placed on top of the blue plane), and all i want to do is basically load a texture and map it to the blue plane. Now, my compiler tells me that this function ---> gltLoadTGA ( FROM the line that reads: pBytes = gltLoadTGA("stone.tga", &iWidth, &iHeight, &iComponents, &eFormat);)
is not declared... what am i doing wrong?


#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#include <stdlib.h>

#define ESC_KEY 27


GLuint textures[4];
void drawRGBCube();
void display ( void ) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glPushMatrix(); //----> SAVE
glTranslatef(-2.0f, -3.0f, -2.f);
drawRGBCube(); //DRAW CUBE
glPopMatrix(); //---------> RESTORE

}

void drawRGBCube() {

//WHITE PLANE
glBegin(GL_QUADS);
glColor4f(1.0,1.0,1.0,1.0);
glVertex3f(-.15f, 10.0f, -6.0f);
glVertex3f( 4.5f, 10.0f, -6.0f);
glVertex3f( 6.5f,-2.0f, -6.0f);
glVertex3f(-2.5f,-2.0f, -6.0f);
glEnd();

//BLUE PLANE
glColor3f(0.0f, 0.0f, 0.90f); // Blue
glBegin(GL_QUADS);
glVertex3f(-100.0f, -25.3f, -100.0f);
glVertex3f(-100.0f, -25.3f, 100.0f);
glVertex3f(100.0f, -25.3f, 100.0f);
glVertex3f(100.0f, -25.3f, -100.0f);
glEnd();

//TEXTURE: mapping of texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); //TexCoord mapping the textures coordinate with the primitive or the blue plane
glVertex3f(-100.0f, -25.3f, -100.0f);
glTexCoord2f(0.0f, 1.0f); //the corners of the texture correspond to the corners of the quad.
glVertex3f(-100.0f, -25.3f, 100.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(100.0f, -25.3f, 100.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(100.0f, -25.3f, -100.0f);
glEnd();


glutPostRedisplay();
glutSwapBuffers();

}


void SetupRC()
{

GLbyte *pBytes;
GLint iWidth, iHeight, iComponents;
GLenum eFormat;


// LOAD TEXTURE
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
pBytes = gltLoadTGA("stone.tga", &iWidth, &iHeight, &iComponents, &eFormat); //here is the problem gltLoadTGA is not declared in this scope... uhhmmm where do i declare this? i mean what am i supposed to write? I dont understand this function
glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);

//TEXTURE PARAMETERS
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);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);
}




//THE WINDOW
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (height==0)
gluPerspective ( 175, float(width), 1.0, 500.0 ); //75 er en y-størrelse, af FOW, dvs kamera/aksen bliver ændre og ikke objektet
else
gluPerspective ( 175, float(width)/height, 1.0, 500.0); //-1.0 tæt på kamera
glMatrixMode(GL_MODELVIEW);
}
//WINDOW END


void keyboard ( unsigned char key, int x, int y )

{

glLoadIdentity();
switch ( key )
{
case ESC_KEY:
exit(1);
break;

glutPostRedisplay();
break;

default:
break;
}
glutPostRedisplay();
}


int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize(300, 300 );
glutCreateWindow("Cube Animation");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glEnable(GL_DEPTH_TEST);
SetupRC();
glutMainLoop();
return 0;

}

overlay
11-02-2008, 10:43 AM
By googling about gltLoadTGA, it looks like you took your code from the superbible book or from a tutorial based on the superbible book.

Superbible book website:
http://www.starstonesoftware.com/OpenGL/

Superbible book source code:
http://www.starstonesoftware.net/files/SB-AllSource.zip

The function gltLoadTGA() is defined in:

SB-AllSource/examples/src/shared/gltools.cpp

I guess the "t" in "glt" stands for "tools".

dvwood
11-02-2008, 12:06 PM
Yes, that is correct. Its actually part of a code i copied pasted from one of their examples, as i am trying to understand the code and make up a room so i can add textures.

Yeah thats another thing, i copied the header file into my project, but i still get the error: gltools.h: No such file or directory... i basically include it with the rest of the files in the code:

#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#include <stdlib.h>
#include <gltools.h>

Basically i thought this was a standard function that loads the texture onto the scene... i have read the Superbible and i still dont know how to load a texture... Any ideas to what i could do?

dletozeun
11-02-2008, 01:21 PM
Yeah thats another thing, i copied the header file into my project, but i still get the error: gltools.h: No such file or directory... i basically include it with the rest of the files in the code:

#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#include <stdlib.h>
#include <gltools.h>


try:

#include "gltools.h" instead since you include from your project directory and not your IDE search ones.

dvwood
11-02-2008, 02:33 PM
I tried that, didnt work either same thing. I even put this file into the Header folder....
I am on a Mac with Xcode, dont know if it has something to say about that... ?

dvwood
11-02-2008, 02:33 PM
oh btw, what is the difference between <file.h> and "file.h" ?

dletozeun
11-02-2008, 02:52 PM
More information here:

http://www.cplusplus.com/doc/tutorial/preprocessor.html

This is also valid for C language.

dvwood
11-03-2008, 10:21 AM
Ok, so i have now found the error... seems that Superbible have tons of header files with tons of functions... basically it was a nightmare sorting everything out to find out that it doesnt work..... so now i have to look into OpenCV. Why isnt there a simple way to map some textures to my scene..... ???
Anyone got any suggestions??

dletozeun
11-03-2008, 10:34 AM
The solution is to not use the superbible framework.

This the code I use to load a tga picture. It is C++ rewritten from an C code snippet I found on the web. You can use it as you want.



/**
* @brief loads a no-compressed TGA picture in memory
* @param filename file path
* @param width address of a Texture.width
* @param height address of a Texture.height
* @return Timage the picture data
*/
GLubyte* loadTGA(char *filename, GLuint*width, GLuint*height) throw (Error)
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};// Header d'un TGA non compressé
GLubyte TGAcompare[12]; // Header TGA utilisé pour une comparaison
GLubyte header[6]; // 6 premiers bytes utiles du header
int imageSize; // taille de l'image
int type; // type de l'image // l'Timage sous forme de données
int bpp; // bits par pixel de l'image
GLubyte*Timage=NULL; // picture data array

// Lit le fichier et son header
FILE *fichier = fopen(filename, "rb"); // ouvre le fichier
if(fichier==NULL)
{
throw Error("loadTGA::Error: Can not open Targa file: ", filename);
return NULL;
}
else cout<<filename<<" opened"<<endl;
if (fread(TGAcompare,1,sizeof(TGAcompare),fichier)!=s izeof(TGAcompare)) EXIT; // 12 bytes à lire ?
if (memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 ) EXIT; // Correspondent-t-ils à ce qu'on a ?
if (fread(header,1,sizeof(header),fichier)!=sizeof(he ader)) EXIT; // Lisons les 6 bytes suivants

// Récupère les infos de l'Timage
*width = header[1] * 256 + header[0]; // Détermine la largeur de l'Timage
*height = header[3] * 256 + header[2]; // Détermine la largeur de l'Timage
bpp = header[4]; // Détermine le nombre de bits par pixel de l'Timage
if (bpp==24) type=GL_RGB;
else type=GL_RGBA;
imageSize = (*width)*(*height)*bpp/8; // Calcule la taille de l'Timage

// Charge l'Timage
Timage = (GLubyte *) malloc ( imageSize );
if ((int)fread(Timage, 1, imageSize, fichier) != imageSize)
{
throw Error("loadTGA::Error: invalid file");
free (Timage);
EXIT;
}

// Inverse R et B
int t, i;
for ( i = 0; i < imageSize; i += bpp / 8 )
{
t = Timage[i];
Timage[i] = Timage[i+2];
Timage[i+2] = t;
}


fclose (fichier);
return Timage;
}

dvwood
11-04-2008, 10:58 AM
Thanks a bunch will definitely give it a try!