• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

# Fighting the (glDraw)Elements

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.

10 replies to this topic

### #1webwraith  Members   -  Reputation: 235

Like
0Likes
Like

Posted 31 December 2010 - 05:53 AM

I'm trying to get a simple grid drawn in OpenGL for a proof of concept, but I'm having troubles. Originally, the grid was procedurally generated from a number of vertices, but I couldn't get the grid to render. I tried reducing the code to a pre-calculated 5 square verts, but I still get no grid. I'm pretty sure it's either the order of indices, or an error in my calls to render.

the code that defines the 5x5 grid, and the indices for a set of triangle strips;
float grid[] = {	-2.f, 0.f,  2.f,	-1.f, 0.f,  2.f,	0.f, 0.f,  2.f,		1.f, 0.f,  2.f,		2.f, 0.f,  2.f ,	-2.f, 0.f,  1.f,	-1.f, 0.f,  1.f,	0.f, 0.f,  1.f,		1.f, 0.f,  1.f,		2.f, 0.f,  1.f ,	-2.f, 0.f,  0.f,	-1.f, 0.f,  0.f,	0.f, 0.f,  0.f,		1.f, 0.f,  0.f,		2.f, 0.f,  0.f ,	-2.f, 0.f, -1.f,	-1.f, 0.f, -1.f,	0.f, 0.f, -1.f,		1.f, 0.f, -1.f,		2.f, 0.f, -1.f ,	-2.f, 0.f, -2.f,	-1.f, 0.f, -2.f,	0.f, 0.f, -2.f,		1.f, 0.f, -2.f,		2.f, 0.f, -2.f };unsigned short index[] = {	 0,  5,  1,  6,  2,  7,  3,  8,  4,  9 ,	 5, 10,  6, 11,  7, 12,  8, 13,  9, 14 ,	10, 15, 11, 16, 12, 17, 13, 18, 14, 19 ,	15, 20, 16, 21, 17, 22, 18, 23, 19, 24 };

and the code to render;
glLoadIdentity();		// move the modelview matrix by 0.008 * the fine coords		glTranslatef( 0.0f, -4.0f, ( 0.008f * ( float ) cam.fine_y ) * -1 );		glRotatef( angle, 1.f, 0.f, 0.f);		// render the vertices in strips, which don't change their		  // relationships with each other.		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );				glDrawRangeElements( GL_TRIANGLE_STRIP, 0, 9, 14, GL_UNSIGNED_SHORT, index );

The "angle" variable is updated each frame, in an attempt to make sure I'm not looking in the wrong direction.

I've tried glDrawElements, glDrawRangeElements, glMultiDrawElements and glDrawArray, all with no success. I get the feeling I should be using glMultiDrawArrays, but if I've done the above wrong, it would really help for someone to point out which value/variable should actually be used.

I know that the screen is updating successfully, as I can get the clear colour to change during runtime.

### #2AndyEsser  GDNet+   -  Reputation: 386

Like
0Likes
Like

Posted 31 December 2010 - 06:30 AM

I haven't checked through all your indices to check they're correct. But one omission is the fact that you never tell it where the Vertex array is.

glVertexPointer(3, GL_FLOAT, 0, grid);

Place that line before the glDrawRangeElements(); and see if you get any further.

### #3Erik Rufelt  Crossbones+   -  Reputation: 3642

Like
0Likes
Like

Posted 31 December 2010 - 06:31 AM

If nothing at all is drawn, try glDisable(GL_CULL_FACE). Your triangles might be back-facing. How do you set up you projection matrix?
You might need to move the camera further back, depending on what value cam.fine_y has, and make sure the near and far plane of the projection matrix have reasonable values.

As for drawing triangle strips, you need to restart the strip when changing to the next row, or insert zero-area invisible triangles to continue drawing. It might be easier to use triangle-lists, especially if the end result is supposed to be a grid.

When using glDrawRangeElements, it should be something like glDrawRangeElements(GL_TRIANGLE_STRIP, 0, 24(last vertex), 40(index count), GL_UNSIGNED_SHORT, index). In this case you will still get problems with starting on a new row, but you get the idea.

### #4webwraith  Members   -  Reputation: 235

Like
0Likes
Like

Posted 31 December 2010 - 08:16 PM

Nope, I already call glVertexPointer, I'd just forgotten to cnp it. I've tried disabling culling, but I still don't see anything, and cam.fine_y translates to 0 at the moment, as I all but stripped out the movement code. My projection matrix is set up with
gluLookAt( 0.f, 6.f, 0.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f );

### #5AndyEsser  GDNet+   -  Reputation: 386

Like
0Likes
Like

Posted 01 January 2011 - 02:25 AM

If cam.fine_y is 0 at the moment, then chances are you are rendering in front of the near plane. Try translate -2.0f in the Z direction and see if it appears.

### #6webwraith  Members   -  Reputation: 235

Like
0Likes
Like

Posted 01 January 2011 - 02:46 AM

Right, I tried both changing the z position in gluLookAt(), and in glTranslatef(), both individually and together, and I'm still not having any luck. I'm going to go try the new tutorials over on NeHe, and see if they work, although I'm using SFML for the window, and not SDL.

EDIT: OK, so I'm back from rewriting this tutorial, and I'm still getting nothing. I'm beginning to wonder if I should switch to SDL and try that to see if I have the same problem...

[Edited by - webwraith on January 1, 2011 9:46:51 AM]

### #7Erik Rufelt  Crossbones+   -  Reputation: 3642

Like
0Likes
Like

Posted 01 January 2011 - 03:29 AM

gluLookAt isn't a projection matrix, it's a view transform. You should do something like this:
glMatrixMode(GL_PROJECTION);glLoadIdentity();double aspect = (double)screenWidth / screenHeight;gluPerspective(45.0, aspect, 0.1, 100.0);glMatrixMode(GL_MODEL_VIEW);glLoadIdentity();glTranslatef(0.0f, -5.0f, -10.0f);draw();

Combine with disabling face-culling to make sure you see something.

### #8webwraith  Members   -  Reputation: 235

Like
0Likes
Like

Posted 01 January 2011 - 03:45 AM

Right, I'm still getting nothing. I didn't want to do a code dump, but it's only 80 lines (with a few blank lines for readability), and I just don't understand what could be wrong.

The following code takes into account all comments and suggestions so far, and I still only get the clear color in the window ( I won't post a picture, it'd be pointless )

The code:
#include <SFML/Window.hpp>float grid[] = {	// triangle	-1.5f, 1.0f, -6.0f,		-2.5f, -1.0f, -6.0f,		-0.5f, -1.0f, -6.0f,	// quad	0.5f, 1.0f, -6.0f,	2.5f, 1.0f, -6.0f,	2.5f, -1.0f, -6.0f,	0.5f, -1.0f, -6.0f};float colors[] = {	// triangle	1.0f, 0.0f, 0.0f,		0.0f, 1.0f, 0.0f,		0.0f, 0.0f, 1.0f,	// quad	1.0f, 1.0f, 1.0f,	1.0f, 0.0f, 1.0f,	0.0f, 1.0f, 1.0f,	1.0f, 1.0f, 0.0f};int main(int arg_count, char ** args){		// create the render window	sf::Window window( sf::VideoMode( 800, 600, 32 ), "ProcTerrain", sf::Style::Close );		/// initialize OpenGL	glShadeModel( GL_SMOOTH );	glClearDepth( 1.0f );	glClearColor( 0.2f, 0.0f, 0.2f, 0.0f );		glEnable( GL_DEPTH_TEST );	glDepthFunc( GL_LEQUAL );	glDepthMask( GL_TRUE );		glColor3f( 1.0f, 1.0f, 1.0f );		glMatrixMode( GL_PROJECTION );	glLoadIdentity();		glMatrixMode( GL_MODELVIEW );	glLoadIdentity();		glDisable( GL_CULL_FACE );		glEnableClientState( GL_VERTEX_ARRAY );	glEnableClientState( GL_COLOR_ARRAY );		glVertexPointer( 3, GL_FLOAT, 0, &grid[0] );	glColorPointer( 3, GL_FLOAT, 0, &colors[0] );		/// handle events	sf::Event event;	float angle = 0.f;	float time = 0.f;	while( window.IsOpened() ){		/// deal with events		while( window.GetEvent(event) ){			switch( event.Type ){				case sf::Event::KeyPressed : {					if( event.Key.Code == sf::Key::Escape )						window.Close();					break;				}				case sf::Event::Closed : {					window.Close();				}			}		}				/// to render		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );		glLoadIdentity();				glDrawArrays( GL_TRIANGLES, 0, 3 );		glDrawArrays( GL_QUADS, 3, 4 );				window.Display();	}		glDisableClientState( GL_COLOR_ARRAY );	glDisableClientState( GL_VERTEX_ARRAY );		return 0;}

I'm getting a little frustrated with this now, I hope someone can point out my mistake.
I really appreciate everyones help with this.

### #9__sprite  Members   -  Reputation: 461

Like
0Likes
Like

Posted 01 January 2011 - 04:11 AM

You don't appear to be setting up a projection matrix at all (as Erik's already said).

### #10webwraith  Members   -  Reputation: 235

Like
0Likes
Like

Posted 01 January 2011 - 04:37 AM

Oops, got my two files mixed up. You're right sprite_hound, that was the problem with the file I posted [embarrass]. My apologies for an obviously wrong post.

OK, so my test file (the one in my last post) is now working, and it's now time to sort out my original source. I'll put up a note on whether it works or not a bit later.

### #11webwraith  Members   -  Reputation: 235

Like
0Likes
Like

Posted 01 January 2011 - 06:39 AM

Got it, as usual I needed to muck around with the view to properly translate and rotate the mesh into view. Darn I suck at this whole 3D business... [imwithstupid]

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.

PARTNERS