PDA

View Full Version : texture to terrain



phoenix07p
07-01-2009, 06:36 AM
1>I want to add texture to my terrain. terrain is rendered correctly from bmp using vertex arrays. how do i do it? i tried this

GLuint num_texture;
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitPlanes;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
unsigned char *data;

void LoadTexture()
{
char *filename="//home//aditya//zztrials//tex.bmp";
num_texture = 1;
FILE *file;
char temp;
long i;

if((file = fopen(filename, "rb")) == NULL)
cout << "Problem opening " << filename << ".It may not excist.\n";

fseek(file, 18, SEEK_CUR);
fread(&amp;biWidth, sizeof(int), 1, file);
fread(&amp;biHeight, sizeof(int), 1, file);
fread(&amp;biPlanes, sizeof(short int), 1, file);

if(biPlanes != 1)
{
cout << "Planes from " << file << " is not 1: " << biPlanes << "\n";
return 0;
}

fseek(file, offset, SEEK_SET);

data = (unsigned char *) malloc(biWidth * biHeight * 3);
if(data == NULL)
{
cout << "Error allocating memory for color-corrected image data.\n";
return 0;
}

if((i = fread(data, biWidth * biHeight * 3, 1, file)) != 1)
{
cout << "Error reading image data from " << filename << ".\n";
return 0;
}

for(i = 0;i < (biWidth * biHeight * 3);i += 3)
{
temp = data[i];
data[i] = data[i+2];
data[i+2] = temp;
}

fclose(file);
glGenTextures(1,&amp;num_texture);
glBindTexture(GL_TEXTURE_2D, num_texture);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, biWidth, biHeight, GL_RGB, GL_UNSIGNED_BYTE, data);

free(data);

}

offset used in fseek is where data starts. but i get a segmentaion fault at glubuild2dmipmaps.

2>also i am storing texture coordinates like this

for(int x=0;x<WIDTH;x++)
{
for(int y=0;y<HEIGHT;y++)
{

texture_coords[2*(x+y*WIDTH)] = x/biWidth;
texture_coords[2*(x+y*WIDTH)+1] = y/biHeight;
}
}

i am enableing textures in glenableclientstate and in render function i am using


glVertexPointer(3,GL_FLOAT,0,vertices2);
glNormalPointer(GL_FLOAT,0,normals2);
glTexCoordPointer(3,GL_UNSIGNED_BYTE,0,data);
glColor3f(1.0f,1.0f,0.0f);
glutSolidSphere(0.5,15,15);
glDrawElements(GL_TRIANGLES,((WIDTH-1)*(HEIGHT-1)*6),GL_UNSIGNED_INT,indices2);

this works properly without the texture part.but since it stops at glubuild2dmipmaps i wanted to confirm if this is right?
Thanks