Jump to content

  • Log In with Google      Sign In   
  • Create Account


glvertexpointer std::vector, struct memory opengl


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 PpenGlProg   Members   -  Reputation: 120

Like
0Likes
Like

Posted 17 July 2013 - 08:50 AM

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

}
 



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7784

Like
3Likes
Like

Posted 17 July 2013 - 08:59 AM

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



#3 RobTheBloke   Crossbones+   -  Reputation: 2296

Like
2Likes
Like

Posted 17 July 2013 - 09:11 AM

What Bob said...

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



#4 PpenGlProg   Members   -  Reputation: 120

Like
0Likes
Like

Posted 17 July 2013 - 09:32 AM

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



#5 PpenGlProg   Members   -  Reputation: 120

Like
0Likes
Like

Posted 17 July 2013 - 09:33 AM

snimok32pn_9749539_8486051.png



#6 PpenGlProg   Members   -  Reputation: 120

Like
0Likes
Like

Posted 17 July 2013 - 09:34 AM

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



#7 PpenGlProg   Members   -  Reputation: 120

Like
0Likes
Like

Posted 17 July 2013 - 10:05 AM

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


Edited by PpenGlProg, 17 July 2013 - 10:06 AM.


#8 RobTheBloke   Crossbones+   -  Reputation: 2296

Like
0Likes
Like

Posted 17 July 2013 - 10:13 AM

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)



#9 Koehler   Members   -  Reputation: 228

Like
1Likes
Like

Posted 17 July 2013 - 12:03 PM

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


Edited by Koehler, 17 July 2013 - 12:05 PM.


#10 PpenGlProg   Members   -  Reputation: 120

Like
0Likes
Like

Posted 17 July 2013 - 09:38 PM

Thanx! :)


Edited by PpenGlProg, 17 July 2013 - 09:46 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS