Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


youpi1

Member Since 29 Oct 2012
Offline Last Active Jan 25 2013 08:59 AM

#4995295 draw multiple 2D textured elements at once

Posted by youpi1 on 29 October 2012 - 10:41 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 (a star).
The coordinates of each body (x, y, z) are in the "pos" array.

I try to use glTexCoordPointer with the following display function :


void drawPoints()
{
		glEnable(GL_TEXTURE_2D);					// Enable texture mapping.	
		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);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
		glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
		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_POINTS, 0, numBodies);
  
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);

		glutSwapBuffers();

}



before this, I call 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_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,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);
};


My main problem is that only one textured element is displayed at the center of the window (x=0, y=0) ( see figure in attachment).

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();
		}

You can see the expected result on the second image in attachment ( N_Bodies.png).

but I would like to avoid this solution and find a way to draw all the bodies at once (with glDrawArrays)

Any idea ?

Attached Thumbnails

  • 2D_texture.png
  • N_Bodies.png



PARTNERS