glvertexpointer std::vector, struct memory opengl

Started by
8 comments, last by PpenGlProg 10 years, 9 months ago

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);

}

Advertisement

Your vertex position data is not tightly packed, so you need to set the stride parameter accordingly.

What Bob said...

glVertexPointer(3, GL_FLOAT, sizeof(Vertex), verts.data()->pos);

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.

0306778c531et.jpg

snimok32pn_9749539_8486051.png

http://i5.pixs.ru/storage/0/5/1/snimok32pn_9749539_8486051.png

snimok33pn_1299167_8486224.png

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()"

Thanx! :)

This topic is closed to new replies.

Advertisement