Trouble with IBOs

Started by
5 comments, last by SeraphLance 11 years, 3 months ago

So I'm trying to run an extremely barebones OpenTK program -- basically the quickstart program (which worked), but with VBOs/IBOs instead of immediate mode.

I generate a grid of vertices, and associated indices. For testing purposes, that grid is currently statically set to just a 2x2 quad, and the vertices/indices check out when I debug it. If I draw the vertex buffer with GL.DrawArrays, it also works, albeit with some massaging to get it to look right. However, the moment I try to call GL.DrawElements, the entire program crashes, with the worst driver error I have ever seen:

"Too many errors occurred, which indicates a serious problem from which we cannot recover. The application must close."

NVIDIAs site help doesn't really target developers, so none of the information is very helpful to me. Plus, I'm fairly sure my drivers shouldn't have a problem with IBOs no matter how old they are (and I updated them just in case). So, I'm pretty sure despite the contents of the error and the literature on it, it's a problem with the program. I can't seem to figure out what though, since the problem is stupid simple.

Code is below:

http://pastebin.com/RUTBk0LS

I'd appreciate if anyone could see if I'm missing anything.

EDIT: After about 3 hours, I've figured something out. Not sure why it's correct, but it is:


//GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedShort, indices);
//should be
GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedShort, (IntPtr)(0));
Advertisement

Ok, so I've got a new issue, and since this still technically relates to index buffers I'm going to post it here.

I'm trying to write a tile engine. Each tile therefore needs to have it's own texture. However, I don't want to render every single tile in it's own draw call either. As I understand it, the typical way to do this is texture atlasing, and, as I further understand it, this is done by varying the texture coordinates on the vertices. That makes sense when I have duplicate vertices. However, how does that work when I index them? A vertex can only have one texture coordinate, obviously. For example, if I have a 2x2 texture atlas, let's say with rock/dirt/cobblestone/grass tiles, and a 2x2 map.


Atlas
+-------+-------+
|	|	|
|Rock	|Grass	|
|	|	|
+-------+-------+
|	|	|
|Dirt	|Stone	|
|	|	|
+-------+-------+


Map
+-------+-------+
|	|	|
|Grass  |Grass	|
|	|	|
+-------+-------+
|	|	|
|Grass	|Stone	|
|	|	|
+-------+-------+

In this case, what would be the texcoord of the middle vertex on the map? To match the bottom left tile, it would need to be (1, 1), but to match the bottom right tile, it would need to be (0.5, 0.5). How is this resolved? Do people just forget about indices and duplicate the vertices? Or am I fundamentally misunderstanding something about texture atlasing?

This is off the first page, so I'm going to give this one last desperate bump.

This is addressing your first post.

EDIT: After about 3 hours, I've figured something out. Not sure why it's correct, but it is:



//GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedShort, indices);
//should be
GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedShort, (IntPtr)(0));

That's because the fourth element should be an offset into the "enabled" array(s) of elements. From the OpenGL docs:

void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid * indices);

When glDrawElements is called, it uses count sequential elements from an
enabled array, starting at indices to construct a sequence of
geometric primitives.

I'm going to hazard a guess that there's a helper function in OpenTK that takes an array argument and converts it to an IntPtr type. Your working version essentially states "start drawing the enabled elements, and don't skip anything"

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

I'm going to hazard a guess that there's a helper function in OpenTK that takes an array argument and converts it to an IntPtr type. Your working version essentially states "start drawing the enabled elements, and don't skip anything"

I see. The OpenTK docs say something along the lines of

"indices Specifies a pointer to the location where the indices are stored."

As a result, I read my working version as "start drawing these indices at memory location 0" which frankly sounds ludicrous. I think I get how it works after your explanation though, and vaguely recall something similar in the draw calls in XNA as well.

Re: texture atlasing

What's your projected triangle count for the full set of rendered tiles? I ask because the duplicate vertex solution would definitely work, but the memory savings via indexing is vastly different between a 200 poly grid, and a 200k poly grid.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Hard to say at this point, to be honest. It's just one of those things where I plan to massage with my dimensions until I get something that "works" for my project.

It's a pretty lightweight game, so even 200k polys would probably be easily in budget, but I'd like to do things as "right" as I can in this case since its such a modular piece of code.

This topic is closed to new replies.

Advertisement