Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Texture mapping and display from a float image array

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    4

    Question Texture mapping and display from a float image array

    Hi everybody!

    Sorry if this topic has been discussed previously on the web, but I have been looking for examples on the forum an internet and I have not found anything
    I have a question relative to the use of glTexture2D(...).

    I have a float array which contains the value (only a single value) of a image pixel. Nowadays I display it using OpenGL primitives but it is slow, so I want to display it using Texture mapping and grayscale. But the problem is that anytime I try to use it I get nothing (blank screen).

    This is what I do, hope anybody can help me because I am a beginner

    1. Generate texture, textureID is defined as GLuint in init() method
    Code :
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    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);

    2. Enable texture and draw using Texture2D in paint() method. myImage is defined as float* and is obtained after some calculus every time using calculateMyImage() function
    Code :
     
    calculateMyImage(myImage);
     
     
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, tamx, tamz, 0, GL_LUMINANCE, GL_FLOAT, myImage);
     
    glBegin(GL_QUADS);
        glTexCoord2f(1.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
        glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
        glTexCoord2f(0.0f,0.0f); glVertex3f( 1.0f, 1.0f,-1.0f);
        glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f, 1.0f,-1.0f);
    glEnd();
     
    glDisable(GL_TEXTURE_2D);

    That's it, what I am doing wrong? Can anybody help me?
    Thank you very much in advance

    David

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    We may have to write some tutorials here for old OpenGL since it seems to be very common for people to omit certain important function calls.
    For example, if lighting is disabled, be sure to set your color with glColor4f or glColor4ub or one of those other glColor calls.
    For your texture, you should set the texture environment combiners with a call to glTexEnv.
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)

    Anyway, those are the defaults for GL, so I'll assume the problem is elsewhere.
    If you disable texturing, does a white quad appear on screen?
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    4
    Thank you V-man for the fast reply.
    I have set the texture with TextEnvi as you suggest me. If I disable texturing nothing happens and I still get a black screen (I have set default bakground color to black). Any ideas? Is it the code correct?

    Anyway, you refered the code above as old OpenGL...how do you use new OpenGL methods for displaying a float array image using textures? The procedure is quite different? Sorry for my newness.

    Thank you very much!!!!
    David

  4. #4
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    For rendering a fullscreen quad
    Code :
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
     
    glColor4f(1.0, 1.0, 1.0, 1.0);
    and render a quad with vertices
    {-1.0, -1.0, 0.0}, {1.0, -1.0, 0.0}, {1.0, 1.0, 0.0}, {-1.0, 1.0, 0.0}
    and you should have a fullscreen quad on screen
     
    glutSwapBuffers();

    As for old GL vs new GL
    http://www.opengl.org/wiki/Legacy_OpenGL
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  5. #5
    Junior Member Regular Contributor Kopelrativ's Avatar
    Join Date
    Apr 2011
    Posts
    214
    Deleted, I was too late :-)

  6. #6
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    4
    Thank very much but I am not able to get it working.

    I tried at first using glDrawPixels(...) in this way
    Code :
    glDrawPixels(myImage.width, myImage.height, GL_LUMINANCE, GL_FLOAT, myFloatArray);

    and it works, but I have read it is an obsolete function and it's much better to use glTexImage2D instead...hope anyone can guess what is going wrong...

    Thank you in advance!
    David

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2010
    Location
    Germany, Luebeck
    Posts
    17
    It looks to me that you are drawing a quad in the (X,Z) plane (Y is constant == 1) without setting properly the modelview transformation.
    This is why you don't see anything - the quad is there but you are not "looking" at it.

    Start from V-man's post where he tells you how to set the projection, modelview and gives you the proper quad corners to draw. You will obtain a white quad covering your viewport.

    After that you may add the texture coordinates.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •