开发者

Correct format for loading vertex arrays from file

开发者 https://www.devze.com 2023-01-27 22:06 出处:网络
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

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.


You use GL_TRIANGLES to specify the vertices format.

Correct format for loading vertex arrays from file

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

0

精彩评论

暂无评论...
验证码 换一张
取 消