0

First I tried to read in a ppm file and use that for my texture, however I wasn't sure if that is where my problem lies (in reading files). So I hardcoded a small 4x4 image (random colors) and used that. I still cannot get it to work. Does anyone have any insight?

data2, x2, y2 are all the hardcoded image properties.

glPushMatrix();

    FILE *inFile;
    char dump[3];
    int max, k = 0;

    inFile = fopen("mountains.ppm", "r");
    int x;
    int y;

        fscanf(inFile, "%s", dump);
        //printf("%s", dump);
        fscanf(inFile, "%d %d", &x, &y);


        fscanf(inFile, "%d", &max);
        int arraySize = y*(3*x);
        int data[arraySize];

        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                fscanf(inFile, "%d", &data[k++]);
                fscanf(inFile, "%d", &data[k++]);
                fscanf(inFile, "%d", &data[k++]);
            }
        }

    int data2[] = { 100 , 50, 50, 40,  30,  70, 70, 30, 40, 155, 50, 215,
                     50 , 40, 30, 60,  15,  77, 70, 70, 70,  70, 70,  70,
                     50 , 40, 30, 20, 110, 210, 20, 15, 47,  40, 40,  30,
                     15 , 40, 15, 40,  30,  40, 30, 20, 80,  80, 40,  20};

    int x2 = 4;
    int y2 = 4;


    fclose(inFile);

        glGenTextures(1,&texture);
        glBindTexture(GL_TEXTURE_2D,texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);           

        gluBuild2DMipmaps(GL_TEXTURE_2D,3,x2,y2,GL_RGB,GL_UNSIGNED_BYTE,data2);

    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
    glBindTexture(GL_TEXTURE_2D,texture);

    glTexCoord2d(0,0); glVertex3f(0,0.0,0);
    glTexCoord2d(0,1); glVertex3f(0,0.0,80);
    glTexCoord2d(1,1); glVertex3f(80,0.0,80);
    glTexCoord2d(1,0); glVertex3f(80,0.0,0);
    glDisable(GL_TEXTURE_2D);  

glPopMatrix();  
Kromster
  • 7,181
  • 7
  • 63
  • 111
jaiesh
  • 146
  • 2
  • 12
  • 1
    Could you give us a description of the behavior you do get? Does it display an untextured quad, does it not display anything at all? Also, is there any matrix manipulation outside of this section of code? As-is it seems like the quad would not be in view. – nonVirtualThunk Dec 02 '11 at 19:11
  • 1
    "I still cannot get it to work" By which you mean...? – genpfault Dec 02 '11 at 19:12
  • Sorry I meant nothing will show up. And yeah I did set up a view and everything. I just felt the code would get to lengthy if i included it all here. I just included the part i thought might be important. – jaiesh Dec 02 '11 at 19:27
  • @genpfault, haha why did you edit out my "thanks in advance"? Just curious. – jaiesh Dec 02 '11 at 19:36
  • 1
    [Standard Operating Procedure.](http://meta.stackexchange.com/a/3021/150045) – genpfault Dec 02 '11 at 19:40

1 Answers1

5

First, texture loading is not something you should be doing every frame. You create the texture once, then use it when you need to render with that texture. So you need to get the texture loading code out of the rendering logic.

Next:

gluBuild2DMipmaps(GL_TEXTURE_2D,3,x2,y2,GL_RGB,GL_UNSIGNED_BYTE,data2);

data2 is an array of int, which generally is 4 bytes in size. But you're telling OpenGL to assume that every three bytes of the data is a pixel, the first byte being red, the second being green, etc. But your red values are 4 bytes. So OpenGL doesn't get the data correctly.

What you want is to define data2 as a GLubyte array, not an int array.

Also, never use numbers for the image format (the second parameter). I don't care that the specification says that it's legal; just use a real format. Use GL_RGB8 instead of "3".

Lastly:

glTexCoord2d(0,0); glVertex3f(0,0.0,0);
glTexCoord2d(0,1); glVertex3f(0,0.0,80);
glTexCoord2d(1,1); glVertex3f(80,0.0,80);
glTexCoord2d(1,0); glVertex3f(80,0.0,0);

This pretty much does nothing. Without the glBegin()/glEnd() around it, you're just setting some state values. Indeed, calling any glVertex* command outside of a glBegin()/glEnd() pair results in undefined behavior. It certainly doesn't result in rendering (or if it does, it won't be anything useful).

As an aside, you will find that you will be more productive if you always start with working code. For example, if you want to play with textures, start by doing what you know: draw an untextured shape. Once you have that working, you know that if something breaks, it will solely be because of your texturing code.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Okay thanks a lot that makes a lot more sense. I did not know about GluByte and not including glBegin/End was stupidity on my part. Also it does not render everytime, it is actually in a display list. This would be fine for texture loading right? The image shows up correctly now – jaiesh Dec 02 '11 at 19:34
  • sorry, quick side question. How would I read in GluByte using fscanf()? Would I just use fgets() instead? – jaiesh Dec 02 '11 at 19:39