I was working on water ripple effect project i am caught in applying texture.
Below is the code snippet:
void calcwater() {
int x, y;
float n;
for(y = 1; y < WATERSIZE-1; y++) {
for(x = 1; x < WATERSIZE-1; x++) {
n = ( water[t][x-1][y] +
water[t][x+1][y] +
water[t][x][y-1] +
water[t][x][y+1]
) /2;
n -= water[f][x][y];
n = n - (n / DAMP);
water[f][x][y] = n;
}
}
y = 0;
for(x = 1; x < WATERSIZE-1; x++) {
n = ( water[t][x-1][y] +
water[t][x+1][y] +
water[t][x][y+1]
) /2;
n -= water[f][x][y];
n = n - (n / DAMP);
water[f][x][y] = n;
}
x = 0;
for(y = 1; y < WATERSIZE-1; y++) {
n = ( water[t][x+1][y] +
water[t][x][y-1] +
water[t][x][y+1]
) /2;
n -= water[f][x][y];
n = n - (n / DAMP);
water[f][x][y] = n;
}
x = WATERSIZE-1;
for(y = 1; y < WATERSIZE-1; y++) {
n = ( water[t][x-1][y] +
water[t][x][y-1] +
water[t][x][y+1]
) /2;
n -= water[f][x][y];
n = n - (n / DAMP);
water[f][x][y] = n;
}
y = WATERSIZE-1;
for(x = 1; x < WATERSIZE-1; x++) {
n = ( water[t][x-1][y] +
water[t][x+1][y] +
water[t][x][y-1]
) /2;
n -= water[f][x][y];
n = n - (n / DAMP);
water[f][x][y] = n;
}
}
void reshape(int width, int height) {
w = width;
h = height;
glViewport(0, 0, width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}
void display(void) {
int i, j, tmp;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glPushMatrix();
glTranslatef(0, 0, spin_z-220);
glRotatef(spin_x, 0, 1, 0);
glRotatef(spin_y-60, 1, 0, 0);
glColor3f(0, 0, 0.85);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
calcwater();
glBindTexture(GL_TEXTURE_2D,texture);
for(i = 0; i < WATERSIZE; i++)
{
glBegin(GL_QUAD_STRIP);
for(j = 0; j < WATERSIZE; j++) {
//glColor3f(0,0,1);
glVertex3f(j-WATERSIZE/2, (i+1)-WATERSIZE/2, water[t][j][i+1]);
glVertex3f(j-WATERSIZE/2, i-WATERSIZE/2, water[t][j][i]);
}
glEnd();
}
tmp = t; t = f; f = tmp;
glPopMatrix();
glutSwapBuffers();
}
int num = 0;
int delay = 70;
void idle(void)
{
if(!(++num %delay))
{
water[f][rand()%WATERSIZE][rand()%WATERSIZE] = -rand()%200;
delay = rand()%100 + 50;
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
switch(button) {
case 0:
old_x = x - spin_x;
old_y = y - spin_y;
break;
case 2:
old_y = y - spin_z;
move_z = (move_z ? 0 : 1);
}
glutPostRedisplay();
}
void motion(int x, int y) {
if(!move_z) {
spin_x = x - old_x;
spin_y = y - old_y;
} else {
spin_z = y - old_y;
}
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
static int old_x = 50;
static int old_y = 50;
static int old_width = 512;
static int old_height = 512;
switch (key) {
case 'x':
exit(0);
break;
case 'm':
glutPositionWindow(old_x, old_y);
glutReshapeWindow(old_width, old_height);
break;
case 'f':
if (glutGet(GLUT_WINDOW_WIDTH) < glutGet(GLUT_SCREEN_WIDTH)) {
old_x = glutGet(GLUT_WINDOW_X);
old_y = glutGet(GLUT_WINDOW_Y);
old_width = glutGet(GLUT_WINDOW_WIDTH);
old_height = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen();
}
break;
case ' ':
water[f][WATERSIZE/2][WATERSIZE/2] = -1000;
break;
}
}
void init(void) {
int i, j;
w = glutGet(GLUT_WINDOW_WIDTH);
h = glutGet(GLUT_WINDOW_HEIGHT);
glClearColor(0.0, 0.0, 0.0, 0.0); // Black Background
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glClearDepth(1.0); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enable Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glBlendFunc(GL_SRC_COLOR, GL_ONE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Realy Nice perspective calculations
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
texture = LoadTexture("C:/Users/Dhanush V/Downloads/water/reflection.bmp"); // Load the Texture
// enable spherical environment maping
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
for( i = 0; i < WATERSIZE; i++)
for( j = 0; j < WATERSIZE; j++) {
water[0][j][i] = 0;
water[1][j][i] = 0;
}
}
This is texture loading function:
GLuint LoadTexture( const char * filename )
{
GLuint texture;
int width, height , i;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
width = 1024;
height = 512;
data = (unsigned char *)malloc( width * height * 3 );
//int size = fseek(file,);
fread( data, width * height * 3, 1, file );
fclose( file );
for(i=0;i<width*height;++i)
{
int index = i*3;
unsigned char B,R;
B = data[index];
R = data[index+2];
data[index] = R;
data[index+2] = B;
}
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );
return texture;
}
Please help me out… Please…