Draw a pentagon using vertex arrays?

Discuss programming topics that involve the OpenGL API.
Post Reply
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Draw a pentagon using vertex arrays?

Post by Baker »

The rectangle way goes like this:

Code: Select all

	const int VertCount = 4; // Rectangle
	
	const GLfloat Vertex2f [] = 
	{
		drawRect.x1, y1,
		drawRect.x2, y1,
		drawRect.x2, y2,
		drawRect.x1, y2,
	};
	
	const GLfloat TexCoord2f[] = 
	{
		drawRect.left,  drawRect.top,		// Top
		drawRect.right, drawRect.top,		// Right
		drawRect.right, drawRect.bottom,	// Bottom
		drawRect.left,  drawRect.bottom,	// Left
	};

	glEnable  				(GL_TEXTURE_2D);
	glBindTexture			(GL_TEXTURE_2D, TexSlot);
	
	// size (Specifies  the  number  of  coordinates per array element), type (GL_FLOAT, etc), stride, pointer
	glTexCoordPointer		(2, GL_FLOAT, 0, TexCoord2f); 
	glEnableClientState		(GL_TEXTURE_COORD_ARRAY);	
	
	glVertexPointer			(2, GL_FLOAT, 0, Vertex2f);
	glEnableClientState		(GL_VERTEX_ARRAY);
	
	glDrawArrays			(GL_TRIANGLE_FAN, 0, VertCount);	// First, count
	
	glDisableClientState	(GL_VERTEX_ARRAY);
	glEnableClientState		(GL_TEXTURE_COORD_ARRAY);
But I'm not seeing this work for, say, a pentagon or octagon. For a rectangle, conveniently a set of 4 verts you can take the first 3 and draw a triangle and then the last 3 and draw a triangle. On paper, I'm not seeing this work for a pentagon.

Thoughts?

And if not, I'm sure I'll eventually self-solve and post answer.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Draw a pentagon using vertex arrays?

Post by Baker »

Image

Nevermind. ^^ Triangle fan.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

Re: Draw a pentagon using vertex arrays?

Post by frag.machine »

Baker wrote:Image

Nevermind. ^^ Triangle fan.
Shouldn't be a triangle strip ?
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
mh
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Re: Draw a pentagon using vertex arrays?

Post by mh »

Fan or strip can do it. GL_TRIANGLES with indexes is better though.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Draw a pentagon using vertex arrays?

Post by Baker »

frag.machine wrote:Shouldn't be a triangle strip ?
A triangle fan let's me essentially use an existing array of vertices to draw a convex polygon. Vert 1, 2, 3, 4 5 = done

A trianglestrip was what I was thinking I was using, and was thinking in those terms.
mh wrote:Fan or strip can do it. GL_TRIANGLES with indexes is better though.
I'm doing some elementary things with simple shapes. Just want to give it literally 5 verts for a 2D polygon for a pentagram or 8 for an octagon. My little geometry thread at QuakeOne has taken me to somewhere interesting and I'm working on 2D representation of these ideas.

[And geometry applies to image manipulation and on-screen controls and the user interface ... ]
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Post Reply