How to draw a background in openGL

I have a program for animating the solar system. The program draws the sun and several orbital planets. I like to apply the pixmap nebula in the background, but I don't know how to do it. What I tried does not seem to work. Here is what I have tried.

1) Use glDrawPixels to draw a pixmap. With this approach, I was able to draw a pixmap, but it covered solar objects. I tried to translate pixmap, but that didn't work either.

2) Draw GL_QUADS as a plane behind solar objects. The plane was like a tiny parallelogram.

For everyone I know, this may be a completely wrong approach. I would appreciate it if someone could point me in the right direction. Here is the code for method number 1.

    
void
universe :: createObj ()
{
    QPixmap map ("hst_orion_nebula.jpg");

    glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE, map.width (), map.height (), 0,
                  GL_BGRA, GL_UNSIGNED_BYTE, map.convertToImage (). Bits ());

    callId = glGenLists (itemId :: next ());

    glNewList (callId, GL_COMPILE);

      glDrawPixels (map.width (), map.height (), GL_RGBA,
         GL_UNSIGNED_BYTE, map.convertToImage (). Bits ());

    glEnable (GL_TEXTURE_2D);
    glEndList ();
}

void
universe :: draw ()
{
   glPushMatrix ();
   glPushAttrib (GL_COLOR_BUFFER_BIT | GL_LIGHTING_BIT);

   glCallList (callId);

   glPopMatrix ();
   glPopAttrib ();
}

/////////////////////////////////
void
sun :: createObj ()
{
    QPixmap map ("sunmap.jpg");

    glGenTextures (10, canvas :: _ instance -> _ texture);
    glBindTexture (GL_TEXTURE_2D, canvas :: _ instance -> _ texture [0]);

    glTexImage2D (GL_TEXTURE_2D, 0, GL_LUMINANCE, map.width (), map.height (), 0,
                  GL_BGRA, GL_UNSIGNED_BYTE, map.convertToImage (). Bits ());

    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_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    callId = glGenLists (itemId :: next ());

    glNewList (callId, GL_COMPILE);

    GLUquadricObj * sphere_quadric = gluNewQuadric ();
    gluQuadricTexture (sphere_quadric, GL_TRUE);
    gluQuadricDrawStyle (sphere_quadric, (GLenum) GLU_SMOOTH);
    gluSphere (sphere_quadric, 25, 36, 36);

    glEnable (GL_TEXTURE_2D);
    glEndList ();
}

void
sun :: draw ()
{
   glEnable (GL_LIGHTING);

   glPushMatrix ();

   glPushAttrib (GL_COLOR_BUFFER_BIT | GL_LIGHTING_BIT);

   GLfloat color1 [4] = {1, 0.50, .0, 1};

   glRotated (_xangle, 1, 0, 0);
   glRotated (_yangle, 0, 1, 0);
   glRotated (_zangle, 0, 0, 1);

    glBindTexture (GL_TEXTURE_2D, canvas :: _ instance -> _ texture [0]);
   GLfloat shine [1] = {128};
   glMaterialfv (GL_FRONT_AND_BACK, GL_EMISSION, color1);
   glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, shine);
   glCallList (callId);
   glPopMatrix ();
   glPopAttrib ();
}



+5
source share
1 answer

To draw a full-screen ATV:

glBegin(GL_QUADS);
  glVertex2f(1,1);
  glVertex2f(1,0);
  glVertex2f(0,0);
  glVertex2f(0,1);
glEnd();

Call first and then your whole scene. and use the shader program as follows:

void main(void){
   gl_Position =  gl_Vertex * 2.0 - 1.0;
   gl_TexCoord[0]  = gl_Vertex;
  }

also beware because gl_Vertex is officially deprecated. If you need to use a fixed pipeline, this is the way:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glBegin( GL_QUADS );

    glTexCoord2f(0,0);
    glVertex2f(-1.0f, -1.0f);
    glTexCoord2f(1,0);
    glVertex2f(1.0f, -1.0f);
    glTexCoord2f(1,1);
    glVertex2f(1.0f, 1.0f);
    glTexCoord2f(0,1);
    glVertex2f(-1.0f, 1.0f);

glEnd();

glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
+3
source

All Articles