PDA

View Full Version : BumpMap



Hamid Reza
08-20-2010, 04:31 AM
Hi,
I write this code to make a simple bump-map.


//In the inital
SLight.X = -10;
SLight.Y = 1;
SLight.Z = 0;
glDisable(GL_LIGHTING);
Moving_Light = SLight.X;
glGenTextures(1, &CubeMap);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, CubeMap);
Textures.InitCubeMap(128);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
--------------------------------------
//In the each render
glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, CubeMap); //Load CubeMap
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE) ;
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE) ;

glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
Textures.ApplyByName("NormalMaps.Test"); //Load NormalMap
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB) ;
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS) ;
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE) ;

glActiveTextureARB(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
Textures.ApplyByName("Water"); //Load a simple texture
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBegin(GL_QUADS); //Draw a bumpmap quad
LightVector.X=Moving_Light+10;
LightVector.Y=SLight.Y;
LightVector.Z=SLight.Z-10;
LightVector.ScaleTo_0_upto_1(); //Scale light vector values to 0 upto 1
glMultiTexCoord3fARB(GL_TEXTURE0, LightVector.X, LightVector.Y, LightVector.Z);
glMultiTexCoord2fARB(GL_TEXTURE1, 0.0f, 1.0f);
glMultiTexCoord2fARB(GL_TEXTURE2, 0.0f, 1.0f);
glVertex3f(-10,0,10);

LightVector.X=Moving_Light-10;
LightVector.Y=SLight.Y;
LightVector.Z=SLight.Z-10;
LightVector.ScaleTo_0_upto_1();
glMultiTexCoord3fARB(GL_TEXTURE0, LightVector.X, LightVector.Y, LightVector.Z);
glMultiTexCoord2fARB(GL_TEXTURE1, 1.0f, 1.0f);
glMultiTexCoord2fARB(GL_TEXTURE2, 1.0f, 1.0f);
glVertex3f(10,0,10);

LightVector.X=Moving_Light-10;
LightVector.Y=SLight.Y;
LightVector.Z=SLight.Z+10;
LightVector.ScaleTo_0_upto_1();
glMultiTexCoord3fARB(GL_TEXTURE0, LightVector.X, LightVector.Y, LightVector.Z);
glMultiTexCoord2fARB(GL_TEXTURE1, 1.0f, 0.0f);
glMultiTexCoord2fARB(GL_TEXTURE2, 1.0f,0.0f);
glVertex3f(10,0,-10);


LightVector.X=Moving_Light+10;
LightVector.Y=SLight.Y;
LightVector.Z=SLight.Z+10;
LightVector.ScaleTo_0_upto_1();
glMultiTexCoord3fARB(GL_TEXTURE0, LightVector.X, LightVector.Y, LightVector.Z);
glMultiTexCoord2fARB(GL_TEXTURE1, 0.0f, 0.0f);
glMultiTexCoord2fARB(GL_TEXTURE2, 0.0f, 0.0f);
glVertex3f(-10,0,-10);
glEnd();
------------------------------------------------------------
//Textures.cpp
void Textures::InitCubeMap(int size){
unsigned char * data=new unsigned char[size*size*3];
float offset=0.1;
float halfSize=(size/2);
Vector3f CVector;
int Index=0;

for(int j=0; j<size; j++)
{
for(int i=0; i<size; i++)
{
CVector.X=(halfSize);
CVector.Y=(-(j+offset-halfSize));
CVector.Z=(-(i+offset-halfSize));
CVector.ScaleTo_0_upto_1();

data[Index] =(unsigned char)(CVector.X*255);
data[Index+1]=(unsigned char)(CVector.Y*255);
data[Index+2]=(unsigned char)(CVector.Z*255);

Index+=3;
}
}
glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

Index=0;

for(j=0; j<size; j++)
{
for(int i=0; i<size; i++)
{
CVector.X=(-halfSize);
CVector.Y=(-(j+offset-halfSize));
CVector.Z=((i+offset-halfSize));
CVector.ScaleTo_0_upto_1();

data[Index] =(unsigned char)(CVector.X*255);
data[Index+1]=(unsigned char)(CVector.Y*255);
data[Index+2]=(unsigned char)(CVector.Z*255);

Index+=3;
}
}
glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

Index=0;

for(j=0; j<size; j++)
{
for(int i=0; i<size; i++)
{
CVector.X=(i+offset-halfSize);
CVector.Y=(halfSize);
CVector.Z=((j+offset-halfSize));
CVector.ScaleTo_0_upto_1();

data[Index] =(unsigned char)(CVector.X*255);
data[Index+1]=(unsigned char)(CVector.Y*255);
data[Index+2]=(unsigned char)(CVector.Z*255);

Index+=3;
}
}
glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

Index=0;

for(j=0; j<size; j++)
{
for(int i=0; i<size; i++)
{
CVector.X=(i+offset-halfSize);
CVector.Y=(-halfSize);
CVector.Z=(-(j+offset-halfSize));
CVector.ScaleTo_0_upto_1();

data[Index] =(unsigned char)(CVector.X*255);
data[Index+1]=(unsigned char)(CVector.Y*255);
data[Index+2]=(unsigned char)(CVector.Z*255);

Index+=3;
}
}
glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

Index=0;

for(j=0; j<size; j++)
{
for(int i=0; i<size; i++)
{
CVector.X=(i+offset-halfSize);
CVector.Y=(-(j+offset-halfSize));
CVector.Z=(halfSize);
CVector.ScaleTo_0_upto_1();

data[Index] =(unsigned char)(CVector.X*255);
data[Index+1]=(unsigned char)(CVector.Y*255);
data[Index+2]=(unsigned char)(CVector.Z*255);

Index+=3;
}
}
glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

Index=0;

for(j=0; j<size; j++)
{
for(int i=0; i<size; i++)
{
CVector.X=(-(i+offset-halfSize));
CVector.Y=(-(j+offset-halfSize));
CVector.Z=(-halfSize);
CVector.ScaleTo_0_upto_1();

data[Index] =(unsigned char)(CVector.X*255);
data[Index+1]=(unsigned char)(CVector.Y*255);
data[Index+2]=(unsigned char)(CVector.Z*255);

Index+=3;
}
}
glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

delete [] data;

};

But output is a simple textured quad and not existing any effect from light motion.Where is my mistake?
http://imgdl.ir/s2/893dfdf.jpg
Please help me.