# Help using glDrawArrays correctly

This topic is 2430 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I got this 1D array that represents 3D array. Here is its definition
 typedef GLint PlaneType[HEIGHT*WIDTH*DEPTH]; PlaneType m_grid; size_t m_stepSize; const size_t GRID_SIZE = HEIGHT*WIDTH*DEPTH; 

And I am trying to use glDrawArrays to the above m_grid as so:
 void draw()const{ glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3,GL_INT,0,m_grid); glDrawArrays(GL_QUADS,0,GRID_SIZE/4); glDisableClientState(GL_VERTEX_ARRAY); } 

but its not actually displaying the 3D plane correctly. Here is how I initialize the m_grid:
 void _initGrid() { for(int y = 0; y < HEIGHT; ++y){ for(int x = 0; x < WIDTH; ++x){ //(x,y,z) for current point size_t posX = _getCoordinateMapping(y,x,0); size_t posY = _getCoordinateMapping(y,x,1); size_t posZ = _getCoordinateMapping(y,x,2); m_grid[posX] = x; m_grid[posY] = y; m_grid[posZ] = 0; } } } //converts 3d array coordinate to 1d array coordinate size_t _getCoordinateMapping(size_t row, size_t col, size_t depth)const{ return (row * WIDTH + col) * 3 + depth; } 

Note that if I use glBegin/glEnd like so :
 void draw1()const { const size_t MAX_HEIGHT = HEIGHT - m_stepSize; const size_t MAX_WIDTH = WIDTH - m_stepSize; glBegin(GL_QUADS); for(int y = 0; y < MAX_HEIGHT; y += m_stepSize) { for(int x = 0; x < MAX_WIDTH; x += m_stepSize) { size_t dx = x + m_stepSize; size_t dy = y + m_stepSize; glVertex3f(m_grid[_getCoordinateMapping(y,x,0)], m_grid[_getCoordinateMapping(y,x,1)], m_grid[_getCoordinateMapping(y,x,2)]); glVertex3f(m_grid[_getCoordinateMapping(y,dx,0)], m_grid[_getCoordinateMapping(y,dx,1)], m_grid[_getCoordinateMapping(y,dx,2)]); glVertex3f(m_grid[_getCoordinateMapping(dy,dx,0)], m_grid[_getCoordinateMapping(dy,dx,1)], m_grid[_getCoordinateMapping(dy,dx,2)]); glVertex3f(m_grid[_getCoordinateMapping(dy,x,0)], m_grid[_getCoordinateMapping(dy,x,1)], m_grid[_getCoordinateMapping(dy,x,2)]); } } glEnd(); } 

then it correctly displays the grid. I'm trying to move towards the glDrawArrays first but its not displaying correctly. Any help guys? Also does the m_grid have to be initialized a certain way, maybe to adapt to clockwise or counterclockwise position?

EDIT: Oh I see. To use glDrawArrays, the vertices needs to be repeated! Well I guess, I'll see my options. Anyways, any input to solve this problem would be great help

##### Share on other sites
GLfloat (and GL_FLOAT) will be a better choice than ints for starters, for the basic reason that GPUs use floats internally, so using an int will require some kind of intermediate conversion stage.

The last param of glDrawArrays is wrong in your code - it should be the number of vertices you're using, not the number of primitives.

I'm not sure what your _getCoordinateMapping is doing, but the way to set up positions in a vertex array for quads is something like this:
GLfloat quadpositions[NUMVERTS][3]; // NUMVERTS should be number of quads * 4 // first vertex (of 4 to define a quad) quadpositions[0][0] = x0; quadpositions[0][1] = y0; quadpositions[0][2] = z0; // second, and etc quadpositions[1][0] = x1; quadpositions[1][1] = y1; quadpositions[1][2] = z1; . . . glVertexPointer (3, GL_FLOAT, 0, quadverts); glDrawArrays (GL_QUADS, 0, NUMVERTS);

For a grid you'll have a lot of repeated vertices here, so you will normally want to index it and use glDrawElements instead (in the old days you would use triangle strips and you'll find a lot of out of date info on the web advising to use strips; on more or less any hardware released over the past 10 years indexed GL_TRIANGLES are better, unless you're targetting certain mobile hardware that still prefers strips). That would complicate things a little too much for now I think, so let's just focus on getting it working with glDrawArrays first.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10

• 15
• 13
• 14
• 75
• 22
• ### Forum Statistics

• Total Topics
632145
• Total Posts
3004344

×