PDA

View Full Version : multiples textured elements do not display



youpi1
10-28-2012, 11:54 PM
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.
The coordinates of each body (x, y, z) is in the "pos" array. 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 body is displayed.

Anyone sees what's wrong ?

siddharth.cmdz
10-29-2012, 07:09 AM
I dont see you actually uploading any texels here...i presume you have created your texture object corrected before this method call. You have a typo " lEnableClientState(GL_TEXTURE_COORD_ARRAY);".. if you have multiple textures for different texture units, then you have to enable those texture units before you bind them. Also i dont see you using glEnable(GL_TEXTURE_2D)..

youpi1
10-29-2012, 08:22 PM
Indeed, I use the following function for loading the 2D texture :


// Load Bitmaps And Convert To Textures
void LoadGLTextures(void)
{
// Load Texture
Image *image1;

// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}

if (!ImageLoad("Data/Star.bmp", image1)) {
exit(1);
}

// Create Textures
glGenTextures(3, &texture[0]);

// linear filtered texture
glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR); // scale linearly when image smalled than texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
};

and I have put glEnable(GL_TEXTURE_2D) in the above drawPoints() function but the result is the same : only one textured element is displayed at the center of the window (x=0, y=0).

It seems that I don't use correctly the function glTexCoordPointer . In the above drawPoints() function, I call it with :


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

where pos is the array of coordinates of each body (x, y, z).

I found another solution for plotting all the bodies with glTranslate in a loop on all the elements :


for(int i=0;i<numBodies;i++)
{
glPushMatrix();
glTranslatef(pos[i],pos[i+1],pos[i+2]);
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
glPopMatrix();

}



but I would like to do the same thing with drawing all at once.

Any idea ?