Public Group

# Drawing a whole primitive with OpenGL.

This topic is 3085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I was wondering how to draw an entire primitive with a single float array. For example what would I do to draw a triangle using only a one float array?

I have tried this (note: this wasn't intended to be a triangle):

		glBegin(GL_QUADS);		int i;		for (i = 0; i < sizeof(gunType1); i = i + 3)		{			glVertex3fv(gunType1);		}                glEnd();

But, it says: error: cannot convert 'float' to 'const GLfloat*' for argument '1' to 'void glVertex3fv(const GLfloat*)'

##### Share on other sites
glVertex3fv:
"v" means that a pointer needs to be passed to the function, and gunType1[i] is a value, not a pointer.

try glVertex3fv(gunType1+i);
And there's another bug: sizeof returns the size of stuff in bytes, so not the number of elements in an array.
So use sizeof(gunType1)/sizeof(whatever the type of gunType1 is: float/double for example) But it would be better to simply use the number of elements explicitly.

##### Share on other sites
Somewhere (but not shown to us) is a line like
float gunType1[n];

Using the [] operator resolves to one single element of that array. But you want to use 3 (in this case) elements in sequence. Hence you need to tell where the first element is, e.g.
& gunType1[n]
where the special case of n=0 is equivalent to simply
gunType1

So try
glVertex3fv(&gunType1);

EDIT: szecs was faster. However, his solution is equivalent but uses a different syntax.

##### Share on other sites
glVertex3fv() takes an array of 3 floats that specify position for a single vertex:

float pos[] = { 0.0f, 1.0f, 2.0f };glVertex3fv(pos);

If gunType1 is an array of floats, then gunType1 is a single float, so you can't pass it to glVertex3fv(). You can either make 3 calls to glVertex3f() (notice there's no v), or you can declare gunType1 differently:

float gunType1[][3] = {    { 0, 1, 2, },    { 3, 4, 5, },    // etc.};

Also, this:

i < sizeof(gunType1)

is incorrect. sizeof returns the number of bytes that the array occupies in memory. If you want to know the number of elements in the array, you can take that size and divide it by the size of a single element:

int arrSize = sizeof(gunType1) / sizeof(gunType1[0]);

This is a pretty common trick.

##### Share on other sites
Thanks everyone for their help with my problem. Also, thanks correcting me in my for loop sizeof mistake I was making.

1. 1
2. 2
Rutin
20
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013614
×