Problem with Vertex Arrays

Started by
6 comments, last by iNsAn1tY 19 years, 6 months ago
I'm working on a software implementation of Bresenham's Algorithm for drawing lines. Thus far I've used multiple calls to glVertex2i to draw the individual points but I'd like to use glVertexArrays instead. For some reason I don't get any output on my screen. Here's the code. I ran a sanity check on the input by inserting glVertex2i() calls that loop thru the pts array vector and that works fine so I know its something with these calls. The point size is 5 and the current color is blue (just in case you were thinking that was the problem).

void drawPoints(const std::vector<int> &pts) {
	glVertexPointer(2, GL_INT, 0, &pts[0]);
	glEnableClientState(GL_VERTEX_ARRAY);
	glDrawArrays(GL_POINTS, 0, pts.size());
	glDisableClientState(GL_VERTEX_ARRAY);
}


Any got any ideas as to what I'm doing wrong here? ~omnisis
Advertisement
Oh yes. There's no way you can pass an std::vector to OpenGL. It has to be a vanilla C array. glVertexPointer probably accepts the pointer to the first element, so you don't get a compiler error, but it won't find any others, because std::vector doesn't store it's elements sequentially (well, it does, but there's other stuff in there too). Looping through with glBegin/glVertex2i/glEnd will work just fine, because you use std::vector's overloaded operators and/or functions to access each element, but to use glDrawArrays, you need to use C arrays so that OpenGL can run through the array itself. The same goes for any OpenGL function that accepts arrays...

EDIT: Most, if not all, of the stuff above is wrong. My bad.

[Edited by - iNsAn1tY on October 2, 2004 9:01:54 PM]
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
sorry, no, but you are wrong iNsAn1tY, its is perfectly fine to use &vec[0] to reference a std::vector as if it was a C-style array. While the object its self does indeed contain extra infomation you are all but garenteed that the address returned by &vec[0] is the starting point of a contiguous block of memory ONLY containing the data in the array, nuffin more, I know this as i've used it to interface with WinSock to send data to and from the network and that expects C arrays as well.
Now, you couldnt do this trick with an std::list, but doing it with an std::vector is perfectly fine.

note: I said 'all but garenteed' earlier because the spec doesnt say it has to be stored like that afaik (i'd check my copy but i'm too tired) so it is possible that some implimentation doesnt store it in one block of memory, however all the ones I know of do store it like that and i'd be shocked if one didnt (i'm pretty certain a vector has to have O(n) access time)
Hmmm, interesting. Didn't think you could do that. You learn something new everyday, huh? So, in theory, omnisis's code works? I can't see anything else wrong with it. In fact, the only thing I can see is that std::vector::size() returns size_type, not GLsizei. Might need a cast, but that probably isn't it. Could the std::vector be to blame in another way?
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
_the_phantom_ is correct. Vectors allocate contiguous blocks of memory. The only thing you have to watch with vectors is when you resize them, pointers to data within the vector may become invalid (due to array doubling or whatever reallocation method is used). Not an issue here though.

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Ok, I feel like a total moron! I figured out the problem was because I was still calling glDrawArrays() between a glBegin() and glEnd() block in the higher level display callback (having started out with multiple glVertex2i() calls). At any rate I feel silly for not having seen this earlier. Thanks for all your help guys and for those who didn't know YES you can use stl::vectors with glDrawArrays() at least I was able to using the VS6.0 compiler.

Thanks For All Your Help,

~omnisis
If anyone wants more information on the safety and portability of using vectors as raw arrays, check out this article from Herb Sutter.

Enigma
Quote:Original post by omnisis
Ok, I feel like a total moron!

You're not the only one [smile]. Glad you got it sorted, though.

Quote:Original post by Enigma
If anyone wants more information on the safety and portability of using vectors as raw arrays, check out this article from Herb Sutter.

Nice one. Bookmarked so I can have a thorough read later...
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]

This topic is closed to new replies.

Advertisement