I'm new to OpenGL and I'm trying to load obj files into my code. I have a code of a simple animating cube. When I declare the vertices and indices as follows it works properly:
GLfloat cube_vertices[] = {
// front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
};
GLushort cube_elements[] = {
// front
0, 1, 2,
2, 3, 0,
// top
1, 5, 6,
6, 2, 1,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 0, 3,
3, 7, 4,
// left
4, 5, 1,
1, 0, 4,
// right
3, 2, 6,
6, 7, 3,
};
However, when I'm trying to get similar number from a file, the program runs without error but shows nothing in the window. Here is my code for loading the obj file:
vector<GLfloat> vertices;
vector<GLushort> elements;
ifstream in("cube.obj", ios::in);
if (!in)
{
cerr << "Cannot open " << "sample.obj" << endl; exit(1);
}
string line;
while (getline(in, line))
{
if (line.substr(0, 2) == "v ")
{
istringstream s(line.substr(2));
GLfloat v;
s >> v; vertices.push_back(v);
s >> v; vertices.push_back(v);
s >> v; vertices.push_back(v);
}
else if (line.substr(0, 2) == "f ")
{
istringstream s(line.substr(2));
GLushort a, b, c;
s >> a; s >> b; s >> c;
elements.push_back(a); elements.push_back(b); elements.push_back(c);
}
}
The cube.obj file is saved as follows:
o cube
v -1.0 -1.0 1.0
v 1.0 -1.0 1.0
v 1.0 1.0 1.0
v -1.0 1.0 1.0
v -1.0 -1.0 -1.0
v 1.0 -1.0 -1.0
v 1.0 1.0 -1.0
v -1.0 1.0 -1.0
f 0 1 2
f 2 3 0
f 1 5 6
f 6 2 1
f 7 6 5
f 5 4 7
f 4 0 3
f 3 7 4
f 4 5 1
f 1 0 4
f 3 2 6
f 6 7 3
I'm just getting a blank window in the output. Do you have any idea why the loader doesn't work?
I upload the data thus
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), &elements, GL_STATIC_DRAW);
When I don't put the &
operator before vertices
and elements
it gives compilation error of "no suitable conversion".