PDA

View Full Version : Can't get glDrawElements to draw what I want it to



tom384
03-17-2010, 11:34 AM
Hi guys,

I am using glDrawElements to draw my character model in opengl, but cannot get it to actually do anything in the situation I need it to.

It's easier to show you than explain it. In the code below, calling view_that_does_not_work() produces a plain screen, with nothing drawn at all.

view_that_does_work() produces a single polygon.


(The following most likely contains a wrong assumption) They are both drawing the same polygon, with the same coordinate values and index values. And yet one works and the other doesn't.

The code is C++


class My_Vertex {
public:
float v_coord[3];

My_Vertex();
My_Vertex(float x, float y, float z)
{
v_coord[0] = x;
v_coord[1] = y;
v_coord[2] = z;
};

float* get_coord()
{
return v_coord;
};

// void add(float*);
};

class My_Poly {
public:
vector<int> v_indices;

My_Poly(){};

My_Poly(vector<int> indices)
{
v_indices = indices;
}

vector<int>&amp; get_v_indices()
{
return v_indices;
}

void set_v_indices(vector<int> verts)
{
v_indices = verts;
}
};



void view_that_does_not_work()
{
My_Vertex v1 = My_Vertex(0.0f, 0.0f, 0.0f);
My_Vertex v2 = My_Vertex(1.0f, 0.0f, 0.0f);
My_Vertex v3 = My_Vertex(0.0f, 1.0f, 0.0f);

vector<My_Vertex> vertices;
vertices.push_back(v1);
vertices.push_back(v2);
vertices.push_back(v3);

vector<int> polygon_indices;
polygon_indices.push_back(0);
polygon_indices.push_back(1);
polygon_indices.push_back(2);

My_Poly p1 = My_Poly(polygon_indices);

vector<My_Poly> polygons;
polygons.push_back(p1);

GLfloat* vertex_array = new GLfloat[vertices.size()*3];
for (int i=0; i<(int)vertices.size(); i++)
{
GLfloat x = vertices[i].get_coord()[0];
GLfloat y = vertices[i].get_coord()[1];
GLfloat z = vertices[i].get_coord()[2];
vertex_array[(3*i)] = x;
vertex_array[(3*i)] = y;
vertex_array[(3*i)] = z;
}

GLuint* indices = new GLuint[polygons.size()*3];
for (int i=0; i<(int)polygons.size(); i++)
{
GLuint i1 = polygons[i].get_v_indices()[0];
GLuint i2 = polygons[i].get_v_indices()[1];
GLuint i3 = polygons[i].get_v_indices()[2];
indices[(3*i)] = i1;
indices[(3*i)+1] = i2;
indices[(3*i)+2] = i3;
}


glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertex_array);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
glDisableClientState(GL_VERTEX_ARRAY);
}

void view_that_does_work()
{
GLfloat vertex_array[] = {
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0
};

GLuint indices[] = {
0, 1, 2
};

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertex_array);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
glDisableClientState(GL_VERTEX_ARRAY);
}


Would someone please explain what is happening here?

Regards,

Tom

Alfonse Reinheart
03-17-2010, 12:12 PM
vertex_array[(3*i)] = x;
vertex_array[(3*i)] = y;
vertex_array[(3*i)] = z;


Look really hard at this.

tom384
03-17-2010, 12:32 PM
Haha, good spot. Silly me.

I made this code to demonstrate a problem in one of my projects. As the project is rather substantial I didn't want to post it, so made a much smaller section of code to demonstrate the problem. Seems I didn't translate the problem to this demo code at all.

I did however, not make the mistake you found in my original project. Will redo the code to capture the error and post it back.