PDA

View Full Version : glVertex3f not drawing



tom384
03-12-2010, 10:02 AM
I am having trouble running my OpenGL program, as my calls to glVertex3f don't seem to be drawing anything.

My vertex class 'Ver' is:



class Ver {
public:

// ATTRIBUTES
std::vector<float> v_coord;


// CONSTRUCTORS
Ver();
Ver(float, float, float);


// ACCESSORS
std::vector<float>&amp; get_coord();

};


This is the code for drawing the scene:



int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

glBegin(GL_TRIANGLES);
character.view(); // member function of my character class
glEnd();


if ((errCode = glGetError()) != GL_NO_ERROR) { // GLenum errCode;
errString = gluErrorString(errCode); // const GLubyte * errString;
MessageBox(NULL,(char *)errString,"ERROR",MB_OK|MB_ICONEXCLAMATION);
}

return TRUE;
}




void Character::view()
{
int num_polys = Character::get_mesh().get_num_polys();

vector<Ver> vers;
vers.resize(Character::mesh.get_num_vertices());



// *** I included this in the post for completeness here
// *** This for loop creates Ver objects which have 3 elements in their vectors
// *** And populates the vers vector with them
for (int i=0; i<(int)Character::bones.size(); i++)
{
vector<float> final_matrix;

multiply_matrices(Character::get_bones()[i].get_o_matrix(), Character::get_bones()[i].get_t_matrix(), final_matrix);

vector<float> matrix;
Ver new_ver;


for (int j=0; j<(int)Character::bones[i].get_v_indices().size(); j++)
{
multiply_matrices(Character::bones[i].get_weights()[j], final_matrix, matrix);
multiply_matrix_vertex(matrix, Character::mesh.get_vertex(Character::bones[i].get_v_indices()[j]).get_coord(), new_ver.get_coord());
vers[Character::bones[i].get_v_indices()[j]].add(new_ver.get_coord());
}
}



// *** Through couts, I have double checked that this past section works. It doesn't collapse the vector into nothingness
// *** or set everything to 0.
for (int i = 0; i < (int)vers.size(); i++)
{
// Write to terminal:
// Make sure that each vertex has 3 points, and that they are not 0.
if ((vers[i].get_coord()[0] == 0) || (vers[i].get_coord()[1] == 0) || (vers[i].get_coord()[2] == 0))
cout << "Coord is 0" << endl;
if ( (vers[i].get_coord().size() != 3) )
cout << "There aren't 3" << endl;
}



for (int i = 0; i < 1 /*num_polys*/; i++)
{
// *** These 3 glVertex3f commands draw a polygon
//glVertex3f(0.0572981f, -0.00736302f, -2.52117f);
//glVertex3f(0.031869f, -0.35291f, -2.3234f);
//glVertex3f(-0.105954f, -1.89219f, -1.49456f);


// *** These 3 don't do anything
glVertex3f((vers[0]).get_coord()[0], (vers[0]).get_coord()[1], (vers[0]).get_coord()[2]);
glVertex3f((vers[1]).get_coord()[0], (vers[1]).get_coord()[1], (vers[1]).get_coord()[2]);
glVertex3f((vers[2]).get_coord()[0], (vers[2]).get_coord()[1], (vers[2]).get_coord()[2]);


}
}



So the problem is that the 2 blocks of glVertex commands produce different outputs. This is despite the numbers in the first block being the values given when I print out the commands in the second block, i.e. "vers[0].get_coord()[0]" etc.

Can anyone please tell me why the second block doesn't actually draw anything?

dorbie
03-12-2010, 03:30 PM
Holy [censored], this is a beautiful disaster :-).

You don't want to be calling a method 3 times for every vertex then doing pointer arithmetic for 6 array accesses every call.

Try using glVertex3fv at least. That way you can just do a single access, but please build your vertices into an attribute array and draw those with DrawArrays or Elements.

Also this doesn't address your problem. Confirm that get_coord is returning valid data.

I'm not an STL expert but try just returning v_coord as a float* when you move to the packed method. And again, don't stick with this, rewrite it to use a contiguous packed array and a single dispatch call.

tom384
03-12-2010, 05:34 PM
Hi Dorbie, thanks for the info, pretty new to OpenGL and you certainly don't often get a book pointing out efficiency mistakes. Will switch to glVertex3fv when I get chance tomorrow. My OpenGL FPS will certainly thank you =)

So would implementing my Ver class to consist of an array of doubles, which I use to pass to glVertex3fv be what you are implying?

Regard to my problem, how can I "confirm that get_coord is returning valid data"? I cout the 2nd block of glVertex3f (the function calls to retrieve data from the vector) and I use the results to hardcode the 1st block. So the numbers should definately be drawing something. And wouldn't an incorrect type offer up some sort of GLerror? I'm just completely oblivious to how it's not doing anything...

dorbie
03-14-2010, 10:45 PM
Not doubles, just floats. A single one dimensional array of packed floats. So if you have one big vector for all verts it'll work for array dispatch.

To verify, use a debugger or use printf....