PDA

View Full Version : Need help with glBindTexture



12-01-2002, 09:59 AM
This used to work when I didn't use glBindTexture. I know the load_texture function works right. Please help. Thank you.

#include <stdio.h>
#include <stdlib.h>

#include <GL/glut.h>

#define NUMTEXTURES 2
#define GROUND 0
#define ZAP 1

// for scaling factor
int k;
float s;

// texture dimensions
int n;
int m;

GLuint textures[NUMTEXTURES];

GLuint *load_texture(char *);
void bind_textures(void);
void display();
void myreshape(int, int);

int main(int argc, char **argv)
{
bind_textures();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 500);
glutInitWindowPosition(0,0);
glutCreateWindow("image");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

void bind_textures(void)
{
GLuint *ground, *zap;

glGenTextures(NUMTEXTURES, textures);

ground = load_texture("rock.ppm");
glPixelTransferf(GL_RED_SCALE, s);
glPixelTransferf(GL_GREEN_SCALE, s);
glPixelTransferf(GL_BLUE_SCALE, s);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);

glBindTexture(GL_TEXTURE_2D, textures[GROUND]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, n, m, 0, GL_RGB, GL_UNSIGNED_INT, ground);
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_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

zap = load_texture("Zapotec.ppm");
glPixelTransferf(GL_RED_SCALE, s);
glPixelTransferf(GL_GREEN_SCALE, s);
glPixelTransferf(GL_BLUE_SCALE, s);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);

glBindTexture(GL_TEXTURE_2D, textures[ZAP]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, n, m, 0, GL_RGB, GL_UNSIGNED_INT, zap);
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_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, textures[GROUND]);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex2i(0, 0);
glTexCoord2f(0.0, 1);
glVertex2i(0, 200);
glTexCoord2f(1, 1);
glVertex2i(200, 200);
glTexCoord2f(1, 0.0);
glVertex2i(200, 0);
glEnd();
glFlush();
}


void myreshape(int h, int w)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,h,w);
}

GLuint *load_texture(char *b)
{
GLuint *image;
FILE *fd;
int nm;
char c;
int i;
int red, green, blue;

printf("Hello\n");
printf("\n%s\n", b);
fd = fopen(b, "r");
fscanf(fd, "%[^\n] ", b);

if(b[0] != 'P'&#0124; &#0124; b[1] != '3')
{
printf("%s is not a PPM file!\n", b);
exit(0);
}

printf("%s is a PPM file\n", b);
fscanf(fd, "%c", &c);

while(c == '#')
{
fscanf(fd, "%[^\n] ", b);
printf("%s\n",b);
fscanf(fd, "%c",&c);
}

ungetc(c,fd);
fscanf(fd, "%d %d %d", &n, &m, &k);

//printf("%d rows %d columns max value= %d\n",n,m,k);

nm = n*m;
image = malloc(3*sizeof(GLuint)*nm);
s = 255./k;

for(i = 0; i < nm; i++)
{
fscanf(fd,"%d %d %d", &red, &green, &blue );
image[3*nm-3*i-3] = red;
image[3*nm-3*i-2] = green;
image[3*nm-3*i-1] = blue;
}

printf("Read image\n");

return image;
}

12-01-2002, 10:00 AM
All I get is white image in place of the texture.

Bob
12-01-2002, 10:31 AM
In you main function, call bind_textures after glutCreateWindow. You must have a rendering context before you can use any OpenGL functions.

12-01-2002, 12:31 PM
That works, thanks, but only for one texture! When I comment out the lines from zap = load_texture("Zapotec.ppm"); to glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);, the programs works fine, but when I include those lines I get an error saying that it can't load the file because it's not a PPM file. However, I know Zapotec.ppm is valid because I am able to load Zapotec.ppm as a single texture.