Olie
Olie

Reputation: 24675

How to draw lines (wireframe) using GLKit?

What I have: A GLKViewController and its associated view that draws my cube objects (rotating, if I want :) in the expected location, scale, color, etc.

*What I want: Rather than cubes, I'd like to specify an object with geometry that is an array of vertex-pairs and convince GLKit (implied: OpenGL-ES-2) to draw each pair of vertices as a line-segment in my view space.

NOTE: My line segments do NOT form a line-strip, so no line-strips. My line-segments do NOT form triangles as part of a mesh (or any other geometry.) They're just individual line-segments in space, with no relation to each other. Think "pick-up sticks" or something similar.

What I tried: I created an array of vertex pairs (GLKVector3) and vertex colors (GLKVector4) like this:

lineVertices  = calloc (verts, sizeof(GLKVector3));
colorVertices = calloc (verts, sizeof(GLKVector4));

lineVertices[index++] = GLKVector3Make(x, 0, z);
lineVertices[index++] = GLKVector3Make(x2, 0, z2);
// etc., snip
colorVertices[cIndex++] = GLKVector4Make(1, 0.0, 1, 1);
// etc., snip

...and tried to draw them like this:

glEnable(GL_DEPTH_TEST);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2,   // ** WRONG ** "2" should be "3"
                          GL_FLOAT, GL_FALSE, 0, lineVertices);

glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4,
                              GL_FLOAT, GL_FALSE, 0, colorVertices);

glDrawArrays(GL_LINES, 0, lineVertCount);

glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribColor);

(All this code is in the right place, as proven by the fact that my cube-objects, which use the same infrastructure, draw correctly.)

What I get when I try that: Only some of my line segments draw, and they appear to be pointing every which way, not at all in lovely pattern that I'd hoped to see. I can't quite seem to discern the pattern of he line-segments drawn. It's as if I'm getting random memory, except that my lineVertices array is correct (checked.)

Homework completed: I see a lot of SO questions that are close, but most devolve to line-strips or triangle-meshes or otherwise don't quite apply. Some are setup-problems related (already eliminated, as my cubes draw just fine.) Similar not-quite-right for other non-SO answers, tutorials, etc.

NOTE: I'm relying on built-in shaders (that is, I don't have any create-shader code in my app.)

While my end-result geometry is NOT a cube, I'd be ok with drawing a basic wireframe cube as a proof-of-concept, here. Again, without using triangles or line-strips, though, since my final-result is not going to be appropriate for those.

Can anyone give me any pointers to sample-code that does the sort of bare-line-segment drawing for which I'm looking?

Thanks!

Upvotes: 0

Views: 1304

Answers (1)

Olie
Olie

Reputation: 24675

Oops, d'oh! (I hate when this happens!)

While continuing to try to track this down, I ran across this related SO question where they said they were successfully drawing lines. Comparing that code to mine, I noticed that I had mistakenly changed the "number of coordinates per vertex" from 3 (correct value) to 2. Seems I'd slipped a brain-cog and mistook that value for "number of coordinates per piece of geometry" (3 for triangles, 2 for lines.)

Once I fixed that, the "randomness" of my lines got straightened out and now everything works perfectly.

I was going to withdraw/delete the question, but decided to leave it in the hopes that my mistake (and solution) will serve to teach others. My incorrect code, above, is commented with the correction.

Btw, I noticed that in that other post, the "stride" value is mistakenly set to 3 (should be 0.) I commented, hopefully they'll update the code.

Upvotes: 1

Related Questions