Search code examples
opengltexturesgluttexture2d

opengl - draw multiples elements with 2D texture


I am working on a n-body code with "glut functions" display. I would like to display each body with a 2D texture from a bmp image. Currently, I can draw a single textured element with the following code :

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer

        glBindTexture(GL_TEXTURE_2D, texture[0]);    // pick the texture.
        glBegin(GL_QUADS); // begin drawing the textured quad.
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, 0.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
        glEnd(); // done drawing the textured quad.

In the first version of my code, I draw the positions of each body with the following display function :

void drawPoints()
{       
        GLuint vbo;

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);   
        glEnableClientState(GL_VERTEX_ARRAY);

        if (colorVBO) {
            glBindBuffer(GL_ARRAY_BUFFER, id_colorVBO);
            glColorPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
            glEnableClientState(GL_COLOR_ARRAY);
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        }

        glColor3f(1, 1, 0);
        glDrawArrays(GL_POINTS, 0, numBodies);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
}

where pos array contains the coordinates x, y, z of each body. I am using glDrawArrays function to draw all the points at the same time.

Could you tell me how to plot all the textured elements and not only one, i.e a way to use the coordinates pos array for indicate the positions of all the textured bodies and draw them.

Updated :

ok, I try to use glTexCoordPointer with the following display function :

void drawPoints()
{       
    glPushMatrix();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
    glBindTexture(GL_TEXTURE_2D, texture[0]);    // pick the texture.
    glLoadIdentity(); // reset the view before we draw each star.
    glTranslatef(0.0f, 0.0f, zoom);    // zoom into the screen. 

    glEnableClientState(GL_VERTEX_ARRAY);
    lEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);   
    glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);

    // Assign A Color Using Bytes
    glColor4ub(30, 100, 120, 255);
    glBegin(GL_QUADS);  // Begin Drawing The Textured Quad
    glTexCoord2f(0.0f, 0.0f);  glVertex3f(-1.0f,-1.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
    glEnd();   // Done Drawing The Textured Quad

    glDrawArrays(GL_QUADS, 0, numBodies);           
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);   
    glDisableClientState(GL_VERTEX_ARRAY);  

    glPopMatrix();  
}

but only one textured element is displayed.

Anyone sees what's wrong ?


Solution

  • In addition to glVertexPointer, you should use glTexCoordPointer. Do not forget to enable the corresponding client state, GL_TEXTURE_COORD_ARRAY. There is also a complete example on the official wiki, with interleaved attributes.

    Note that the new (3.0+) way is to use glVertexAttribPointer along with shaders. There's a very descriptive page on the wiki on that, as well.