Question

I am writing a program in which I have to use two different textures. I can get one file read in and used as a texture but have so far been unable to get two textures to work. Here is my code to set up the one texture that I have:

        FILE *fd;
        int k, nm;
        char c;
        int i, i;
        char b[256];
        float s;
        int red, green, blue;

        fd = fopen("AI_Lab.ppm", "r");

        // check first line for P3
        fscanf(fd, "%[^\n]", b);
        if (b[0] != 'P' || b[1] != '3') {
                 printf("%s is not a PPM file\n", b);
                 system("pause");
                 exit(0);
                 }
        // skip comments
        fscanf(fd, "%c%c", &c, &c);
        while (c == '#') {
              fscanf(fd, "%[^\n]", b);
              fscanf(fd, "%c%c", &c, &c);
        }

        // put back first character of first non-comment line
        ungetc(c, fd);
        // read file info
        fscanf(fd, "%d %d %d", &o, &m, &k);

        nm = o * m; // overall size

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

        glPixelTransferf(GL_RED_SCALE, s);
        glPixelTransferf(GL_GREEN_SCALE, s);
        glPixelTransferf(GL_BLUE_SCALE, s);
        glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glEnable(GL_TEXTURE_2D);
        glTexImage2D(GL_TEXTURE_2D, 0, 3, o1, m1, 0, GL_RGB, GL_UNSIGNED_INT, image);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

        glEnable (GL_DEPTH_TEST);
        glClearColor(0.0, 0.0, 0.0, 1.0);

I have tried all I can think of and cannot get things to work correctly.

Was it helpful?

Solution

Your texture specification looks to be alright, but you'll need to brush up on the basics of OpenGL. The red-book is a good place to start.

http://www.glprogramming.com/red/chapter09.html

Head on down to the section on Texture Objects.

Before calling glTexImage2d, you will want to generate and bind a new texture object. Each one of your textures should have its own object!

When rendering, you simply bind the texture that you want to render prior to drawing any primitives.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top