Jump to content
  • Advertisement
Sign in to follow this  
lordciriuz

problem with stl vector and glDrawElements

This topic is 4952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am haveing a trouble with stl vector and glDrawElements(). This is my code: vector<cVector3>::iterator vertex = mesh->GetVertexBuffer(); vector<cFace3>::iterator faces = mesh->GetIndexBuffer(); glVertexPointer(3,GL_FLOAT,3*sizeof(GL_FLOAT),vertex); glDrawElements(GL_TRIANGLES,mesh->GetNumIndex(),GL_UNSIGNED_INT,faces); I obtained a partial drawn image. If I increment the faces pointer, in order to point to some memory place near the end of the faces vector, I obtained the rest of the pixels. What would the problem?! Thank you very much! Lord Ciriuz

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by lord_ciriuz
What would the problem?!


The problem is your trying to use an iterator with GL [smile],

You wanna change it to:


typedef std::vector<cVector3> vec_of_vec3;
typedef std::vector<cFace3> vec_of_fac3;

struct mesh_type {

//...

const vec_of_vec3& GetVertexBuffer() const;
vec_of_vec3& GetVertexBuffer();

const vec_of_fac3& GetIndexBuffer() const;
vec_of_fac3& GetIndexBuffer();

//...
};

//....

const vec_of_vec3& vertex = mesh->GetVertexBuffer(); //constant reference
const vec_of_fac3& faces = mesh->GetIndexBuffer(); //constant reference

glVertexPointer(3, GL_FLOAT, 3 * sizeof(GL_FLOAT), &vertex[0]);

glDrawElements(GL_TRIANGLES, mesh->GetNumIndex(), GL_UNSIGNED_INT, &faces[0]);


The standard guareentees that vector is contiguous in memory.

[Edited by - snk_kid on November 27, 2004 6:21:29 AM]

Share this post


Link to post
Share on other sites
Iterators and pointers are not the same thing, even though they behave similarly. You need to use the pointer to the first element in each vector, not the iterator at the first element. Also remember that the pointers must be float* and unsigned int* respectively. So you will have to point to the actual data members of and not to cVector3 and cFace3. I don't know what your structs/classes look like, but if you replace vertex and faces in your gl-calls with something like

glVertexPointer(...,&vertex->X);
glDrawElements(...,&faces->index);

it should work.

Share this post


Link to post
Share on other sites
Quote:
Original post by fiskbil
Also remember that the pointers must be float* and unsigned int* respectively. So you will have to point to the actual data members of and not to cVector3 and cFace3. I don't know what your structs/classes look like, but if you replace vertex and faces in your gl-calls with something like

glVertexPointer(...,&vertex->X);
glDrawElements(...,&faces->index);


I beg to differ this is not true, the fact that glVertexPointer & other simillar GL functions takes a pointer to void, take this example:



#include <iostream>

struct foo {

float x, y, z;

foo(float _x = 0.0f, float _y = 0.0f, float _z = 0.0f)
: x(_x), y(_y), z(_z) {}

};

void foobar(const void* v) {

const float* u = (const float*)v;

std::cout << u[0] << ", " << u[1] << ", " << u[2] << std::endl;

}

int main() {

foo f(3.0f, 3.43f, 33.3f);

foobar(&f);

}

Share this post


Link to post
Share on other sites
Ah yes of course, now that you mention it it's obvious since it's C. And if it wasn't void there would be no need to supply the type of data.

Anyway, it should work and it will cause less change in the code than returning a reference to a vector and indexing it.

Share this post


Link to post
Share on other sites
Hello!

Thank you to everyone for the replies. I change my code to the following one, and I get the same results! :(

class cMesh
{
public:
inline const vector<cVector3>& GetVertexBuffer(void);
inline const vector<cFace3>& GetIndexBuffer(void);
private:
vector<cVector3> m_pVertexBuffer;
vector<cFace3> m_pIndexBuffer;
(...)
};

inline const vector<cVector3>& cMesh::GetVertexBuffer(void)
{
return this->m_pVertexBuffer;
}

inline const vector<cFace3>& cMesh::GetIndexBuffer(void)
{
return this->m_pIndexBuffer;
}


main.cpp

const vector<cVector3>& vertex = mesh->GetVertexBuffer();
const vector<cFace3>& faces = mesh->GetIndexBuffer();

glVertexPointer(3,GL_FLOAT,3*sizeof(GL_FLOAT),&vertex[0]);

glDrawElements(GL_TRIANGLES,mesh->GetNumIndex(),GL_UNSIGNED_INT,&faces[0]);

I always get the same part of the teapot. I have changed the model, getting similar results.

If I use glBegin()/glEnd(), the tpot is drawn perfectly:

glBegin(GL_TRIANGLES);

for (int i = 0 ; i < mesh->GetNumIndex() ; i++)
{
glVertex3f( mesh->GetVertex( mesh->GetIndex(i).v1).x , mesh->GetVertex( mesh->GetIndex(i).v1).y , mesh->GetVertex( mesh->GetIndex(i).v1).z);

glVertex3f( mesh->GetVertex( mesh->GetIndex(i).v2).x , mesh->GetVertex( mesh->GetIndex(i).v2).y , mesh->GetVertex( mesh->GetIndex(i).v2).z);

glVertex3f( mesh->GetVertex( mesh->GetIndex(i).v3).x , mesh->GetVertex( mesh->GetIndex(i).v3).y , mesh->GetVertex( mesh->GetIndex(i).v3).z);

}
glEnd();

I will appreciate any help ;)

Thank you very much!
Lord Ciriuz

Share this post


Link to post
Share on other sites
Hi!

I found the problem! My function GetNumIndex() was wrong!, because I was getting num of faces instead of num of indices. I scale m_iNumFaces * 3, and the problem was solved. I need to sleep something :)

Thank you very much!!!!
Lord Ciriuz

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!