0

I've been banging my head on my keyboard for the past couple of weeks over this. What I'm trying to do is load an array of floats (GLfloat) and an array of unsigned shorts (GLushort) from a text file into equivalent arrays in objective-c so that I can render the contained objects. I've got my arrays loaded into vector objects as

vector<float> vertices;

and

vector<GLushort> indices;

But for some reason I can't figure out why I can't get these to render. Here is my code for rendering the above:

glVertexPointer(3, GL_FLOAT, sizeof(vertices[0])*6, &vertices[0]);
glNormalPoitner(GL_FLOAT, sizeof(vertices[0])*6, &vertices[3]);
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_SHORT, indices);

Sample arrays are below:

vertices: (Vx, Vy, Vz, Nx, Ny, Nz)

{10, 10, 0, 0, 0, 1,
-10, 10, 0, 0, 0, 1,
-10, -10, 0, 0, 0, 1,
10, -10, 0, 0, 0, 1};

indices: (v1, v2, v3)
{0, 1, 2,
0, 2, 3};

The text file I want to load these arrays from for rendering looks like this:

4 //Number of Vertices
###Vertices###
v 10 10 0 0 0 1
v -10 10 0 0 0 1
v -10 -10 0 0 0 1
v 10 -10 0 0 0 1
###Object1###
2 //Number of faces
f 0 1 2
f 3 4 5

Are vector objects the best approach to take? If not, what is? And what am I doing wrong that these won't render? Thanks.

Davido
  • 2,913
  • 24
  • 38
  • "I can't get these to render". Instead you get...what? Crash? Black screen? – genpfault Nov 30 '10 at 23:13
  • Sorry I didn't clarify, it just didn't render anything but my clearcolor background. Same as if I had commented out my glDrawElements line. Speaking of which, is there a more efficient way to render than glDrawElements for what I'm attempting to do? – Davido Dec 01 '10 at 20:50

1 Answers1

2

You use GL_TRIANGLES to specify the vertices format.

alt text

See the graph about GL_TRIANGLES, your format is wrong. And, I prefer to use GL_TRIANGLE_STRIP format. It needs few vertices.

AechoLiu
  • 17,522
  • 9
  • 100
  • 118
  • I finally got it working, for the last parameter of glDrawElements it seemed to want the address to my vector of indices. I'm loading geometry from OBJ files, so how would I go about rendering the geometry using triangle strips if the geometry is already created? I wouldn't be opposed to writing a preprocessor on the desktop that could reorder the indices to take advantage of triangle strips, just not sure where I would start... – Davido Dec 01 '10 at 18:31
  • With the vertices on your post, I show the vertices with (x,y). The vertices flow can be (10, 10) -> (-10, 10) -> (10, -10) -> (-10, -10). – AechoLiu Dec 02 '10 at 12:54
  • I could probably achieve the same thing by writing an optimization routine into my geometry preprocessor that finds duplicate vertices, eliminates duplicates and adjusts the index pointers. Seems like the only difference is that vertices get reused with triangle strips but not with triangles. Any reason why this wouldn't be as fast or almost as fast as triangle strips? – Davido Dec 07 '10 at 15:29
  • If your format is GL_TRIANGLES, then I don't think the format will be slower than the GL_TRIANGLE_STRIP. Maybe you can test them and compare the efficiency with instruments. Because your post only have 4 vertices, so I don't think them are well formated for any format in the graph I provided. – AechoLiu Dec 07 '10 at 22:36