Results 1 to 4 of 4

Thread: Need help with glBindTexture

  1. #1
    Guest

    Need help with glBindTexture

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

  2. #2
    Guest

    Re: Need help with glBindTexture

    All I get is white image in place of the texture.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: Need help with glBindTexture

    In you main function, call bind_textures after glutCreateWindow. You must have a rendering context before you can use any OpenGL functions.

  4. #4
    Guest

    Re: Need help with glBindTexture

    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.

Similar Threads

  1. GlBindTexture?
    By Bagoj in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-09-2004, 10:19 PM
  2. Problem with glBindTexture
    By Caran Elmoth in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-20-2003, 06:35 AM
  3. problem with glBindTexture()
    By MickeyMouse in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-08-2002, 08:34 AM
  4. glBindTexture...
    By RandyU in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 01-17-2001, 01:12 PM
  5. glbindtexture
    By wavey do in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-07-2000, 11:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean