PDA

View Full Version : Texture not properly visible

Dhanush
04-04-2015, 05:11 AM
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);
gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
void display(void) {
int i, j, tmp;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//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++)
{
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
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

// 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;
}
}

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;
}

Dhanush
04-05-2015, 06:38 AM

Dhanush
04-05-2015, 06:48 AM
I ran the attached code but did not get anything displayed.. Can anyone help me out to make this code run??

Alfonse Reinheart
04-05-2015, 08:34 AM
Cross-posting (https://www.opengl.org/discussion_boards/showthread.php/185947-Texture-not-properly-visible) is against the forum guidelines (https://www.opengl.org/discussion_boards/showthread.php/176139-Forum-Posting-Guidelines?p=1230021#post1230021). Unless there's some difference between the code you posted here and there, then you're just asking the same question twice. And even if there is, you should amend that thread, not make a new one.

Dhanush
04-05-2015, 07:01 PM
Cross-posting (https://www.opengl.org/discussion_boards/showthread.php/185947-Texture-not-properly-visible) is against the forum guidelines (https://www.opengl.org/discussion_boards/showthread.php/176139-Forum-Posting-Guidelines?p=1230021#post1230021). Unless there's some difference between the code you posted here and there, then you're just asking the same question twice. And even if there is, you should amend that thread, not make a new one.

They both are different codes, but the problem is same.. i just wanted solution for either of this so i did like that.. please can you help me out.. please..

Alfonse Reinheart
04-05-2015, 08:06 PM
I won't debug your entire program for you, but I will offer a piece of debugging advice.

When you get a black screen (or otherwise nothing is drawing), start stripping away bits of your code. Reduce it down to just rendering a single triangle if you have to. Keep cutting away bits until something is displayed. Once you get back to a working state, start putting the pieces back in. The moment you add something that causes the rendering to not work anymore, you know where the problem is.

Though generally, I would apply this in reverse (or rather, what I described is in reverse). That is, when starting a project, start with something that works, then make a small change. Make sure it works. Then make gradual small changes, ensuring that each one works. That way, you don't write up a big ball of stuff and have no idea where it's broken. If something breaks, there are only a few places where the problem could lie.

Dhanush
04-06-2015, 12:09 PM
thank you for your advice.. in the post "Texture not visible" in that code snippet can you tell me how to calculate normals?? i think tat is why the texture is not visible properly... but i am not getting how to calculate the normals... please help me on this..