• Create Account

## Trouble with IBOs

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1SeraphLance  Members

2403
Like
0Likes
Like

Posted 11 January 2013 - 06:42 PM

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));

Edited by SeraphLance, 11 January 2013 - 11:06 PM.

### #2SeraphLance  Members

2403
Like
0Likes
Like

Posted 12 January 2013 - 10:52 AM

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?

### #3SeraphLance  Members

2403
Like
0Likes
Like

Posted 14 January 2013 - 11:34 AM

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

### #4BCullis  Members

1951
Like
1Likes
Like

Posted 14 January 2013 - 12:20 PM

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"

Edited by BCullis, 14 January 2013 - 12:21 PM.

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

### #5SeraphLance  Members

2403
Like
0Likes
Like

Posted 14 January 2013 - 12:27 PM

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.

### #6BCullis  Members

1951
Like
0Likes
Like

Posted 14 January 2013 - 12:40 PM

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)

### #7SeraphLance  Members

2403
Like
0Likes
Like

Posted 14 January 2013 - 01:40 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.