Results 1 to 7 of 7

Thread: Using glReadPixels to texture a sphere

  1. #1
    Member Contributor
    Join Date
    May 2011
    Location
    Alabama, USA
    Posts
    66

    Using glReadPixels to texture a sphere

    Got another problem folks. This is what I'm trying to do: I have two cubes rotating above a plane and over head and behind them is a sphere. I want to move the camera forward, turn 180 degrees around the y axis, take a screen shot using glReadPixels, restore camera to original orientation and position, covert pixel data to a texture, and map it on to the sphere. The problem I'm having is that the scene comes out in black and white when I do this. I'm stumped. Here's some of my code:

    GLfloat *pix;
    int arraylen;

    glPixelStoref(GL_PACK_ALIGNMENT, 1);

    arraylen = 400 * 400 * 3;
    pix = (GLfloat *)calloc(arraylen, sizeof(GLfloat));

    gluLookAt(0, 5, -22, 0, 0, 0, 0, 1, 0);
    glReadPixels(0, 0, 400, 400, GL_RGB, GL_FLOAT, pix);


    glEnable(GL_TEXTURE_2D);


    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 400, 400, 0, GL_RGB, GL_FLOAT, pix);

    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);


    glLoadIdentity();
    glTranslatef(0.0f, 5.0f, -25.0f);

    glNormal3f(0.0f, 0.0f, 1.0f);
    glutSolidSphere(3.0f, 25, 25);

    glDisable(GL_TEXTURE_GEN_S);
    glDisable(GL_TEXTURE_GEN_T);

    glDisable(GL_TEXTURE_2D);

    Any ideas as to where I'm going wrong?
    Benjamin Barrett aka vindy
    "Our knowledge is finite, while our ignorance is infinite!"

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    458

    Re: Using glReadPixels to texture a sphere

    draw something between your gluLookAt and your glReadPixels.

  3. #3
    Member Contributor
    Join Date
    May 2011
    Location
    Alabama, USA
    Posts
    66

    Re: Using glReadPixels to texture a sphere

    That didn't seem to do the trick.
    Benjamin Barrett aka vindy
    "Our knowledge is finite, while our ignorance is infinite!"

  4. #4
    Member Contributor
    Join Date
    May 2011
    Location
    Alabama, USA
    Posts
    66

    Re: Using glReadPixels to texture a sphere

    Is there anybody else that might be able to help?
    Benjamin Barrett aka vindy
    "Our knowledge is finite, while our ignorance is infinite!"

  5. #5
    V-man
    Guest

    Re: Using glReadPixels to texture a sphere

    When you take a screenshot with your glReadPixels, the scene looks black and white?
    There isn't much in that short piece of code.
    The only thing I can tell you is that you should not use glTexImage2D to update a texture. Use glTexSubImage2D instead. Also, by using floats, you are causing conversion of data which tends to be slow.

    Perhaps you should post the whole project and we'll see what the problem is.

  6. #6
    Junior Member Newbie
    Join Date
    Apr 2011
    Posts
    5

    Re: Using glReadPixels to texture a sphere

    I don't know how to say it, but moving around the camera, take a snapshot and then render it won't work on any graphics api. You have to call gluLookat, render the geometry you want in your texture, copy to texture, clear the scene, and then render again the geometry. And call glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T); right before glEnable(GL_TEXTURE_2D);I hope I understand your problem.

  7. #7
    Member Contributor
    Join Date
    May 2011
    Location
    Alabama, USA
    Posts
    66

    Re: Using glReadPixels to texture a sphere

    I've got a ton of code in my drawScene function. I'm not sure it'll fit here. However, I've been playing with my code and managed to get it to work. I tried using glTexSubImage2D and that didn't do the trick. Instead I'm using gluBuild2DMipmaps. Unfortunately its extremely slow. Here's what I've done:

    GLubyte *pix;
    int width, arraylen;
    GLint viewport[4];
    GLuint tex;

    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glGetIntegerv(GL_VIEWPORT, viewport);

    width = viewport[2] * 3;
    width = (width + 4) & ~3;
    arraylen = width * viewport[3];
    pix = (GLubyte *)calloc(arraylen, 1);

    glPixelStorei(GL_PACK_ALIGNMENT, 3);
    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
    glPixelStorei(GL_PACK_SKIP_ROWS, 0);
    glPixelStorei(GL_PACK_SKIP_PIXELS, 0);

    gluLookAt(0, 5, -20, 0, 0, 0, 0, 1, 0);

    glReadPixels(0, 0, viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pix);

    glEnable(GL_TEXTURE_2D);

    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pix);

    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);


    glLoadIdentity();
    glTranslatef(0.0f, 5.0f, -25.0f);

    glNormal3f(0.0f, 0.0f, 1.0f);
    glutSolidSphere(5.0f, 25, 25);

    glDisable(GL_TEXTURE_GEN_S);
    glDisable(GL_TEXTURE_GEN_T);

    glDisable(GL_TEXTURE_2D);
    Benjamin Barrett aka vindy
    "Our knowledge is finite, while our ignorance is infinite!"

Similar Threads

  1. FBO render to texture and glReadPixels
    By John Qualis in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 10-16-2010, 10:54 AM
  2. glReadPixels from texture
    By ajbiv in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-13-2009, 04:33 AM
  3. Put texture on a sphere
    By Nrtesn Nyarlahotep in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-01-2008, 11:23 AM
  4. Texture on a sphere...
    By Hassan in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-30-2004, 07:42 AM
  5. Texture Sphere
    By detzel in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-27-2001, 03:46 PM

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