0

I'm trying to display a 128x128 array of floats (0.0 -> 1.0) as a texture of green on black within a legacy FLTK application. The rest of the application uses immediate mode for all it's drawing, so I don't want to change the way it works. From reading tutorials, I've come up with the following calls that should be sufficient to load the texture into graphics memory, and draw it to the current context (managed by FLTK). However, all I can get it to do is draw a black square where the texture should be, drawing over anything else that has been drawn there (GL_LINES).

Is there anything basic that I'm missing here? some initialization step that I've neglected? Thanks in advance. Here the code that I'm trying to get running:

// Part of class init
glGenTextures(1, &tex);

// As far as I can tell, this is optional with FLTK having already
// set these parameters. Omitting them does not change output.
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODEL);
glLoadIdentity();
// This is the meat of the problem. reports no GL errors.
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 128, 128, 0, GL_GREEN, GL_FLOAT, &image);
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex2f(-0.9,-0.9);
glTexCoord2d(0,1); glVertex2f(-0.9, 0.9);
glTexCoord2d(1,0); glVertex2f( 0.9,-0.9);
glTexCoord2d(1,1); glVertex2f( 0.9, 0.9);
glEnd();
glDisable(GL_TEXTURE_2D);
// Again, busywork.
glPopMatrix();
Thomas
  • 11,757
  • 4
  • 41
  • 57
  • Please avoid editing questions in ways that invalidate answers. – Reto Koradi Dec 23 '14 at 08:12
  • I'm simply correcting a typo. Your answer is great, if it was a problem caused by the typo. The problem as stated though is that it renders, but as "a black square where the texture should be". – Thomas Dec 23 '14 at 08:20
  • 1
    IMHO, substantially changing questions after they have been answered is not the way this site is supposed to work. Otherwise people are trying to answer to a moving target, and wasting their time. And yes, changing the call that specifies the texture data **is** a substantial change for a problem where your texture data is not showing up. – Reto Koradi Dec 23 '14 at 08:31
  • If I had made that typo in the code itself, it would never have compiled. It is not a "substantial change" to the problem, merely a clarification that no, the typo is not the issue. I understand that this is frustrating since you put in the time and effort to come up with a reason why it would not be working, and write it up. But I'm looking for an answer to the bigger question of "why does code that compiles, and throw no errors, do something subtly wrong like draw a black box instead of a picture?". – Thomas Dec 23 '14 at 08:42
  • `GL_GREEN` is not a valid format for pixel transfer in modern OpenGL. You can get away with this in legacy GL (just like `GL_ALPHA` and `GL_LUMINANCE`), but do you really want a green RGB texture? If you're only interested in one component a `GL_LUMINANCE` (this is probably what you want), `GL_INTENSITY` or `GL_ALPHA` internal format makes more sense depending on the context. You can always use a "current" color of green and multiply your single-component texture by that. – Andon M. Coleman Dec 23 '14 at 13:01
  • @Thomas: There are lots of state variables in fixed-function GL which could interfere with this, like `TEXTURE_ENV_MODE`, lighting, the current color, multitexturing, the current texture unit, and probably a dozen of others. – derhass Dec 23 '14 at 13:14
  • @AndonM.Coleman Should it not report an error if GL_GREEN is no longer supported? Re: luminance multiply, How would I do that? – Thomas Dec 23 '14 at 14:08
  • @derhass do you have any suggestions of where to start looking? – Thomas Dec 23 '14 at 14:08
  • @Thomas: If your implementation supports `glPopMatrix`, then `GL_GREEN` is supported, that actually wasn't the point I was getting at - if `GL_GREEN` is supported then so is `GL_LUMINANCE`. Regarding luminance, think of it this way - you have a monochrome image and you multiply (default behavior) the color value by `glColor (...)`. So, `glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 128, 128, 0, GL_LUMINANCE, GL_FLOAT, &image);` and before you draw, `glColor4f (0.0f, 1.0f, 0.0f, 1.0f);`. A luminance texture is essentially a fancy way of saying `RGBA (luma, luma, luma, 1.0)`. – Andon M. Coleman Dec 23 '14 at 14:24
  • @Thomas: well, the things I explicitely listed are the things I would suggest looking into, in that order. – derhass Dec 23 '14 at 16:37

0 Answers0