Hello, i use struct
struct Vertex
{
float pos[3];
float tex[2] = {0, 0};
float norm[3] = {0, 0, 0};
int index_mtl;
};
i use vector
std::vector<Vertex> verts;
how use "glvertexpointer" for my struct vector.
I wanted to drawing by that function. in the end I did not succeed.
void Obj_render::DrawObject()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, verts.data()->pos);
glDrawArrays(GL_TRIANGLES,0,3);
// deactivate vertex arrays after drawing
glDisableClientState(GL_VERTEX_ARRAY);
}
glvertexpointer std::vector, struct memory opengl
Your vertex position data is not tightly packed, so you need to set the stride parameter accordingly.
thx for answer.
i use this is obj file
# Blender v2.63 (sub 0) OBJ File: ''
# www.blender.org
mtllib 1.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8
this is cube.
void Obj_render::DrawObject()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer( 3, GL_FLOAT, sizeof( Vertex ), &verts.data()->pos );
glNormalPointer(GL_FLOAT,sizeof( Vertex ),&verts.data()->norm);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &verts.data()->tex);
glDrawArrays( GL_TRIANGLES, 12,indices.size());
glDisableClientState(GL_VERTEX_ARRAY);
std::cout << glGetError() << std::endl;
}
Have you expanded the vertex & normal data out correctly?
Have you forgotten to reduce the index values (both normal & vertex) by 1 before expanding the array data? Indices should be zero based, not 1 based (as stored within obj files)
I'm guessing your problem is here:
glDrawArrays( GL_TRIANGLES, 12,indices.size());
This is for non-indexed geometry, where all vertices are stored in order in an array.
You have indexed geometry from your OBJ file, (So you have a minimal array of vertices, and then the array "indices" indexes into that vertex array.) This means you want to use:
glDrawElements( GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, &indices[0]);
Note that &indices[0] is because I'm not sure if you are using a vector or an array. If an array you could just pass "indices". If array vector you could pass "indices.data()"