Jump to content
  • Advertisement
Sign in to follow this  
thyrgle

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!