glDrawArrays causing Access Violations

Started by
20 comments, last by TerranFury 22 years, 7 months ago
I have a "terrain" class with a "draw" method. I have the following protected member variables & pointers (that apply to this problem):
  
	unsigned int width, height;
	GLfloat ** verts;
	GLfloat ** texs;
  
...and in my constructor, I allocate memory like so:
  
		verts = new GLfloat * [width * 2];
		for(unsigned int i = 0; i < width * 2; i++)
		{
			verts[i] = new GLfloat[3];
		}

		texs = new GLfloat * [width * 2];
		for(i = 0; i < width * 2; i++)
		{
			texs[i] = new GLfloat[2];
		}
  
Later, in my draw() function, I do this:
  
		glVertexPointer  (3, GL_FLOAT, 0, verts);
		glTexCoordPointer(2, GL_FLOAT, 0, texs);
  
And then, later in the draw function, an access violation is produced at the following line of code:
  
glDrawArrays(GL_TRIANGLE_STRIP, 0, width * 2);
  
I figure I must be doing something wrong. Could anybody tell me what that thing is?
Advertisement
Don''t use 2D arrays, they won''t work as they''re not contiguous.

[Resist Windows XP''s Invasive Production Activation Technology!]
I had a linear array of vertices, vertices being arrays of GLfloats. I thought I had remembered seeing it done this way, but you say that doesn't work, so I guess I must be remembering incorrectly. So I tried creating larger linear arrays that are three and two times as large, respectively, so that now, instead of my arrays working like this:

verts[0] = {x1, y1, z1};verts[1] = {x2, y2, z2};verts[2] = {x3, y3, z3};    

etc...

...they now are supposed to work like this:

verts[0] = x1; verts[1] = y1; verts[2] = z1;verts[3] = x2; verts[4] = y2; verts[5] = z2;verts[6] = x3; verts[7] = y3; verts[8] = z3;    


Except they don't work. I tried calling glVertexPointer with different arguments, but nothing I tried worked.

How SHOULD my vertex and texture coordinate arrays be set up, and how SHOULD I call the associated OpenGL functions?

Edited by - TerranFury on August 31, 2001 8:29:00 PM
The rest of your code looks ok. I''ve only experimented with vertex arrays a little though, so I may be missing something. You may want to try GL_TRIANGLES or GL_QUADS as a test though.

[Resist Windows XP''s Invasive Production Activation Technology!]
I''ve heard of something called glDrawElements, and that seems to be an alternative. But I don''t understand what the "indices" argument is supposed to be. Could I use that somehow instead of glDrawPointers, and, if so, how?
did u enable the vertex + texture coordinate arrays?
Yes, both are and were enabled. And even if they weren''t, I doubt that would be the cause of the Access Violation. If they weren''t, all that would happen is the triangle strips wouldn''t be drawn.
Maybe your pointers are invalid, or you''re telling glDraw* to access something out of range with your arguments.

Set up a dummy array with a few values and try drawing that to see if its your code or something else.

As for the glDrawElements, you need to have indices to your verts to be able to use them. If you have any verts that are used by more than 1 triange you can use indices to cut down on the number of verts, so you only store the verts once and use indices, which saves 8 bytes for every use of that vert.

------------
- outRider -
sorry i meant the opposite to what i said :D
did u DISABLE the other arrays cause having them enabled with no valid data to point at will most likely cause an access voilation
Do you have a short example of the use of glDrawElements I could see?

This topic is closed to new replies.

Advertisement